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