nfs_subs.c revision 1.221.2.4 1 /* $NetBSD: nfs_subs.c,v 1.221.2.4 2012/04/17 00:08:47 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. 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.221.2.4 2012/04/17 00:08:47 yamt Exp $");
74
75 #ifdef _KERNEL_OPT
76 #include "opt_nfs.h"
77 #endif
78
79 /*
80 * These functions support the macros and help fiddle mbuf chains for
81 * the nfs op functions. They do things like create the rpc header and
82 * copy data between mbuf chains and uio lists.
83 */
84 #include <sys/param.h>
85 #include <sys/proc.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/kmem.h>
89 #include <sys/mount.h>
90 #include <sys/vnode.h>
91 #include <sys/namei.h>
92 #include <sys/mbuf.h>
93 #include <sys/socket.h>
94 #include <sys/stat.h>
95 #include <sys/filedesc.h>
96 #include <sys/time.h>
97 #include <sys/dirent.h>
98 #include <sys/once.h>
99 #include <sys/kauth.h>
100 #include <sys/atomic.h>
101 #include <sys/cprng.h>
102
103 #include <uvm/uvm.h>
104 #include <uvm/uvm_page_array.h>
105
106 #include <nfs/rpcv2.h>
107 #include <nfs/nfsproto.h>
108 #include <nfs/nfsnode.h>
109 #include <nfs/nfs.h>
110 #include <nfs/xdr_subs.h>
111 #include <nfs/nfsm_subs.h>
112 #include <nfs/nfsmount.h>
113 #include <nfs/nfsrtt.h>
114 #include <nfs/nfs_var.h>
115
116 #include <miscfs/specfs/specdev.h>
117
118 #include <netinet/in.h>
119
120 static u_int32_t nfs_xid;
121
122 int nuidhash_max = NFS_MAXUIDHASH;
123 /*
124 * Data items converted to xdr at startup, since they are constant
125 * This is kinda hokey, but may save a little time doing byte swaps
126 */
127 u_int32_t nfs_xdrneg1;
128 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
129 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
130 rpc_auth_kerb;
131 u_int32_t nfs_prog, nfs_true, nfs_false;
132
133 /* And other global data */
134 const nfstype nfsv2_type[9] =
135 { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON, NFCHR, NFNON };
136 const nfstype nfsv3_type[9] =
137 { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON };
138 const enum vtype nv2tov_type[8] =
139 { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
140 const enum vtype nv3tov_type[8] =
141 { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
142 int nfs_ticks;
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 = cprng_fast32();
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 return 0;
1504 }
1505
1506 /*
1507 * This is disgusting, but it must support both modular and monolothic
1508 * configurations. For monolithic builds NFSSERVER may not imply NFS.
1509 *
1510 * Yuck.
1511 */
1512 void
1513 nfs_init(void)
1514 {
1515 static ONCE_DECL(nfs_init_once);
1516
1517 RUN_ONCE(&nfs_init_once, nfs_init0);
1518 }
1519
1520 void
1521 nfs_fini(void)
1522 {
1523
1524 nfsdreq_fini();
1525 nfs_timer_fini();
1526 MOWNER_DETACH(&nfs_mowner);
1527 }
1528
1529 /*
1530 * A fiddled version of m_adj() that ensures null fill to a 32-bit
1531 * boundary and only trims off the back end
1532 *
1533 * 1. trim off 'len' bytes as m_adj(mp, -len).
1534 * 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
1535 */
1536 void
1537 nfs_zeropad(struct mbuf *mp, int len, int nul)
1538 {
1539 struct mbuf *m;
1540 int count;
1541
1542 /*
1543 * Trim from tail. Scan the mbuf chain,
1544 * calculating its length and finding the last mbuf.
1545 * If the adjustment only affects this mbuf, then just
1546 * adjust and return. Otherwise, rescan and truncate
1547 * after the remaining size.
1548 */
1549 count = 0;
1550 m = mp;
1551 for (;;) {
1552 count += m->m_len;
1553 if (m->m_next == NULL)
1554 break;
1555 m = m->m_next;
1556 }
1557
1558 KDASSERT(count >= len);
1559
1560 if (m->m_len >= len) {
1561 m->m_len -= len;
1562 } else {
1563 count -= len;
1564 /*
1565 * Correct length for chain is "count".
1566 * Find the mbuf with last data, adjust its length,
1567 * and toss data from remaining mbufs on chain.
1568 */
1569 for (m = mp; m; m = m->m_next) {
1570 if (m->m_len >= count) {
1571 m->m_len = count;
1572 break;
1573 }
1574 count -= m->m_len;
1575 }
1576 KASSERT(m && m->m_next);
1577 m_freem(m->m_next);
1578 m->m_next = NULL;
1579 }
1580
1581 KDASSERT(m->m_next == NULL);
1582
1583 /*
1584 * zero-padding.
1585 */
1586 if (nul > 0) {
1587 char *cp;
1588 int i;
1589
1590 if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) {
1591 struct mbuf *n;
1592
1593 KDASSERT(MLEN >= nul);
1594 n = m_get(M_WAIT, MT_DATA);
1595 MCLAIM(n, &nfs_mowner);
1596 n->m_len = nul;
1597 n->m_next = NULL;
1598 m->m_next = n;
1599 cp = mtod(n, void *);
1600 } else {
1601 cp = mtod(m, char *) + m->m_len;
1602 m->m_len += nul;
1603 }
1604 for (i = 0; i < nul; i++)
1605 *cp++ = '\0';
1606 }
1607 return;
1608 }
1609
1610 /*
1611 * Make these functions instead of macros, so that the kernel text size
1612 * doesn't get too big...
1613 */
1614 void
1615 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)
1616 {
1617 struct mbuf *mb = *mbp;
1618 char *bpos = *bposp;
1619 u_int32_t *tl;
1620
1621 if (before_ret) {
1622 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1623 *tl = nfs_false;
1624 } else {
1625 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1626 *tl++ = nfs_true;
1627 txdr_hyper(before_vap->va_size, tl);
1628 tl += 2;
1629 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1630 tl += 2;
1631 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1632 }
1633 *bposp = bpos;
1634 *mbp = mb;
1635 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1636 }
1637
1638 void
1639 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1640 {
1641 struct mbuf *mb = *mbp;
1642 char *bpos = *bposp;
1643 u_int32_t *tl;
1644 struct nfs_fattr *fp;
1645
1646 if (after_ret) {
1647 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1648 *tl = nfs_false;
1649 } else {
1650 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1651 *tl++ = nfs_true;
1652 fp = (struct nfs_fattr *)tl;
1653 nfsm_srvfattr(nfsd, after_vap, fp);
1654 }
1655 *mbp = mb;
1656 *bposp = bpos;
1657 }
1658
1659 void
1660 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap, struct nfs_fattr *fp)
1661 {
1662
1663 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1664 fp->fa_uid = txdr_unsigned(vap->va_uid);
1665 fp->fa_gid = txdr_unsigned(vap->va_gid);
1666 if (nfsd->nd_flag & ND_NFSV3) {
1667 fp->fa_type = vtonfsv3_type(vap->va_type);
1668 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1669 txdr_hyper(vap->va_size, &fp->fa3_size);
1670 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1671 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1672 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1673 fp->fa3_fsid.nfsuquad[0] = 0;
1674 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1675 txdr_hyper(vap->va_fileid, &fp->fa3_fileid);
1676 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1677 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1678 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1679 } else {
1680 fp->fa_type = vtonfsv2_type(vap->va_type);
1681 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1682 fp->fa2_size = txdr_unsigned(vap->va_size);
1683 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1684 if (vap->va_type == VFIFO)
1685 fp->fa2_rdev = 0xffffffff;
1686 else
1687 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1688 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1689 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1690 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1691 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1692 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1693 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1694 }
1695 }
1696
1697 /*
1698 * This function compares two net addresses by family and returns true
1699 * if they are the same host.
1700 * If there is any doubt, return false.
1701 * The AF_INET family is handled as a special case so that address mbufs
1702 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1703 */
1704 int
1705 netaddr_match(int family, union nethostaddr *haddr, struct mbuf *nam)
1706 {
1707 struct sockaddr_in *inetaddr;
1708
1709 switch (family) {
1710 case AF_INET:
1711 inetaddr = mtod(nam, struct sockaddr_in *);
1712 if (inetaddr->sin_family == AF_INET &&
1713 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1714 return (1);
1715 break;
1716 case AF_INET6:
1717 {
1718 struct sockaddr_in6 *sin6_1, *sin6_2;
1719
1720 sin6_1 = mtod(nam, struct sockaddr_in6 *);
1721 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
1722 if (sin6_1->sin6_family == AF_INET6 &&
1723 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
1724 return 1;
1725 }
1726 default:
1727 break;
1728 };
1729 return (0);
1730 }
1731
1732 /*
1733 * The write verifier has changed (probably due to a server reboot), so all
1734 * PG_NEEDCOMMIT pages will have to be written again. Since they are marked
1735 * as dirty or are being written out just now, all this takes is clearing
1736 * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
1737 * the mount point.
1738 */
1739 void
1740 nfs_clearcommit(struct mount *mp)
1741 {
1742 struct vnode *vp;
1743 struct nfsnode *np;
1744 struct vm_page *pg;
1745 struct nfsmount *nmp = VFSTONFS(mp);
1746
1747 rw_enter(&nmp->nm_writeverflock, RW_WRITER);
1748 mutex_enter(&mntvnode_lock);
1749 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1750 struct uvm_page_array a;
1751 voff_t off;
1752
1753 KASSERT(vp->v_mount == mp);
1754 if (vp->v_type != VREG)
1755 continue;
1756 mutex_enter(vp->v_interlock);
1757 if (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) {
1758 mutex_exit(vp->v_interlock);
1759 continue;
1760 }
1761 np = VTONFS(vp);
1762 np->n_commitflags &=
1763 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
1764 uvm_page_array_init(&a);
1765 off = 0;
1766 while ((pg = uvm_page_array_fill_and_peek(&a, &vp->v_uobj, off,
1767 0, 0)) != NULL) {
1768 pg->flags &= ~PG_NEEDCOMMIT;
1769 uvm_page_array_advance(&a);
1770 off = pg->offset + PAGE_SIZE;
1771 }
1772 uvm_page_array_fini(&a);
1773 mutex_exit(vp->v_interlock);
1774 }
1775 mutex_exit(&mntvnode_lock);
1776 mutex_enter(&nmp->nm_lock);
1777 nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF;
1778 mutex_exit(&nmp->nm_lock);
1779 rw_exit(&nmp->nm_writeverflock);
1780 }
1781
1782 void
1783 nfs_merge_commit_ranges(struct vnode *vp)
1784 {
1785 struct nfsnode *np = VTONFS(vp);
1786
1787 KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID);
1788 nfs_add_committed_range(vp, np->n_pushlo, np->n_pushhi - np->n_pushlo);
1789 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
1790
1791 #ifdef NFS_DEBUG_COMMIT
1792 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
1793 (unsigned)np->n_pushedhi);
1794 #endif
1795 }
1796
1797 int
1798 nfs_in_committed_range(struct vnode *vp, off_t off, off_t len)
1799 {
1800 struct nfsnode *np = VTONFS(vp);
1801 off_t lo, hi;
1802
1803 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
1804 return 0;
1805 lo = off;
1806 hi = lo + len;
1807
1808 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
1809 }
1810
1811 int
1812 nfs_in_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
1813 {
1814 struct nfsnode *np = VTONFS(vp);
1815 off_t lo, hi;
1816
1817 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
1818 return 0;
1819 lo = off;
1820 hi = lo + len;
1821
1822 return (lo >= np->n_pushlo && hi <= np->n_pushhi);
1823 }
1824
1825 void
1826 nfs_add_committed_range(struct vnode *vp, off_t off, off_t len)
1827 {
1828 struct nfsnode *np = VTONFS(vp);
1829 off_t lo, hi;
1830
1831 lo = off;
1832 hi = lo + len;
1833
1834 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
1835 np->n_pushedlo = lo;
1836 np->n_pushedhi = hi;
1837 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
1838 } else {
1839 /*
1840 * note that it's unsafe to add more than requested.
1841 */
1842 if (hi > np->n_pushedhi && lo <= np->n_pushedhi)
1843 np->n_pushedhi = hi;
1844 if (lo < np->n_pushedlo && hi >= np->n_pushedlo)
1845 np->n_pushedlo = lo;
1846 }
1847 KASSERT(np->n_pushedhi >= np->n_pushedlo);
1848 #ifdef NFS_DEBUG_COMMIT
1849 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
1850 (unsigned)np->n_pushedhi);
1851 #endif
1852 }
1853
1854 void
1855 nfs_del_committed_range(struct vnode *vp, off_t off, off_t len)
1856 {
1857 struct nfsnode *np = VTONFS(vp);
1858 off_t lo, hi;
1859
1860 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
1861 return;
1862
1863 lo = off;
1864 hi = lo + len;
1865
1866 if (lo > np->n_pushedhi || hi < np->n_pushedlo)
1867 return;
1868 if (lo <= np->n_pushedlo)
1869 np->n_pushedlo = hi;
1870 else if (hi >= np->n_pushedhi)
1871 np->n_pushedhi = lo;
1872 else {
1873 /*
1874 * XXX There's only one range. If the deleted range
1875 * is in the middle, pick the largest of the
1876 * contiguous ranges that it leaves.
1877 */
1878 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
1879 np->n_pushedhi = lo;
1880 else
1881 np->n_pushedlo = hi;
1882 }
1883 if (np->n_pushedhi <= np->n_pushedlo) {
1884 np->n_commitflags &= ~NFS_COMMIT_PUSHED_VALID;
1885 }
1886 #ifdef NFS_DEBUG_COMMIT
1887 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
1888 (unsigned)np->n_pushedhi);
1889 #endif
1890 }
1891
1892 void
1893 nfs_add_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
1894 {
1895 struct nfsnode *np = VTONFS(vp);
1896 off_t lo, hi;
1897
1898 lo = off;
1899 hi = lo + len;
1900
1901 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
1902 np->n_pushlo = lo;
1903 np->n_pushhi = hi;
1904 np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
1905 } else {
1906 if (lo < np->n_pushlo)
1907 np->n_pushlo = lo;
1908 if (hi > np->n_pushhi)
1909 np->n_pushhi = hi;
1910 }
1911 KASSERT(np->n_pushhi >= np->n_pushlo);
1912 #ifdef NFS_DEBUG_COMMIT
1913 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
1914 (unsigned)np->n_pushhi);
1915 #endif
1916 }
1917
1918 void
1919 nfs_del_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
1920 {
1921 struct nfsnode *np = VTONFS(vp);
1922 off_t lo, hi;
1923
1924 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
1925 return;
1926
1927 lo = off;
1928 hi = lo + len;
1929
1930 if (lo > np->n_pushhi || hi < np->n_pushlo)
1931 return;
1932
1933 if (lo <= np->n_pushlo)
1934 np->n_pushlo = hi;
1935 else if (hi >= np->n_pushhi)
1936 np->n_pushhi = lo;
1937 else {
1938 /*
1939 * following can cause a lot of small (eg. 64KB) commit rpcs.
1940 */
1941 #if 0
1942 /*
1943 * XXX There's only one range. If the deleted range
1944 * is in the middle, pick the largest of the
1945 * contiguous ranges that it leaves.
1946 */
1947 if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
1948 np->n_pushhi = lo;
1949 else
1950 np->n_pushlo = hi;
1951 #endif
1952 }
1953 if (np->n_pushhi <= np->n_pushlo) {
1954 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
1955 }
1956 #ifdef NFS_DEBUG_COMMIT
1957 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
1958 (unsigned)np->n_pushhi);
1959 #endif
1960 }
1961
1962 /*
1963 * Map errnos to NFS error numbers. For Version 3 also filter out error
1964 * numbers not specified for the associated procedure.
1965 */
1966 int
1967 nfsrv_errmap(struct nfsrv_descript *nd, int err)
1968 {
1969 const short *defaulterrp, *errp;
1970
1971 if (nd->nd_flag & ND_NFSV3) {
1972 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1973 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1974 while (*++errp) {
1975 if (*errp == err)
1976 return (err);
1977 else if (*errp > err)
1978 break;
1979 }
1980 return ((int)*defaulterrp);
1981 } else
1982 return (err & 0xffff);
1983 }
1984 if (err <= ELAST)
1985 return ((int)nfsrv_v2errmap[err - 1]);
1986 return (NFSERR_IO);
1987 }
1988
1989 u_int32_t
1990 nfs_getxid(void)
1991 {
1992 u_int32_t newxid;
1993
1994 /* get next xid. skip 0 */
1995 do {
1996 newxid = atomic_inc_32_nv(&nfs_xid);
1997 } while (__predict_false(newxid == 0));
1998
1999 return txdr_unsigned(newxid);
2000 }
2001
2002 /*
2003 * assign a new xid for existing request.
2004 * used for NFSERR_JUKEBOX handling.
2005 */
2006 void
2007 nfs_renewxid(struct nfsreq *req)
2008 {
2009 u_int32_t xid;
2010 int off;
2011
2012 xid = nfs_getxid();
2013 if (req->r_nmp->nm_sotype == SOCK_STREAM)
2014 off = sizeof(u_int32_t); /* RPC record mark */
2015 else
2016 off = 0;
2017
2018 m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid);
2019 req->r_xid = xid;
2020 }
2021