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