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