nfs_subs.c revision 1.29 1 /* $NetBSD: nfs_subs.c,v 1.29 1996/07/01 10:22:47 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
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 * NOTE: can ony handle iovcnt == 1
813 */
814 int
815 nfsm_uiotombuf(uiop, mq, siz, bpos)
816 register struct uio *uiop;
817 struct mbuf **mq;
818 int siz;
819 caddr_t *bpos;
820 {
821 register char *uiocp;
822 register struct mbuf *mp, *mp2;
823 register int xfer, left, mlen;
824 int uiosiz, clflg, rem;
825 char *cp;
826
827 #ifdef DIAGNOSTIC
828 if (uiop->uio_iovcnt != 1)
829 panic("nfsm_uiotombuf: iovcnt != 1");
830 #endif
831
832 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
833 clflg = 1;
834 else
835 clflg = 0;
836 rem = nfsm_rndup(siz)-siz;
837 mp = mp2 = *mq;
838 while (siz > 0) {
839 left = uiop->uio_iov->iov_len;
840 uiocp = uiop->uio_iov->iov_base;
841 if (left > siz)
842 left = siz;
843 uiosiz = left;
844 while (left > 0) {
845 mlen = M_TRAILINGSPACE(mp);
846 if (mlen == 0) {
847 MGET(mp, M_WAIT, MT_DATA);
848 if (clflg)
849 MCLGET(mp, M_WAIT);
850 mp->m_len = 0;
851 mp2->m_next = mp;
852 mp2 = mp;
853 mlen = M_TRAILINGSPACE(mp);
854 }
855 xfer = (left > mlen) ? mlen : left;
856 #ifdef notdef
857 /* Not Yet.. */
858 if (uiop->uio_iov->iov_op != NULL)
859 (*(uiop->uio_iov->iov_op))
860 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
861 else
862 #endif
863 if (uiop->uio_segflg == UIO_SYSSPACE)
864 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
865 else
866 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
867 mp->m_len += xfer;
868 left -= xfer;
869 uiocp += xfer;
870 uiop->uio_offset += xfer;
871 uiop->uio_resid -= xfer;
872 }
873 uiop->uio_iov->iov_base += uiosiz;
874 uiop->uio_iov->iov_len -= uiosiz;
875 siz -= uiosiz;
876 }
877 if (rem > 0) {
878 if (rem > M_TRAILINGSPACE(mp)) {
879 MGET(mp, M_WAIT, MT_DATA);
880 mp->m_len = 0;
881 mp2->m_next = mp;
882 }
883 cp = mtod(mp, caddr_t)+mp->m_len;
884 for (left = 0; left < rem; left++)
885 *cp++ = '\0';
886 mp->m_len += rem;
887 *bpos = cp;
888 } else
889 *bpos = mtod(mp, caddr_t)+mp->m_len;
890 *mq = mp;
891 return (0);
892 }
893
894 /*
895 * Help break down an mbuf chain by setting the first siz bytes contiguous
896 * pointed to by returned val.
897 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
898 * cases. (The macros use the vars. dpos and dpos2)
899 */
900 int
901 nfsm_disct(mdp, dposp, siz, left, cp2)
902 struct mbuf **mdp;
903 caddr_t *dposp;
904 int siz;
905 int left;
906 caddr_t *cp2;
907 {
908 register struct mbuf *mp, *mp2;
909 register int siz2, xfer;
910 register caddr_t p;
911
912 mp = *mdp;
913 while (left == 0) {
914 *mdp = mp = mp->m_next;
915 if (mp == NULL)
916 return (EBADRPC);
917 left = mp->m_len;
918 *dposp = mtod(mp, caddr_t);
919 }
920 if (left >= siz) {
921 *cp2 = *dposp;
922 *dposp += siz;
923 } else if (mp->m_next == NULL) {
924 return (EBADRPC);
925 } else if (siz > MHLEN) {
926 panic("nfs S too big");
927 } else {
928 MGET(mp2, M_WAIT, MT_DATA);
929 mp2->m_next = mp->m_next;
930 mp->m_next = mp2;
931 mp->m_len -= left;
932 mp = mp2;
933 *cp2 = p = mtod(mp, caddr_t);
934 bcopy(*dposp, p, left); /* Copy what was left */
935 siz2 = siz-left;
936 p += left;
937 mp2 = mp->m_next;
938 /* Loop around copying up the siz2 bytes */
939 while (siz2 > 0) {
940 if (mp2 == NULL)
941 return (EBADRPC);
942 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
943 if (xfer > 0) {
944 bcopy(mtod(mp2, caddr_t), p, xfer);
945 NFSMADV(mp2, xfer);
946 mp2->m_len -= xfer;
947 p += xfer;
948 siz2 -= xfer;
949 }
950 if (siz2 > 0)
951 mp2 = mp2->m_next;
952 }
953 mp->m_len = siz;
954 *mdp = mp2;
955 *dposp = mtod(mp2, caddr_t);
956 }
957 return (0);
958 }
959
960 /*
961 * Advance the position in the mbuf chain.
962 */
963 int
964 nfs_adv(mdp, dposp, offs, left)
965 struct mbuf **mdp;
966 caddr_t *dposp;
967 int offs;
968 int left;
969 {
970 register struct mbuf *m;
971 register int s;
972
973 m = *mdp;
974 s = left;
975 while (s < offs) {
976 offs -= s;
977 m = m->m_next;
978 if (m == NULL)
979 return (EBADRPC);
980 s = m->m_len;
981 }
982 *mdp = m;
983 *dposp = mtod(m, caddr_t)+offs;
984 return (0);
985 }
986
987 /*
988 * Copy a string into mbufs for the hard cases...
989 */
990 int
991 nfsm_strtmbuf(mb, bpos, cp, siz)
992 struct mbuf **mb;
993 char **bpos;
994 char *cp;
995 long siz;
996 {
997 register struct mbuf *m1 = NULL, *m2;
998 long left, xfer, len, tlen;
999 u_int32_t *tl;
1000 int putsize;
1001
1002 putsize = 1;
1003 m2 = *mb;
1004 left = M_TRAILINGSPACE(m2);
1005 if (left > 0) {
1006 tl = ((u_int32_t *)(*bpos));
1007 *tl++ = txdr_unsigned(siz);
1008 putsize = 0;
1009 left -= NFSX_UNSIGNED;
1010 m2->m_len += NFSX_UNSIGNED;
1011 if (left > 0) {
1012 bcopy(cp, (caddr_t) tl, left);
1013 siz -= left;
1014 cp += left;
1015 m2->m_len += left;
1016 left = 0;
1017 }
1018 }
1019 /* Loop around adding mbufs */
1020 while (siz > 0) {
1021 MGET(m1, M_WAIT, MT_DATA);
1022 if (siz > MLEN)
1023 MCLGET(m1, M_WAIT);
1024 m1->m_len = NFSMSIZ(m1);
1025 m2->m_next = m1;
1026 m2 = m1;
1027 tl = mtod(m1, u_int32_t *);
1028 tlen = 0;
1029 if (putsize) {
1030 *tl++ = txdr_unsigned(siz);
1031 m1->m_len -= NFSX_UNSIGNED;
1032 tlen = NFSX_UNSIGNED;
1033 putsize = 0;
1034 }
1035 if (siz < m1->m_len) {
1036 len = nfsm_rndup(siz);
1037 xfer = siz;
1038 if (xfer < len)
1039 *(tl+(xfer>>2)) = 0;
1040 } else {
1041 xfer = len = m1->m_len;
1042 }
1043 bcopy(cp, (caddr_t) tl, xfer);
1044 m1->m_len = len+tlen;
1045 siz -= xfer;
1046 cp += xfer;
1047 }
1048 *mb = m1;
1049 *bpos = mtod(m1, caddr_t)+m1->m_len;
1050 return (0);
1051 }
1052
1053 /*
1054 * Called once to initialize data structures...
1055 */
1056 void
1057 nfs_init()
1058 {
1059 register int i;
1060
1061 #if !defined(alpha) && defined(DIAGNOSTIC)
1062 /*
1063 * Check to see if major data structures haven't bloated.
1064 */
1065 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1066 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1067 printf("Try reducing NFS_SMALLFH\n");
1068 }
1069 if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
1070 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
1071 printf("Try reducing NFS_MUIDHASHSIZ\n");
1072 }
1073 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1074 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1075 printf("Try reducing NFS_UIDHASHSIZ\n");
1076 }
1077 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1078 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1079 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1080 }
1081 #endif
1082
1083 nfsrtt.pos = 0;
1084 rpc_vers = txdr_unsigned(RPC_VER2);
1085 rpc_call = txdr_unsigned(RPC_CALL);
1086 rpc_reply = txdr_unsigned(RPC_REPLY);
1087 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1088 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1089 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1090 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1091 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1092 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1093 nfs_prog = txdr_unsigned(NFS_PROG);
1094 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1095 nfs_true = txdr_unsigned(TRUE);
1096 nfs_false = txdr_unsigned(FALSE);
1097 nfs_xdrneg1 = txdr_unsigned(-1);
1098 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1099 if (nfs_ticks < 1)
1100 nfs_ticks = 1;
1101 #ifdef NFSCLIENT
1102 /* Ensure async daemons disabled */
1103 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1104 nfs_iodwant[i] = (struct proc *)0;
1105 TAILQ_INIT(&nfs_bufq);
1106 nfs_nhinit(); /* Init the nfsnode table */
1107 #endif /* NFSCLIENT */
1108 #ifdef NFSSERVER
1109 nfsrv_init(0); /* Init server data structures */
1110 nfsrv_initcache(); /* Init the server request cache */
1111 #endif /* NFSSERVER */
1112
1113 /*
1114 * Initialize the nqnfs server stuff.
1115 */
1116 if (nqnfsstarttime == 0) {
1117 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1118 + nqsrv_clockskew + nqsrv_writeslack;
1119 NQLOADNOVRAM(nqnfsstarttime);
1120 CIRCLEQ_INIT(&nqtimerhead);
1121 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1122 }
1123
1124 /*
1125 * Initialize reply list and start timer
1126 */
1127 TAILQ_INIT(&nfs_reqq);
1128 nfs_timer(NULL);
1129 }
1130
1131 #ifdef NFSCLIENT
1132 /*
1133 * Attribute cache routines.
1134 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1135 * that are on the mbuf list
1136 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1137 * error otherwise
1138 */
1139
1140 /*
1141 * Load the attribute cache (that lives in the nfsnode entry) with
1142 * the values on the mbuf list and
1143 * Iff vap not NULL
1144 * copy the attributes to *vaper
1145 */
1146 int
1147 nfs_loadattrcache(vpp, mdp, dposp, vaper)
1148 struct vnode **vpp;
1149 struct mbuf **mdp;
1150 caddr_t *dposp;
1151 struct vattr *vaper;
1152 {
1153 register struct vnode *vp = *vpp;
1154 register struct vattr *vap;
1155 register struct nfs_fattr *fp;
1156 extern int (**spec_nfsv2nodeop_p) __P((void *));
1157 register struct nfsnode *np;
1158 register int32_t t1;
1159 caddr_t cp2;
1160 int error = 0;
1161 int32_t rdev;
1162 struct mbuf *md;
1163 enum vtype vtyp;
1164 u_short vmode;
1165 struct timespec mtime;
1166 struct vnode *nvp;
1167 int v3 = NFS_ISV3(vp);
1168
1169 md = *mdp;
1170 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1171 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1172 if (error)
1173 return (error);
1174 fp = (struct nfs_fattr *)cp2;
1175 if (v3) {
1176 vtyp = nfsv3tov_type(fp->fa_type);
1177 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1178 rdev = makedev(fxdr_unsigned(u_char, fp->fa3_rdev.specdata1),
1179 fxdr_unsigned(u_char, fp->fa3_rdev.specdata2));
1180 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1181 } else {
1182 vtyp = nfsv2tov_type(fp->fa_type);
1183 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1184 if (vtyp == VNON || vtyp == VREG)
1185 vtyp = IFTOVT(vmode);
1186 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1187 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1188
1189 /*
1190 * Really ugly NFSv2 kludge.
1191 */
1192 if (vtyp == VCHR && rdev == 0xffffffff)
1193 vtyp = VFIFO;
1194 }
1195
1196 /*
1197 * If v_type == VNON it is a new node, so fill in the v_type,
1198 * n_mtime fields. Check to see if it represents a special
1199 * device, and if so, check for a possible alias. Once the
1200 * correct vnode has been obtained, fill in the rest of the
1201 * information.
1202 */
1203 np = VTONFS(vp);
1204 if (vp->v_type != vtyp) {
1205 vp->v_type = vtyp;
1206 if (vp->v_type == VFIFO) {
1207 #ifndef FIFO
1208 return (EOPNOTSUPP);
1209 #else
1210 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1211 vp->v_op = fifo_nfsv2nodeop_p;
1212 #endif /* FIFO */
1213 }
1214 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1215 vp->v_op = spec_nfsv2nodeop_p;
1216 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1217 if (nvp) {
1218 /*
1219 * Discard unneeded vnode, but save its nfsnode.
1220 * Since the nfsnode does not have a lock, its
1221 * vnode lock has to be carried over.
1222 */
1223 #ifdef Lite2_integrated
1224 nvp->v_vnlock = vp->v_vnlock;
1225 vp->v_vnlock = NULL;
1226 #endif
1227 nvp->v_data = vp->v_data;
1228 vp->v_data = NULL;
1229 vp->v_op = spec_vnodeop_p;
1230 vrele(vp);
1231 vgone(vp);
1232 /*
1233 * Reinitialize aliased node.
1234 */
1235 np->n_vnode = nvp;
1236 *vpp = vp = nvp;
1237 }
1238 }
1239 np->n_mtime = mtime.tv_sec;
1240 }
1241 vap = &np->n_vattr;
1242 vap->va_type = vtyp;
1243 vap->va_mode = (vmode & 07777);
1244 vap->va_rdev = (dev_t)rdev;
1245 vap->va_mtime = mtime;
1246 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1247 if (v3) {
1248 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1249 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1250 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1251 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1252 vap->va_blocksize = NFS_FABLKSIZE;
1253 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1254 vap->va_fileid = fxdr_unsigned(int32_t,
1255 fp->fa3_fileid.nfsuquad[1]);
1256 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1257 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1258 vap->va_flags = 0;
1259 vap->va_filerev = 0;
1260 } else {
1261 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1262 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1263 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1264 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1265 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1266 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1267 * NFS_FABLKSIZE;
1268 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1269 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1270 vap->va_flags = 0;
1271 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1272 fp->fa2_ctime.nfsv2_sec);
1273 vap->va_ctime.tv_nsec = 0;
1274 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1275 vap->va_filerev = 0;
1276 }
1277 if (vap->va_size != np->n_size) {
1278 if (vap->va_type == VREG) {
1279 if (np->n_flag & NMODIFIED) {
1280 if (vap->va_size < np->n_size)
1281 vap->va_size = np->n_size;
1282 else
1283 np->n_size = vap->va_size;
1284 } else
1285 np->n_size = vap->va_size;
1286 vnode_pager_setsize(vp, (u_long)np->n_size);
1287 } else
1288 np->n_size = vap->va_size;
1289 }
1290 np->n_attrstamp = time.tv_sec;
1291 if (vaper != NULL) {
1292 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1293 if (np->n_flag & NCHG) {
1294 if (np->n_flag & NACC)
1295 vaper->va_atime = np->n_atim;
1296 if (np->n_flag & NUPD)
1297 vaper->va_mtime = np->n_mtim;
1298 }
1299 }
1300 return (0);
1301 }
1302
1303 /*
1304 * Check the time stamp
1305 * If the cache is valid, copy contents to *vap and return 0
1306 * otherwise return an error
1307 */
1308 int
1309 nfs_getattrcache(vp, vaper)
1310 register struct vnode *vp;
1311 struct vattr *vaper;
1312 {
1313 register struct nfsnode *np = VTONFS(vp);
1314 register struct vattr *vap;
1315
1316 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1317 nfsstats.attrcache_misses++;
1318 return (ENOENT);
1319 }
1320 nfsstats.attrcache_hits++;
1321 vap = &np->n_vattr;
1322 if (vap->va_size != np->n_size) {
1323 if (vap->va_type == VREG) {
1324 if (np->n_flag & NMODIFIED) {
1325 if (vap->va_size < np->n_size)
1326 vap->va_size = np->n_size;
1327 else
1328 np->n_size = vap->va_size;
1329 } else
1330 np->n_size = vap->va_size;
1331 vnode_pager_setsize(vp, (u_long)np->n_size);
1332 } else
1333 np->n_size = vap->va_size;
1334 }
1335 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1336 if (np->n_flag & NCHG) {
1337 if (np->n_flag & NACC)
1338 vaper->va_atime = np->n_atim;
1339 if (np->n_flag & NUPD)
1340 vaper->va_mtime = np->n_mtim;
1341 }
1342 return (0);
1343 }
1344 #endif /* NFSCLIENT */
1345
1346 /*
1347 * Set up nameidata for a lookup() call and do it
1348 */
1349 int
1350 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag)
1351 register struct nameidata *ndp;
1352 fhandle_t *fhp;
1353 int len;
1354 struct nfssvc_sock *slp;
1355 struct mbuf *nam;
1356 struct mbuf **mdp;
1357 caddr_t *dposp;
1358 struct vnode **retdirp;
1359 struct proc *p;
1360 int kerbflag;
1361 {
1362 register int i, rem;
1363 register struct mbuf *md;
1364 register char *fromcp, *tocp;
1365 struct vnode *dp;
1366 int error, rdonly;
1367 struct componentname *cnp = &ndp->ni_cnd;
1368
1369 *retdirp = (struct vnode *)0;
1370 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
1371 /*
1372 * Copy the name from the mbuf list to ndp->ni_pnbuf
1373 * and set the various ndp fields appropriately.
1374 */
1375 fromcp = *dposp;
1376 tocp = cnp->cn_pnbuf;
1377 md = *mdp;
1378 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1379 for (i = 0; i < len; i++) {
1380 while (rem == 0) {
1381 md = md->m_next;
1382 if (md == NULL) {
1383 error = EBADRPC;
1384 goto out;
1385 }
1386 fromcp = mtod(md, caddr_t);
1387 rem = md->m_len;
1388 }
1389 if (*fromcp == '\0' || *fromcp == '/') {
1390 error = EACCES;
1391 goto out;
1392 }
1393 *tocp++ = *fromcp++;
1394 rem--;
1395 }
1396 *tocp = '\0';
1397 *mdp = md;
1398 *dposp = fromcp;
1399 len = nfsm_rndup(len)-len;
1400 if (len > 0) {
1401 if (rem >= len)
1402 *dposp += len;
1403 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1404 goto out;
1405 }
1406 ndp->ni_pathlen = tocp - cnp->cn_pnbuf;
1407 cnp->cn_nameptr = cnp->cn_pnbuf;
1408 /*
1409 * Extract and set starting directory.
1410 */
1411 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1412 nam, &rdonly, kerbflag);
1413 if (error)
1414 goto out;
1415 if (dp->v_type != VDIR) {
1416 vrele(dp);
1417 error = ENOTDIR;
1418 goto out;
1419 }
1420 VREF(dp);
1421 *retdirp = dp;
1422 ndp->ni_startdir = dp;
1423 if (rdonly)
1424 cnp->cn_flags |= (NOCROSSMOUNT | RDONLY);
1425 else
1426 cnp->cn_flags |= NOCROSSMOUNT;
1427 /*
1428 * And call lookup() to do the real work
1429 */
1430 cnp->cn_proc = p;
1431 error = lookup(ndp);
1432 if (error)
1433 goto out;
1434 /*
1435 * Check for encountering a symbolic link
1436 */
1437 if (cnp->cn_flags & ISSYMLINK) {
1438 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1439 vput(ndp->ni_dvp);
1440 else
1441 vrele(ndp->ni_dvp);
1442 vput(ndp->ni_vp);
1443 ndp->ni_vp = NULL;
1444 error = EINVAL;
1445 goto out;
1446 }
1447 /*
1448 * Check for saved name request
1449 */
1450 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1451 cnp->cn_flags |= HASBUF;
1452 return (0);
1453 }
1454 out:
1455 FREE(cnp->cn_pnbuf, M_NAMEI);
1456 return (error);
1457 }
1458
1459 /*
1460 * A fiddled version of m_adj() that ensures null fill to a long
1461 * boundary and only trims off the back end
1462 */
1463 void
1464 nfsm_adj(mp, len, nul)
1465 struct mbuf *mp;
1466 register int len;
1467 int nul;
1468 {
1469 register struct mbuf *m;
1470 register int count, i;
1471 register char *cp;
1472
1473 /*
1474 * Trim from tail. Scan the mbuf chain,
1475 * calculating its length and finding the last mbuf.
1476 * If the adjustment only affects this mbuf, then just
1477 * adjust and return. Otherwise, rescan and truncate
1478 * after the remaining size.
1479 */
1480 count = 0;
1481 m = mp;
1482 for (;;) {
1483 count += m->m_len;
1484 if (m->m_next == (struct mbuf *)0)
1485 break;
1486 m = m->m_next;
1487 }
1488 if (m->m_len > len) {
1489 m->m_len -= len;
1490 if (nul > 0) {
1491 cp = mtod(m, caddr_t)+m->m_len-nul;
1492 for (i = 0; i < nul; i++)
1493 *cp++ = '\0';
1494 }
1495 return;
1496 }
1497 count -= len;
1498 if (count < 0)
1499 count = 0;
1500 /*
1501 * Correct length for chain is "count".
1502 * Find the mbuf with last data, adjust its length,
1503 * and toss data from remaining mbufs on chain.
1504 */
1505 for (m = mp; m; m = m->m_next) {
1506 if (m->m_len >= count) {
1507 m->m_len = count;
1508 if (nul > 0) {
1509 cp = mtod(m, caddr_t)+m->m_len-nul;
1510 for (i = 0; i < nul; i++)
1511 *cp++ = '\0';
1512 }
1513 break;
1514 }
1515 count -= m->m_len;
1516 }
1517 for (m = m->m_next;m;m = m->m_next)
1518 m->m_len = 0;
1519 }
1520
1521 /*
1522 * Make these functions instead of macros, so that the kernel text size
1523 * doesn't get too big...
1524 */
1525 void
1526 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1527 struct nfsrv_descript *nfsd;
1528 int before_ret;
1529 register struct vattr *before_vap;
1530 int after_ret;
1531 struct vattr *after_vap;
1532 struct mbuf **mbp;
1533 char **bposp;
1534 {
1535 register struct mbuf *mb = *mbp, *mb2;
1536 register char *bpos = *bposp;
1537 register u_int32_t *tl;
1538
1539 if (before_ret) {
1540 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1541 *tl = nfs_false;
1542 } else {
1543 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1544 *tl++ = nfs_true;
1545 txdr_hyper(&(before_vap->va_size), tl);
1546 tl += 2;
1547 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1548 tl += 2;
1549 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1550 }
1551 *bposp = bpos;
1552 *mbp = mb;
1553 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1554 }
1555
1556 void
1557 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1558 struct nfsrv_descript *nfsd;
1559 int after_ret;
1560 struct vattr *after_vap;
1561 struct mbuf **mbp;
1562 char **bposp;
1563 {
1564 register struct mbuf *mb = *mbp, *mb2;
1565 register char *bpos = *bposp;
1566 register u_int32_t *tl;
1567 register struct nfs_fattr *fp;
1568
1569 if (after_ret) {
1570 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1571 *tl = nfs_false;
1572 } else {
1573 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1574 *tl++ = nfs_true;
1575 fp = (struct nfs_fattr *)tl;
1576 nfsm_srvfattr(nfsd, after_vap, fp);
1577 }
1578 *mbp = mb;
1579 *bposp = bpos;
1580 }
1581
1582 void
1583 nfsm_srvfattr(nfsd, vap, fp)
1584 register struct nfsrv_descript *nfsd;
1585 register struct vattr *vap;
1586 register struct nfs_fattr *fp;
1587 {
1588
1589 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1590 fp->fa_uid = txdr_unsigned(vap->va_uid);
1591 fp->fa_gid = txdr_unsigned(vap->va_gid);
1592 if (nfsd->nd_flag & ND_NFSV3) {
1593 fp->fa_type = vtonfsv3_type(vap->va_type);
1594 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1595 txdr_hyper(&vap->va_size, &fp->fa3_size);
1596 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1597 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1598 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1599 fp->fa3_fsid.nfsuquad[0] = 0;
1600 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1601 fp->fa3_fileid.nfsuquad[0] = 0;
1602 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1603 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1604 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1605 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1606 } else {
1607 fp->fa_type = vtonfsv2_type(vap->va_type);
1608 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1609 fp->fa2_size = txdr_unsigned(vap->va_size);
1610 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1611 if (vap->va_type == VFIFO)
1612 fp->fa2_rdev = 0xffffffff;
1613 else
1614 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1615 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1616 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1617 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1618 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1619 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1620 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1621 }
1622 }
1623
1624 /*
1625 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1626 * - look up fsid in mount list (if not found ret error)
1627 * - get vp and export rights by calling VFS_FHTOVP()
1628 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1629 * - if not lockflag unlock it with VOP_UNLOCK()
1630 */
1631 int
1632 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag)
1633 fhandle_t *fhp;
1634 int lockflag;
1635 struct vnode **vpp;
1636 struct ucred *cred;
1637 struct nfssvc_sock *slp;
1638 struct mbuf *nam;
1639 int *rdonlyp;
1640 int kerbflag;
1641 {
1642 #ifdef Lite2_integrated
1643 struct proc *p = curproc; /* XXX */
1644 #endif
1645 register struct mount *mp;
1646 register int i;
1647 struct ucred *credanon;
1648 int error, exflags;
1649
1650 *vpp = (struct vnode *)0;
1651 #ifdef Lite2_integrated
1652 mp = vfs_getvfs(&fhp->fh_fsid);
1653 #else
1654 mp = getvfs(&fhp->fh_fsid);
1655 #endif
1656 if (!mp)
1657 return (ESTALE);
1658 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1659 if (error)
1660 return (error);
1661 /*
1662 * Check/setup credentials.
1663 */
1664 if (exflags & MNT_EXKERB) {
1665 if (!kerbflag) {
1666 vput(*vpp);
1667 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1668 }
1669 } else if (kerbflag) {
1670 vput(*vpp);
1671 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1672 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1673 cred->cr_uid = credanon->cr_uid;
1674 cred->cr_gid = credanon->cr_gid;
1675 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1676 cred->cr_groups[i] = credanon->cr_groups[i];
1677 cred->cr_ngroups = i;
1678 }
1679 if (exflags & MNT_EXRDONLY)
1680 *rdonlyp = 1;
1681 else
1682 *rdonlyp = 0;
1683 if (!lockflag)
1684 #ifdef Lite2_integrated
1685 VOP_UNLOCK(*vpp, 0, p);
1686 #else
1687 VOP_UNLOCK(*vpp);
1688 #endif
1689 return (0);
1690 }
1691
1692 /*
1693 * This function compares two net addresses by family and returns TRUE
1694 * if they are the same host.
1695 * If there is any doubt, return FALSE.
1696 * The AF_INET family is handled as a special case so that address mbufs
1697 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1698 */
1699 int
1700 netaddr_match(family, haddr, nam)
1701 int family;
1702 union nethostaddr *haddr;
1703 struct mbuf *nam;
1704 {
1705 register struct sockaddr_in *inetaddr;
1706
1707 switch (family) {
1708 case AF_INET:
1709 inetaddr = mtod(nam, struct sockaddr_in *);
1710 if (inetaddr->sin_family == AF_INET &&
1711 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1712 return (1);
1713 break;
1714 #ifdef ISO
1715 case AF_ISO:
1716 {
1717 register struct sockaddr_iso *isoaddr1, *isoaddr2;
1718
1719 isoaddr1 = mtod(nam, struct sockaddr_iso *);
1720 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
1721 if (isoaddr1->siso_family == AF_ISO &&
1722 isoaddr1->siso_nlen > 0 &&
1723 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1724 SAME_ISOADDR(isoaddr1, isoaddr2))
1725 return (1);
1726 break;
1727 }
1728 #endif /* ISO */
1729 default:
1730 break;
1731 };
1732 return (0);
1733 }
1734
1735 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
1736 /*
1737 * This function finds the directory cookie that corresponds to the
1738 * logical byte offset given.
1739 */
1740 nfsuint64 *
1741 nfs_getcookie(np, off, add)
1742 register struct nfsnode *np;
1743 off_t off;
1744 int add;
1745 {
1746 register struct nfsdmap *dp, *dp2;
1747 register int pos;
1748
1749 pos = off / NFS_DIRBLKSIZ;
1750 if (pos == 0) {
1751 #ifdef DIAGNOSTIC
1752 if (add)
1753 panic("nfs getcookie add at 0");
1754 #endif
1755 return (&nfs_nullcookie);
1756 }
1757 pos--;
1758 dp = np->n_cookies.lh_first;
1759 if (!dp) {
1760 if (add) {
1761 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1762 M_NFSDIROFF, M_WAITOK);
1763 dp->ndm_eocookie = 0;
1764 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1765 } else
1766 return ((nfsuint64 *)0);
1767 }
1768 while (pos >= NFSNUMCOOKIES) {
1769 pos -= NFSNUMCOOKIES;
1770 if (dp->ndm_list.le_next) {
1771 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1772 pos >= dp->ndm_eocookie)
1773 return ((nfsuint64 *)0);
1774 dp = dp->ndm_list.le_next;
1775 } else if (add) {
1776 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
1777 M_NFSDIROFF, M_WAITOK);
1778 dp2->ndm_eocookie = 0;
1779 LIST_INSERT_AFTER(dp, dp2, ndm_list);
1780 dp = dp2;
1781 } else
1782 return ((nfsuint64 *)0);
1783 }
1784 if (pos >= dp->ndm_eocookie) {
1785 if (add)
1786 dp->ndm_eocookie = pos + 1;
1787 else
1788 return ((nfsuint64 *)0);
1789 }
1790 return (&dp->ndm_cookies[pos]);
1791 }
1792
1793 /*
1794 * Invalidate cached directory information, except for the actual directory
1795 * blocks (which are invalidated separately).
1796 * Done mainly to avoid the use of stale offset cookies.
1797 */
1798 void
1799 nfs_invaldir(vp)
1800 register struct vnode *vp;
1801 {
1802 #ifdef notdef /* XXX */
1803 register struct nfsnode *np = VTONFS(vp);
1804
1805 #ifdef DIAGNOSTIC
1806 if (vp->v_type != VDIR)
1807 panic("nfs: invaldir not dir");
1808 #endif
1809 np->n_direofoffset = 0;
1810 np->n_cookieverf.nfsuquad[0] = 0;
1811 np->n_cookieverf.nfsuquad[1] = 0;
1812 if (np->n_cookies.lh_first)
1813 np->n_cookies.lh_first->ndm_eocookie = 0;
1814 #endif
1815 }
1816
1817 /*
1818 * The write verifier has changed (probably due to a server reboot), so all
1819 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
1820 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
1821 * flag. Once done the new write verifier can be set for the mount point.
1822 */
1823 void
1824 nfs_clearcommit(mp)
1825 struct mount *mp;
1826 {
1827 register struct vnode *vp, *nvp;
1828 register struct buf *bp, *nbp;
1829 int s;
1830
1831 s = splbio();
1832 loop:
1833 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1834 if (vp->v_mount != mp) /* Paranoia */
1835 goto loop;
1836 nvp = vp->v_mntvnodes.le_next;
1837 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
1838 nbp = bp->b_vnbufs.le_next;
1839 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
1840 == (B_DELWRI | B_NEEDCOMMIT))
1841 bp->b_flags &= ~B_NEEDCOMMIT;
1842 }
1843 }
1844 splx(s);
1845 }
1846
1847 /*
1848 * Map errnos to NFS error numbers. For Version 3 also filter out error
1849 * numbers not specified for the associated procedure.
1850 */
1851 int
1852 nfsrv_errmap(nd, err)
1853 struct nfsrv_descript *nd;
1854 register int err;
1855 {
1856 register short *defaulterrp, *errp;
1857
1858 if (nd->nd_flag & ND_NFSV3) {
1859 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1860 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1861 while (*++errp) {
1862 if (*errp == err)
1863 return (err);
1864 else if (*errp > err)
1865 break;
1866 }
1867 return ((int)*defaulterrp);
1868 } else
1869 return (err & 0xffff);
1870 }
1871 if (err <= ELAST)
1872 return ((int)nfsrv_v2errmap[err - 1]);
1873 return (NFSERR_IO);
1874 }
1875
1876 /*
1877 * Sort the group list in increasing numerical order.
1878 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1879 * that used to be here.)
1880 */
1881 void
1882 nfsrvw_sort(list, num)
1883 register gid_t *list;
1884 register int num;
1885 {
1886 register int i, j;
1887 gid_t v;
1888
1889 /* Insertion sort. */
1890 for (i = 1; i < num; i++) {
1891 v = list[i];
1892 /* find correct slot for value v, moving others up */
1893 for (j = i; --j >= 0 && v < list[j];)
1894 list[j + 1] = list[j];
1895 list[j + 1] = v;
1896 }
1897 }
1898
1899 /*
1900 * copy credentials making sure that the result can be compared with bcmp().
1901 */
1902 void
1903 nfsrv_setcred(incred, outcred)
1904 register struct ucred *incred, *outcred;
1905 {
1906 register int i;
1907
1908 bzero((caddr_t)outcred, sizeof (struct ucred));
1909 outcred->cr_ref = 1;
1910 outcred->cr_uid = incred->cr_uid;
1911 outcred->cr_gid = incred->cr_gid;
1912 outcred->cr_ngroups = incred->cr_ngroups;
1913 for (i = 0; i < incred->cr_ngroups; i++)
1914 outcred->cr_groups[i] = incred->cr_groups[i];
1915 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
1916 }
1917