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