nfs_subs.c revision 1.45 1 /* $NetBSD: nfs_subs.c,v 1.45 1997/07/14 20:46:20 fvdl 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 /*
43 * These functions support the macros and help fiddle mbuf chains for
44 * the nfs op functions. They do things like create the rpc header and
45 * copy data between mbuf chains and uio lists.
46 */
47 #include <sys/param.h>
48 #include <sys/proc.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/mbuf.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/malloc.h>
58 #include <sys/time.h>
59 #include <sys/dirent.h>
60
61 #include <vm/vm.h>
62
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/nfsnode.h>
66 #include <nfs/nfs.h>
67 #include <nfs/xdr_subs.h>
68 #include <nfs/nfsm_subs.h>
69 #include <nfs/nfsmount.h>
70 #include <nfs/nqnfs.h>
71 #include <nfs/nfsrtt.h>
72 #include <nfs/nfs_var.h>
73
74 #include <miscfs/specfs/specdev.h>
75
76 #include <vm/vm.h>
77
78 #include <netinet/in.h>
79 #ifdef ISO
80 #include <netiso/iso.h>
81 #endif
82
83 /*
84 * Data items converted to xdr at startup, since they are constant
85 * This is kinda hokey, but may save a little time doing byte swaps
86 */
87 u_int32_t nfs_xdrneg1;
88 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
89 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
90 rpc_auth_kerb;
91 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
92
93 /* And other global data */
94 static u_int32_t nfs_xid = 0;
95 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
96 NFCHR, NFNON };
97 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
98 NFFIFO, NFNON };
99 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
100 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
101 int nfs_ticks;
102 extern struct nfs_public nfs_pub;
103
104 /* NFS client/server stats. */
105 struct nfsstats nfsstats;
106
107 /*
108 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
109 */
110 int nfsv3_procid[NFS_NPROCS] = {
111 NFSPROC_NULL,
112 NFSPROC_GETATTR,
113 NFSPROC_SETATTR,
114 NFSPROC_NOOP,
115 NFSPROC_LOOKUP,
116 NFSPROC_READLINK,
117 NFSPROC_READ,
118 NFSPROC_NOOP,
119 NFSPROC_WRITE,
120 NFSPROC_CREATE,
121 NFSPROC_REMOVE,
122 NFSPROC_RENAME,
123 NFSPROC_LINK,
124 NFSPROC_SYMLINK,
125 NFSPROC_MKDIR,
126 NFSPROC_RMDIR,
127 NFSPROC_READDIR,
128 NFSPROC_FSSTAT,
129 NFSPROC_NOOP,
130 NFSPROC_NOOP,
131 NFSPROC_NOOP,
132 NFSPROC_NOOP,
133 NFSPROC_NOOP,
134 NFSPROC_NOOP,
135 NFSPROC_NOOP,
136 NFSPROC_NOOP
137 };
138
139 /*
140 * and the reverse mapping from generic to Version 2 procedure numbers
141 */
142 int nfsv2_procid[NFS_NPROCS] = {
143 NFSV2PROC_NULL,
144 NFSV2PROC_GETATTR,
145 NFSV2PROC_SETATTR,
146 NFSV2PROC_LOOKUP,
147 NFSV2PROC_NOOP,
148 NFSV2PROC_READLINK,
149 NFSV2PROC_READ,
150 NFSV2PROC_WRITE,
151 NFSV2PROC_CREATE,
152 NFSV2PROC_MKDIR,
153 NFSV2PROC_SYMLINK,
154 NFSV2PROC_CREATE,
155 NFSV2PROC_REMOVE,
156 NFSV2PROC_RMDIR,
157 NFSV2PROC_RENAME,
158 NFSV2PROC_LINK,
159 NFSV2PROC_READDIR,
160 NFSV2PROC_NOOP,
161 NFSV2PROC_STATFS,
162 NFSV2PROC_NOOP,
163 NFSV2PROC_NOOP,
164 NFSV2PROC_NOOP,
165 NFSV2PROC_NOOP,
166 NFSV2PROC_NOOP,
167 NFSV2PROC_NOOP,
168 NFSV2PROC_NOOP,
169 };
170
171 /*
172 * Maps errno values to nfs error numbers.
173 * Use NFSERR_IO as the catch all for ones not specifically defined in
174 * RFC 1094.
175 */
176 static u_char nfsrv_v2errmap[ELAST] = {
177 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
178 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
179 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
180 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
181 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
182 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
183 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
184 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
185 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
186 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
187 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
188 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
189 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
190 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
191 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
192 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
193 NFSERR_IO,
194 };
195
196 /*
197 * Maps errno values to nfs error numbers.
198 * Although it is not obvious whether or not NFS clients really care if
199 * a returned error value is in the specified list for the procedure, the
200 * safest thing to do is filter them appropriately. For Version 2, the
201 * X/Open XNFS document is the only specification that defines error values
202 * for each RPC (The RFC simply lists all possible error values for all RPCs),
203 * so I have decided to not do this for Version 2.
204 * The first entry is the default error return and the rest are the valid
205 * errors for that RPC in increasing numeric order.
206 */
207 static short nfsv3err_null[] = {
208 0,
209 0,
210 };
211
212 static short nfsv3err_getattr[] = {
213 NFSERR_IO,
214 NFSERR_IO,
215 NFSERR_STALE,
216 NFSERR_BADHANDLE,
217 NFSERR_SERVERFAULT,
218 0,
219 };
220
221 static short nfsv3err_setattr[] = {
222 NFSERR_IO,
223 NFSERR_PERM,
224 NFSERR_IO,
225 NFSERR_ACCES,
226 NFSERR_INVAL,
227 NFSERR_NOSPC,
228 NFSERR_ROFS,
229 NFSERR_DQUOT,
230 NFSERR_STALE,
231 NFSERR_BADHANDLE,
232 NFSERR_NOT_SYNC,
233 NFSERR_SERVERFAULT,
234 0,
235 };
236
237 static short nfsv3err_lookup[] = {
238 NFSERR_IO,
239 NFSERR_NOENT,
240 NFSERR_IO,
241 NFSERR_ACCES,
242 NFSERR_NOTDIR,
243 NFSERR_NAMETOL,
244 NFSERR_STALE,
245 NFSERR_BADHANDLE,
246 NFSERR_SERVERFAULT,
247 0,
248 };
249
250 static short nfsv3err_access[] = {
251 NFSERR_IO,
252 NFSERR_IO,
253 NFSERR_STALE,
254 NFSERR_BADHANDLE,
255 NFSERR_SERVERFAULT,
256 0,
257 };
258
259 static short nfsv3err_readlink[] = {
260 NFSERR_IO,
261 NFSERR_IO,
262 NFSERR_ACCES,
263 NFSERR_INVAL,
264 NFSERR_STALE,
265 NFSERR_BADHANDLE,
266 NFSERR_NOTSUPP,
267 NFSERR_SERVERFAULT,
268 0,
269 };
270
271 static short nfsv3err_read[] = {
272 NFSERR_IO,
273 NFSERR_IO,
274 NFSERR_NXIO,
275 NFSERR_ACCES,
276 NFSERR_INVAL,
277 NFSERR_STALE,
278 NFSERR_BADHANDLE,
279 NFSERR_SERVERFAULT,
280 0,
281 };
282
283 static short nfsv3err_write[] = {
284 NFSERR_IO,
285 NFSERR_IO,
286 NFSERR_ACCES,
287 NFSERR_INVAL,
288 NFSERR_FBIG,
289 NFSERR_NOSPC,
290 NFSERR_ROFS,
291 NFSERR_DQUOT,
292 NFSERR_STALE,
293 NFSERR_BADHANDLE,
294 NFSERR_SERVERFAULT,
295 0,
296 };
297
298 static short nfsv3err_create[] = {
299 NFSERR_IO,
300 NFSERR_IO,
301 NFSERR_ACCES,
302 NFSERR_EXIST,
303 NFSERR_NOTDIR,
304 NFSERR_NOSPC,
305 NFSERR_ROFS,
306 NFSERR_NAMETOL,
307 NFSERR_DQUOT,
308 NFSERR_STALE,
309 NFSERR_BADHANDLE,
310 NFSERR_NOTSUPP,
311 NFSERR_SERVERFAULT,
312 0,
313 };
314
315 static short nfsv3err_mkdir[] = {
316 NFSERR_IO,
317 NFSERR_IO,
318 NFSERR_ACCES,
319 NFSERR_EXIST,
320 NFSERR_NOTDIR,
321 NFSERR_NOSPC,
322 NFSERR_ROFS,
323 NFSERR_NAMETOL,
324 NFSERR_DQUOT,
325 NFSERR_STALE,
326 NFSERR_BADHANDLE,
327 NFSERR_NOTSUPP,
328 NFSERR_SERVERFAULT,
329 0,
330 };
331
332 static short nfsv3err_symlink[] = {
333 NFSERR_IO,
334 NFSERR_IO,
335 NFSERR_ACCES,
336 NFSERR_EXIST,
337 NFSERR_NOTDIR,
338 NFSERR_NOSPC,
339 NFSERR_ROFS,
340 NFSERR_NAMETOL,
341 NFSERR_DQUOT,
342 NFSERR_STALE,
343 NFSERR_BADHANDLE,
344 NFSERR_NOTSUPP,
345 NFSERR_SERVERFAULT,
346 0,
347 };
348
349 static short nfsv3err_mknod[] = {
350 NFSERR_IO,
351 NFSERR_IO,
352 NFSERR_ACCES,
353 NFSERR_EXIST,
354 NFSERR_NOTDIR,
355 NFSERR_NOSPC,
356 NFSERR_ROFS,
357 NFSERR_NAMETOL,
358 NFSERR_DQUOT,
359 NFSERR_STALE,
360 NFSERR_BADHANDLE,
361 NFSERR_NOTSUPP,
362 NFSERR_SERVERFAULT,
363 NFSERR_BADTYPE,
364 0,
365 };
366
367 static short nfsv3err_remove[] = {
368 NFSERR_IO,
369 NFSERR_NOENT,
370 NFSERR_IO,
371 NFSERR_ACCES,
372 NFSERR_NOTDIR,
373 NFSERR_ROFS,
374 NFSERR_NAMETOL,
375 NFSERR_STALE,
376 NFSERR_BADHANDLE,
377 NFSERR_SERVERFAULT,
378 0,
379 };
380
381 static short nfsv3err_rmdir[] = {
382 NFSERR_IO,
383 NFSERR_NOENT,
384 NFSERR_IO,
385 NFSERR_ACCES,
386 NFSERR_EXIST,
387 NFSERR_NOTDIR,
388 NFSERR_INVAL,
389 NFSERR_ROFS,
390 NFSERR_NAMETOL,
391 NFSERR_NOTEMPTY,
392 NFSERR_STALE,
393 NFSERR_BADHANDLE,
394 NFSERR_NOTSUPP,
395 NFSERR_SERVERFAULT,
396 0,
397 };
398
399 static short nfsv3err_rename[] = {
400 NFSERR_IO,
401 NFSERR_NOENT,
402 NFSERR_IO,
403 NFSERR_ACCES,
404 NFSERR_EXIST,
405 NFSERR_XDEV,
406 NFSERR_NOTDIR,
407 NFSERR_ISDIR,
408 NFSERR_INVAL,
409 NFSERR_NOSPC,
410 NFSERR_ROFS,
411 NFSERR_MLINK,
412 NFSERR_NAMETOL,
413 NFSERR_NOTEMPTY,
414 NFSERR_DQUOT,
415 NFSERR_STALE,
416 NFSERR_BADHANDLE,
417 NFSERR_NOTSUPP,
418 NFSERR_SERVERFAULT,
419 0,
420 };
421
422 static short nfsv3err_link[] = {
423 NFSERR_IO,
424 NFSERR_IO,
425 NFSERR_ACCES,
426 NFSERR_EXIST,
427 NFSERR_XDEV,
428 NFSERR_NOTDIR,
429 NFSERR_INVAL,
430 NFSERR_NOSPC,
431 NFSERR_ROFS,
432 NFSERR_MLINK,
433 NFSERR_NAMETOL,
434 NFSERR_DQUOT,
435 NFSERR_STALE,
436 NFSERR_BADHANDLE,
437 NFSERR_NOTSUPP,
438 NFSERR_SERVERFAULT,
439 0,
440 };
441
442 static short nfsv3err_readdir[] = {
443 NFSERR_IO,
444 NFSERR_IO,
445 NFSERR_ACCES,
446 NFSERR_NOTDIR,
447 NFSERR_STALE,
448 NFSERR_BADHANDLE,
449 NFSERR_BAD_COOKIE,
450 NFSERR_TOOSMALL,
451 NFSERR_SERVERFAULT,
452 0,
453 };
454
455 static short nfsv3err_readdirplus[] = {
456 NFSERR_IO,
457 NFSERR_IO,
458 NFSERR_ACCES,
459 NFSERR_NOTDIR,
460 NFSERR_STALE,
461 NFSERR_BADHANDLE,
462 NFSERR_BAD_COOKIE,
463 NFSERR_NOTSUPP,
464 NFSERR_TOOSMALL,
465 NFSERR_SERVERFAULT,
466 0,
467 };
468
469 static short nfsv3err_fsstat[] = {
470 NFSERR_IO,
471 NFSERR_IO,
472 NFSERR_STALE,
473 NFSERR_BADHANDLE,
474 NFSERR_SERVERFAULT,
475 0,
476 };
477
478 static short nfsv3err_fsinfo[] = {
479 NFSERR_STALE,
480 NFSERR_STALE,
481 NFSERR_BADHANDLE,
482 NFSERR_SERVERFAULT,
483 0,
484 };
485
486 static short nfsv3err_pathconf[] = {
487 NFSERR_STALE,
488 NFSERR_STALE,
489 NFSERR_BADHANDLE,
490 NFSERR_SERVERFAULT,
491 0,
492 };
493
494 static short nfsv3err_commit[] = {
495 NFSERR_IO,
496 NFSERR_IO,
497 NFSERR_STALE,
498 NFSERR_BADHANDLE,
499 NFSERR_SERVERFAULT,
500 0,
501 };
502
503 static short *nfsrv_v3errmap[] = {
504 nfsv3err_null,
505 nfsv3err_getattr,
506 nfsv3err_setattr,
507 nfsv3err_lookup,
508 nfsv3err_access,
509 nfsv3err_readlink,
510 nfsv3err_read,
511 nfsv3err_write,
512 nfsv3err_create,
513 nfsv3err_mkdir,
514 nfsv3err_symlink,
515 nfsv3err_mknod,
516 nfsv3err_remove,
517 nfsv3err_rmdir,
518 nfsv3err_rename,
519 nfsv3err_link,
520 nfsv3err_readdir,
521 nfsv3err_readdirplus,
522 nfsv3err_fsstat,
523 nfsv3err_fsinfo,
524 nfsv3err_pathconf,
525 nfsv3err_commit,
526 };
527
528 extern struct nfsrtt nfsrtt;
529 extern time_t nqnfsstarttime;
530 extern int nqsrv_clockskew;
531 extern int nqsrv_writeslack;
532 extern int nqsrv_maxlease;
533 extern int nqnfs_piggy[NFS_NPROCS];
534 extern nfstype nfsv2_type[9];
535 extern nfstype nfsv3_type[9];
536 extern struct nfsnodehashhead *nfsnodehashtbl;
537 extern u_long nfsnodehash;
538
539 LIST_HEAD(nfsnodehashhead, nfsnode);
540
541 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
542
543 /*
544 * Create the header for an rpc request packet
545 * The hsiz is the size of the rest of the nfs request header.
546 * (just used to decide if a cluster is a good idea)
547 */
548 struct mbuf *
549 nfsm_reqh(vp, procid, hsiz, bposp)
550 struct vnode *vp;
551 u_long procid;
552 int hsiz;
553 caddr_t *bposp;
554 {
555 register struct mbuf *mb;
556 register u_int32_t *tl;
557 register caddr_t bpos;
558 struct mbuf *mb2;
559 struct nfsmount *nmp;
560 int nqflag;
561
562 MGET(mb, M_WAIT, MT_DATA);
563 if (hsiz >= MINCLSIZE)
564 MCLGET(mb, M_WAIT);
565 mb->m_len = 0;
566 bpos = mtod(mb, caddr_t);
567
568 /*
569 * For NQNFS, add lease request.
570 */
571 if (vp) {
572 nmp = VFSTONFS(vp->v_mount);
573 if (nmp->nm_flag & NFSMNT_NQNFS) {
574 nqflag = NQNFS_NEEDLEASE(vp, procid);
575 if (nqflag) {
576 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
577 *tl++ = txdr_unsigned(nqflag);
578 *tl = txdr_unsigned(nmp->nm_leaseterm);
579 } else {
580 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
581 *tl = 0;
582 }
583 }
584 }
585 /* Finally, return values */
586 *bposp = bpos;
587 return (mb);
588 }
589
590 /*
591 * Build the RPC header and fill in the authorization info.
592 * The authorization string argument is only used when the credentials
593 * come from outside of the kernel.
594 * Returns the head of the mbuf list.
595 */
596 struct mbuf *
597 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
598 verf_str, mrest, mrest_len, mbp, xidp)
599 register struct ucred *cr;
600 int nmflag;
601 int procid;
602 int auth_type;
603 int auth_len;
604 char *auth_str;
605 int verf_len;
606 char *verf_str;
607 struct mbuf *mrest;
608 int mrest_len;
609 struct mbuf **mbp;
610 u_int32_t *xidp;
611 {
612 register struct mbuf *mb;
613 register u_int32_t *tl;
614 register caddr_t bpos;
615 register int i;
616 struct mbuf *mreq, *mb2;
617 int siz, grpsiz, authsiz;
618 struct timeval tv;
619 static u_int32_t base;
620
621 authsiz = nfsm_rndup(auth_len);
622 MGETHDR(mb, M_WAIT, MT_DATA);
623 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
624 MCLGET(mb, M_WAIT);
625 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
626 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
627 } else {
628 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
629 }
630 mb->m_len = 0;
631 mreq = mb;
632 bpos = mtod(mb, caddr_t);
633
634 /*
635 * First the RPC header.
636 */
637 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
638
639 /*
640 * derive initial xid from system time
641 * XXX time is invalid if root not yet mounted
642 */
643 if (!base && (rootvp)) {
644 microtime(&tv);
645 base = tv.tv_sec << 12;
646 nfs_xid = base;
647 }
648 /*
649 * Skip zero xid if it should ever happen.
650 */
651 if (++nfs_xid == 0)
652 nfs_xid++;
653
654 *tl++ = *xidp = txdr_unsigned(nfs_xid);
655 *tl++ = rpc_call;
656 *tl++ = rpc_vers;
657 if (nmflag & NFSMNT_NQNFS) {
658 *tl++ = txdr_unsigned(NQNFS_PROG);
659 *tl++ = txdr_unsigned(NQNFS_VER3);
660 } else {
661 *tl++ = txdr_unsigned(NFS_PROG);
662 if (nmflag & NFSMNT_NFSV3)
663 *tl++ = txdr_unsigned(NFS_VER3);
664 else
665 *tl++ = txdr_unsigned(NFS_VER2);
666 }
667 if (nmflag & NFSMNT_NFSV3)
668 *tl++ = txdr_unsigned(procid);
669 else
670 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
671
672 /*
673 * And then the authorization cred.
674 */
675 *tl++ = txdr_unsigned(auth_type);
676 *tl = txdr_unsigned(authsiz);
677 switch (auth_type) {
678 case RPCAUTH_UNIX:
679 nfsm_build(tl, u_int32_t *, auth_len);
680 *tl++ = 0; /* stamp ?? */
681 *tl++ = 0; /* NULL hostname */
682 *tl++ = txdr_unsigned(cr->cr_uid);
683 *tl++ = txdr_unsigned(cr->cr_gid);
684 grpsiz = (auth_len >> 2) - 5;
685 *tl++ = txdr_unsigned(grpsiz);
686 for (i = 0; i < grpsiz; i++)
687 *tl++ = txdr_unsigned(cr->cr_groups[i]);
688 break;
689 case RPCAUTH_KERB4:
690 siz = auth_len;
691 while (siz > 0) {
692 if (M_TRAILINGSPACE(mb) == 0) {
693 MGET(mb2, M_WAIT, MT_DATA);
694 if (siz >= MINCLSIZE)
695 MCLGET(mb2, M_WAIT);
696 mb->m_next = mb2;
697 mb = mb2;
698 mb->m_len = 0;
699 bpos = mtod(mb, caddr_t);
700 }
701 i = min(siz, M_TRAILINGSPACE(mb));
702 bcopy(auth_str, bpos, i);
703 mb->m_len += i;
704 auth_str += i;
705 bpos += i;
706 siz -= i;
707 }
708 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
709 for (i = 0; i < siz; i++)
710 *bpos++ = '\0';
711 mb->m_len += siz;
712 }
713 break;
714 };
715
716 /*
717 * And the verifier...
718 */
719 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
720 if (verf_str) {
721 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
722 *tl = txdr_unsigned(verf_len);
723 siz = verf_len;
724 while (siz > 0) {
725 if (M_TRAILINGSPACE(mb) == 0) {
726 MGET(mb2, M_WAIT, MT_DATA);
727 if (siz >= MINCLSIZE)
728 MCLGET(mb2, M_WAIT);
729 mb->m_next = mb2;
730 mb = mb2;
731 mb->m_len = 0;
732 bpos = mtod(mb, caddr_t);
733 }
734 i = min(siz, M_TRAILINGSPACE(mb));
735 bcopy(verf_str, bpos, i);
736 mb->m_len += i;
737 verf_str += i;
738 bpos += i;
739 siz -= i;
740 }
741 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
742 for (i = 0; i < siz; i++)
743 *bpos++ = '\0';
744 mb->m_len += siz;
745 }
746 } else {
747 *tl++ = txdr_unsigned(RPCAUTH_NULL);
748 *tl = 0;
749 }
750 mb->m_next = mrest;
751 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
752 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
753 *mbp = mb;
754 return (mreq);
755 }
756
757 /*
758 * copies mbuf chain to the uio scatter/gather list
759 */
760 int
761 nfsm_mbuftouio(mrep, uiop, siz, dpos)
762 struct mbuf **mrep;
763 register struct uio *uiop;
764 int siz;
765 caddr_t *dpos;
766 {
767 register char *mbufcp, *uiocp;
768 register int xfer, left, len;
769 register struct mbuf *mp;
770 long uiosiz, rem;
771 int error = 0;
772
773 mp = *mrep;
774 mbufcp = *dpos;
775 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
776 rem = nfsm_rndup(siz)-siz;
777 while (siz > 0) {
778 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
779 return (EFBIG);
780 left = uiop->uio_iov->iov_len;
781 uiocp = uiop->uio_iov->iov_base;
782 if (left > siz)
783 left = siz;
784 uiosiz = left;
785 while (left > 0) {
786 while (len == 0) {
787 mp = mp->m_next;
788 if (mp == NULL)
789 return (EBADRPC);
790 mbufcp = mtod(mp, caddr_t);
791 len = mp->m_len;
792 }
793 xfer = (left > len) ? len : left;
794 #ifdef notdef
795 /* Not Yet.. */
796 if (uiop->uio_iov->iov_op != NULL)
797 (*(uiop->uio_iov->iov_op))
798 (mbufcp, uiocp, xfer);
799 else
800 #endif
801 if (uiop->uio_segflg == UIO_SYSSPACE)
802 bcopy(mbufcp, uiocp, xfer);
803 else
804 copyout(mbufcp, uiocp, xfer);
805 left -= xfer;
806 len -= xfer;
807 mbufcp += xfer;
808 uiocp += xfer;
809 uiop->uio_offset += xfer;
810 uiop->uio_resid -= xfer;
811 }
812 if (uiop->uio_iov->iov_len <= siz) {
813 uiop->uio_iovcnt--;
814 uiop->uio_iov++;
815 } else {
816 uiop->uio_iov->iov_base += uiosiz;
817 uiop->uio_iov->iov_len -= uiosiz;
818 }
819 siz -= uiosiz;
820 }
821 *dpos = mbufcp;
822 *mrep = mp;
823 if (rem > 0) {
824 if (len < rem)
825 error = nfs_adv(mrep, dpos, rem, len);
826 else
827 *dpos += rem;
828 }
829 return (error);
830 }
831
832 /*
833 * copies a uio scatter/gather list to an mbuf chain.
834 * NOTE: can ony handle iovcnt == 1
835 */
836 int
837 nfsm_uiotombuf(uiop, mq, siz, bpos)
838 register struct uio *uiop;
839 struct mbuf **mq;
840 int siz;
841 caddr_t *bpos;
842 {
843 register char *uiocp;
844 register struct mbuf *mp, *mp2;
845 register int xfer, left, mlen;
846 int uiosiz, clflg, rem;
847 char *cp;
848
849 #ifdef DIAGNOSTIC
850 if (uiop->uio_iovcnt != 1)
851 panic("nfsm_uiotombuf: iovcnt != 1");
852 #endif
853
854 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
855 clflg = 1;
856 else
857 clflg = 0;
858 rem = nfsm_rndup(siz)-siz;
859 mp = mp2 = *mq;
860 while (siz > 0) {
861 left = uiop->uio_iov->iov_len;
862 uiocp = uiop->uio_iov->iov_base;
863 if (left > siz)
864 left = siz;
865 uiosiz = left;
866 while (left > 0) {
867 mlen = M_TRAILINGSPACE(mp);
868 if (mlen == 0) {
869 MGET(mp, M_WAIT, MT_DATA);
870 if (clflg)
871 MCLGET(mp, M_WAIT);
872 mp->m_len = 0;
873 mp2->m_next = mp;
874 mp2 = mp;
875 mlen = M_TRAILINGSPACE(mp);
876 }
877 xfer = (left > mlen) ? mlen : left;
878 #ifdef notdef
879 /* Not Yet.. */
880 if (uiop->uio_iov->iov_op != NULL)
881 (*(uiop->uio_iov->iov_op))
882 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
883 else
884 #endif
885 if (uiop->uio_segflg == UIO_SYSSPACE)
886 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
887 else
888 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
889 mp->m_len += xfer;
890 left -= xfer;
891 uiocp += xfer;
892 uiop->uio_offset += xfer;
893 uiop->uio_resid -= xfer;
894 }
895 uiop->uio_iov->iov_base += uiosiz;
896 uiop->uio_iov->iov_len -= uiosiz;
897 siz -= uiosiz;
898 }
899 if (rem > 0) {
900 if (rem > M_TRAILINGSPACE(mp)) {
901 MGET(mp, M_WAIT, MT_DATA);
902 mp->m_len = 0;
903 mp2->m_next = mp;
904 }
905 cp = mtod(mp, caddr_t)+mp->m_len;
906 for (left = 0; left < rem; left++)
907 *cp++ = '\0';
908 mp->m_len += rem;
909 *bpos = cp;
910 } else
911 *bpos = mtod(mp, caddr_t)+mp->m_len;
912 *mq = mp;
913 return (0);
914 }
915
916 /*
917 * Get at least "siz" bytes of correctly aligned data.
918 * When called the mbuf pointers are not necessarily correct,
919 * dsosp points to what ought to be in m_data and left contains
920 * what ought to be in m_len.
921 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
922 * cases. (The macros use the vars. dpos and dpos2)
923 */
924 int
925 nfsm_disct(mdp, dposp, siz, left, cp2)
926 struct mbuf **mdp;
927 caddr_t *dposp;
928 int siz;
929 int left;
930 caddr_t *cp2;
931 {
932 register struct mbuf *m1, *m2;
933 struct mbuf *havebuf = NULL;
934 caddr_t src = *dposp;
935 caddr_t dst;
936 int len;
937
938 #ifdef DEBUG
939 if (left < 0)
940 panic("nfsm_disct: left < 0");
941 #endif
942 m1 = *mdp;
943 /*
944 * Skip through the mbuf chain looking for an mbuf with
945 * some data. If the first mbuf found has enough data
946 * and it is correctly aligned return it.
947 */
948 while (left == 0) {
949 havebuf = m1;
950 *mdp = m1 = m1->m_next;
951 if (m1 == NULL)
952 return (EBADRPC);
953 src = mtod(m1, caddr_t);
954 left = m1->m_len;
955 /*
956 * If we start a new mbuf and it is big enough
957 * and correctly aligned just return it, don't
958 * do any pull up.
959 */
960 if (left >= siz && nfsm_aligned(src)) {
961 *cp2 = src;
962 *dposp = src + siz;
963 return (0);
964 }
965 }
966 if (m1->m_flags & M_EXT) {
967 if (havebuf) {
968 /* If the first mbuf with data has external data
969 * and there is a previous empty mbuf use it
970 * to move the data into.
971 */
972 m2 = m1;
973 *mdp = m1 = havebuf;
974 if (m1->m_flags & M_EXT) {
975 MEXTREMOVE(m1);
976 }
977 } else {
978 /*
979 * If the first mbuf has a external data
980 * and there is no previous empty mbuf
981 * allocate a new mbuf and move the external
982 * data to the new mbuf. Also make the first
983 * mbuf look empty.
984 */
985 m2 = m_get(M_WAIT, MT_DATA);
986 m2->m_ext = m1->m_ext;
987 m2->m_data = src;
988 m2->m_len = left;
989 MCLADDREFERENCE(m1, m2);
990 MEXTREMOVE(m1);
991 m2->m_next = m1->m_next;
992 m1->m_next = m2;
993 }
994 m1->m_len = 0;
995 dst = m1->m_dat;
996 } else {
997 /*
998 * If the first mbuf has no external data
999 * move the data to the front of the mbuf.
1000 */
1001 if ((dst = m1->m_dat) != src)
1002 ovbcopy(src, dst, left);
1003 dst += left;
1004 m1->m_len = left;
1005 m2 = m1->m_next;
1006 }
1007 m1->m_flags &= ~M_PKTHDR;
1008 *cp2 = m1->m_data = m1->m_dat; /* data is at beginning of buffer */
1009 *dposp = mtod(m1, caddr_t) + siz;
1010 /*
1011 * Loop through mbufs pulling data up into first mbuf until
1012 * the first mbuf is full or there is no more data to
1013 * pullup.
1014 */
1015 while ((len = (MLEN - m1->m_len)) != 0 && m2) {
1016 if ((len = min(len, m2->m_len)) != 0)
1017 bcopy(m2->m_data, dst, len);
1018 m1->m_len += len;
1019 dst += len;
1020 m2->m_data += len;
1021 m2->m_len -= len;
1022 m2 = m2->m_next;
1023 }
1024 if (m1->m_len < siz)
1025 return (EBADRPC);
1026 return (0);
1027 }
1028
1029 /*
1030 * Advance the position in the mbuf chain.
1031 */
1032 int
1033 nfs_adv(mdp, dposp, offs, left)
1034 struct mbuf **mdp;
1035 caddr_t *dposp;
1036 int offs;
1037 int left;
1038 {
1039 register struct mbuf *m;
1040 register int s;
1041
1042 m = *mdp;
1043 s = left;
1044 while (s < offs) {
1045 offs -= s;
1046 m = m->m_next;
1047 if (m == NULL)
1048 return (EBADRPC);
1049 s = m->m_len;
1050 }
1051 *mdp = m;
1052 *dposp = mtod(m, caddr_t)+offs;
1053 return (0);
1054 }
1055
1056 /*
1057 * Copy a string into mbufs for the hard cases...
1058 */
1059 int
1060 nfsm_strtmbuf(mb, bpos, cp, siz)
1061 struct mbuf **mb;
1062 char **bpos;
1063 const char *cp;
1064 long siz;
1065 {
1066 register struct mbuf *m1 = NULL, *m2;
1067 long left, xfer, len, tlen;
1068 u_int32_t *tl;
1069 int putsize;
1070
1071 putsize = 1;
1072 m2 = *mb;
1073 left = M_TRAILINGSPACE(m2);
1074 if (left > 0) {
1075 tl = ((u_int32_t *)(*bpos));
1076 *tl++ = txdr_unsigned(siz);
1077 putsize = 0;
1078 left -= NFSX_UNSIGNED;
1079 m2->m_len += NFSX_UNSIGNED;
1080 if (left > 0) {
1081 bcopy(cp, (caddr_t) tl, left);
1082 siz -= left;
1083 cp += left;
1084 m2->m_len += left;
1085 left = 0;
1086 }
1087 }
1088 /* Loop around adding mbufs */
1089 while (siz > 0) {
1090 MGET(m1, M_WAIT, MT_DATA);
1091 if (siz > MLEN)
1092 MCLGET(m1, M_WAIT);
1093 m1->m_len = NFSMSIZ(m1);
1094 m2->m_next = m1;
1095 m2 = m1;
1096 tl = mtod(m1, u_int32_t *);
1097 tlen = 0;
1098 if (putsize) {
1099 *tl++ = txdr_unsigned(siz);
1100 m1->m_len -= NFSX_UNSIGNED;
1101 tlen = NFSX_UNSIGNED;
1102 putsize = 0;
1103 }
1104 if (siz < m1->m_len) {
1105 len = nfsm_rndup(siz);
1106 xfer = siz;
1107 if (xfer < len)
1108 *(tl+(xfer>>2)) = 0;
1109 } else {
1110 xfer = len = m1->m_len;
1111 }
1112 bcopy(cp, (caddr_t) tl, xfer);
1113 m1->m_len = len+tlen;
1114 siz -= xfer;
1115 cp += xfer;
1116 }
1117 *mb = m1;
1118 *bpos = mtod(m1, caddr_t)+m1->m_len;
1119 return (0);
1120 }
1121
1122 /*
1123 * Called once before VFS init to initialize shared and
1124 * server-specific data structures.
1125 */
1126 void
1127 nfs_init()
1128 {
1129
1130 #if !defined(alpha) && defined(DIAGNOSTIC)
1131 /*
1132 * Check to see if major data structures haven't bloated.
1133 */
1134 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1135 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1136 printf("Try reducing NFS_SMALLFH\n");
1137 }
1138 if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
1139 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
1140 printf("Try reducing NFS_MUIDHASHSIZ\n");
1141 }
1142 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1143 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1144 printf("Try reducing NFS_UIDHASHSIZ\n");
1145 }
1146 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1147 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1148 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1149 }
1150 #endif
1151
1152 nfsrtt.pos = 0;
1153 rpc_vers = txdr_unsigned(RPC_VER2);
1154 rpc_call = txdr_unsigned(RPC_CALL);
1155 rpc_reply = txdr_unsigned(RPC_REPLY);
1156 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1157 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1158 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1159 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1160 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1161 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1162 nfs_prog = txdr_unsigned(NFS_PROG);
1163 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1164 nfs_true = txdr_unsigned(TRUE);
1165 nfs_false = txdr_unsigned(FALSE);
1166 nfs_xdrneg1 = txdr_unsigned(-1);
1167 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1168 if (nfs_ticks < 1)
1169 nfs_ticks = 1;
1170 #ifdef NFSSERVER
1171 nfsrv_init(0); /* Init server data structures */
1172 nfsrv_initcache(); /* Init the server request cache */
1173 #endif /* NFSSERVER */
1174
1175 /*
1176 * Initialize the nqnfs data structures.
1177 */
1178 if (nqnfsstarttime == 0) {
1179 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1180 + nqsrv_clockskew + nqsrv_writeslack;
1181 NQLOADNOVRAM(nqnfsstarttime);
1182 CIRCLEQ_INIT(&nqtimerhead);
1183 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1184 }
1185
1186 /*
1187 * Initialize reply list and start timer
1188 */
1189 TAILQ_INIT(&nfs_reqq);
1190 nfs_timer(NULL);
1191 }
1192
1193 #ifdef NFS
1194 /*
1195 * Called once at VFS init to initialize client-specific data structures.
1196 */
1197 void
1198 nfs_vfs_init()
1199 {
1200 register int i;
1201
1202 /* Ensure async daemons disabled */
1203 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1204 nfs_iodwant[i] = (struct proc *)0;
1205 nfs_iodmount[i] = (struct nfsmount *)0;
1206 }
1207 nfs_nhinit(); /* Init the nfsnode table */
1208 }
1209
1210 /*
1211 * Attribute cache routines.
1212 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1213 * that are on the mbuf list
1214 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1215 * error otherwise
1216 */
1217
1218 /*
1219 * Load the attribute cache (that lives in the nfsnode entry) with
1220 * the values on the mbuf list and
1221 * Iff vap not NULL
1222 * copy the attributes to *vaper
1223 */
1224 int
1225 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
1226 struct vnode **vpp;
1227 struct mbuf **mdp;
1228 caddr_t *dposp;
1229 struct vattr *vaper;
1230 {
1231 register int32_t t1;
1232 caddr_t cp2;
1233 int error = 0;
1234 struct mbuf *md;
1235 int v3 = NFS_ISV3(*vpp);
1236
1237 md = *mdp;
1238 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1239 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1240 if (error)
1241 return (error);
1242 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
1243 }
1244
1245 int
1246 nfs_loadattrcache(vpp, fp, vaper)
1247 struct vnode **vpp;
1248 struct nfs_fattr *fp;
1249 struct vattr *vaper;
1250 {
1251 register struct vnode *vp = *vpp;
1252 register struct vattr *vap;
1253 int v3 = NFS_ISV3(vp);
1254 enum vtype vtyp;
1255 u_short vmode;
1256 struct timespec mtime;
1257 struct vnode *nvp;
1258 int32_t rdev;
1259 register struct nfsnode *np;
1260 extern int (**spec_nfsv2nodeop_p) __P((void *));
1261
1262 if (v3) {
1263 vtyp = nfsv3tov_type(fp->fa_type);
1264 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1265 rdev = makedev(fxdr_unsigned(u_char, fp->fa3_rdev.specdata1),
1266 fxdr_unsigned(u_char, fp->fa3_rdev.specdata2));
1267 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1268 } else {
1269 vtyp = nfsv2tov_type(fp->fa_type);
1270 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1271 if (vtyp == VNON || vtyp == VREG)
1272 vtyp = IFTOVT(vmode);
1273 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1274 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1275
1276 /*
1277 * Really ugly NFSv2 kludge.
1278 */
1279 if (vtyp == VCHR && rdev == 0xffffffff)
1280 vtyp = VFIFO;
1281 }
1282
1283 /*
1284 * If v_type == VNON it is a new node, so fill in the v_type,
1285 * n_mtime fields. Check to see if it represents a special
1286 * device, and if so, check for a possible alias. Once the
1287 * correct vnode has been obtained, fill in the rest of the
1288 * information.
1289 */
1290 np = VTONFS(vp);
1291 if (vp->v_type != vtyp) {
1292 vp->v_type = vtyp;
1293 if (vp->v_type == VFIFO) {
1294 #ifndef FIFO
1295 return (EOPNOTSUPP);
1296 #else
1297 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1298 vp->v_op = fifo_nfsv2nodeop_p;
1299 #endif /* FIFO */
1300 }
1301 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1302 vp->v_op = spec_nfsv2nodeop_p;
1303 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1304 if (nvp) {
1305 /*
1306 * Discard unneeded vnode, but save its nfsnode.
1307 * Since the nfsnode does not have a lock, its
1308 * vnode lock has to be carried over.
1309 */
1310 #ifdef Lite2_integrated
1311 nvp->v_vnlock = vp->v_vnlock;
1312 vp->v_vnlock = NULL;
1313 #endif
1314 nvp->v_data = vp->v_data;
1315 vp->v_data = NULL;
1316 vp->v_op = spec_vnodeop_p;
1317 vrele(vp);
1318 vgone(vp);
1319 /*
1320 * Reinitialize aliased node.
1321 */
1322 np->n_vnode = nvp;
1323 *vpp = vp = nvp;
1324 }
1325 }
1326 np->n_mtime = mtime.tv_sec;
1327 }
1328 vap = &np->n_vattr;
1329 vap->va_type = vtyp;
1330 vap->va_mode = vmode & ALLPERMS;
1331 vap->va_rdev = (dev_t)rdev;
1332 vap->va_mtime = mtime;
1333 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1334 if (v3) {
1335 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1336 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1337 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1338 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1339 vap->va_blocksize = NFS_FABLKSIZE;
1340 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1341 vap->va_fileid = fxdr_unsigned(int32_t,
1342 fp->fa3_fileid.nfsuquad[1]);
1343 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1344 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1345 vap->va_flags = 0;
1346 vap->va_filerev = 0;
1347 } else {
1348 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1349 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1350 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1351 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1352 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1353 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1354 * NFS_FABLKSIZE;
1355 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1356 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1357 vap->va_flags = 0;
1358 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1359 fp->fa2_ctime.nfsv2_sec);
1360 vap->va_ctime.tv_nsec = 0;
1361 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1362 vap->va_filerev = 0;
1363 }
1364 if (vap->va_size != np->n_size) {
1365 if (vap->va_type == VREG) {
1366 if (np->n_flag & NMODIFIED) {
1367 if (vap->va_size < np->n_size)
1368 vap->va_size = np->n_size;
1369 else
1370 np->n_size = vap->va_size;
1371 } else
1372 np->n_size = vap->va_size;
1373 vnode_pager_setsize(vp, np->n_size);
1374 } else
1375 np->n_size = vap->va_size;
1376 }
1377 np->n_attrstamp = time.tv_sec;
1378 if (vaper != NULL) {
1379 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1380 if (np->n_flag & NCHG) {
1381 if (np->n_flag & NACC)
1382 vaper->va_atime = np->n_atim;
1383 if (np->n_flag & NUPD)
1384 vaper->va_mtime = np->n_mtim;
1385 }
1386 }
1387 return (0);
1388 }
1389
1390 /*
1391 * Check the time stamp
1392 * If the cache is valid, copy contents to *vap and return 0
1393 * otherwise return an error
1394 */
1395 int
1396 nfs_getattrcache(vp, vaper)
1397 register struct vnode *vp;
1398 struct vattr *vaper;
1399 {
1400 register struct nfsnode *np = VTONFS(vp);
1401 register struct vattr *vap;
1402
1403 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1404 nfsstats.attrcache_misses++;
1405 return (ENOENT);
1406 }
1407 nfsstats.attrcache_hits++;
1408 vap = &np->n_vattr;
1409 if (vap->va_size != np->n_size) {
1410 if (vap->va_type == VREG) {
1411 if (np->n_flag & NMODIFIED) {
1412 if (vap->va_size < np->n_size)
1413 vap->va_size = np->n_size;
1414 else
1415 np->n_size = vap->va_size;
1416 } else
1417 np->n_size = vap->va_size;
1418 vnode_pager_setsize(vp, np->n_size);
1419 } else
1420 np->n_size = vap->va_size;
1421 }
1422 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1423 if (np->n_flag & NCHG) {
1424 if (np->n_flag & NACC)
1425 vaper->va_atime = np->n_atim;
1426 if (np->n_flag & NUPD)
1427 vaper->va_mtime = np->n_mtim;
1428 }
1429 return (0);
1430 }
1431 #endif /* NFS */
1432
1433 /*
1434 * Set up nameidata for a lookup() call and do it.
1435 *
1436 * If pubflag is set, this call is done for a lookup operation on the
1437 * public filehandle. In that case we allow crossing mountpoints and
1438 * absolute pathnames. However, the caller is expected to check that
1439 * the lookup result is within the public fs, and deny access if
1440 * it is not.
1441 */
1442 int
1443 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1444 register struct nameidata *ndp;
1445 fhandle_t *fhp;
1446 int len;
1447 struct nfssvc_sock *slp;
1448 struct mbuf *nam;
1449 struct mbuf **mdp;
1450 caddr_t *dposp;
1451 struct vnode **retdirp;
1452 struct proc *p;
1453 int kerbflag, pubflag;
1454 {
1455 register int i, rem;
1456 register struct mbuf *md;
1457 register char *fromcp, *tocp, *cp;
1458 struct iovec aiov;
1459 struct uio auio;
1460 struct vnode *dp;
1461 int error, rdonly, linklen;
1462 struct componentname *cnp = &ndp->ni_cnd;
1463
1464 *retdirp = (struct vnode *)0;
1465 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
1466 /*
1467 * Copy the name from the mbuf list to ndp->ni_pnbuf
1468 * and set the various ndp fields appropriately.
1469 */
1470 fromcp = *dposp;
1471 tocp = cnp->cn_pnbuf;
1472 md = *mdp;
1473 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1474 for (i = 0; i < len; i++) {
1475 while (rem == 0) {
1476 md = md->m_next;
1477 if (md == NULL) {
1478 error = EBADRPC;
1479 goto out;
1480 }
1481 fromcp = mtod(md, caddr_t);
1482 rem = md->m_len;
1483 }
1484 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1485 error = EACCES;
1486 goto out;
1487 }
1488 *tocp++ = *fromcp++;
1489 rem--;
1490 }
1491 *tocp = '\0';
1492 *mdp = md;
1493 *dposp = fromcp;
1494 len = nfsm_rndup(len)-len;
1495 if (len > 0) {
1496 if (rem >= len)
1497 *dposp += len;
1498 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1499 goto out;
1500 }
1501
1502 /*
1503 * Extract and set starting directory.
1504 */
1505 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1506 nam, &rdonly, kerbflag, pubflag);
1507 if (error)
1508 goto out;
1509 if (dp->v_type != VDIR) {
1510 vrele(dp);
1511 error = ENOTDIR;
1512 goto out;
1513 }
1514
1515 if (rdonly)
1516 cnp->cn_flags |= RDONLY;
1517
1518 *retdirp = dp;
1519
1520 if (pubflag) {
1521 /*
1522 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1523 * and the 'native path' indicator.
1524 */
1525 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1526 fromcp = cnp->cn_pnbuf;
1527 tocp = cp;
1528 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1529 switch ((unsigned char)*fromcp) {
1530 case WEBNFS_NATIVE_CHAR:
1531 /*
1532 * 'Native' path for us is the same
1533 * as a path according to the NFS spec,
1534 * just skip the escape char.
1535 */
1536 fromcp++;
1537 break;
1538 /*
1539 * More may be added in the future, range 0x80-0xff
1540 */
1541 default:
1542 error = EIO;
1543 FREE(cp, M_NAMEI);
1544 goto out;
1545 }
1546 }
1547 /*
1548 * Translate the '%' escapes, URL-style.
1549 */
1550 while (*fromcp != '\0') {
1551 if (*fromcp == WEBNFS_ESC_CHAR) {
1552 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1553 fromcp++;
1554 *tocp++ = HEXSTRTOI(fromcp);
1555 fromcp += 2;
1556 continue;
1557 } else {
1558 error = ENOENT;
1559 FREE(cp, M_NAMEI);
1560 goto out;
1561 }
1562 } else
1563 *tocp++ = *fromcp++;
1564 }
1565 *tocp = '\0';
1566 FREE(cnp->cn_pnbuf, M_NAMEI);
1567 cnp->cn_pnbuf = cp;
1568 }
1569
1570 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1571 ndp->ni_segflg = UIO_SYSSPACE;
1572
1573 if (pubflag) {
1574 ndp->ni_rootdir = rootvnode;
1575 ndp->ni_loopcnt = 0;
1576 if (cnp->cn_pnbuf[0] == '/')
1577 dp = rootvnode;
1578 } else {
1579 cnp->cn_flags |= NOCROSSMOUNT;
1580 }
1581
1582 cnp->cn_proc = p;
1583 VREF(dp);
1584
1585 for (;;) {
1586 cnp->cn_nameptr = cnp->cn_pnbuf;
1587 ndp->ni_startdir = dp;
1588 /*
1589 * And call lookup() to do the real work
1590 */
1591 error = lookup(ndp);
1592 if (error)
1593 break;
1594 /*
1595 * Check for encountering a symbolic link
1596 */
1597 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1598 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1599 cnp->cn_flags |= HASBUF;
1600 return (0);
1601 }
1602 break;
1603 } else {
1604 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1605 VOP_UNLOCK(ndp->ni_dvp);
1606 if (!pubflag) {
1607 vrele(ndp->ni_dvp);
1608 vput(ndp->ni_vp);
1609 ndp->ni_vp = NULL;
1610 error = EINVAL;
1611 break;
1612 }
1613
1614 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1615 error = ELOOP;
1616 break;
1617 }
1618 if (ndp->ni_pathlen > 1)
1619 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1620 else
1621 cp = cnp->cn_pnbuf;
1622 aiov.iov_base = cp;
1623 aiov.iov_len = MAXPATHLEN;
1624 auio.uio_iov = &aiov;
1625 auio.uio_iovcnt = 1;
1626 auio.uio_offset = 0;
1627 auio.uio_rw = UIO_READ;
1628 auio.uio_segflg = UIO_SYSSPACE;
1629 auio.uio_procp = (struct proc *)0;
1630 auio.uio_resid = MAXPATHLEN;
1631 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1632 if (error) {
1633 badlink:
1634 if (ndp->ni_pathlen > 1)
1635 FREE(cp, M_NAMEI);
1636 break;
1637 }
1638 linklen = MAXPATHLEN - auio.uio_resid;
1639 if (linklen == 0) {
1640 error = ENOENT;
1641 goto badlink;
1642 }
1643 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1644 error = ENAMETOOLONG;
1645 goto badlink;
1646 }
1647 if (ndp->ni_pathlen > 1) {
1648 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1649 FREE(cnp->cn_pnbuf, M_NAMEI);
1650 cnp->cn_pnbuf = cp;
1651 } else
1652 cnp->cn_pnbuf[linklen] = '\0';
1653 ndp->ni_pathlen += linklen;
1654 vput(ndp->ni_vp);
1655 dp = ndp->ni_dvp;
1656 /*
1657 * Check if root directory should replace current directory.
1658 */
1659 if (cnp->cn_pnbuf[0] == '/') {
1660 vrele(dp);
1661 dp = ndp->ni_rootdir;
1662 VREF(dp);
1663 }
1664 }
1665 }
1666 out:
1667 FREE(cnp->cn_pnbuf, M_NAMEI);
1668 return (error);
1669 }
1670
1671 /*
1672 * A fiddled version of m_adj() that ensures null fill to a long
1673 * boundary and only trims off the back end
1674 */
1675 void
1676 nfsm_adj(mp, len, nul)
1677 struct mbuf *mp;
1678 register int len;
1679 int nul;
1680 {
1681 register struct mbuf *m;
1682 register int count, i;
1683 register char *cp;
1684
1685 /*
1686 * Trim from tail. Scan the mbuf chain,
1687 * calculating its length and finding the last mbuf.
1688 * If the adjustment only affects this mbuf, then just
1689 * adjust and return. Otherwise, rescan and truncate
1690 * after the remaining size.
1691 */
1692 count = 0;
1693 m = mp;
1694 for (;;) {
1695 count += m->m_len;
1696 if (m->m_next == (struct mbuf *)0)
1697 break;
1698 m = m->m_next;
1699 }
1700 if (m->m_len > len) {
1701 m->m_len -= len;
1702 if (nul > 0) {
1703 cp = mtod(m, caddr_t)+m->m_len-nul;
1704 for (i = 0; i < nul; i++)
1705 *cp++ = '\0';
1706 }
1707 return;
1708 }
1709 count -= len;
1710 if (count < 0)
1711 count = 0;
1712 /*
1713 * Correct length for chain is "count".
1714 * Find the mbuf with last data, adjust its length,
1715 * and toss data from remaining mbufs on chain.
1716 */
1717 for (m = mp; m; m = m->m_next) {
1718 if (m->m_len >= count) {
1719 m->m_len = count;
1720 if (nul > 0) {
1721 cp = mtod(m, caddr_t)+m->m_len-nul;
1722 for (i = 0; i < nul; i++)
1723 *cp++ = '\0';
1724 }
1725 break;
1726 }
1727 count -= m->m_len;
1728 }
1729 for (m = m->m_next;m;m = m->m_next)
1730 m->m_len = 0;
1731 }
1732
1733 /*
1734 * Make these functions instead of macros, so that the kernel text size
1735 * doesn't get too big...
1736 */
1737 void
1738 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1739 struct nfsrv_descript *nfsd;
1740 int before_ret;
1741 register struct vattr *before_vap;
1742 int after_ret;
1743 struct vattr *after_vap;
1744 struct mbuf **mbp;
1745 char **bposp;
1746 {
1747 register struct mbuf *mb = *mbp, *mb2;
1748 register char *bpos = *bposp;
1749 register u_int32_t *tl;
1750
1751 if (before_ret) {
1752 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1753 *tl = nfs_false;
1754 } else {
1755 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1756 *tl++ = nfs_true;
1757 txdr_hyper(&(before_vap->va_size), tl);
1758 tl += 2;
1759 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1760 tl += 2;
1761 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1762 }
1763 *bposp = bpos;
1764 *mbp = mb;
1765 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1766 }
1767
1768 void
1769 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1770 struct nfsrv_descript *nfsd;
1771 int after_ret;
1772 struct vattr *after_vap;
1773 struct mbuf **mbp;
1774 char **bposp;
1775 {
1776 register struct mbuf *mb = *mbp, *mb2;
1777 register char *bpos = *bposp;
1778 register u_int32_t *tl;
1779 register struct nfs_fattr *fp;
1780
1781 if (after_ret) {
1782 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1783 *tl = nfs_false;
1784 } else {
1785 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1786 *tl++ = nfs_true;
1787 fp = (struct nfs_fattr *)tl;
1788 nfsm_srvfattr(nfsd, after_vap, fp);
1789 }
1790 *mbp = mb;
1791 *bposp = bpos;
1792 }
1793
1794 void
1795 nfsm_srvfattr(nfsd, vap, fp)
1796 register struct nfsrv_descript *nfsd;
1797 register struct vattr *vap;
1798 register struct nfs_fattr *fp;
1799 {
1800
1801 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1802 fp->fa_uid = txdr_unsigned(vap->va_uid);
1803 fp->fa_gid = txdr_unsigned(vap->va_gid);
1804 if (nfsd->nd_flag & ND_NFSV3) {
1805 fp->fa_type = vtonfsv3_type(vap->va_type);
1806 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1807 txdr_hyper(&vap->va_size, &fp->fa3_size);
1808 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1809 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1810 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1811 fp->fa3_fsid.nfsuquad[0] = 0;
1812 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1813 fp->fa3_fileid.nfsuquad[0] = 0;
1814 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1815 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1816 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1817 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1818 } else {
1819 fp->fa_type = vtonfsv2_type(vap->va_type);
1820 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1821 fp->fa2_size = txdr_unsigned(vap->va_size);
1822 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1823 if (vap->va_type == VFIFO)
1824 fp->fa2_rdev = 0xffffffff;
1825 else
1826 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1827 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1828 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1829 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1830 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1831 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1832 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1833 }
1834 }
1835
1836 /*
1837 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1838 * - look up fsid in mount list (if not found ret error)
1839 * - get vp and export rights by calling VFS_FHTOVP()
1840 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1841 * - if not lockflag unlock it with VOP_UNLOCK()
1842 */
1843 int
1844 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1845 fhandle_t *fhp;
1846 int lockflag;
1847 struct vnode **vpp;
1848 struct ucred *cred;
1849 struct nfssvc_sock *slp;
1850 struct mbuf *nam;
1851 int *rdonlyp;
1852 int kerbflag;
1853 {
1854 #ifdef Lite2_integrated
1855 struct proc *p = curproc; /* XXX */
1856 #endif
1857 register struct mount *mp;
1858 register int i;
1859 struct ucred *credanon;
1860 int error, exflags;
1861 struct sockaddr_in *saddr;
1862
1863 *vpp = (struct vnode *)0;
1864
1865 if (nfs_ispublicfh(fhp)) {
1866 if (!pubflag || !nfs_pub.np_valid)
1867 return (ESTALE);
1868 fhp = &nfs_pub.np_handle;
1869 }
1870
1871 #ifdef Lite2_integrated
1872 mp = vfs_getvfs(&fhp->fh_fsid);
1873 #else
1874 mp = getvfs(&fhp->fh_fsid);
1875 #endif
1876 if (!mp)
1877 return (ESTALE);
1878 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1879 if (error)
1880 return (error);
1881
1882 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1883 saddr = mtod(nam, struct sockaddr_in *);
1884 if (saddr->sin_family == AF_INET &&
1885 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1886 vput(*vpp);
1887 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1888 }
1889 }
1890 /*
1891 * Check/setup credentials.
1892 */
1893 if (exflags & MNT_EXKERB) {
1894 if (!kerbflag) {
1895 vput(*vpp);
1896 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1897 }
1898 } else if (kerbflag) {
1899 vput(*vpp);
1900 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1901 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1902 cred->cr_uid = credanon->cr_uid;
1903 cred->cr_gid = credanon->cr_gid;
1904 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1905 cred->cr_groups[i] = credanon->cr_groups[i];
1906 cred->cr_ngroups = i;
1907 }
1908 if (exflags & MNT_EXRDONLY)
1909 *rdonlyp = 1;
1910 else
1911 *rdonlyp = 0;
1912 if (!lockflag)
1913 #ifdef Lite2_integrated
1914 VOP_UNLOCK(*vpp, 0, p);
1915 #else
1916 VOP_UNLOCK(*vpp);
1917 #endif
1918 return (0);
1919 }
1920
1921 /*
1922 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1923 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1924 * transformed this to all zeroes in both cases, so check for it.
1925 */
1926 int
1927 nfs_ispublicfh(fhp)
1928 fhandle_t *fhp;
1929 {
1930 char *cp = (char *)fhp;
1931 int i;
1932
1933 for (i = 0; i < NFSX_V3FH; i++)
1934 if (*cp++ != 0)
1935 return (FALSE);
1936 return (TRUE);
1937 }
1938
1939 /*
1940 * This function compares two net addresses by family and returns TRUE
1941 * if they are the same host.
1942 * If there is any doubt, return FALSE.
1943 * The AF_INET family is handled as a special case so that address mbufs
1944 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1945 */
1946 int
1947 netaddr_match(family, haddr, nam)
1948 int family;
1949 union nethostaddr *haddr;
1950 struct mbuf *nam;
1951 {
1952 register struct sockaddr_in *inetaddr;
1953
1954 switch (family) {
1955 case AF_INET:
1956 inetaddr = mtod(nam, struct sockaddr_in *);
1957 if (inetaddr->sin_family == AF_INET &&
1958 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1959 return (1);
1960 break;
1961 #ifdef ISO
1962 case AF_ISO:
1963 {
1964 register struct sockaddr_iso *isoaddr1, *isoaddr2;
1965
1966 isoaddr1 = mtod(nam, struct sockaddr_iso *);
1967 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
1968 if (isoaddr1->siso_family == AF_ISO &&
1969 isoaddr1->siso_nlen > 0 &&
1970 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1971 SAME_ISOADDR(isoaddr1, isoaddr2))
1972 return (1);
1973 break;
1974 }
1975 #endif /* ISO */
1976 default:
1977 break;
1978 };
1979 return (0);
1980 }
1981
1982 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
1983 /*
1984 * This function finds the directory cookie that corresponds to the
1985 * logical byte offset given.
1986 */
1987 nfsuint64 *
1988 nfs_getcookie(np, off, add)
1989 register struct nfsnode *np;
1990 off_t off;
1991 int add;
1992 {
1993 register struct nfsdmap *dp, *dp2;
1994 register int pos;
1995
1996 pos = off / NFS_DIRBLKSIZ;
1997 if (pos == 0) {
1998 #ifdef DIAGNOSTIC
1999 if (add)
2000 panic("nfs getcookie add at 0");
2001 #endif
2002 return (&nfs_nullcookie);
2003 }
2004 pos--;
2005 dp = np->n_cookies.lh_first;
2006 if (!dp) {
2007 if (add) {
2008 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2009 M_NFSDIROFF, M_WAITOK);
2010 dp->ndm_eocookie = 0;
2011 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2012 } else
2013 return ((nfsuint64 *)0);
2014 }
2015 while (pos >= NFSNUMCOOKIES) {
2016 pos -= NFSNUMCOOKIES;
2017 if (dp->ndm_list.le_next) {
2018 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2019 pos >= dp->ndm_eocookie)
2020 return ((nfsuint64 *)0);
2021 dp = dp->ndm_list.le_next;
2022 } else if (add) {
2023 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2024 M_NFSDIROFF, M_WAITOK);
2025 dp2->ndm_eocookie = 0;
2026 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2027 dp = dp2;
2028 } else
2029 return ((nfsuint64 *)0);
2030 }
2031 if (pos >= dp->ndm_eocookie) {
2032 if (add)
2033 dp->ndm_eocookie = pos + 1;
2034 else
2035 return ((nfsuint64 *)0);
2036 }
2037 return (&dp->ndm_cookies[pos]);
2038 }
2039
2040 /*
2041 * Invalidate cached directory information, except for the actual directory
2042 * blocks (which are invalidated separately).
2043 * Done mainly to avoid the use of stale offset cookies.
2044 */
2045 void
2046 nfs_invaldir(vp)
2047 register struct vnode *vp;
2048 {
2049 #ifdef notdef /* XXX */
2050 register struct nfsnode *np = VTONFS(vp);
2051
2052 #ifdef DIAGNOSTIC
2053 if (vp->v_type != VDIR)
2054 panic("nfs: invaldir not dir");
2055 #endif
2056 np->n_direofoffset = 0;
2057 np->n_cookieverf.nfsuquad[0] = 0;
2058 np->n_cookieverf.nfsuquad[1] = 0;
2059 if (np->n_cookies.lh_first)
2060 np->n_cookies.lh_first->ndm_eocookie = 0;
2061 #endif
2062 }
2063
2064 /*
2065 * The write verifier has changed (probably due to a server reboot), so all
2066 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2067 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2068 * flag. Once done the new write verifier can be set for the mount point.
2069 */
2070 void
2071 nfs_clearcommit(mp)
2072 struct mount *mp;
2073 {
2074 register struct vnode *vp, *nvp;
2075 register struct buf *bp, *nbp;
2076 int s;
2077
2078 s = splbio();
2079 loop:
2080 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2081 if (vp->v_mount != mp) /* Paranoia */
2082 goto loop;
2083 nvp = vp->v_mntvnodes.le_next;
2084 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2085 nbp = bp->b_vnbufs.le_next;
2086 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2087 == (B_DELWRI | B_NEEDCOMMIT))
2088 bp->b_flags &= ~B_NEEDCOMMIT;
2089 }
2090 }
2091 splx(s);
2092 }
2093
2094 /*
2095 * Map errnos to NFS error numbers. For Version 3 also filter out error
2096 * numbers not specified for the associated procedure.
2097 */
2098 int
2099 nfsrv_errmap(nd, err)
2100 struct nfsrv_descript *nd;
2101 register int err;
2102 {
2103 register short *defaulterrp, *errp;
2104
2105 if (nd->nd_flag & ND_NFSV3) {
2106 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2107 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2108 while (*++errp) {
2109 if (*errp == err)
2110 return (err);
2111 else if (*errp > err)
2112 break;
2113 }
2114 return ((int)*defaulterrp);
2115 } else
2116 return (err & 0xffff);
2117 }
2118 if (err <= ELAST)
2119 return ((int)nfsrv_v2errmap[err - 1]);
2120 return (NFSERR_IO);
2121 }
2122
2123 /*
2124 * Sort the group list in increasing numerical order.
2125 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2126 * that used to be here.)
2127 */
2128 void
2129 nfsrvw_sort(list, num)
2130 register gid_t *list;
2131 register int num;
2132 {
2133 register int i, j;
2134 gid_t v;
2135
2136 /* Insertion sort. */
2137 for (i = 1; i < num; i++) {
2138 v = list[i];
2139 /* find correct slot for value v, moving others up */
2140 for (j = i; --j >= 0 && v < list[j];)
2141 list[j + 1] = list[j];
2142 list[j + 1] = v;
2143 }
2144 }
2145
2146 /*
2147 * copy credentials making sure that the result can be compared with bcmp().
2148 */
2149 void
2150 nfsrv_setcred(incred, outcred)
2151 register struct ucred *incred, *outcred;
2152 {
2153 register int i;
2154
2155 bzero((caddr_t)outcred, sizeof (struct ucred));
2156 outcred->cr_ref = 1;
2157 outcred->cr_uid = incred->cr_uid;
2158 outcred->cr_gid = incred->cr_gid;
2159 outcred->cr_ngroups = incred->cr_ngroups;
2160 for (i = 0; i < incred->cr_ngroups; i++)
2161 outcred->cr_groups[i] = incred->cr_groups[i];
2162 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2163 }
2164