xref: /netbsd-src/sys/nfs/nfs_subs.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: nfs_subs.c,v 1.122 2003/06/09 13:10:31 yamt Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
39  */
40 
41 /*
42  * Copyright 2000 Wasabi Systems, Inc.
43  * All rights reserved.
44  *
45  * Written by Frank van der Linden for Wasabi Systems, Inc.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *      This product includes software developed for the NetBSD Project by
58  *      Wasabi Systems, Inc.
59  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
60  *    or promote products derived from this software without specific prior
61  *    written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
65  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
66  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
67  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
68  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
69  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
70  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
71  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
72  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
73  * POSSIBILITY OF SUCH DAMAGE.
74  */
75 
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: nfs_subs.c,v 1.122 2003/06/09 13:10:31 yamt Exp $");
78 
79 #include "fs_nfs.h"
80 #include "opt_nfs.h"
81 #include "opt_nfsserver.h"
82 #include "opt_iso.h"
83 #include "opt_inet.h"
84 
85 /*
86  * These functions support the macros and help fiddle mbuf chains for
87  * the nfs op functions. They do things like create the rpc header and
88  * copy data between mbuf chains and uio lists.
89  */
90 #include <sys/param.h>
91 #include <sys/proc.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/mount.h>
95 #include <sys/vnode.h>
96 #include <sys/namei.h>
97 #include <sys/mbuf.h>
98 #include <sys/socket.h>
99 #include <sys/stat.h>
100 #include <sys/malloc.h>
101 #include <sys/filedesc.h>
102 #include <sys/time.h>
103 #include <sys/dirent.h>
104 
105 #include <uvm/uvm_extern.h>
106 
107 #include <nfs/rpcv2.h>
108 #include <nfs/nfsproto.h>
109 #include <nfs/nfsnode.h>
110 #include <nfs/nfs.h>
111 #include <nfs/xdr_subs.h>
112 #include <nfs/nfsm_subs.h>
113 #include <nfs/nfsmount.h>
114 #include <nfs/nqnfs.h>
115 #include <nfs/nfsrtt.h>
116 #include <nfs/nfs_var.h>
117 
118 #include <miscfs/specfs/specdev.h>
119 
120 #include <netinet/in.h>
121 #ifdef ISO
122 #include <netiso/iso.h>
123 #endif
124 
125 /*
126  * Data items converted to xdr at startup, since they are constant
127  * This is kinda hokey, but may save a little time doing byte swaps
128  */
129 u_int32_t nfs_xdrneg1;
130 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
131 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
132 	rpc_auth_kerb;
133 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
134 
135 /* And other global data */
136 static u_int32_t nfs_xid = 0;
137 const nfstype nfsv2_type[9] =
138 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON, NFCHR, NFNON };
139 const nfstype nfsv3_type[9] =
140 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON };
141 const enum vtype nv2tov_type[8] =
142 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
143 const enum vtype nv3tov_type[8] =
144 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
145 int nfs_ticks;
146 int nfs_commitsize;
147 
148 MALLOC_DEFINE(M_NFSDIROFF, "NFS diroff", "NFS directory cookies");
149 
150 /* NFS client/server stats. */
151 struct nfsstats nfsstats;
152 
153 /*
154  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
155  */
156 const int nfsv3_procid[NFS_NPROCS] = {
157 	NFSPROC_NULL,
158 	NFSPROC_GETATTR,
159 	NFSPROC_SETATTR,
160 	NFSPROC_NOOP,
161 	NFSPROC_LOOKUP,
162 	NFSPROC_READLINK,
163 	NFSPROC_READ,
164 	NFSPROC_NOOP,
165 	NFSPROC_WRITE,
166 	NFSPROC_CREATE,
167 	NFSPROC_REMOVE,
168 	NFSPROC_RENAME,
169 	NFSPROC_LINK,
170 	NFSPROC_SYMLINK,
171 	NFSPROC_MKDIR,
172 	NFSPROC_RMDIR,
173 	NFSPROC_READDIR,
174 	NFSPROC_FSSTAT,
175 	NFSPROC_NOOP,
176 	NFSPROC_NOOP,
177 	NFSPROC_NOOP,
178 	NFSPROC_NOOP,
179 	NFSPROC_NOOP,
180 	NFSPROC_NOOP,
181 	NFSPROC_NOOP,
182 	NFSPROC_NOOP
183 };
184 
185 /*
186  * and the reverse mapping from generic to Version 2 procedure numbers
187  */
188 const int nfsv2_procid[NFS_NPROCS] = {
189 	NFSV2PROC_NULL,
190 	NFSV2PROC_GETATTR,
191 	NFSV2PROC_SETATTR,
192 	NFSV2PROC_LOOKUP,
193 	NFSV2PROC_NOOP,
194 	NFSV2PROC_READLINK,
195 	NFSV2PROC_READ,
196 	NFSV2PROC_WRITE,
197 	NFSV2PROC_CREATE,
198 	NFSV2PROC_MKDIR,
199 	NFSV2PROC_SYMLINK,
200 	NFSV2PROC_CREATE,
201 	NFSV2PROC_REMOVE,
202 	NFSV2PROC_RMDIR,
203 	NFSV2PROC_RENAME,
204 	NFSV2PROC_LINK,
205 	NFSV2PROC_READDIR,
206 	NFSV2PROC_NOOP,
207 	NFSV2PROC_STATFS,
208 	NFSV2PROC_NOOP,
209 	NFSV2PROC_NOOP,
210 	NFSV2PROC_NOOP,
211 	NFSV2PROC_NOOP,
212 	NFSV2PROC_NOOP,
213 	NFSV2PROC_NOOP,
214 	NFSV2PROC_NOOP,
215 };
216 
217 /*
218  * Maps errno values to nfs error numbers.
219  * Use NFSERR_IO as the catch all for ones not specifically defined in
220  * RFC 1094.
221  */
222 static const u_char nfsrv_v2errmap[ELAST] = {
223   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
224   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
225   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
226   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
227   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
228   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
229   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
230   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
231   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
232   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
233   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
234   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
235   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
236   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
237   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
238   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
239   NFSERR_IO,	NFSERR_IO,
240 };
241 
242 /*
243  * Maps errno values to nfs error numbers.
244  * Although it is not obvious whether or not NFS clients really care if
245  * a returned error value is in the specified list for the procedure, the
246  * safest thing to do is filter them appropriately. For Version 2, the
247  * X/Open XNFS document is the only specification that defines error values
248  * for each RPC (The RFC simply lists all possible error values for all RPCs),
249  * so I have decided to not do this for Version 2.
250  * The first entry is the default error return and the rest are the valid
251  * errors for that RPC in increasing numeric order.
252  */
253 static const short nfsv3err_null[] = {
254 	0,
255 	0,
256 };
257 
258 static const short nfsv3err_getattr[] = {
259 	NFSERR_IO,
260 	NFSERR_IO,
261 	NFSERR_STALE,
262 	NFSERR_BADHANDLE,
263 	NFSERR_SERVERFAULT,
264 	0,
265 };
266 
267 static const short nfsv3err_setattr[] = {
268 	NFSERR_IO,
269 	NFSERR_PERM,
270 	NFSERR_IO,
271 	NFSERR_ACCES,
272 	NFSERR_INVAL,
273 	NFSERR_NOSPC,
274 	NFSERR_ROFS,
275 	NFSERR_DQUOT,
276 	NFSERR_STALE,
277 	NFSERR_BADHANDLE,
278 	NFSERR_NOT_SYNC,
279 	NFSERR_SERVERFAULT,
280 	0,
281 };
282 
283 static const short nfsv3err_lookup[] = {
284 	NFSERR_IO,
285 	NFSERR_NOENT,
286 	NFSERR_IO,
287 	NFSERR_ACCES,
288 	NFSERR_NOTDIR,
289 	NFSERR_NAMETOL,
290 	NFSERR_STALE,
291 	NFSERR_BADHANDLE,
292 	NFSERR_SERVERFAULT,
293 	0,
294 };
295 
296 static const short nfsv3err_access[] = {
297 	NFSERR_IO,
298 	NFSERR_IO,
299 	NFSERR_STALE,
300 	NFSERR_BADHANDLE,
301 	NFSERR_SERVERFAULT,
302 	0,
303 };
304 
305 static const short nfsv3err_readlink[] = {
306 	NFSERR_IO,
307 	NFSERR_IO,
308 	NFSERR_ACCES,
309 	NFSERR_INVAL,
310 	NFSERR_STALE,
311 	NFSERR_BADHANDLE,
312 	NFSERR_NOTSUPP,
313 	NFSERR_SERVERFAULT,
314 	0,
315 };
316 
317 static const short nfsv3err_read[] = {
318 	NFSERR_IO,
319 	NFSERR_IO,
320 	NFSERR_NXIO,
321 	NFSERR_ACCES,
322 	NFSERR_INVAL,
323 	NFSERR_STALE,
324 	NFSERR_BADHANDLE,
325 	NFSERR_SERVERFAULT,
326 	NFSERR_JUKEBOX,
327 	0,
328 };
329 
330 static const short nfsv3err_write[] = {
331 	NFSERR_IO,
332 	NFSERR_IO,
333 	NFSERR_ACCES,
334 	NFSERR_INVAL,
335 	NFSERR_FBIG,
336 	NFSERR_NOSPC,
337 	NFSERR_ROFS,
338 	NFSERR_DQUOT,
339 	NFSERR_STALE,
340 	NFSERR_BADHANDLE,
341 	NFSERR_SERVERFAULT,
342 	NFSERR_JUKEBOX,
343 	0,
344 };
345 
346 static const short nfsv3err_create[] = {
347 	NFSERR_IO,
348 	NFSERR_IO,
349 	NFSERR_ACCES,
350 	NFSERR_EXIST,
351 	NFSERR_NOTDIR,
352 	NFSERR_NOSPC,
353 	NFSERR_ROFS,
354 	NFSERR_NAMETOL,
355 	NFSERR_DQUOT,
356 	NFSERR_STALE,
357 	NFSERR_BADHANDLE,
358 	NFSERR_NOTSUPP,
359 	NFSERR_SERVERFAULT,
360 	0,
361 };
362 
363 static const short nfsv3err_mkdir[] = {
364 	NFSERR_IO,
365 	NFSERR_IO,
366 	NFSERR_ACCES,
367 	NFSERR_EXIST,
368 	NFSERR_NOTDIR,
369 	NFSERR_NOSPC,
370 	NFSERR_ROFS,
371 	NFSERR_NAMETOL,
372 	NFSERR_DQUOT,
373 	NFSERR_STALE,
374 	NFSERR_BADHANDLE,
375 	NFSERR_NOTSUPP,
376 	NFSERR_SERVERFAULT,
377 	0,
378 };
379 
380 static const short nfsv3err_symlink[] = {
381 	NFSERR_IO,
382 	NFSERR_IO,
383 	NFSERR_ACCES,
384 	NFSERR_EXIST,
385 	NFSERR_NOTDIR,
386 	NFSERR_NOSPC,
387 	NFSERR_ROFS,
388 	NFSERR_NAMETOL,
389 	NFSERR_DQUOT,
390 	NFSERR_STALE,
391 	NFSERR_BADHANDLE,
392 	NFSERR_NOTSUPP,
393 	NFSERR_SERVERFAULT,
394 	0,
395 };
396 
397 static const short nfsv3err_mknod[] = {
398 	NFSERR_IO,
399 	NFSERR_IO,
400 	NFSERR_ACCES,
401 	NFSERR_EXIST,
402 	NFSERR_NOTDIR,
403 	NFSERR_NOSPC,
404 	NFSERR_ROFS,
405 	NFSERR_NAMETOL,
406 	NFSERR_DQUOT,
407 	NFSERR_STALE,
408 	NFSERR_BADHANDLE,
409 	NFSERR_NOTSUPP,
410 	NFSERR_SERVERFAULT,
411 	NFSERR_BADTYPE,
412 	0,
413 };
414 
415 static const short nfsv3err_remove[] = {
416 	NFSERR_IO,
417 	NFSERR_NOENT,
418 	NFSERR_IO,
419 	NFSERR_ACCES,
420 	NFSERR_NOTDIR,
421 	NFSERR_ROFS,
422 	NFSERR_NAMETOL,
423 	NFSERR_STALE,
424 	NFSERR_BADHANDLE,
425 	NFSERR_SERVERFAULT,
426 	0,
427 };
428 
429 static const short nfsv3err_rmdir[] = {
430 	NFSERR_IO,
431 	NFSERR_NOENT,
432 	NFSERR_IO,
433 	NFSERR_ACCES,
434 	NFSERR_EXIST,
435 	NFSERR_NOTDIR,
436 	NFSERR_INVAL,
437 	NFSERR_ROFS,
438 	NFSERR_NAMETOL,
439 	NFSERR_NOTEMPTY,
440 	NFSERR_STALE,
441 	NFSERR_BADHANDLE,
442 	NFSERR_NOTSUPP,
443 	NFSERR_SERVERFAULT,
444 	0,
445 };
446 
447 static const short nfsv3err_rename[] = {
448 	NFSERR_IO,
449 	NFSERR_NOENT,
450 	NFSERR_IO,
451 	NFSERR_ACCES,
452 	NFSERR_EXIST,
453 	NFSERR_XDEV,
454 	NFSERR_NOTDIR,
455 	NFSERR_ISDIR,
456 	NFSERR_INVAL,
457 	NFSERR_NOSPC,
458 	NFSERR_ROFS,
459 	NFSERR_MLINK,
460 	NFSERR_NAMETOL,
461 	NFSERR_NOTEMPTY,
462 	NFSERR_DQUOT,
463 	NFSERR_STALE,
464 	NFSERR_BADHANDLE,
465 	NFSERR_NOTSUPP,
466 	NFSERR_SERVERFAULT,
467 	0,
468 };
469 
470 static const short nfsv3err_link[] = {
471 	NFSERR_IO,
472 	NFSERR_IO,
473 	NFSERR_ACCES,
474 	NFSERR_EXIST,
475 	NFSERR_XDEV,
476 	NFSERR_NOTDIR,
477 	NFSERR_INVAL,
478 	NFSERR_NOSPC,
479 	NFSERR_ROFS,
480 	NFSERR_MLINK,
481 	NFSERR_NAMETOL,
482 	NFSERR_DQUOT,
483 	NFSERR_STALE,
484 	NFSERR_BADHANDLE,
485 	NFSERR_NOTSUPP,
486 	NFSERR_SERVERFAULT,
487 	0,
488 };
489 
490 static const short nfsv3err_readdir[] = {
491 	NFSERR_IO,
492 	NFSERR_IO,
493 	NFSERR_ACCES,
494 	NFSERR_NOTDIR,
495 	NFSERR_STALE,
496 	NFSERR_BADHANDLE,
497 	NFSERR_BAD_COOKIE,
498 	NFSERR_TOOSMALL,
499 	NFSERR_SERVERFAULT,
500 	0,
501 };
502 
503 static const short nfsv3err_readdirplus[] = {
504 	NFSERR_IO,
505 	NFSERR_IO,
506 	NFSERR_ACCES,
507 	NFSERR_NOTDIR,
508 	NFSERR_STALE,
509 	NFSERR_BADHANDLE,
510 	NFSERR_BAD_COOKIE,
511 	NFSERR_NOTSUPP,
512 	NFSERR_TOOSMALL,
513 	NFSERR_SERVERFAULT,
514 	0,
515 };
516 
517 static const short nfsv3err_fsstat[] = {
518 	NFSERR_IO,
519 	NFSERR_IO,
520 	NFSERR_STALE,
521 	NFSERR_BADHANDLE,
522 	NFSERR_SERVERFAULT,
523 	0,
524 };
525 
526 static const short nfsv3err_fsinfo[] = {
527 	NFSERR_STALE,
528 	NFSERR_STALE,
529 	NFSERR_BADHANDLE,
530 	NFSERR_SERVERFAULT,
531 	0,
532 };
533 
534 static const short nfsv3err_pathconf[] = {
535 	NFSERR_STALE,
536 	NFSERR_STALE,
537 	NFSERR_BADHANDLE,
538 	NFSERR_SERVERFAULT,
539 	0,
540 };
541 
542 static const short nfsv3err_commit[] = {
543 	NFSERR_IO,
544 	NFSERR_IO,
545 	NFSERR_STALE,
546 	NFSERR_BADHANDLE,
547 	NFSERR_SERVERFAULT,
548 	0,
549 };
550 
551 static const short * const nfsrv_v3errmap[] = {
552 	nfsv3err_null,
553 	nfsv3err_getattr,
554 	nfsv3err_setattr,
555 	nfsv3err_lookup,
556 	nfsv3err_access,
557 	nfsv3err_readlink,
558 	nfsv3err_read,
559 	nfsv3err_write,
560 	nfsv3err_create,
561 	nfsv3err_mkdir,
562 	nfsv3err_symlink,
563 	nfsv3err_mknod,
564 	nfsv3err_remove,
565 	nfsv3err_rmdir,
566 	nfsv3err_rename,
567 	nfsv3err_link,
568 	nfsv3err_readdir,
569 	nfsv3err_readdirplus,
570 	nfsv3err_fsstat,
571 	nfsv3err_fsinfo,
572 	nfsv3err_pathconf,
573 	nfsv3err_commit,
574 };
575 
576 extern struct nfsrtt nfsrtt;
577 extern time_t nqnfsstarttime;
578 extern int nqsrv_clockskew;
579 extern int nqsrv_writeslack;
580 extern int nqsrv_maxlease;
581 extern const int nqnfs_piggy[NFS_NPROCS];
582 extern struct nfsnodehashhead *nfsnodehashtbl;
583 extern u_long nfsnodehash;
584 
585 u_long nfsdirhashmask;
586 
587 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
588 
589 /*
590  * Create the header for an rpc request packet
591  * The hsiz is the size of the rest of the nfs request header.
592  * (just used to decide if a cluster is a good idea)
593  */
594 struct mbuf *
595 nfsm_reqh(np, procid, hsiz, bposp)
596 	struct nfsnode *np;
597 	u_long procid;
598 	int hsiz;
599 	caddr_t *bposp;
600 {
601 	struct mbuf *mb;
602 	caddr_t bpos;
603 #ifndef NFS_V2_ONLY
604 	struct nfsmount *nmp;
605 	u_int32_t *tl;
606 	int nqflag;
607 #endif
608 
609 	mb = m_get(M_WAIT, MT_DATA);
610 	MCLAIM(mb, &nfs_mowner);
611 	if (hsiz >= MINCLSIZE)
612 		m_clget(mb, M_WAIT);
613 	mb->m_len = 0;
614 	bpos = mtod(mb, caddr_t);
615 
616 #ifndef NFS_V2_ONLY
617 	/*
618 	 * For NQNFS, add lease request.
619 	 */
620 	if (np) {
621 		nmp = VFSTONFS(np->n_vnode->v_mount);
622 		if (nmp->nm_flag & NFSMNT_NQNFS) {
623 			nqflag = NQNFS_NEEDLEASE(np, procid);
624 			if (nqflag) {
625 				nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
626 				*tl++ = txdr_unsigned(nqflag);
627 				*tl = txdr_unsigned(nmp->nm_leaseterm);
628 			} else {
629 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
630 				*tl = 0;
631 			}
632 		}
633 	}
634 #endif
635 	/* Finally, return values */
636 	*bposp = bpos;
637 	return (mb);
638 }
639 
640 /*
641  * Build the RPC header and fill in the authorization info.
642  * The authorization string argument is only used when the credentials
643  * come from outside of the kernel.
644  * Returns the head of the mbuf list.
645  */
646 struct mbuf *
647 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
648 	verf_str, mrest, mrest_len, mbp, xidp)
649 	struct ucred *cr;
650 	int nmflag;
651 	int procid;
652 	int auth_type;
653 	int auth_len;
654 	char *auth_str;
655 	int verf_len;
656 	char *verf_str;
657 	struct mbuf *mrest;
658 	int mrest_len;
659 	struct mbuf **mbp;
660 	u_int32_t *xidp;
661 {
662 	struct mbuf *mb;
663 	u_int32_t *tl;
664 	caddr_t bpos;
665 	int i;
666 	struct mbuf *mreq;
667 	int siz, grpsiz, authsiz;
668 	struct timeval tv;
669 	static u_int32_t base;
670 
671 	authsiz = nfsm_rndup(auth_len);
672 	mb = m_gethdr(M_WAIT, MT_DATA);
673 	MCLAIM(mb, &nfs_mowner);
674 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
675 		m_clget(mb, M_WAIT);
676 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
677 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
678 	} else {
679 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
680 	}
681 	mb->m_len = 0;
682 	mreq = mb;
683 	bpos = mtod(mb, caddr_t);
684 
685 	/*
686 	 * First the RPC header.
687 	 */
688 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
689 
690 	/*
691 	 * derive initial xid from system time
692 	 * XXX time is invalid if root not yet mounted
693 	 */
694 	if (!base && (rootvp)) {
695 		microtime(&tv);
696 		base = tv.tv_sec << 12;
697 		nfs_xid = base;
698 	}
699 	/*
700 	 * Skip zero xid if it should ever happen.
701 	 */
702 	if (++nfs_xid == 0)
703 		nfs_xid++;
704 
705 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
706 	*tl++ = rpc_call;
707 	*tl++ = rpc_vers;
708 	if (nmflag & NFSMNT_NQNFS) {
709 		*tl++ = txdr_unsigned(NQNFS_PROG);
710 		*tl++ = txdr_unsigned(NQNFS_VER3);
711 	} else {
712 		*tl++ = txdr_unsigned(NFS_PROG);
713 		if (nmflag & NFSMNT_NFSV3)
714 			*tl++ = txdr_unsigned(NFS_VER3);
715 		else
716 			*tl++ = txdr_unsigned(NFS_VER2);
717 	}
718 	if (nmflag & NFSMNT_NFSV3)
719 		*tl++ = txdr_unsigned(procid);
720 	else
721 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
722 
723 	/*
724 	 * And then the authorization cred.
725 	 */
726 	*tl++ = txdr_unsigned(auth_type);
727 	*tl = txdr_unsigned(authsiz);
728 	switch (auth_type) {
729 	case RPCAUTH_UNIX:
730 		nfsm_build(tl, u_int32_t *, auth_len);
731 		*tl++ = 0;		/* stamp ?? */
732 		*tl++ = 0;		/* NULL hostname */
733 		*tl++ = txdr_unsigned(cr->cr_uid);
734 		*tl++ = txdr_unsigned(cr->cr_gid);
735 		grpsiz = (auth_len >> 2) - 5;
736 		*tl++ = txdr_unsigned(grpsiz);
737 		for (i = 0; i < grpsiz; i++)
738 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
739 		break;
740 	case RPCAUTH_KERB4:
741 		siz = auth_len;
742 		while (siz > 0) {
743 			if (M_TRAILINGSPACE(mb) == 0) {
744 				struct mbuf *mb2;
745 				mb2 = m_get(M_WAIT, MT_DATA);
746 				MCLAIM(mb2, &nfs_mowner);
747 				if (siz >= MINCLSIZE)
748 					m_clget(mb2, M_WAIT);
749 				mb->m_next = mb2;
750 				mb = mb2;
751 				mb->m_len = 0;
752 				bpos = mtod(mb, caddr_t);
753 			}
754 			i = min(siz, M_TRAILINGSPACE(mb));
755 			memcpy(bpos, auth_str, i);
756 			mb->m_len += i;
757 			auth_str += i;
758 			bpos += i;
759 			siz -= i;
760 		}
761 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
762 			for (i = 0; i < siz; i++)
763 				*bpos++ = '\0';
764 			mb->m_len += siz;
765 		}
766 		break;
767 	};
768 
769 	/*
770 	 * And the verifier...
771 	 */
772 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
773 	if (verf_str) {
774 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
775 		*tl = txdr_unsigned(verf_len);
776 		siz = verf_len;
777 		while (siz > 0) {
778 			if (M_TRAILINGSPACE(mb) == 0) {
779 				struct mbuf *mb2;
780 				mb2 = m_get(M_WAIT, MT_DATA);
781 				MCLAIM(mb2, &nfs_mowner);
782 				if (siz >= MINCLSIZE)
783 					m_clget(mb2, M_WAIT);
784 				mb->m_next = mb2;
785 				mb = mb2;
786 				mb->m_len = 0;
787 				bpos = mtod(mb, caddr_t);
788 			}
789 			i = min(siz, M_TRAILINGSPACE(mb));
790 			memcpy(bpos, verf_str, i);
791 			mb->m_len += i;
792 			verf_str += i;
793 			bpos += i;
794 			siz -= i;
795 		}
796 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
797 			for (i = 0; i < siz; i++)
798 				*bpos++ = '\0';
799 			mb->m_len += siz;
800 		}
801 	} else {
802 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
803 		*tl = 0;
804 	}
805 	mb->m_next = mrest;
806 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
807 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
808 	*mbp = mb;
809 	return (mreq);
810 }
811 
812 /*
813  * copies mbuf chain to the uio scatter/gather list
814  */
815 int
816 nfsm_mbuftouio(mrep, uiop, siz, dpos)
817 	struct mbuf **mrep;
818 	struct uio *uiop;
819 	int siz;
820 	caddr_t *dpos;
821 {
822 	char *mbufcp, *uiocp;
823 	int xfer, left, len;
824 	struct mbuf *mp;
825 	long uiosiz, rem;
826 	int error = 0;
827 
828 	mp = *mrep;
829 	mbufcp = *dpos;
830 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
831 	rem = nfsm_rndup(siz)-siz;
832 	while (siz > 0) {
833 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
834 			return (EFBIG);
835 		left = uiop->uio_iov->iov_len;
836 		uiocp = uiop->uio_iov->iov_base;
837 		if (left > siz)
838 			left = siz;
839 		uiosiz = left;
840 		while (left > 0) {
841 			while (len == 0) {
842 				mp = mp->m_next;
843 				if (mp == NULL)
844 					return (EBADRPC);
845 				mbufcp = mtod(mp, caddr_t);
846 				len = mp->m_len;
847 			}
848 			xfer = (left > len) ? len : left;
849 #ifdef notdef
850 			/* Not Yet.. */
851 			if (uiop->uio_iov->iov_op != NULL)
852 				(*(uiop->uio_iov->iov_op))
853 				(mbufcp, uiocp, xfer);
854 			else
855 #endif
856 			if (uiop->uio_segflg == UIO_SYSSPACE)
857 				memcpy(uiocp, mbufcp, xfer);
858 			else
859 				copyout(mbufcp, uiocp, xfer);
860 			left -= xfer;
861 			len -= xfer;
862 			mbufcp += xfer;
863 			uiocp += xfer;
864 			uiop->uio_offset += xfer;
865 			uiop->uio_resid -= xfer;
866 		}
867 		if (uiop->uio_iov->iov_len <= siz) {
868 			uiop->uio_iovcnt--;
869 			uiop->uio_iov++;
870 		} else {
871 			uiop->uio_iov->iov_base =
872 			    (caddr_t)uiop->uio_iov->iov_base + uiosiz;
873 			uiop->uio_iov->iov_len -= uiosiz;
874 		}
875 		siz -= uiosiz;
876 	}
877 	*dpos = mbufcp;
878 	*mrep = mp;
879 	if (rem > 0) {
880 		if (len < rem)
881 			error = nfs_adv(mrep, dpos, rem, len);
882 		else
883 			*dpos += rem;
884 	}
885 	return (error);
886 }
887 
888 /*
889  * copies a uio scatter/gather list to an mbuf chain.
890  * NOTE: can ony handle iovcnt == 1
891  */
892 int
893 nfsm_uiotombuf(uiop, mq, siz, bpos)
894 	struct uio *uiop;
895 	struct mbuf **mq;
896 	int siz;
897 	caddr_t *bpos;
898 {
899 	char *uiocp;
900 	struct mbuf *mp, *mp2;
901 	int xfer, left, mlen;
902 	int uiosiz, clflg, rem;
903 	char *cp;
904 
905 #ifdef DIAGNOSTIC
906 	if (uiop->uio_iovcnt != 1)
907 		panic("nfsm_uiotombuf: iovcnt != 1");
908 #endif
909 
910 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
911 		clflg = 1;
912 	else
913 		clflg = 0;
914 	rem = nfsm_rndup(siz)-siz;
915 	mp = mp2 = *mq;
916 	while (siz > 0) {
917 		left = uiop->uio_iov->iov_len;
918 		uiocp = uiop->uio_iov->iov_base;
919 		if (left > siz)
920 			left = siz;
921 		uiosiz = left;
922 		while (left > 0) {
923 			mlen = M_TRAILINGSPACE(mp);
924 			if (mlen == 0) {
925 				mp = m_get(M_WAIT, MT_DATA);
926 				MCLAIM(mp, &nfs_mowner);
927 				if (clflg)
928 					m_clget(mp, M_WAIT);
929 				mp->m_len = 0;
930 				mp2->m_next = mp;
931 				mp2 = mp;
932 				mlen = M_TRAILINGSPACE(mp);
933 			}
934 			xfer = (left > mlen) ? mlen : left;
935 #ifdef notdef
936 			/* Not Yet.. */
937 			if (uiop->uio_iov->iov_op != NULL)
938 				(*(uiop->uio_iov->iov_op))
939 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
940 			else
941 #endif
942 			if (uiop->uio_segflg == UIO_SYSSPACE)
943 				memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
944 			else
945 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
946 			mp->m_len += xfer;
947 			left -= xfer;
948 			uiocp += xfer;
949 			uiop->uio_offset += xfer;
950 			uiop->uio_resid -= xfer;
951 		}
952 		uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
953 		    uiosiz;
954 		uiop->uio_iov->iov_len -= uiosiz;
955 		siz -= uiosiz;
956 	}
957 	if (rem > 0) {
958 		if (rem > M_TRAILINGSPACE(mp)) {
959 			mp = m_get(M_WAIT, MT_DATA);
960 			MCLAIM(mp, &nfs_mowner);
961 			mp->m_len = 0;
962 			mp2->m_next = mp;
963 		}
964 		cp = mtod(mp, caddr_t)+mp->m_len;
965 		for (left = 0; left < rem; left++)
966 			*cp++ = '\0';
967 		mp->m_len += rem;
968 		*bpos = cp;
969 	} else
970 		*bpos = mtod(mp, caddr_t)+mp->m_len;
971 	*mq = mp;
972 	return (0);
973 }
974 
975 /*
976  * Get at least "siz" bytes of correctly aligned data.
977  * When called the mbuf pointers are not necessarily correct,
978  * dsosp points to what ought to be in m_data and left contains
979  * what ought to be in m_len.
980  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
981  * cases. (The macros use the vars. dpos and dpos2)
982  */
983 int
984 nfsm_disct(mdp, dposp, siz, left, cp2)
985 	struct mbuf **mdp;
986 	caddr_t *dposp;
987 	int siz;
988 	int left;
989 	caddr_t *cp2;
990 {
991 	struct mbuf *m1, *m2;
992 	struct mbuf *havebuf = NULL;
993 	caddr_t src = *dposp;
994 	caddr_t dst;
995 	int len;
996 
997 #ifdef DEBUG
998 	if (left < 0)
999 		panic("nfsm_disct: left < 0");
1000 #endif
1001 	m1 = *mdp;
1002 	/*
1003 	 * Skip through the mbuf chain looking for an mbuf with
1004 	 * some data. If the first mbuf found has enough data
1005 	 * and it is correctly aligned return it.
1006 	 */
1007 	while (left == 0) {
1008 		havebuf = m1;
1009 		*mdp = m1 = m1->m_next;
1010 		if (m1 == NULL)
1011 			return (EBADRPC);
1012 		src = mtod(m1, caddr_t);
1013 		left = m1->m_len;
1014 		/*
1015 		 * If we start a new mbuf and it is big enough
1016 		 * and correctly aligned just return it, don't
1017 		 * do any pull up.
1018 		 */
1019 		if (left >= siz && nfsm_aligned(src)) {
1020 			*cp2 = src;
1021 			*dposp = src + siz;
1022 			return (0);
1023 		}
1024 	}
1025 	if (m1->m_flags & M_EXT) {
1026 		if (havebuf) {
1027 			/* If the first mbuf with data has external data
1028 			 * and there is a previous empty mbuf use it
1029 			 * to move the data into.
1030 			 */
1031 			m2 = m1;
1032 			*mdp = m1 = havebuf;
1033 			if (m1->m_flags & M_EXT) {
1034 				MEXTREMOVE(m1);
1035 			}
1036 		} else {
1037 			/*
1038 			 * If the first mbuf has a external data
1039 			 * and there is no previous empty mbuf
1040 			 * allocate a new mbuf and move the external
1041 			 * data to the new mbuf. Also make the first
1042 			 * mbuf look empty.
1043 			 */
1044 			m2 = m_get(M_WAIT, MT_DATA);
1045 			m2->m_ext = m1->m_ext;
1046 			m2->m_data = src;
1047 			m2->m_len = left;
1048 			MCLADDREFERENCE(m1, m2);
1049 			MEXTREMOVE(m1);
1050 			m2->m_next = m1->m_next;
1051 			m1->m_next = m2;
1052 		}
1053 		m1->m_len = 0;
1054 		dst = m1->m_dat;
1055 	} else {
1056 		/*
1057 		 * If the first mbuf has no external data
1058 		 * move the data to the front of the mbuf.
1059 		 */
1060 		if ((dst = m1->m_dat) != src)
1061 			memmove(dst, src, left);
1062 		dst += left;
1063 		m1->m_len = left;
1064 		m2 = m1->m_next;
1065 	}
1066 	m1->m_flags &= ~M_PKTHDR;
1067 	*cp2 = m1->m_data = m1->m_dat;   /* data is at beginning of buffer */
1068 	*dposp = mtod(m1, caddr_t) + siz;
1069 	/*
1070 	 * Loop through mbufs pulling data up into first mbuf until
1071 	 * the first mbuf is full or there is no more data to
1072 	 * pullup.
1073 	 */
1074 	while ((len = (MLEN - m1->m_len)) != 0 && m2) {
1075 		if ((len = min(len, m2->m_len)) != 0)
1076 			memcpy(dst, m2->m_data, len);
1077 		m1->m_len += len;
1078 		dst += len;
1079 		m2->m_data += len;
1080 		m2->m_len -= len;
1081 		m2 = m2->m_next;
1082 	}
1083 	if (m1->m_len < siz)
1084 		return (EBADRPC);
1085 	return (0);
1086 }
1087 
1088 /*
1089  * Advance the position in the mbuf chain.
1090  */
1091 int
1092 nfs_adv(mdp, dposp, offs, left)
1093 	struct mbuf **mdp;
1094 	caddr_t *dposp;
1095 	int offs;
1096 	int left;
1097 {
1098 	struct mbuf *m;
1099 	int s;
1100 
1101 	m = *mdp;
1102 	s = left;
1103 	while (s < offs) {
1104 		offs -= s;
1105 		m = m->m_next;
1106 		if (m == NULL)
1107 			return (EBADRPC);
1108 		s = m->m_len;
1109 	}
1110 	*mdp = m;
1111 	*dposp = mtod(m, caddr_t)+offs;
1112 	return (0);
1113 }
1114 
1115 /*
1116  * Copy a string into mbufs for the hard cases...
1117  */
1118 int
1119 nfsm_strtmbuf(mb, bpos, cp, siz)
1120 	struct mbuf **mb;
1121 	char **bpos;
1122 	const char *cp;
1123 	long siz;
1124 {
1125 	struct mbuf *m1 = NULL, *m2;
1126 	long left, xfer, len, tlen;
1127 	u_int32_t *tl;
1128 	int putsize;
1129 
1130 	putsize = 1;
1131 	m2 = *mb;
1132 	left = M_TRAILINGSPACE(m2);
1133 	if (left > 0) {
1134 		tl = ((u_int32_t *)(*bpos));
1135 		*tl++ = txdr_unsigned(siz);
1136 		putsize = 0;
1137 		left -= NFSX_UNSIGNED;
1138 		m2->m_len += NFSX_UNSIGNED;
1139 		if (left > 0) {
1140 			memcpy((caddr_t) tl, cp, left);
1141 			siz -= left;
1142 			cp += left;
1143 			m2->m_len += left;
1144 			left = 0;
1145 		}
1146 	}
1147 	/* Loop around adding mbufs */
1148 	while (siz > 0) {
1149 		m1 = m_get(M_WAIT, MT_DATA);
1150 		MCLAIM(m1, &nfs_mowner);
1151 		if (siz > MLEN)
1152 			m_clget(m1, M_WAIT);
1153 		m1->m_len = NFSMSIZ(m1);
1154 		m2->m_next = m1;
1155 		m2 = m1;
1156 		tl = mtod(m1, u_int32_t *);
1157 		tlen = 0;
1158 		if (putsize) {
1159 			*tl++ = txdr_unsigned(siz);
1160 			m1->m_len -= NFSX_UNSIGNED;
1161 			tlen = NFSX_UNSIGNED;
1162 			putsize = 0;
1163 		}
1164 		if (siz < m1->m_len) {
1165 			len = nfsm_rndup(siz);
1166 			xfer = siz;
1167 			if (xfer < len)
1168 				*(tl+(xfer>>2)) = 0;
1169 		} else {
1170 			xfer = len = m1->m_len;
1171 		}
1172 		memcpy((caddr_t) tl, cp, xfer);
1173 		m1->m_len = len+tlen;
1174 		siz -= xfer;
1175 		cp += xfer;
1176 	}
1177 	*mb = m1;
1178 	*bpos = mtod(m1, caddr_t)+m1->m_len;
1179 	return (0);
1180 }
1181 
1182 /*
1183  * Directory caching routines. They work as follows:
1184  * - a cache is maintained per VDIR nfsnode.
1185  * - for each offset cookie that is exported to userspace, and can
1186  *   thus be thrown back at us as an offset to VOP_READDIR, store
1187  *   information in the cache.
1188  * - cached are:
1189  *   - cookie itself
1190  *   - blocknumber (essentially just a search key in the buffer cache)
1191  *   - entry number in block.
1192  *   - offset cookie of block in which this entry is stored
1193  *   - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
1194  * - entries are looked up in a hash table
1195  * - also maintained is an LRU list of entries, used to determine
1196  *   which ones to delete if the cache grows too large.
1197  * - if 32 <-> 64 translation mode is requested for a filesystem,
1198  *   the cache also functions as a translation table
1199  * - in the translation case, invalidating the cache does not mean
1200  *   flushing it, but just marking entries as invalid, except for
1201  *   the <64bit cookie, 32bitcookie> pair which is still valid, to
1202  *   still be able to use the cache as a translation table.
1203  * - 32 bit cookies are uniquely created by combining the hash table
1204  *   entry value, and one generation count per hash table entry,
1205  *   incremented each time an entry is appended to the chain.
1206  * - the cache is invalidated each time a direcory is modified
1207  * - sanity checks are also done; if an entry in a block turns
1208  *   out not to have a matching cookie, the cache is invalidated
1209  *   and a new block starting from the wanted offset is fetched from
1210  *   the server.
1211  * - directory entries as read from the server are extended to contain
1212  *   the 64bit and, optionally, the 32bit cookies, for sanity checking
1213  *   the cache and exporting them to userspace through the cookie
1214  *   argument to VOP_READDIR.
1215  */
1216 
1217 u_long
1218 nfs_dirhash(off)
1219 	off_t off;
1220 {
1221 	int i;
1222 	char *cp = (char *)&off;
1223 	u_long sum = 0L;
1224 
1225 	for (i = 0 ; i < sizeof (off); i++)
1226 		sum += *cp++;
1227 
1228 	return sum;
1229 }
1230 
1231 void
1232 nfs_initdircache(vp)
1233 	struct vnode *vp;
1234 {
1235 	struct nfsnode *np = VTONFS(vp);
1236 
1237 	KASSERT(np->n_dircache == NULL);
1238 
1239 	np->n_dircachesize = 0;
1240 	np->n_dblkno = 1;
1241 	np->n_dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF,
1242 	    M_WAITOK, &nfsdirhashmask);
1243 	TAILQ_INIT(&np->n_dirchain);
1244 }
1245 
1246 void
1247 nfs_initdirxlatecookie(vp)
1248 	struct vnode *vp;
1249 {
1250 	struct nfsnode *np = VTONFS(vp);
1251 
1252 	KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE);
1253 	KASSERT(np->n_dirgens == NULL);
1254 
1255 	MALLOC(np->n_dirgens, unsigned *,
1256 	    NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF, M_WAITOK);
1257 	memset((caddr_t)np->n_dirgens, 0, NFS_DIRHASHSIZ * sizeof (unsigned));
1258 }
1259 
1260 static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
1261 
1262 struct nfsdircache *
1263 nfs_searchdircache(vp, off, do32, hashent)
1264 	struct vnode *vp;
1265 	off_t off;
1266 	int do32;
1267 	int *hashent;
1268 {
1269 	struct nfsdirhashhead *ndhp;
1270 	struct nfsdircache *ndp = NULL;
1271 	struct nfsnode *np = VTONFS(vp);
1272 	unsigned ent;
1273 
1274 	/*
1275 	 * Zero is always a valid cookie.
1276 	 */
1277 	if (off == 0)
1278 		return &dzero;
1279 
1280 	/*
1281 	 * We use a 32bit cookie as search key, directly reconstruct
1282 	 * the hashentry. Else use the hashfunction.
1283 	 */
1284 	if (do32) {
1285 		ent = (u_int32_t)off >> 24;
1286 		if (ent >= NFS_DIRHASHSIZ)
1287 			return NULL;
1288 		ndhp = &np->n_dircache[ent];
1289 	} else {
1290 		ndhp = NFSDIRHASH(np, off);
1291 	}
1292 
1293 	if (hashent)
1294 		*hashent = (int)(ndhp - np->n_dircache);
1295 	if (do32) {
1296 		LIST_FOREACH(ndp, ndhp, dc_hash) {
1297 			if (ndp->dc_cookie32 == (u_int32_t)off) {
1298 				/*
1299 				 * An invalidated entry will become the
1300 				 * start of a new block fetched from
1301 				 * the server.
1302 				 */
1303 				if (ndp->dc_blkno == -1) {
1304 					ndp->dc_blkcookie = ndp->dc_cookie;
1305 					ndp->dc_blkno = np->n_dblkno++;
1306 					ndp->dc_entry = 0;
1307 				}
1308 				break;
1309 			}
1310 		}
1311 	} else {
1312 		LIST_FOREACH(ndp, ndhp, dc_hash) {
1313 			if (ndp->dc_cookie == off)
1314 				break;
1315 		}
1316 	}
1317 	return ndp;
1318 }
1319 
1320 
1321 struct nfsdircache *
1322 nfs_enterdircache(vp, off, blkoff, en, blkno)
1323 	struct vnode *vp;
1324 	off_t off, blkoff;
1325 	int en;
1326 	daddr_t blkno;
1327 {
1328 	struct nfsnode *np = VTONFS(vp);
1329 	struct nfsdirhashhead *ndhp;
1330 	struct nfsdircache *ndp = NULL, *first;
1331 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1332 	int hashent, gen, overwrite;
1333 
1334 	if (!np->n_dircache)
1335 		/*
1336 		 * XXX would like to do this in nfs_nget but vtype
1337 		 * isn't known at that time.
1338 		 */
1339 		nfs_initdircache(vp);
1340 
1341 	if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens)
1342 		nfs_initdirxlatecookie(vp);
1343 
1344 	/*
1345 	 * XXX refuse entries for offset 0. amd(8) erroneously sets
1346 	 * cookie 0 for the '.' entry, making this necessary. This
1347 	 * isn't so bad, as 0 is a special case anyway.
1348 	 */
1349 	if (off == 0)
1350 		return &dzero;
1351 
1352 	ndp = nfs_searchdircache(vp, off, 0, &hashent);
1353 
1354 	if (ndp && ndp->dc_blkno != -1) {
1355 		/*
1356 		 * Overwriting an old entry. Check if it's the same.
1357 		 * If so, just return. If not, remove the old entry.
1358 		 */
1359 		if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
1360 			return ndp;
1361 		TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1362 		LIST_REMOVE(ndp, dc_hash);
1363 		FREE(ndp, M_NFSDIROFF);
1364 		ndp = 0;
1365 	}
1366 
1367 	ndhp = &np->n_dircache[hashent];
1368 
1369 	if (!ndp) {
1370 		MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
1371 		    M_WAITOK);
1372 		overwrite = 0;
1373 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1374 			/*
1375 			 * We're allocating a new entry, so bump the
1376 			 * generation number.
1377 			 */
1378 			gen = ++np->n_dirgens[hashent];
1379 			if (gen == 0) {
1380 				np->n_dirgens[hashent]++;
1381 				gen++;
1382 			}
1383 			ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
1384 		}
1385 	} else
1386 		overwrite = 1;
1387 
1388 	/*
1389 	 * If the entry number is 0, we are at the start of a new block, so
1390 	 * allocate a new blocknumber.
1391 	 */
1392 	if (en == 0)
1393 		ndp->dc_blkno = np->n_dblkno++;
1394 	else
1395 		ndp->dc_blkno = blkno;
1396 
1397 	ndp->dc_cookie = off;
1398 	ndp->dc_blkcookie = blkoff;
1399 	ndp->dc_entry = en;
1400 
1401 	if (overwrite)
1402 		return ndp;
1403 
1404 	/*
1405 	 * If the maximum directory cookie cache size has been reached
1406 	 * for this node, take one off the front. The idea is that
1407 	 * directories are typically read front-to-back once, so that
1408 	 * the oldest entries can be thrown away without much performance
1409 	 * loss.
1410 	 */
1411 	if (np->n_dircachesize == NFS_MAXDIRCACHE) {
1412 		first = TAILQ_FIRST(&np->n_dirchain);
1413 		TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
1414 		LIST_REMOVE(first, dc_hash);
1415 		FREE(first, M_NFSDIROFF);
1416 	} else
1417 		np->n_dircachesize++;
1418 
1419 	LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
1420 	TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
1421 	return ndp;
1422 }
1423 
1424 void
1425 nfs_invaldircache(vp, forcefree)
1426 	struct vnode *vp;
1427 	int forcefree;
1428 {
1429 	struct nfsnode *np = VTONFS(vp);
1430 	struct nfsdircache *ndp = NULL;
1431 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1432 
1433 #ifdef DIAGNOSTIC
1434 	if (vp->v_type != VDIR)
1435 		panic("nfs: invaldircache: not dir");
1436 #endif
1437 
1438 	if (!np->n_dircache)
1439 		return;
1440 
1441 	if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
1442 		while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != 0) {
1443 			TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1444 			LIST_REMOVE(ndp, dc_hash);
1445 			FREE(ndp, M_NFSDIROFF);
1446 		}
1447 		np->n_dircachesize = 0;
1448 		if (forcefree && np->n_dirgens) {
1449 			FREE(np->n_dirgens, M_NFSDIROFF);
1450 			np->n_dirgens = NULL;
1451 		}
1452 	} else {
1453 		TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain) {
1454 			ndp->dc_blkno = -1;
1455 		}
1456 	}
1457 
1458 	np->n_dblkno = 1;
1459 }
1460 
1461 /*
1462  * Called once before VFS init to initialize shared and
1463  * server-specific data structures.
1464  */
1465 void
1466 nfs_init()
1467 {
1468 	nfsrtt.pos = 0;
1469 	rpc_vers = txdr_unsigned(RPC_VER2);
1470 	rpc_call = txdr_unsigned(RPC_CALL);
1471 	rpc_reply = txdr_unsigned(RPC_REPLY);
1472 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1473 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1474 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1475 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1476 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1477 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1478 	nfs_prog = txdr_unsigned(NFS_PROG);
1479 	nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1480 	nfs_true = txdr_unsigned(TRUE);
1481 	nfs_false = txdr_unsigned(FALSE);
1482 	nfs_xdrneg1 = txdr_unsigned(-1);
1483 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1484 	if (nfs_ticks < 1)
1485 		nfs_ticks = 1;
1486 #ifdef NFSSERVER
1487 	nfsrv_init(0);			/* Init server data structures */
1488 	nfsrv_initcache();		/* Init the server request cache */
1489 	pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
1490 	    0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr);
1491 #endif /* NFSSERVER */
1492 
1493 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
1494 	/*
1495 	 * Initialize the nqnfs data structures.
1496 	 */
1497 	if (nqnfsstarttime == 0) {
1498 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1499 			+ nqsrv_clockskew + nqsrv_writeslack;
1500 		NQLOADNOVRAM(nqnfsstarttime);
1501 		CIRCLEQ_INIT(&nqtimerhead);
1502 		nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE,
1503 		    M_WAITOK, &nqfhhash);
1504 	}
1505 #endif
1506 
1507 	exithook_establish(nfs_exit, NULL);
1508 
1509 	/*
1510 	 * Initialize reply list and start timer
1511 	 */
1512 	TAILQ_INIT(&nfs_reqq);
1513 	nfs_timer(NULL);
1514 	MOWNER_ATTACH(&nfs_mowner);
1515 
1516 #ifdef NFS
1517 	/* Initialize the kqueue structures */
1518 	nfs_kqinit();
1519 	/* Initialize the iod structures */
1520 	nfs_iodinit();
1521 #endif
1522 }
1523 
1524 #ifdef NFS
1525 /*
1526  * Called once at VFS init to initialize client-specific data structures.
1527  */
1528 void
1529 nfs_vfs_init()
1530 {
1531 	nfs_nhinit();			/* Init the nfsnode table */
1532 	nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
1533 }
1534 
1535 void
1536 nfs_vfs_reinit()
1537 {
1538 	nfs_nhreinit();
1539 }
1540 
1541 void
1542 nfs_vfs_done()
1543 {
1544 	nfs_nhdone();
1545 }
1546 
1547 /*
1548  * Attribute cache routines.
1549  * nfs_loadattrcache() - loads or updates the cache contents from attributes
1550  *	that are on the mbuf list
1551  * nfs_getattrcache() - returns valid attributes if found in cache, returns
1552  *	error otherwise
1553  */
1554 
1555 /*
1556  * Load the attribute cache (that lives in the nfsnode entry) with
1557  * the values on the mbuf list and
1558  * Iff vap not NULL
1559  *    copy the attributes to *vaper
1560  */
1561 int
1562 nfsm_loadattrcache(vpp, mdp, dposp, vaper, flags)
1563 	struct vnode **vpp;
1564 	struct mbuf **mdp;
1565 	caddr_t *dposp;
1566 	struct vattr *vaper;
1567 	int flags;
1568 {
1569 	int32_t t1;
1570 	caddr_t cp2;
1571 	int error = 0;
1572 	struct mbuf *md;
1573 	int v3 = NFS_ISV3(*vpp);
1574 
1575 	md = *mdp;
1576 	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1577 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1578 	if (error)
1579 		return (error);
1580 	return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper, flags);
1581 }
1582 
1583 int
1584 nfs_loadattrcache(vpp, fp, vaper, flags)
1585 	struct vnode **vpp;
1586 	struct nfs_fattr *fp;
1587 	struct vattr *vaper;
1588 	int flags;
1589 {
1590 	struct vnode *vp = *vpp;
1591 	struct vattr *vap;
1592 	int v3 = NFS_ISV3(vp);
1593 	enum vtype vtyp;
1594 	u_short vmode;
1595 	struct timespec mtime;
1596 	struct vnode *nvp;
1597 	int32_t rdev;
1598 	struct nfsnode *np;
1599 	extern int (**spec_nfsv2nodeop_p) __P((void *));
1600 	uid_t uid;
1601 	gid_t gid;
1602 
1603 	if (v3) {
1604 		vtyp = nfsv3tov_type(fp->fa_type);
1605 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1606 		rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
1607 			fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
1608 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1609 	} else {
1610 		vtyp = nfsv2tov_type(fp->fa_type);
1611 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1612 		if (vtyp == VNON || vtyp == VREG)
1613 			vtyp = IFTOVT(vmode);
1614 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1615 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1616 
1617 		/*
1618 		 * Really ugly NFSv2 kludge.
1619 		 */
1620 		if (vtyp == VCHR && rdev == 0xffffffff)
1621 			vtyp = VFIFO;
1622 	}
1623 
1624 	vmode &= ALLPERMS;
1625 
1626 	/*
1627 	 * If v_type == VNON it is a new node, so fill in the v_type,
1628 	 * n_mtime fields. Check to see if it represents a special
1629 	 * device, and if so, check for a possible alias. Once the
1630 	 * correct vnode has been obtained, fill in the rest of the
1631 	 * information.
1632 	 */
1633 	np = VTONFS(vp);
1634 	if (vp->v_type == VNON) {
1635 		vp->v_type = vtyp;
1636 		if (vp->v_type == VFIFO) {
1637 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
1638 			vp->v_op = fifo_nfsv2nodeop_p;
1639 		}
1640 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
1641 			vp->v_op = spec_nfsv2nodeop_p;
1642 			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1643 			if (nvp) {
1644 				/*
1645 				 * Discard unneeded vnode, but save its nfsnode.
1646 				 * Since the nfsnode does not have a lock, its
1647 				 * vnode lock has to be carried over.
1648 				 */
1649 				/*
1650 				 * XXX is the old node sure to be locked here?
1651 				 */
1652 				KASSERT(lockstatus(&vp->v_lock) ==
1653 				    LK_EXCLUSIVE);
1654 				nvp->v_data = vp->v_data;
1655 				vp->v_data = NULL;
1656 				VOP_UNLOCK(vp, 0);
1657 				vp->v_op = spec_vnodeop_p;
1658 				vrele(vp);
1659 				vgone(vp);
1660 				lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
1661 				    &nvp->v_interlock);
1662 				/*
1663 				 * Reinitialize aliased node.
1664 				 */
1665 				np->n_vnode = nvp;
1666 				*vpp = vp = nvp;
1667 			}
1668 		}
1669 		np->n_mtime = mtime.tv_sec;
1670 	}
1671 	uid = fxdr_unsigned(uid_t, fp->fa_uid);
1672 	gid = fxdr_unsigned(gid_t, fp->fa_gid);
1673 	vap = np->n_vattr;
1674 
1675 	/*
1676 	 * Invalidate access cache if uid, gid or mode changed.
1677 	 */
1678 	if (np->n_accstamp != -1 &&
1679 	    (gid != vap->va_gid || uid != vap->va_uid || vmode != vap->va_mode))
1680 		np->n_accstamp = -1;
1681 
1682 	vap->va_type = vtyp;
1683 	vap->va_mode = vmode;
1684 	vap->va_rdev = (dev_t)rdev;
1685 	vap->va_mtime = mtime;
1686 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1687 	switch (vtyp) {
1688 	case VDIR:
1689 		vap->va_blocksize = NFS_DIRFRAGSIZ;
1690 		break;
1691 	case VBLK:
1692 		vap->va_blocksize = BLKDEV_IOSIZE;
1693 		break;
1694 	case VCHR:
1695 		vap->va_blocksize = MAXBSIZE;
1696 		break;
1697 	default:
1698 		vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
1699 		    fxdr_unsigned(int32_t, fp->fa2_blocksize);
1700 		break;
1701 	}
1702 	if (v3) {
1703 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1704 		vap->va_uid = uid;
1705 		vap->va_gid = gid;
1706 		vap->va_size = fxdr_hyper(&fp->fa3_size);
1707 		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1708 		vap->va_fileid = fxdr_unsigned(int32_t,
1709 		    fp->fa3_fileid.nfsuquad[1]);
1710 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1711 		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1712 		vap->va_flags = 0;
1713 		vap->va_filerev = 0;
1714 	} else {
1715 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1716 		vap->va_uid = uid;
1717 		vap->va_gid = gid;
1718 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1719 		vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1720 		    * NFS_FABLKSIZE;
1721 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1722 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1723 		vap->va_flags = 0;
1724 		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1725 		    fp->fa2_ctime.nfsv2_sec);
1726 		vap->va_ctime.tv_nsec = 0;
1727 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1728 		vap->va_filerev = 0;
1729 	}
1730 	if (vap->va_size != np->n_size) {
1731 		if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) {
1732 			vap->va_size = np->n_size;
1733 		} else {
1734 			np->n_size = vap->va_size;
1735 			if (vap->va_type == VREG) {
1736 				if ((flags & NAC_NOTRUNC)
1737 				    && np->n_size < vp->v_size) {
1738 					/*
1739 					 * we can't free pages now because
1740 					 * the pages can be owned by ourselves.
1741 					 */
1742 					np->n_flag |= NTRUNCDELAYED;
1743 				}
1744 				else {
1745 					uvm_vnp_setsize(vp, np->n_size);
1746 				}
1747 			}
1748 		}
1749 	}
1750 	np->n_attrstamp = time.tv_sec;
1751 	if (vaper != NULL) {
1752 		memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1753 		if (np->n_flag & NCHG) {
1754 			if (np->n_flag & NACC)
1755 				vaper->va_atime = np->n_atim;
1756 			if (np->n_flag & NUPD)
1757 				vaper->va_mtime = np->n_mtim;
1758 		}
1759 	}
1760 	return (0);
1761 }
1762 
1763 /*
1764  * Check the time stamp
1765  * If the cache is valid, copy contents to *vap and return 0
1766  * otherwise return an error
1767  */
1768 int
1769 nfs_getattrcache(vp, vaper)
1770 	struct vnode *vp;
1771 	struct vattr *vaper;
1772 {
1773 	struct nfsnode *np = VTONFS(vp);
1774 	struct vattr *vap;
1775 
1776 	if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1777 		nfsstats.attrcache_misses++;
1778 		return (ENOENT);
1779 	}
1780 	nfsstats.attrcache_hits++;
1781 	vap = np->n_vattr;
1782 	if (vap->va_size != np->n_size) {
1783 		if (vap->va_type == VREG) {
1784 			if (np->n_flag & NMODIFIED) {
1785 				if (vap->va_size < np->n_size)
1786 					vap->va_size = np->n_size;
1787 				else
1788 					np->n_size = vap->va_size;
1789 			} else
1790 				np->n_size = vap->va_size;
1791 			uvm_vnp_setsize(vp, np->n_size);
1792 		} else
1793 			np->n_size = vap->va_size;
1794 	}
1795 	memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
1796 	if (np->n_flag & NCHG) {
1797 		if (np->n_flag & NACC)
1798 			vaper->va_atime = np->n_atim;
1799 		if (np->n_flag & NUPD)
1800 			vaper->va_mtime = np->n_mtim;
1801 	}
1802 	return (0);
1803 }
1804 
1805 void
1806 nfs_delayedtruncate(vp)
1807 	struct vnode *vp;
1808 {
1809 	struct nfsnode *np = VTONFS(vp);
1810 
1811 	if (np->n_flag & NTRUNCDELAYED) {
1812 		np->n_flag &= ~NTRUNCDELAYED;
1813 		uvm_vnp_setsize(vp, np->n_size);
1814 	}
1815 }
1816 
1817 /*
1818  * Heuristic to see if the server XDR encodes directory cookies or not.
1819  * it is not supposed to, but a lot of servers may do this. Also, since
1820  * most/all servers will implement V2 as well, it is expected that they
1821  * may return just 32 bits worth of cookie information, so we need to
1822  * find out in which 32 bits this information is available. We do this
1823  * to avoid trouble with emulated binaries that can't handle 64 bit
1824  * directory offsets.
1825  */
1826 
1827 void
1828 nfs_cookieheuristic(vp, flagp, p, cred)
1829 	struct vnode *vp;
1830 	int *flagp;
1831 	struct proc *p;
1832 	struct ucred *cred;
1833 {
1834 	struct uio auio;
1835 	struct iovec aiov;
1836 	caddr_t buf, cp;
1837 	struct dirent *dp;
1838 	off_t *cookies = NULL, *cop;
1839 	int error, eof, nc, len;
1840 
1841 	MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
1842 
1843 	aiov.iov_base = buf;
1844 	aiov.iov_len = NFS_DIRFRAGSIZ;
1845 	auio.uio_iov = &aiov;
1846 	auio.uio_iovcnt = 1;
1847 	auio.uio_rw = UIO_READ;
1848 	auio.uio_segflg = UIO_SYSSPACE;
1849 	auio.uio_procp = p;
1850 	auio.uio_resid = NFS_DIRFRAGSIZ;
1851 	auio.uio_offset = 0;
1852 
1853 	error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
1854 
1855 	len = NFS_DIRFRAGSIZ - auio.uio_resid;
1856 	if (error || len == 0) {
1857 		FREE(buf, M_TEMP);
1858 		if (cookies)
1859 			free(cookies, M_TEMP);
1860 		return;
1861 	}
1862 
1863 	/*
1864 	 * Find the first valid entry and look at its offset cookie.
1865 	 */
1866 
1867 	cp = buf;
1868 	for (cop = cookies; len > 0; len -= dp->d_reclen) {
1869 		dp = (struct dirent *)cp;
1870 		if (dp->d_fileno != 0 && len >= dp->d_reclen) {
1871 			if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
1872 				*flagp |= NFSMNT_SWAPCOOKIE;
1873 				nfs_invaldircache(vp, 0);
1874 				nfs_vinvalbuf(vp, 0, cred, p, 1);
1875 			}
1876 			break;
1877 		}
1878 		cop++;
1879 		cp += dp->d_reclen;
1880 	}
1881 
1882 	FREE(buf, M_TEMP);
1883 	free(cookies, M_TEMP);
1884 }
1885 #endif /* NFS */
1886 
1887 /*
1888  * Set up nameidata for a lookup() call and do it.
1889  *
1890  * If pubflag is set, this call is done for a lookup operation on the
1891  * public filehandle. In that case we allow crossing mountpoints and
1892  * absolute pathnames. However, the caller is expected to check that
1893  * the lookup result is within the public fs, and deny access if
1894  * it is not.
1895  */
1896 int
1897 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1898 	struct nameidata *ndp;
1899 	fhandle_t *fhp;
1900 	uint32_t len;
1901 	struct nfssvc_sock *slp;
1902 	struct mbuf *nam;
1903 	struct mbuf **mdp;
1904 	caddr_t *dposp;
1905 	struct vnode **retdirp;
1906 	struct proc *p;
1907 	int kerbflag, pubflag;
1908 {
1909 	int i, rem;
1910 	struct mbuf *md;
1911 	char *fromcp, *tocp, *cp;
1912 	struct iovec aiov;
1913 	struct uio auio;
1914 	struct vnode *dp;
1915 	int error, rdonly, linklen;
1916 	struct componentname *cnp = &ndp->ni_cnd;
1917 
1918 	*retdirp = (struct vnode *)0;
1919 
1920 	if ((len + 1) > MAXPATHLEN)
1921 		return (ENAMETOOLONG);
1922 	cnp->cn_pnbuf = PNBUF_GET();
1923 
1924 	/*
1925 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
1926 	 * and set the various ndp fields appropriately.
1927 	 */
1928 	fromcp = *dposp;
1929 	tocp = cnp->cn_pnbuf;
1930 	md = *mdp;
1931 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
1932 	for (i = 0; i < len; i++) {
1933 		while (rem == 0) {
1934 			md = md->m_next;
1935 			if (md == NULL) {
1936 				error = EBADRPC;
1937 				goto out;
1938 			}
1939 			fromcp = mtod(md, caddr_t);
1940 			rem = md->m_len;
1941 		}
1942 		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1943 			error = EACCES;
1944 			goto out;
1945 		}
1946 		*tocp++ = *fromcp++;
1947 		rem--;
1948 	}
1949 	*tocp = '\0';
1950 	*mdp = md;
1951 	*dposp = fromcp;
1952 	len = nfsm_rndup(len)-len;
1953 	if (len > 0) {
1954 		if (rem >= len)
1955 			*dposp += len;
1956 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1957 			goto out;
1958 	}
1959 
1960 	/*
1961 	 * Extract and set starting directory.
1962 	 */
1963 	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1964 	    nam, &rdonly, kerbflag, pubflag);
1965 	if (error)
1966 		goto out;
1967 	if (dp->v_type != VDIR) {
1968 		vrele(dp);
1969 		error = ENOTDIR;
1970 		goto out;
1971 	}
1972 
1973 	if (rdonly)
1974 		cnp->cn_flags |= RDONLY;
1975 
1976 	*retdirp = dp;
1977 
1978 	if (pubflag) {
1979 		/*
1980 		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1981 		 * and the 'native path' indicator.
1982 		 */
1983 		cp = PNBUF_GET();
1984 		fromcp = cnp->cn_pnbuf;
1985 		tocp = cp;
1986 		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1987 			switch ((unsigned char)*fromcp) {
1988 			case WEBNFS_NATIVE_CHAR:
1989 				/*
1990 				 * 'Native' path for us is the same
1991 				 * as a path according to the NFS spec,
1992 				 * just skip the escape char.
1993 				 */
1994 				fromcp++;
1995 				break;
1996 			/*
1997 			 * More may be added in the future, range 0x80-0xff
1998 			 */
1999 			default:
2000 				error = EIO;
2001 				PNBUF_PUT(cp);
2002 				goto out;
2003 			}
2004 		}
2005 		/*
2006 		 * Translate the '%' escapes, URL-style.
2007 		 */
2008 		while (*fromcp != '\0') {
2009 			if (*fromcp == WEBNFS_ESC_CHAR) {
2010 				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
2011 					fromcp++;
2012 					*tocp++ = HEXSTRTOI(fromcp);
2013 					fromcp += 2;
2014 					continue;
2015 				} else {
2016 					error = ENOENT;
2017 					PNBUF_PUT(cp);
2018 					goto out;
2019 				}
2020 			} else
2021 				*tocp++ = *fromcp++;
2022 		}
2023 		*tocp = '\0';
2024 		PNBUF_PUT(cnp->cn_pnbuf);
2025 		cnp->cn_pnbuf = cp;
2026 	}
2027 
2028 	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
2029 	ndp->ni_segflg = UIO_SYSSPACE;
2030 	ndp->ni_rootdir = rootvnode;
2031 
2032 	if (pubflag) {
2033 		ndp->ni_loopcnt = 0;
2034 		if (cnp->cn_pnbuf[0] == '/')
2035 			dp = rootvnode;
2036 	} else {
2037 		cnp->cn_flags |= NOCROSSMOUNT;
2038 	}
2039 
2040 	cnp->cn_proc = p;
2041 	VREF(dp);
2042 
2043     for (;;) {
2044 	cnp->cn_nameptr = cnp->cn_pnbuf;
2045 	ndp->ni_startdir = dp;
2046 	/*
2047 	 * And call lookup() to do the real work
2048 	 */
2049 	error = lookup(ndp);
2050 	if (error) {
2051 		PNBUF_PUT(cnp->cn_pnbuf);
2052 		return (error);
2053 	}
2054 	/*
2055 	 * Check for encountering a symbolic link
2056 	 */
2057 	if ((cnp->cn_flags & ISSYMLINK) == 0) {
2058 		if (cnp->cn_flags & (SAVENAME | SAVESTART))
2059 			cnp->cn_flags |= HASBUF;
2060 		else
2061 			PNBUF_PUT(cnp->cn_pnbuf);
2062 		return (0);
2063 	} else {
2064 		if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
2065 			VOP_UNLOCK(ndp->ni_dvp, 0);
2066 		if (!pubflag) {
2067 			error = EINVAL;
2068 			break;
2069 		}
2070 
2071 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
2072 			error = ELOOP;
2073 			break;
2074 		}
2075 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
2076 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
2077 			    cnp->cn_proc);
2078 			if (error != 0)
2079 				break;
2080 		}
2081 		if (ndp->ni_pathlen > 1)
2082 			cp = PNBUF_GET();
2083 		else
2084 			cp = cnp->cn_pnbuf;
2085 		aiov.iov_base = cp;
2086 		aiov.iov_len = MAXPATHLEN;
2087 		auio.uio_iov = &aiov;
2088 		auio.uio_iovcnt = 1;
2089 		auio.uio_offset = 0;
2090 		auio.uio_rw = UIO_READ;
2091 		auio.uio_segflg = UIO_SYSSPACE;
2092 		auio.uio_procp = (struct proc *)0;
2093 		auio.uio_resid = MAXPATHLEN;
2094 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2095 		if (error) {
2096 		badlink:
2097 			if (ndp->ni_pathlen > 1)
2098 				PNBUF_PUT(cp);
2099 			break;
2100 		}
2101 		linklen = MAXPATHLEN - auio.uio_resid;
2102 		if (linklen == 0) {
2103 			error = ENOENT;
2104 			goto badlink;
2105 		}
2106 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2107 			error = ENAMETOOLONG;
2108 			goto badlink;
2109 		}
2110 		if (ndp->ni_pathlen > 1) {
2111 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
2112 			PNBUF_PUT(cnp->cn_pnbuf);
2113 			cnp->cn_pnbuf = cp;
2114 		} else
2115 			cnp->cn_pnbuf[linklen] = '\0';
2116 		ndp->ni_pathlen += linklen;
2117 		vput(ndp->ni_vp);
2118 		dp = ndp->ni_dvp;
2119 		/*
2120 		 * Check if root directory should replace current directory.
2121 		 */
2122 		if (cnp->cn_pnbuf[0] == '/') {
2123 			vrele(dp);
2124 			dp = ndp->ni_rootdir;
2125 			VREF(dp);
2126 		}
2127 	}
2128    }
2129 	vrele(ndp->ni_dvp);
2130 	vput(ndp->ni_vp);
2131 	ndp->ni_vp = NULL;
2132 out:
2133 	PNBUF_PUT(cnp->cn_pnbuf);
2134 	return (error);
2135 }
2136 
2137 /*
2138  * A fiddled version of m_adj() that ensures null fill to a 32-bit
2139  * boundary and only trims off the back end
2140  *
2141  * 1. trim off 'len' bytes as m_adj(mp, -len).
2142  * 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
2143  */
2144 void
2145 nfs_zeropad(mp, len, nul)
2146 	struct mbuf *mp;
2147 	int len;
2148 	int nul;
2149 {
2150 	struct mbuf *m;
2151 	int count, i;
2152 	char *cp;
2153 
2154 	/*
2155 	 * Trim from tail.  Scan the mbuf chain,
2156 	 * calculating its length and finding the last mbuf.
2157 	 * If the adjustment only affects this mbuf, then just
2158 	 * adjust and return.  Otherwise, rescan and truncate
2159 	 * after the remaining size.
2160 	 */
2161 	count = 0;
2162 	m = mp;
2163 	for (;;) {
2164 		count += m->m_len;
2165 		if (m->m_next == NULL)
2166 			break;
2167 		m = m->m_next;
2168 	}
2169 
2170 	KDASSERT(count >= len);
2171 
2172 	if (m->m_len >= len) {
2173 		m->m_len -= len;
2174 	} else {
2175 		count -= len;
2176 		/*
2177 		 * Correct length for chain is "count".
2178 		 * Find the mbuf with last data, adjust its length,
2179 		 * and toss data from remaining mbufs on chain.
2180 		 */
2181 		for (m = mp; m; m = m->m_next) {
2182 			if (m->m_len >= count) {
2183 				m->m_len = count;
2184 				break;
2185 			}
2186 			count -= m->m_len;
2187 		}
2188 		m_freem(m->m_next);
2189 		m->m_next = NULL;
2190 	}
2191 
2192 	/*
2193 	 * zero-padding.
2194 	 */
2195 	if (nul > 0) {
2196 		if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) {
2197 			struct mbuf *n;
2198 
2199 			KDASSERT(MLEN >= nul);
2200 			n = m_get(M_WAIT, MT_DATA);
2201 			MCLAIM(n, &nfs_mowner);
2202 			n->m_len = nul;
2203 			n->m_next = m->m_next;
2204 			m->m_next = n;
2205 			m = n;
2206 			cp = mtod(n, caddr_t);
2207 		} else {
2208 			cp = mtod(m, caddr_t) + m->m_len;
2209 			m->m_len += nul;
2210 		}
2211 		for (i = 0; i < nul; i++)
2212 			*cp++ = '\0';
2213 	}
2214 	return;
2215 }
2216 
2217 /*
2218  * Make these functions instead of macros, so that the kernel text size
2219  * doesn't get too big...
2220  */
2221 void
2222 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2223 	struct nfsrv_descript *nfsd;
2224 	int before_ret;
2225 	struct vattr *before_vap;
2226 	int after_ret;
2227 	struct vattr *after_vap;
2228 	struct mbuf **mbp;
2229 	char **bposp;
2230 {
2231 	struct mbuf *mb = *mbp;
2232 	char *bpos = *bposp;
2233 	u_int32_t *tl;
2234 
2235 	if (before_ret) {
2236 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2237 		*tl = nfs_false;
2238 	} else {
2239 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2240 		*tl++ = nfs_true;
2241 		txdr_hyper(before_vap->va_size, tl);
2242 		tl += 2;
2243 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
2244 		tl += 2;
2245 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
2246 	}
2247 	*bposp = bpos;
2248 	*mbp = mb;
2249 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2250 }
2251 
2252 void
2253 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2254 	struct nfsrv_descript *nfsd;
2255 	int after_ret;
2256 	struct vattr *after_vap;
2257 	struct mbuf **mbp;
2258 	char **bposp;
2259 {
2260 	struct mbuf *mb = *mbp;
2261 	char *bpos = *bposp;
2262 	u_int32_t *tl;
2263 	struct nfs_fattr *fp;
2264 
2265 	if (after_ret) {
2266 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2267 		*tl = nfs_false;
2268 	} else {
2269 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2270 		*tl++ = nfs_true;
2271 		fp = (struct nfs_fattr *)tl;
2272 		nfsm_srvfattr(nfsd, after_vap, fp);
2273 	}
2274 	*mbp = mb;
2275 	*bposp = bpos;
2276 }
2277 
2278 void
2279 nfsm_srvfattr(nfsd, vap, fp)
2280 	struct nfsrv_descript *nfsd;
2281 	struct vattr *vap;
2282 	struct nfs_fattr *fp;
2283 {
2284 
2285 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2286 	fp->fa_uid = txdr_unsigned(vap->va_uid);
2287 	fp->fa_gid = txdr_unsigned(vap->va_gid);
2288 	if (nfsd->nd_flag & ND_NFSV3) {
2289 		fp->fa_type = vtonfsv3_type(vap->va_type);
2290 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2291 		txdr_hyper(vap->va_size, &fp->fa3_size);
2292 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
2293 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2294 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2295 		fp->fa3_fsid.nfsuquad[0] = 0;
2296 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2297 		fp->fa3_fileid.nfsuquad[0] = 0;
2298 		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
2299 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2300 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2301 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2302 	} else {
2303 		fp->fa_type = vtonfsv2_type(vap->va_type);
2304 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2305 		fp->fa2_size = txdr_unsigned(vap->va_size);
2306 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2307 		if (vap->va_type == VFIFO)
2308 			fp->fa2_rdev = 0xffffffff;
2309 		else
2310 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2311 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2312 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2313 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2314 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2315 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2316 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2317 	}
2318 }
2319 
2320 /*
2321  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2322  * 	- look up fsid in mount list (if not found ret error)
2323  *	- get vp and export rights by calling VFS_FHTOVP()
2324  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2325  *	- if not lockflag unlock it with VOP_UNLOCK()
2326  */
2327 int
2328 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2329 	fhandle_t *fhp;
2330 	int lockflag;
2331 	struct vnode **vpp;
2332 	struct ucred *cred;
2333 	struct nfssvc_sock *slp;
2334 	struct mbuf *nam;
2335 	int *rdonlyp;
2336 	int kerbflag;
2337 {
2338 	struct mount *mp;
2339 	int i;
2340 	struct ucred *credanon;
2341 	int error, exflags;
2342 	struct sockaddr_in *saddr;
2343 
2344 	*vpp = (struct vnode *)0;
2345 
2346 	if (nfs_ispublicfh(fhp)) {
2347 		if (!pubflag || !nfs_pub.np_valid)
2348 			return (ESTALE);
2349 		fhp = &nfs_pub.np_handle;
2350 	}
2351 
2352 	mp = vfs_getvfs(&fhp->fh_fsid);
2353 	if (!mp)
2354 		return (ESTALE);
2355 	error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
2356 	if (error)
2357 		return (error);
2358 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2359 	if (error)
2360 		return (error);
2361 
2362 	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2363 		saddr = mtod(nam, struct sockaddr_in *);
2364 		if ((saddr->sin_family == AF_INET) &&
2365 		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2366 			vput(*vpp);
2367 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2368 		}
2369 #ifdef INET6
2370 		if ((saddr->sin_family == AF_INET6) &&
2371 		    ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
2372 			vput(*vpp);
2373 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2374 		}
2375 #endif
2376 	}
2377 	/*
2378 	 * Check/setup credentials.
2379 	 */
2380 	if (exflags & MNT_EXKERB) {
2381 		if (!kerbflag) {
2382 			vput(*vpp);
2383 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2384 		}
2385 	} else if (kerbflag) {
2386 		vput(*vpp);
2387 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2388 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2389 		cred->cr_uid = credanon->cr_uid;
2390 		cred->cr_gid = credanon->cr_gid;
2391 		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2392 			cred->cr_groups[i] = credanon->cr_groups[i];
2393 		cred->cr_ngroups = i;
2394 	}
2395 	if (exflags & MNT_EXRDONLY)
2396 		*rdonlyp = 1;
2397 	else
2398 		*rdonlyp = 0;
2399 	if (!lockflag)
2400 		VOP_UNLOCK(*vpp, 0);
2401 	return (0);
2402 }
2403 
2404 /*
2405  * WebNFS: check if a filehandle is a public filehandle. For v3, this
2406  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2407  * transformed this to all zeroes in both cases, so check for it.
2408  */
2409 int
2410 nfs_ispublicfh(fhp)
2411 	fhandle_t *fhp;
2412 {
2413 	char *cp = (char *)fhp;
2414 	int i;
2415 
2416 	for (i = 0; i < NFSX_V3FH; i++)
2417 		if (*cp++ != 0)
2418 			return (FALSE);
2419 	return (TRUE);
2420 }
2421 
2422 /*
2423  * This function compares two net addresses by family and returns TRUE
2424  * if they are the same host.
2425  * If there is any doubt, return FALSE.
2426  * The AF_INET family is handled as a special case so that address mbufs
2427  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2428  */
2429 int
2430 netaddr_match(family, haddr, nam)
2431 	int family;
2432 	union nethostaddr *haddr;
2433 	struct mbuf *nam;
2434 {
2435 	struct sockaddr_in *inetaddr;
2436 
2437 	switch (family) {
2438 	case AF_INET:
2439 		inetaddr = mtod(nam, struct sockaddr_in *);
2440 		if (inetaddr->sin_family == AF_INET &&
2441 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2442 			return (1);
2443 		break;
2444 #ifdef INET6
2445 	case AF_INET6:
2446 	    {
2447 		struct sockaddr_in6 *sin6_1, *sin6_2;
2448 
2449 		sin6_1 = mtod(nam, struct sockaddr_in6 *);
2450 		sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
2451 		if (sin6_1->sin6_family == AF_INET6 &&
2452 		    IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
2453 			return 1;
2454 	    }
2455 #endif
2456 #ifdef ISO
2457 	case AF_ISO:
2458 	    {
2459 		struct sockaddr_iso *isoaddr1, *isoaddr2;
2460 
2461 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
2462 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2463 		if (isoaddr1->siso_family == AF_ISO &&
2464 		    isoaddr1->siso_nlen > 0 &&
2465 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2466 		    SAME_ISOADDR(isoaddr1, isoaddr2))
2467 			return (1);
2468 		break;
2469 	    }
2470 #endif	/* ISO */
2471 	default:
2472 		break;
2473 	};
2474 	return (0);
2475 }
2476 
2477 /*
2478  * The write verifier has changed (probably due to a server reboot), so all
2479  * PG_NEEDCOMMIT pages will have to be written again. Since they are marked
2480  * as dirty or are being written out just now, all this takes is clearing
2481  * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
2482  * the mount point.
2483  */
2484 void
2485 nfs_clearcommit(mp)
2486 	struct mount *mp;
2487 {
2488 	struct vnode *vp;
2489 	struct nfsnode *np;
2490 	struct vm_page *pg;
2491 	struct nfsmount *nmp = VFSTONFS(mp);
2492 
2493 	lockmgr(&nmp->nm_writeverflock, LK_EXCLUSIVE, NULL);
2494 
2495 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2496 		KASSERT(vp->v_mount == mp);
2497 		if (vp->v_type == VNON)
2498 			continue;
2499 		np = VTONFS(vp);
2500 		np->n_pushlo = np->n_pushhi = np->n_pushedlo =
2501 		    np->n_pushedhi = 0;
2502 		np->n_commitflags &=
2503 		    ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
2504 		simple_lock(&vp->v_uobj.vmobjlock);
2505 		TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq) {
2506 			pg->flags &= ~PG_NEEDCOMMIT;
2507 		}
2508 		simple_unlock(&vp->v_uobj.vmobjlock);
2509 	}
2510 	simple_lock(&nmp->nm_slock);
2511 	nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF;
2512 	simple_unlock(&nmp->nm_slock);
2513 	lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
2514 }
2515 
2516 void
2517 nfs_merge_commit_ranges(vp)
2518 	struct vnode *vp;
2519 {
2520 	struct nfsnode *np = VTONFS(vp);
2521 
2522 	KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID);
2523 
2524 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2525 		np->n_pushedlo = np->n_pushlo;
2526 		np->n_pushedhi = np->n_pushhi;
2527 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2528 	} else {
2529 		if (np->n_pushlo < np->n_pushedlo)
2530 			np->n_pushedlo = np->n_pushlo;
2531 		if (np->n_pushhi > np->n_pushedhi)
2532 			np->n_pushedhi = np->n_pushhi;
2533 	}
2534 
2535 	np->n_pushlo = np->n_pushhi = 0;
2536 	np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
2537 
2538 #ifdef NFS_DEBUG_COMMIT
2539 	printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2540 	    (unsigned)np->n_pushedhi);
2541 #endif
2542 }
2543 
2544 int
2545 nfs_in_committed_range(vp, off, len)
2546 	struct vnode *vp;
2547 	off_t off, len;
2548 {
2549 	struct nfsnode *np = VTONFS(vp);
2550 	off_t lo, hi;
2551 
2552 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2553 		return 0;
2554 	lo = off;
2555 	hi = lo + len;
2556 
2557 	return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
2558 }
2559 
2560 int
2561 nfs_in_tobecommitted_range(vp, off, len)
2562 	struct vnode *vp;
2563 	off_t off, len;
2564 {
2565 	struct nfsnode *np = VTONFS(vp);
2566 	off_t lo, hi;
2567 
2568 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2569 		return 0;
2570 	lo = off;
2571 	hi = lo + len;
2572 
2573 	return (lo >= np->n_pushlo && hi <= np->n_pushhi);
2574 }
2575 
2576 void
2577 nfs_add_committed_range(vp, off, len)
2578 	struct vnode *vp;
2579 	off_t off, len;
2580 {
2581 	struct nfsnode *np = VTONFS(vp);
2582 	off_t lo, hi;
2583 
2584 	lo = off;
2585 	hi = lo + len;
2586 
2587 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2588 		np->n_pushedlo = lo;
2589 		np->n_pushedhi = hi;
2590 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2591 	} else {
2592 		if (hi > np->n_pushedhi)
2593 			np->n_pushedhi = hi;
2594 		if (lo < np->n_pushedlo)
2595 			np->n_pushedlo = lo;
2596 	}
2597 #ifdef NFS_DEBUG_COMMIT
2598 	printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2599 	    (unsigned)np->n_pushedhi);
2600 #endif
2601 }
2602 
2603 void
2604 nfs_del_committed_range(vp, off, len)
2605 	struct vnode *vp;
2606 	off_t off, len;
2607 {
2608 	struct nfsnode *np = VTONFS(vp);
2609 	off_t lo, hi;
2610 
2611 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2612 		return;
2613 
2614 	lo = off;
2615 	hi = lo + len;
2616 
2617 	if (lo > np->n_pushedhi || hi < np->n_pushedlo)
2618 		return;
2619 	if (lo <= np->n_pushedlo)
2620 		np->n_pushedlo = hi;
2621 	else if (hi >= np->n_pushedhi)
2622 		np->n_pushedhi = lo;
2623 	else {
2624 		/*
2625 		 * XXX There's only one range. If the deleted range
2626 		 * is in the middle, pick the largest of the
2627 		 * contiguous ranges that it leaves.
2628 		 */
2629 		if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
2630 			np->n_pushedhi = lo;
2631 		else
2632 			np->n_pushedlo = hi;
2633 	}
2634 #ifdef NFS_DEBUG_COMMIT
2635 	printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2636 	    (unsigned)np->n_pushedhi);
2637 #endif
2638 }
2639 
2640 void
2641 nfs_add_tobecommitted_range(vp, off, len)
2642 	struct vnode *vp;
2643 	off_t off, len;
2644 {
2645 	struct nfsnode *np = VTONFS(vp);
2646 	off_t lo, hi;
2647 
2648 	lo = off;
2649 	hi = lo + len;
2650 
2651 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
2652 		np->n_pushlo = lo;
2653 		np->n_pushhi = hi;
2654 		np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
2655 	} else {
2656 		if (lo < np->n_pushlo)
2657 			np->n_pushlo = lo;
2658 		if (hi > np->n_pushhi)
2659 			np->n_pushhi = hi;
2660 	}
2661 #ifdef NFS_DEBUG_COMMIT
2662 	printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2663 	    (unsigned)np->n_pushhi);
2664 #endif
2665 }
2666 
2667 void
2668 nfs_del_tobecommitted_range(vp, off, len)
2669 	struct vnode *vp;
2670 	off_t off, len;
2671 {
2672 	struct nfsnode *np = VTONFS(vp);
2673 	off_t lo, hi;
2674 
2675 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2676 		return;
2677 
2678 	lo = off;
2679 	hi = lo + len;
2680 
2681 	if (lo > np->n_pushhi || hi < np->n_pushlo)
2682 		return;
2683 
2684 	if (lo <= np->n_pushlo)
2685 		np->n_pushlo = hi;
2686 	else if (hi >= np->n_pushhi)
2687 		np->n_pushhi = lo;
2688 	else {
2689 		/*
2690 		 * XXX There's only one range. If the deleted range
2691 		 * is in the middle, pick the largest of the
2692 		 * contiguous ranges that it leaves.
2693 		 */
2694 		if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
2695 			np->n_pushhi = lo;
2696 		else
2697 			np->n_pushlo = hi;
2698 	}
2699 #ifdef NFS_DEBUG_COMMIT
2700 	printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2701 	    (unsigned)np->n_pushhi);
2702 #endif
2703 }
2704 
2705 /*
2706  * Map errnos to NFS error numbers. For Version 3 also filter out error
2707  * numbers not specified for the associated procedure.
2708  */
2709 int
2710 nfsrv_errmap(nd, err)
2711 	struct nfsrv_descript *nd;
2712 	int err;
2713 {
2714 	const short *defaulterrp, *errp;
2715 
2716 	if (nd->nd_flag & ND_NFSV3) {
2717 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
2718 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2719 		while (*++errp) {
2720 			if (*errp == err)
2721 				return (err);
2722 			else if (*errp > err)
2723 				break;
2724 		}
2725 		return ((int)*defaulterrp);
2726 	    } else
2727 		return (err & 0xffff);
2728 	}
2729 	if (err <= ELAST)
2730 		return ((int)nfsrv_v2errmap[err - 1]);
2731 	return (NFSERR_IO);
2732 }
2733 
2734 /*
2735  * Sort the group list in increasing numerical order.
2736  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2737  *  that used to be here.)
2738  */
2739 void
2740 nfsrvw_sort(list, num)
2741         gid_t *list;
2742         int num;
2743 {
2744 	int i, j;
2745 	gid_t v;
2746 
2747 	/* Insertion sort. */
2748 	for (i = 1; i < num; i++) {
2749 		v = list[i];
2750 		/* find correct slot for value v, moving others up */
2751 		for (j = i; --j >= 0 && v < list[j];)
2752 			list[j + 1] = list[j];
2753 		list[j + 1] = v;
2754 	}
2755 }
2756 
2757 /*
2758  * copy credentials making sure that the result can be compared with memcmp().
2759  */
2760 void
2761 nfsrv_setcred(incred, outcred)
2762 	struct ucred *incred, *outcred;
2763 {
2764 	int i;
2765 
2766 	memset((caddr_t)outcred, 0, sizeof (struct ucred));
2767 	outcred->cr_ref = 1;
2768 	outcred->cr_uid = incred->cr_uid;
2769 	outcred->cr_gid = incred->cr_gid;
2770 	outcred->cr_ngroups = incred->cr_ngroups;
2771 	for (i = 0; i < incred->cr_ngroups; i++)
2772 		outcred->cr_groups[i] = incred->cr_groups[i];
2773 	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2774 }
2775