nfs_subs.c revision 1.88 1 /* $NetBSD: nfs_subs.c,v 1.88 2000/11/08 14:28:15 ad 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_nfs.h"
78 #include "opt_nfsserver.h"
79 #include "opt_iso.h"
80 #include "opt_inet.h"
81
82 /*
83 * These functions support the macros and help fiddle mbuf chains for
84 * the nfs op functions. They do things like create the rpc header and
85 * copy data between mbuf chains and uio lists.
86 */
87 #include <sys/param.h>
88 #include <sys/proc.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/mount.h>
92 #include <sys/vnode.h>
93 #include <sys/namei.h>
94 #include <sys/mbuf.h>
95 #include <sys/socket.h>
96 #include <sys/stat.h>
97 #include <sys/malloc.h>
98 #include <sys/time.h>
99 #include <sys/dirent.h>
100
101 #include <uvm/uvm_extern.h>
102
103 #include <nfs/rpcv2.h>
104 #include <nfs/nfsproto.h>
105 #include <nfs/nfsnode.h>
106 #include <nfs/nfs.h>
107 #include <nfs/xdr_subs.h>
108 #include <nfs/nfsm_subs.h>
109 #include <nfs/nfsmount.h>
110 #include <nfs/nqnfs.h>
111 #include <nfs/nfsrtt.h>
112 #include <nfs/nfs_var.h>
113
114 #include <miscfs/specfs/specdev.h>
115
116 #include <netinet/in.h>
117 #ifdef ISO
118 #include <netiso/iso.h>
119 #endif
120
121 /*
122 * Data items converted to xdr at startup, since they are constant
123 * This is kinda hokey, but may save a little time doing byte swaps
124 */
125 u_int32_t nfs_xdrneg1;
126 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
127 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
128 rpc_auth_kerb;
129 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
130
131 /* And other global data */
132 static u_int32_t nfs_xid = 0;
133 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
134 NFCHR, NFNON };
135 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
136 NFFIFO, NFNON };
137 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
138 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
139 int nfs_ticks;
140
141 /* NFS client/server stats. */
142 struct nfsstats nfsstats;
143
144 /*
145 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
146 */
147 int nfsv3_procid[NFS_NPROCS] = {
148 NFSPROC_NULL,
149 NFSPROC_GETATTR,
150 NFSPROC_SETATTR,
151 NFSPROC_NOOP,
152 NFSPROC_LOOKUP,
153 NFSPROC_READLINK,
154 NFSPROC_READ,
155 NFSPROC_NOOP,
156 NFSPROC_WRITE,
157 NFSPROC_CREATE,
158 NFSPROC_REMOVE,
159 NFSPROC_RENAME,
160 NFSPROC_LINK,
161 NFSPROC_SYMLINK,
162 NFSPROC_MKDIR,
163 NFSPROC_RMDIR,
164 NFSPROC_READDIR,
165 NFSPROC_FSSTAT,
166 NFSPROC_NOOP,
167 NFSPROC_NOOP,
168 NFSPROC_NOOP,
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 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 NFSV2PROC_NOOP,
204 NFSV2PROC_NOOP,
205 NFSV2PROC_NOOP,
206 };
207
208 /*
209 * Maps errno values to nfs error numbers.
210 * Use NFSERR_IO as the catch all for ones not specifically defined in
211 * RFC 1094.
212 */
213 static u_char nfsrv_v2errmap[ELAST] = {
214 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
215 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
216 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
217 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
218 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
219 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
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_IO, NFSERR_IO, NFSERR_IO,
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_NAMETOL, NFSERR_IO, NFSERR_IO,
227 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
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,
231 };
232
233 /*
234 * Maps errno values to nfs error numbers.
235 * Although it is not obvious whether or not NFS clients really care if
236 * a returned error value is in the specified list for the procedure, the
237 * safest thing to do is filter them appropriately. For Version 2, the
238 * X/Open XNFS document is the only specification that defines error values
239 * for each RPC (The RFC simply lists all possible error values for all RPCs),
240 * so I have decided to not do this for Version 2.
241 * The first entry is the default error return and the rest are the valid
242 * errors for that RPC in increasing numeric order.
243 */
244 static short nfsv3err_null[] = {
245 0,
246 0,
247 };
248
249 static short nfsv3err_getattr[] = {
250 NFSERR_IO,
251 NFSERR_IO,
252 NFSERR_STALE,
253 NFSERR_BADHANDLE,
254 NFSERR_SERVERFAULT,
255 0,
256 };
257
258 static short nfsv3err_setattr[] = {
259 NFSERR_IO,
260 NFSERR_PERM,
261 NFSERR_IO,
262 NFSERR_ACCES,
263 NFSERR_INVAL,
264 NFSERR_NOSPC,
265 NFSERR_ROFS,
266 NFSERR_DQUOT,
267 NFSERR_STALE,
268 NFSERR_BADHANDLE,
269 NFSERR_NOT_SYNC,
270 NFSERR_SERVERFAULT,
271 0,
272 };
273
274 static short nfsv3err_lookup[] = {
275 NFSERR_IO,
276 NFSERR_NOENT,
277 NFSERR_IO,
278 NFSERR_ACCES,
279 NFSERR_NOTDIR,
280 NFSERR_NAMETOL,
281 NFSERR_STALE,
282 NFSERR_BADHANDLE,
283 NFSERR_SERVERFAULT,
284 0,
285 };
286
287 static short nfsv3err_access[] = {
288 NFSERR_IO,
289 NFSERR_IO,
290 NFSERR_STALE,
291 NFSERR_BADHANDLE,
292 NFSERR_SERVERFAULT,
293 0,
294 };
295
296 static short nfsv3err_readlink[] = {
297 NFSERR_IO,
298 NFSERR_IO,
299 NFSERR_ACCES,
300 NFSERR_INVAL,
301 NFSERR_STALE,
302 NFSERR_BADHANDLE,
303 NFSERR_NOTSUPP,
304 NFSERR_SERVERFAULT,
305 0,
306 };
307
308 static short nfsv3err_read[] = {
309 NFSERR_IO,
310 NFSERR_IO,
311 NFSERR_NXIO,
312 NFSERR_ACCES,
313 NFSERR_INVAL,
314 NFSERR_STALE,
315 NFSERR_BADHANDLE,
316 NFSERR_SERVERFAULT,
317 NFSERR_JUKEBOX,
318 0,
319 };
320
321 static short nfsv3err_write[] = {
322 NFSERR_IO,
323 NFSERR_IO,
324 NFSERR_ACCES,
325 NFSERR_INVAL,
326 NFSERR_FBIG,
327 NFSERR_NOSPC,
328 NFSERR_ROFS,
329 NFSERR_DQUOT,
330 NFSERR_STALE,
331 NFSERR_BADHANDLE,
332 NFSERR_SERVERFAULT,
333 NFSERR_JUKEBOX,
334 0,
335 };
336
337 static short nfsv3err_create[] = {
338 NFSERR_IO,
339 NFSERR_IO,
340 NFSERR_ACCES,
341 NFSERR_EXIST,
342 NFSERR_NOTDIR,
343 NFSERR_NOSPC,
344 NFSERR_ROFS,
345 NFSERR_NAMETOL,
346 NFSERR_DQUOT,
347 NFSERR_STALE,
348 NFSERR_BADHANDLE,
349 NFSERR_NOTSUPP,
350 NFSERR_SERVERFAULT,
351 0,
352 };
353
354 static short nfsv3err_mkdir[] = {
355 NFSERR_IO,
356 NFSERR_IO,
357 NFSERR_ACCES,
358 NFSERR_EXIST,
359 NFSERR_NOTDIR,
360 NFSERR_NOSPC,
361 NFSERR_ROFS,
362 NFSERR_NAMETOL,
363 NFSERR_DQUOT,
364 NFSERR_STALE,
365 NFSERR_BADHANDLE,
366 NFSERR_NOTSUPP,
367 NFSERR_SERVERFAULT,
368 0,
369 };
370
371 static short nfsv3err_symlink[] = {
372 NFSERR_IO,
373 NFSERR_IO,
374 NFSERR_ACCES,
375 NFSERR_EXIST,
376 NFSERR_NOTDIR,
377 NFSERR_NOSPC,
378 NFSERR_ROFS,
379 NFSERR_NAMETOL,
380 NFSERR_DQUOT,
381 NFSERR_STALE,
382 NFSERR_BADHANDLE,
383 NFSERR_NOTSUPP,
384 NFSERR_SERVERFAULT,
385 0,
386 };
387
388 static short nfsv3err_mknod[] = {
389 NFSERR_IO,
390 NFSERR_IO,
391 NFSERR_ACCES,
392 NFSERR_EXIST,
393 NFSERR_NOTDIR,
394 NFSERR_NOSPC,
395 NFSERR_ROFS,
396 NFSERR_NAMETOL,
397 NFSERR_DQUOT,
398 NFSERR_STALE,
399 NFSERR_BADHANDLE,
400 NFSERR_NOTSUPP,
401 NFSERR_SERVERFAULT,
402 NFSERR_BADTYPE,
403 0,
404 };
405
406 static short nfsv3err_remove[] = {
407 NFSERR_IO,
408 NFSERR_NOENT,
409 NFSERR_IO,
410 NFSERR_ACCES,
411 NFSERR_NOTDIR,
412 NFSERR_ROFS,
413 NFSERR_NAMETOL,
414 NFSERR_STALE,
415 NFSERR_BADHANDLE,
416 NFSERR_SERVERFAULT,
417 0,
418 };
419
420 static short nfsv3err_rmdir[] = {
421 NFSERR_IO,
422 NFSERR_NOENT,
423 NFSERR_IO,
424 NFSERR_ACCES,
425 NFSERR_EXIST,
426 NFSERR_NOTDIR,
427 NFSERR_INVAL,
428 NFSERR_ROFS,
429 NFSERR_NAMETOL,
430 NFSERR_NOTEMPTY,
431 NFSERR_STALE,
432 NFSERR_BADHANDLE,
433 NFSERR_NOTSUPP,
434 NFSERR_SERVERFAULT,
435 0,
436 };
437
438 static short nfsv3err_rename[] = {
439 NFSERR_IO,
440 NFSERR_NOENT,
441 NFSERR_IO,
442 NFSERR_ACCES,
443 NFSERR_EXIST,
444 NFSERR_XDEV,
445 NFSERR_NOTDIR,
446 NFSERR_ISDIR,
447 NFSERR_INVAL,
448 NFSERR_NOSPC,
449 NFSERR_ROFS,
450 NFSERR_MLINK,
451 NFSERR_NAMETOL,
452 NFSERR_NOTEMPTY,
453 NFSERR_DQUOT,
454 NFSERR_STALE,
455 NFSERR_BADHANDLE,
456 NFSERR_NOTSUPP,
457 NFSERR_SERVERFAULT,
458 0,
459 };
460
461 static short nfsv3err_link[] = {
462 NFSERR_IO,
463 NFSERR_IO,
464 NFSERR_ACCES,
465 NFSERR_EXIST,
466 NFSERR_XDEV,
467 NFSERR_NOTDIR,
468 NFSERR_INVAL,
469 NFSERR_NOSPC,
470 NFSERR_ROFS,
471 NFSERR_MLINK,
472 NFSERR_NAMETOL,
473 NFSERR_DQUOT,
474 NFSERR_STALE,
475 NFSERR_BADHANDLE,
476 NFSERR_NOTSUPP,
477 NFSERR_SERVERFAULT,
478 0,
479 };
480
481 static short nfsv3err_readdir[] = {
482 NFSERR_IO,
483 NFSERR_IO,
484 NFSERR_ACCES,
485 NFSERR_NOTDIR,
486 NFSERR_STALE,
487 NFSERR_BADHANDLE,
488 NFSERR_BAD_COOKIE,
489 NFSERR_TOOSMALL,
490 NFSERR_SERVERFAULT,
491 0,
492 };
493
494 static short nfsv3err_readdirplus[] = {
495 NFSERR_IO,
496 NFSERR_IO,
497 NFSERR_ACCES,
498 NFSERR_NOTDIR,
499 NFSERR_STALE,
500 NFSERR_BADHANDLE,
501 NFSERR_BAD_COOKIE,
502 NFSERR_NOTSUPP,
503 NFSERR_TOOSMALL,
504 NFSERR_SERVERFAULT,
505 0,
506 };
507
508 static short nfsv3err_fsstat[] = {
509 NFSERR_IO,
510 NFSERR_IO,
511 NFSERR_STALE,
512 NFSERR_BADHANDLE,
513 NFSERR_SERVERFAULT,
514 0,
515 };
516
517 static short nfsv3err_fsinfo[] = {
518 NFSERR_STALE,
519 NFSERR_STALE,
520 NFSERR_BADHANDLE,
521 NFSERR_SERVERFAULT,
522 0,
523 };
524
525 static short nfsv3err_pathconf[] = {
526 NFSERR_STALE,
527 NFSERR_STALE,
528 NFSERR_BADHANDLE,
529 NFSERR_SERVERFAULT,
530 0,
531 };
532
533 static short nfsv3err_commit[] = {
534 NFSERR_IO,
535 NFSERR_IO,
536 NFSERR_STALE,
537 NFSERR_BADHANDLE,
538 NFSERR_SERVERFAULT,
539 0,
540 };
541
542 static short *nfsrv_v3errmap[] = {
543 nfsv3err_null,
544 nfsv3err_getattr,
545 nfsv3err_setattr,
546 nfsv3err_lookup,
547 nfsv3err_access,
548 nfsv3err_readlink,
549 nfsv3err_read,
550 nfsv3err_write,
551 nfsv3err_create,
552 nfsv3err_mkdir,
553 nfsv3err_symlink,
554 nfsv3err_mknod,
555 nfsv3err_remove,
556 nfsv3err_rmdir,
557 nfsv3err_rename,
558 nfsv3err_link,
559 nfsv3err_readdir,
560 nfsv3err_readdirplus,
561 nfsv3err_fsstat,
562 nfsv3err_fsinfo,
563 nfsv3err_pathconf,
564 nfsv3err_commit,
565 };
566
567 extern struct nfsrtt nfsrtt;
568 extern time_t nqnfsstarttime;
569 extern int nqsrv_clockskew;
570 extern int nqsrv_writeslack;
571 extern int nqsrv_maxlease;
572 extern int nqnfs_piggy[NFS_NPROCS];
573 extern struct nfsnodehashhead *nfsnodehashtbl;
574 extern u_long nfsnodehash;
575
576 LIST_HEAD(nfsnodehashhead, nfsnode);
577 u_long nfsdirhashmask;
578
579 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
580
581 /*
582 * Create the header for an rpc request packet
583 * The hsiz is the size of the rest of the nfs request header.
584 * (just used to decide if a cluster is a good idea)
585 */
586 struct mbuf *
587 nfsm_reqh(vp, procid, hsiz, bposp)
588 struct vnode *vp;
589 u_long procid;
590 int hsiz;
591 caddr_t *bposp;
592 {
593 struct mbuf *mb;
594 caddr_t bpos;
595 struct nfsmount *nmp;
596 #ifndef NFS_V2_ONLY
597 u_int32_t *tl;
598 struct mbuf *mb2;
599 int nqflag;
600 #endif
601
602 MGET(mb, M_WAIT, MT_DATA);
603 if (hsiz >= MINCLSIZE)
604 MCLGET(mb, M_WAIT);
605 mb->m_len = 0;
606 bpos = mtod(mb, caddr_t);
607
608 /*
609 * For NQNFS, add lease request.
610 */
611 if (vp) {
612 nmp = VFSTONFS(vp->v_mount);
613 #ifndef NFS_V2_ONLY
614 if (nmp->nm_flag & NFSMNT_NQNFS) {
615 nqflag = NQNFS_NEEDLEASE(vp, procid);
616 if (nqflag) {
617 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
618 *tl++ = txdr_unsigned(nqflag);
619 *tl = txdr_unsigned(nmp->nm_leaseterm);
620 } else {
621 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
622 *tl = 0;
623 }
624 }
625 #endif
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 = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF,
1223 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 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
1461 /*
1462 * Initialize the nqnfs data structures.
1463 */
1464 if (nqnfsstarttime == 0) {
1465 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1466 + nqsrv_clockskew + nqsrv_writeslack;
1467 NQLOADNOVRAM(nqnfsstarttime);
1468 CIRCLEQ_INIT(&nqtimerhead);
1469 nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE,
1470 M_WAITOK, &nqfhhash);
1471 }
1472 #endif
1473
1474 /*
1475 * Initialize reply list and start timer
1476 */
1477 TAILQ_INIT(&nfs_reqq);
1478 nfs_timer(NULL);
1479 }
1480
1481 #ifdef NFS
1482 /*
1483 * Called once at VFS init to initialize client-specific data structures.
1484 */
1485 void
1486 nfs_vfs_init()
1487 {
1488 int i;
1489
1490 /* Ensure async daemons disabled */
1491 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1492 nfs_iodwant[i] = (struct proc *)0;
1493 nfs_iodmount[i] = (struct nfsmount *)0;
1494 }
1495 nfs_nhinit(); /* Init the nfsnode table */
1496 }
1497
1498 void
1499 nfs_vfs_done()
1500 {
1501 nfs_nhdone();
1502 }
1503
1504 /*
1505 * Attribute cache routines.
1506 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1507 * that are on the mbuf list
1508 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1509 * error otherwise
1510 */
1511
1512 /*
1513 * Load the attribute cache (that lives in the nfsnode entry) with
1514 * the values on the mbuf list and
1515 * Iff vap not NULL
1516 * copy the attributes to *vaper
1517 */
1518 int
1519 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
1520 struct vnode **vpp;
1521 struct mbuf **mdp;
1522 caddr_t *dposp;
1523 struct vattr *vaper;
1524 {
1525 int32_t t1;
1526 caddr_t cp2;
1527 int error = 0;
1528 struct mbuf *md;
1529 int v3 = NFS_ISV3(*vpp);
1530
1531 md = *mdp;
1532 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1533 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1534 if (error)
1535 return (error);
1536 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
1537 }
1538
1539 int
1540 nfs_loadattrcache(vpp, fp, vaper)
1541 struct vnode **vpp;
1542 struct nfs_fattr *fp;
1543 struct vattr *vaper;
1544 {
1545 struct vnode *vp = *vpp;
1546 struct vattr *vap;
1547 int v3 = NFS_ISV3(vp);
1548 enum vtype vtyp;
1549 u_short vmode;
1550 struct timespec mtime;
1551 struct vnode *nvp;
1552 int32_t rdev;
1553 struct nfsnode *np;
1554 extern int (**spec_nfsv2nodeop_p) __P((void *));
1555
1556 if (v3) {
1557 vtyp = nfsv3tov_type(fp->fa_type);
1558 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1559 rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
1560 fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
1561 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1562 } else {
1563 vtyp = nfsv2tov_type(fp->fa_type);
1564 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1565 if (vtyp == VNON || vtyp == VREG)
1566 vtyp = IFTOVT(vmode);
1567 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1568 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1569
1570 /*
1571 * Really ugly NFSv2 kludge.
1572 */
1573 if (vtyp == VCHR && rdev == 0xffffffff)
1574 vtyp = VFIFO;
1575 }
1576
1577 /*
1578 * If v_type == VNON it is a new node, so fill in the v_type,
1579 * n_mtime fields. Check to see if it represents a special
1580 * device, and if so, check for a possible alias. Once the
1581 * correct vnode has been obtained, fill in the rest of the
1582 * information.
1583 */
1584 np = VTONFS(vp);
1585 if (vp->v_type == VNON) {
1586 vp->v_type = vtyp;
1587 if (vp->v_type == VFIFO) {
1588 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1589 vp->v_op = fifo_nfsv2nodeop_p;
1590 }
1591 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1592 vp->v_op = spec_nfsv2nodeop_p;
1593 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1594 if (nvp) {
1595 /*
1596 * Discard unneeded vnode, but save its nfsnode.
1597 * Since the nfsnode does not have a lock, its
1598 * vnode lock has to be carried over.
1599 */
1600 nvp->v_data = vp->v_data;
1601 vp->v_data = NULL;
1602 vp->v_op = spec_vnodeop_p;
1603 vput(vp);
1604 vgone(vp);
1605 /*
1606 * XXX When nfs starts locking, we need to
1607 * lock the new node here.
1608 */
1609 /*
1610 * Reinitialize aliased node.
1611 */
1612 np->n_vnode = nvp;
1613 *vpp = vp = nvp;
1614 }
1615 }
1616 np->n_mtime = mtime.tv_sec;
1617 }
1618 vap = np->n_vattr;
1619 vap->va_type = vtyp;
1620 vap->va_mode = vmode & ALLPERMS;
1621 vap->va_rdev = (dev_t)rdev;
1622 vap->va_mtime = mtime;
1623 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1624 switch (vtyp) {
1625 case VDIR:
1626 vap->va_blocksize = NFS_DIRFRAGSIZ;
1627 break;
1628 case VBLK:
1629 vap->va_blocksize = BLKDEV_IOSIZE;
1630 break;
1631 case VCHR:
1632 vap->va_blocksize = MAXBSIZE;
1633 break;
1634 default:
1635 vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
1636 fxdr_unsigned(int32_t, fp->fa2_blocksize);
1637 break;
1638 }
1639 if (v3) {
1640 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1641 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1642 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1643 vap->va_size = fxdr_hyper(&fp->fa3_size);
1644 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1645 vap->va_fileid = fxdr_unsigned(int32_t,
1646 fp->fa3_fileid.nfsuquad[1]);
1647 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1648 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1649 vap->va_flags = 0;
1650 vap->va_filerev = 0;
1651 } else {
1652 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1653 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1654 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1655 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1656 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1657 * NFS_FABLKSIZE;
1658 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1659 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1660 vap->va_flags = 0;
1661 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1662 fp->fa2_ctime.nfsv2_sec);
1663 vap->va_ctime.tv_nsec = 0;
1664 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1665 vap->va_filerev = 0;
1666 }
1667 if (vap->va_size != np->n_size) {
1668 if (vap->va_type == VREG) {
1669 if (np->n_flag & NMODIFIED) {
1670 if (vap->va_size < np->n_size)
1671 vap->va_size = np->n_size;
1672 else
1673 np->n_size = vap->va_size;
1674 } else
1675 np->n_size = vap->va_size;
1676 uvm_vnp_setsize(vp, np->n_size);
1677 } else
1678 np->n_size = vap->va_size;
1679 }
1680 np->n_attrstamp = time.tv_sec;
1681 if (vaper != NULL) {
1682 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1683 if (np->n_flag & NCHG) {
1684 if (np->n_flag & NACC)
1685 vaper->va_atime = np->n_atim;
1686 if (np->n_flag & NUPD)
1687 vaper->va_mtime = np->n_mtim;
1688 }
1689 }
1690 return (0);
1691 }
1692
1693 /*
1694 * Check the time stamp
1695 * If the cache is valid, copy contents to *vap and return 0
1696 * otherwise return an error
1697 */
1698 int
1699 nfs_getattrcache(vp, vaper)
1700 struct vnode *vp;
1701 struct vattr *vaper;
1702 {
1703 struct nfsnode *np = VTONFS(vp);
1704 struct vattr *vap;
1705
1706 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1707 nfsstats.attrcache_misses++;
1708 return (ENOENT);
1709 }
1710 nfsstats.attrcache_hits++;
1711 vap = np->n_vattr;
1712 if (vap->va_size != np->n_size) {
1713 if (vap->va_type == VREG) {
1714 if (np->n_flag & NMODIFIED) {
1715 if (vap->va_size < np->n_size)
1716 vap->va_size = np->n_size;
1717 else
1718 np->n_size = vap->va_size;
1719 } else
1720 np->n_size = vap->va_size;
1721 uvm_vnp_setsize(vp, np->n_size);
1722 } else
1723 np->n_size = vap->va_size;
1724 }
1725 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
1726 if (np->n_flag & NCHG) {
1727 if (np->n_flag & NACC)
1728 vaper->va_atime = np->n_atim;
1729 if (np->n_flag & NUPD)
1730 vaper->va_mtime = np->n_mtim;
1731 }
1732 return (0);
1733 }
1734
1735 /*
1736 * Heuristic to see if the server XDR encodes directory cookies or not.
1737 * it is not supposed to, but a lot of servers may do this. Also, since
1738 * most/all servers will implement V2 as well, it is expected that they
1739 * may return just 32 bits worth of cookie information, so we need to
1740 * find out in which 32 bits this information is available. We do this
1741 * to avoid trouble with emulated binaries that can't handle 64 bit
1742 * directory offsets.
1743 */
1744
1745 void
1746 nfs_cookieheuristic(vp, flagp, p, cred)
1747 struct vnode *vp;
1748 int *flagp;
1749 struct proc *p;
1750 struct ucred *cred;
1751 {
1752 struct uio auio;
1753 struct iovec aiov;
1754 caddr_t buf, cp;
1755 struct dirent *dp;
1756 off_t *cookies = NULL, *cop;
1757 int error, eof, nc, len;
1758
1759 MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
1760
1761 aiov.iov_base = buf;
1762 aiov.iov_len = NFS_DIRFRAGSIZ;
1763 auio.uio_iov = &aiov;
1764 auio.uio_iovcnt = 1;
1765 auio.uio_rw = UIO_READ;
1766 auio.uio_segflg = UIO_SYSSPACE;
1767 auio.uio_procp = p;
1768 auio.uio_resid = NFS_DIRFRAGSIZ;
1769 auio.uio_offset = 0;
1770
1771 error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
1772
1773 len = NFS_DIRFRAGSIZ - auio.uio_resid;
1774 if (error || len == 0) {
1775 FREE(buf, M_TEMP);
1776 if (cookies)
1777 free(cookies, M_TEMP);
1778 return;
1779 }
1780
1781 /*
1782 * Find the first valid entry and look at its offset cookie.
1783 */
1784
1785 cp = buf;
1786 for (cop = cookies; len > 0; len -= dp->d_reclen) {
1787 dp = (struct dirent *)cp;
1788 if (dp->d_fileno != 0 && len >= dp->d_reclen) {
1789 if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
1790 *flagp |= NFSMNT_SWAPCOOKIE;
1791 nfs_invaldircache(vp, 0);
1792 nfs_vinvalbuf(vp, 0, cred, p, 1);
1793 }
1794 break;
1795 }
1796 cop++;
1797 cp += dp->d_reclen;
1798 }
1799
1800 FREE(buf, M_TEMP);
1801 free(cookies, M_TEMP);
1802 }
1803 #endif /* NFS */
1804
1805 /*
1806 * Set up nameidata for a lookup() call and do it.
1807 *
1808 * If pubflag is set, this call is done for a lookup operation on the
1809 * public filehandle. In that case we allow crossing mountpoints and
1810 * absolute pathnames. However, the caller is expected to check that
1811 * the lookup result is within the public fs, and deny access if
1812 * it is not.
1813 */
1814 int
1815 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1816 struct nameidata *ndp;
1817 fhandle_t *fhp;
1818 int len;
1819 struct nfssvc_sock *slp;
1820 struct mbuf *nam;
1821 struct mbuf **mdp;
1822 caddr_t *dposp;
1823 struct vnode **retdirp;
1824 struct proc *p;
1825 int kerbflag, pubflag;
1826 {
1827 int i, rem;
1828 struct mbuf *md;
1829 char *fromcp, *tocp, *cp;
1830 struct iovec aiov;
1831 struct uio auio;
1832 struct vnode *dp;
1833 int error, rdonly, linklen;
1834 struct componentname *cnp = &ndp->ni_cnd;
1835
1836 *retdirp = (struct vnode *)0;
1837
1838 if ((len + 1) > MAXPATHLEN)
1839 return (ENAMETOOLONG);
1840 cnp->cn_pnbuf = PNBUF_GET();
1841
1842 /*
1843 * Copy the name from the mbuf list to ndp->ni_pnbuf
1844 * and set the various ndp fields appropriately.
1845 */
1846 fromcp = *dposp;
1847 tocp = cnp->cn_pnbuf;
1848 md = *mdp;
1849 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1850 for (i = 0; i < len; i++) {
1851 while (rem == 0) {
1852 md = md->m_next;
1853 if (md == NULL) {
1854 error = EBADRPC;
1855 goto out;
1856 }
1857 fromcp = mtod(md, caddr_t);
1858 rem = md->m_len;
1859 }
1860 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1861 error = EACCES;
1862 goto out;
1863 }
1864 *tocp++ = *fromcp++;
1865 rem--;
1866 }
1867 *tocp = '\0';
1868 *mdp = md;
1869 *dposp = fromcp;
1870 len = nfsm_rndup(len)-len;
1871 if (len > 0) {
1872 if (rem >= len)
1873 *dposp += len;
1874 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1875 goto out;
1876 }
1877
1878 /*
1879 * Extract and set starting directory.
1880 */
1881 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1882 nam, &rdonly, kerbflag, pubflag);
1883 if (error)
1884 goto out;
1885 if (dp->v_type != VDIR) {
1886 vrele(dp);
1887 error = ENOTDIR;
1888 goto out;
1889 }
1890
1891 if (rdonly)
1892 cnp->cn_flags |= RDONLY;
1893
1894 *retdirp = dp;
1895
1896 if (pubflag) {
1897 /*
1898 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1899 * and the 'native path' indicator.
1900 */
1901 cp = PNBUF_GET();
1902 fromcp = cnp->cn_pnbuf;
1903 tocp = cp;
1904 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1905 switch ((unsigned char)*fromcp) {
1906 case WEBNFS_NATIVE_CHAR:
1907 /*
1908 * 'Native' path for us is the same
1909 * as a path according to the NFS spec,
1910 * just skip the escape char.
1911 */
1912 fromcp++;
1913 break;
1914 /*
1915 * More may be added in the future, range 0x80-0xff
1916 */
1917 default:
1918 error = EIO;
1919 FREE(cp, M_NAMEI);
1920 goto out;
1921 }
1922 }
1923 /*
1924 * Translate the '%' escapes, URL-style.
1925 */
1926 while (*fromcp != '\0') {
1927 if (*fromcp == WEBNFS_ESC_CHAR) {
1928 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1929 fromcp++;
1930 *tocp++ = HEXSTRTOI(fromcp);
1931 fromcp += 2;
1932 continue;
1933 } else {
1934 error = ENOENT;
1935 FREE(cp, M_NAMEI);
1936 goto out;
1937 }
1938 } else
1939 *tocp++ = *fromcp++;
1940 }
1941 *tocp = '\0';
1942 PNBUF_PUT(cnp->cn_pnbuf);
1943 cnp->cn_pnbuf = cp;
1944 }
1945
1946 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1947 ndp->ni_segflg = UIO_SYSSPACE;
1948
1949 if (pubflag) {
1950 ndp->ni_rootdir = rootvnode;
1951 ndp->ni_loopcnt = 0;
1952 if (cnp->cn_pnbuf[0] == '/')
1953 dp = rootvnode;
1954 } else {
1955 cnp->cn_flags |= NOCROSSMOUNT;
1956 }
1957
1958 cnp->cn_proc = p;
1959 VREF(dp);
1960
1961 for (;;) {
1962 cnp->cn_nameptr = cnp->cn_pnbuf;
1963 ndp->ni_startdir = dp;
1964 /*
1965 * And call lookup() to do the real work
1966 */
1967 error = lookup(ndp);
1968 if (error)
1969 break;
1970 /*
1971 * Check for encountering a symbolic link
1972 */
1973 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1974 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1975 cnp->cn_flags |= HASBUF;
1976 return (0);
1977 }
1978 break;
1979 } else {
1980 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1981 VOP_UNLOCK(ndp->ni_dvp, 0);
1982 if (!pubflag) {
1983 vrele(ndp->ni_dvp);
1984 vput(ndp->ni_vp);
1985 ndp->ni_vp = NULL;
1986 error = EINVAL;
1987 break;
1988 }
1989
1990 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1991 error = ELOOP;
1992 break;
1993 }
1994 if (ndp->ni_pathlen > 1)
1995 cp = PNBUF_GET();
1996 else
1997 cp = cnp->cn_pnbuf;
1998 aiov.iov_base = cp;
1999 aiov.iov_len = MAXPATHLEN;
2000 auio.uio_iov = &aiov;
2001 auio.uio_iovcnt = 1;
2002 auio.uio_offset = 0;
2003 auio.uio_rw = UIO_READ;
2004 auio.uio_segflg = UIO_SYSSPACE;
2005 auio.uio_procp = (struct proc *)0;
2006 auio.uio_resid = MAXPATHLEN;
2007 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2008 if (error) {
2009 badlink:
2010 if (ndp->ni_pathlen > 1)
2011 PNBUF_PUT(cp);
2012 break;
2013 }
2014 linklen = MAXPATHLEN - auio.uio_resid;
2015 if (linklen == 0) {
2016 error = ENOENT;
2017 goto badlink;
2018 }
2019 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2020 error = ENAMETOOLONG;
2021 goto badlink;
2022 }
2023 if (ndp->ni_pathlen > 1) {
2024 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
2025 PNBUF_PUT(cnp->cn_pnbuf);
2026 cnp->cn_pnbuf = cp;
2027 } else
2028 cnp->cn_pnbuf[linklen] = '\0';
2029 ndp->ni_pathlen += linklen;
2030 vput(ndp->ni_vp);
2031 dp = ndp->ni_dvp;
2032 /*
2033 * Check if root directory should replace current directory.
2034 */
2035 if (cnp->cn_pnbuf[0] == '/') {
2036 vrele(dp);
2037 dp = ndp->ni_rootdir;
2038 VREF(dp);
2039 }
2040 }
2041 }
2042 out:
2043 PNBUF_PUT(cnp->cn_pnbuf);
2044 return (error);
2045 }
2046
2047 /*
2048 * A fiddled version of m_adj() that ensures null fill to a long
2049 * boundary and only trims off the back end
2050 */
2051 void
2052 nfsm_adj(mp, len, nul)
2053 struct mbuf *mp;
2054 int len;
2055 int nul;
2056 {
2057 struct mbuf *m;
2058 int count, i;
2059 char *cp;
2060
2061 /*
2062 * Trim from tail. Scan the mbuf chain,
2063 * calculating its length and finding the last mbuf.
2064 * If the adjustment only affects this mbuf, then just
2065 * adjust and return. Otherwise, rescan and truncate
2066 * after the remaining size.
2067 */
2068 count = 0;
2069 m = mp;
2070 for (;;) {
2071 count += m->m_len;
2072 if (m->m_next == (struct mbuf *)0)
2073 break;
2074 m = m->m_next;
2075 }
2076 if (m->m_len > len) {
2077 m->m_len -= len;
2078 if (nul > 0) {
2079 cp = mtod(m, caddr_t)+m->m_len-nul;
2080 for (i = 0; i < nul; i++)
2081 *cp++ = '\0';
2082 }
2083 return;
2084 }
2085 count -= len;
2086 if (count < 0)
2087 count = 0;
2088 /*
2089 * Correct length for chain is "count".
2090 * Find the mbuf with last data, adjust its length,
2091 * and toss data from remaining mbufs on chain.
2092 */
2093 for (m = mp; m; m = m->m_next) {
2094 if (m->m_len >= count) {
2095 m->m_len = count;
2096 if (nul > 0) {
2097 cp = mtod(m, caddr_t)+m->m_len-nul;
2098 for (i = 0; i < nul; i++)
2099 *cp++ = '\0';
2100 }
2101 break;
2102 }
2103 count -= m->m_len;
2104 }
2105 for (m = m->m_next;m;m = m->m_next)
2106 m->m_len = 0;
2107 }
2108
2109 /*
2110 * Make these functions instead of macros, so that the kernel text size
2111 * doesn't get too big...
2112 */
2113 void
2114 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2115 struct nfsrv_descript *nfsd;
2116 int before_ret;
2117 struct vattr *before_vap;
2118 int after_ret;
2119 struct vattr *after_vap;
2120 struct mbuf **mbp;
2121 char **bposp;
2122 {
2123 struct mbuf *mb = *mbp, *mb2;
2124 char *bpos = *bposp;
2125 u_int32_t *tl;
2126
2127 if (before_ret) {
2128 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2129 *tl = nfs_false;
2130 } else {
2131 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2132 *tl++ = nfs_true;
2133 txdr_hyper(before_vap->va_size, tl);
2134 tl += 2;
2135 txdr_nfsv3time(&(before_vap->va_mtime), tl);
2136 tl += 2;
2137 txdr_nfsv3time(&(before_vap->va_ctime), tl);
2138 }
2139 *bposp = bpos;
2140 *mbp = mb;
2141 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2142 }
2143
2144 void
2145 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2146 struct nfsrv_descript *nfsd;
2147 int after_ret;
2148 struct vattr *after_vap;
2149 struct mbuf **mbp;
2150 char **bposp;
2151 {
2152 struct mbuf *mb = *mbp, *mb2;
2153 char *bpos = *bposp;
2154 u_int32_t *tl;
2155 struct nfs_fattr *fp;
2156
2157 if (after_ret) {
2158 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2159 *tl = nfs_false;
2160 } else {
2161 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2162 *tl++ = nfs_true;
2163 fp = (struct nfs_fattr *)tl;
2164 nfsm_srvfattr(nfsd, after_vap, fp);
2165 }
2166 *mbp = mb;
2167 *bposp = bpos;
2168 }
2169
2170 void
2171 nfsm_srvfattr(nfsd, vap, fp)
2172 struct nfsrv_descript *nfsd;
2173 struct vattr *vap;
2174 struct nfs_fattr *fp;
2175 {
2176
2177 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2178 fp->fa_uid = txdr_unsigned(vap->va_uid);
2179 fp->fa_gid = txdr_unsigned(vap->va_gid);
2180 if (nfsd->nd_flag & ND_NFSV3) {
2181 fp->fa_type = vtonfsv3_type(vap->va_type);
2182 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2183 txdr_hyper(vap->va_size, &fp->fa3_size);
2184 txdr_hyper(vap->va_bytes, &fp->fa3_used);
2185 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2186 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2187 fp->fa3_fsid.nfsuquad[0] = 0;
2188 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2189 fp->fa3_fileid.nfsuquad[0] = 0;
2190 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
2191 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2192 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2193 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2194 } else {
2195 fp->fa_type = vtonfsv2_type(vap->va_type);
2196 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2197 fp->fa2_size = txdr_unsigned(vap->va_size);
2198 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2199 if (vap->va_type == VFIFO)
2200 fp->fa2_rdev = 0xffffffff;
2201 else
2202 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2203 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2204 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2205 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2206 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2207 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2208 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2209 }
2210 }
2211
2212 /*
2213 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2214 * - look up fsid in mount list (if not found ret error)
2215 * - get vp and export rights by calling VFS_FHTOVP()
2216 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2217 * - if not lockflag unlock it with VOP_UNLOCK()
2218 */
2219 int
2220 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2221 fhandle_t *fhp;
2222 int lockflag;
2223 struct vnode **vpp;
2224 struct ucred *cred;
2225 struct nfssvc_sock *slp;
2226 struct mbuf *nam;
2227 int *rdonlyp;
2228 int kerbflag;
2229 {
2230 struct mount *mp;
2231 int i;
2232 struct ucred *credanon;
2233 int error, exflags;
2234 struct sockaddr_in *saddr;
2235
2236 *vpp = (struct vnode *)0;
2237
2238 if (nfs_ispublicfh(fhp)) {
2239 if (!pubflag || !nfs_pub.np_valid)
2240 return (ESTALE);
2241 fhp = &nfs_pub.np_handle;
2242 }
2243
2244 mp = vfs_getvfs(&fhp->fh_fsid);
2245 if (!mp)
2246 return (ESTALE);
2247 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
2248 if (error)
2249 return (error);
2250 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2251 if (error)
2252 return (error);
2253
2254 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2255 saddr = mtod(nam, struct sockaddr_in *);
2256 if ((saddr->sin_family == AF_INET) &&
2257 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2258 vput(*vpp);
2259 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2260 }
2261 #ifdef INET6
2262 if ((saddr->sin_family == AF_INET6) &&
2263 ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
2264 vput(*vpp);
2265 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2266 }
2267 #endif
2268 }
2269 /*
2270 * Check/setup credentials.
2271 */
2272 if (exflags & MNT_EXKERB) {
2273 if (!kerbflag) {
2274 vput(*vpp);
2275 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2276 }
2277 } else if (kerbflag) {
2278 vput(*vpp);
2279 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2280 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2281 cred->cr_uid = credanon->cr_uid;
2282 cred->cr_gid = credanon->cr_gid;
2283 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2284 cred->cr_groups[i] = credanon->cr_groups[i];
2285 cred->cr_ngroups = i;
2286 }
2287 if (exflags & MNT_EXRDONLY)
2288 *rdonlyp = 1;
2289 else
2290 *rdonlyp = 0;
2291 if (!lockflag)
2292 VOP_UNLOCK(*vpp, 0);
2293 return (0);
2294 }
2295
2296 /*
2297 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2298 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2299 * transformed this to all zeroes in both cases, so check for it.
2300 */
2301 int
2302 nfs_ispublicfh(fhp)
2303 fhandle_t *fhp;
2304 {
2305 char *cp = (char *)fhp;
2306 int i;
2307
2308 for (i = 0; i < NFSX_V3FH; i++)
2309 if (*cp++ != 0)
2310 return (FALSE);
2311 return (TRUE);
2312 }
2313
2314 /*
2315 * This function compares two net addresses by family and returns TRUE
2316 * if they are the same host.
2317 * If there is any doubt, return FALSE.
2318 * The AF_INET family is handled as a special case so that address mbufs
2319 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2320 */
2321 int
2322 netaddr_match(family, haddr, nam)
2323 int family;
2324 union nethostaddr *haddr;
2325 struct mbuf *nam;
2326 {
2327 struct sockaddr_in *inetaddr;
2328
2329 switch (family) {
2330 case AF_INET:
2331 inetaddr = mtod(nam, struct sockaddr_in *);
2332 if (inetaddr->sin_family == AF_INET &&
2333 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2334 return (1);
2335 break;
2336 #ifdef INET6
2337 case AF_INET6:
2338 {
2339 struct sockaddr_in6 *sin6_1, *sin6_2;
2340
2341 sin6_1 = mtod(nam, struct sockaddr_in6 *);
2342 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
2343 if (sin6_1->sin6_family == AF_INET6 &&
2344 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
2345 return 1;
2346 }
2347 #endif
2348 #ifdef ISO
2349 case AF_ISO:
2350 {
2351 struct sockaddr_iso *isoaddr1, *isoaddr2;
2352
2353 isoaddr1 = mtod(nam, struct sockaddr_iso *);
2354 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2355 if (isoaddr1->siso_family == AF_ISO &&
2356 isoaddr1->siso_nlen > 0 &&
2357 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2358 SAME_ISOADDR(isoaddr1, isoaddr2))
2359 return (1);
2360 break;
2361 }
2362 #endif /* ISO */
2363 default:
2364 break;
2365 };
2366 return (0);
2367 }
2368
2369
2370 /*
2371 * The write verifier has changed (probably due to a server reboot), so all
2372 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2373 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2374 * flag. Once done the new write verifier can be set for the mount point.
2375 */
2376 void
2377 nfs_clearcommit(mp)
2378 struct mount *mp;
2379 {
2380 struct vnode *vp, *nvp;
2381 struct buf *bp, *nbp;
2382 struct nfsnode *np;
2383 int s;
2384
2385 s = splbio();
2386 loop:
2387 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2388 if (vp->v_mount != mp) /* Paranoia */
2389 goto loop;
2390 nvp = vp->v_mntvnodes.le_next;
2391 if (vp->v_type == VNON)
2392 continue;
2393 np = VTONFS(vp);
2394 np->n_pushlo = np->n_pushhi = np->n_pushedlo =
2395 np->n_pushedhi = 0;
2396 np->n_commitflags &=
2397 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
2398 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2399 nbp = bp->b_vnbufs.le_next;
2400 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2401 == (B_DELWRI | B_NEEDCOMMIT))
2402 bp->b_flags &= ~B_NEEDCOMMIT;
2403 }
2404 }
2405 splx(s);
2406 }
2407
2408 void
2409 nfs_merge_commit_ranges(vp)
2410 struct vnode *vp;
2411 {
2412 struct nfsnode *np = VTONFS(vp);
2413
2414 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2415 np->n_pushedlo = np->n_pushlo;
2416 np->n_pushedhi = np->n_pushhi;
2417 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2418 } else {
2419 if (np->n_pushlo < np->n_pushedlo)
2420 np->n_pushedlo = np->n_pushlo;
2421 if (np->n_pushhi > np->n_pushedhi)
2422 np->n_pushedhi = np->n_pushhi;
2423 }
2424
2425 np->n_pushlo = np->n_pushhi = 0;
2426 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
2427
2428 #ifdef fvdl_debug
2429 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2430 (unsigned)np->n_pushedhi);
2431 #endif
2432 }
2433
2434 int
2435 nfs_in_committed_range(vp, bp)
2436 struct vnode *vp;
2437 struct buf *bp;
2438 {
2439 struct nfsnode *np = VTONFS(vp);
2440 off_t lo, hi;
2441
2442 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2443 return 0;
2444 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2445 hi = lo + bp->b_dirtyend;
2446
2447 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
2448 }
2449
2450 int
2451 nfs_in_tobecommitted_range(vp, bp)
2452 struct vnode *vp;
2453 struct buf *bp;
2454 {
2455 struct nfsnode *np = VTONFS(vp);
2456 off_t lo, hi;
2457
2458 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2459 return 0;
2460 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2461 hi = lo + bp->b_dirtyend;
2462
2463 return (lo >= np->n_pushlo && hi <= np->n_pushhi);
2464 }
2465
2466 void
2467 nfs_add_committed_range(vp, bp)
2468 struct vnode *vp;
2469 struct buf *bp;
2470 {
2471 struct nfsnode *np = VTONFS(vp);
2472 off_t lo, hi;
2473
2474 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2475 hi = lo + bp->b_dirtyend;
2476
2477 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2478 np->n_pushedlo = lo;
2479 np->n_pushedhi = hi;
2480 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2481 } else {
2482 if (hi > np->n_pushedhi)
2483 np->n_pushedhi = hi;
2484 if (lo < np->n_pushedlo)
2485 np->n_pushedlo = lo;
2486 }
2487 #ifdef fvdl_debug
2488 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2489 (unsigned)np->n_pushedhi);
2490 #endif
2491 }
2492
2493 void
2494 nfs_del_committed_range(vp, bp)
2495 struct vnode *vp;
2496 struct buf *bp;
2497 {
2498 struct nfsnode *np = VTONFS(vp);
2499 off_t lo, hi;
2500
2501 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2502 return;
2503
2504 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2505 hi = lo + bp->b_dirtyend;
2506
2507 if (lo > np->n_pushedhi || hi < np->n_pushedlo)
2508 return;
2509 if (lo <= np->n_pushedlo)
2510 np->n_pushedlo = hi;
2511 else if (hi >= np->n_pushedhi)
2512 np->n_pushedhi = lo;
2513 else {
2514 /*
2515 * XXX There's only one range. If the deleted range
2516 * is in the middle, pick the largest of the
2517 * contiguous ranges that it leaves.
2518 */
2519 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
2520 np->n_pushedhi = lo;
2521 else
2522 np->n_pushedlo = hi;
2523 }
2524 #ifdef fvdl_debug
2525 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2526 (unsigned)np->n_pushedhi);
2527 #endif
2528 }
2529
2530 void
2531 nfs_add_tobecommitted_range(vp, bp)
2532 struct vnode *vp;
2533 struct buf *bp;
2534 {
2535 struct nfsnode *np = VTONFS(vp);
2536 off_t lo, hi;
2537
2538 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2539 hi = lo + bp->b_dirtyend;
2540
2541 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
2542 np->n_pushlo = lo;
2543 np->n_pushhi = hi;
2544 np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
2545 } else {
2546 if (lo < np->n_pushlo)
2547 np->n_pushlo = lo;
2548 if (hi > np->n_pushhi)
2549 np->n_pushhi = hi;
2550 }
2551 #ifdef fvdl_debug
2552 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2553 (unsigned)np->n_pushhi);
2554 #endif
2555 }
2556
2557 void
2558 nfs_del_tobecommitted_range(vp, bp)
2559 struct vnode *vp;
2560 struct buf *bp;
2561 {
2562 struct nfsnode *np = VTONFS(vp);
2563 off_t lo, hi;
2564
2565 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2566 return;
2567
2568 lo = (off_t)bp->b_blkno * DEV_BSIZE;
2569 hi = lo + bp->b_dirtyend;
2570
2571 if (lo > np->n_pushhi || hi < np->n_pushlo)
2572 return;
2573
2574 if (lo <= np->n_pushlo)
2575 np->n_pushlo = hi;
2576 else if (hi >= np->n_pushhi)
2577 np->n_pushhi = lo;
2578 else {
2579 /*
2580 * XXX There's only one range. If the deleted range
2581 * is in the middle, pick the largest of the
2582 * contiguous ranges that it leaves.
2583 */
2584 if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
2585 np->n_pushhi = lo;
2586 else
2587 np->n_pushlo = hi;
2588 }
2589 #ifdef fvdl_debug
2590 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2591 (unsigned)np->n_pushhi);
2592 #endif
2593 }
2594
2595 /*
2596 * Map errnos to NFS error numbers. For Version 3 also filter out error
2597 * numbers not specified for the associated procedure.
2598 */
2599 int
2600 nfsrv_errmap(nd, err)
2601 struct nfsrv_descript *nd;
2602 int err;
2603 {
2604 short *defaulterrp, *errp;
2605
2606 if (nd->nd_flag & ND_NFSV3) {
2607 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2608 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2609 while (*++errp) {
2610 if (*errp == err)
2611 return (err);
2612 else if (*errp > err)
2613 break;
2614 }
2615 return ((int)*defaulterrp);
2616 } else
2617 return (err & 0xffff);
2618 }
2619 if (err <= ELAST)
2620 return ((int)nfsrv_v2errmap[err - 1]);
2621 return (NFSERR_IO);
2622 }
2623
2624 /*
2625 * Sort the group list in increasing numerical order.
2626 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2627 * that used to be here.)
2628 */
2629 void
2630 nfsrvw_sort(list, num)
2631 gid_t *list;
2632 int num;
2633 {
2634 int i, j;
2635 gid_t v;
2636
2637 /* Insertion sort. */
2638 for (i = 1; i < num; i++) {
2639 v = list[i];
2640 /* find correct slot for value v, moving others up */
2641 for (j = i; --j >= 0 && v < list[j];)
2642 list[j + 1] = list[j];
2643 list[j + 1] = v;
2644 }
2645 }
2646
2647 /*
2648 * copy credentials making sure that the result can be compared with memcmp().
2649 */
2650 void
2651 nfsrv_setcred(incred, outcred)
2652 struct ucred *incred, *outcred;
2653 {
2654 int i;
2655
2656 memset((caddr_t)outcred, 0, sizeof (struct ucred));
2657 outcred->cr_ref = 1;
2658 outcred->cr_uid = incred->cr_uid;
2659 outcred->cr_gid = incred->cr_gid;
2660 outcred->cr_ngroups = incred->cr_ngroups;
2661 for (i = 0; i < incred->cr_ngroups; i++)
2662 outcred->cr_groups[i] = incred->cr_groups[i];
2663 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2664 }
2665