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