nfs_subs.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.3 (Berkeley) 1/4/94
37 */
38
39 /*
40 * These functions support the macros and help fiddle mbuf chains for
41 * the nfs op functions. They do things like create the rpc header and
42 * copy data between mbuf chains and uio lists.
43 */
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <sys/namei.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/stat.h>
54
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsv2.h>
57 #include <nfs/nfsnode.h>
58 #include <nfs/nfs.h>
59 #include <nfs/xdr_subs.h>
60 #include <nfs/nfsm_subs.h>
61 #include <nfs/nfsmount.h>
62 #include <nfs/nqnfs.h>
63 #include <nfs/nfsrtt.h>
64
65 #include <miscfs/specfs/specdev.h>
66
67 #include <netinet/in.h>
68 #ifdef ISO
69 #include <netiso/iso.h>
70 #endif
71
72 #define TRUE 1
73 #define FALSE 0
74
75 /*
76 * Data items converted to xdr at startup, since they are constant
77 * This is kinda hokey, but may save a little time doing byte swaps
78 */
79 u_long nfs_procids[NFS_NPROCS];
80 u_long nfs_xdrneg1;
81 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
82 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred,
83 rpc_auth_kerb;
84 u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
85
86 /* And other global data */
87 static u_long nfs_xid = 0;
88 enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON };
89 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
90 extern struct nfsreq nfsreqh;
91 extern int nqnfs_piggy[NFS_NPROCS];
92 extern struct nfsrtt nfsrtt;
93 extern time_t nqnfsstarttime;
94 extern u_long nqnfs_prog, nqnfs_vers;
95 extern int nqsrv_clockskew;
96 extern int nqsrv_writeslack;
97 extern int nqsrv_maxlease;
98
99 /*
100 * Create the header for an rpc request packet
101 * The hsiz is the size of the rest of the nfs request header.
102 * (just used to decide if a cluster is a good idea)
103 */
104 struct mbuf *
105 nfsm_reqh(vp, procid, hsiz, bposp)
106 struct vnode *vp;
107 u_long procid;
108 int hsiz;
109 caddr_t *bposp;
110 {
111 register struct mbuf *mb;
112 register u_long *tl;
113 register caddr_t bpos;
114 struct mbuf *mb2;
115 struct nfsmount *nmp;
116 int nqflag;
117
118 MGET(mb, M_WAIT, MT_DATA);
119 if (hsiz >= MINCLSIZE)
120 MCLGET(mb, M_WAIT);
121 mb->m_len = 0;
122 bpos = mtod(mb, caddr_t);
123
124 /*
125 * For NQNFS, add lease request.
126 */
127 if (vp) {
128 nmp = VFSTONFS(vp->v_mount);
129 if (nmp->nm_flag & NFSMNT_NQNFS) {
130 nqflag = NQNFS_NEEDLEASE(vp, procid);
131 if (nqflag) {
132 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
133 *tl++ = txdr_unsigned(nqflag);
134 *tl = txdr_unsigned(nmp->nm_leaseterm);
135 } else {
136 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
137 *tl = 0;
138 }
139 }
140 }
141 /* Finally, return values */
142 *bposp = bpos;
143 return (mb);
144 }
145
146 /*
147 * Build the RPC header and fill in the authorization info.
148 * The authorization string argument is only used when the credentials
149 * come from outside of the kernel.
150 * Returns the head of the mbuf list.
151 */
152 struct mbuf *
153 nfsm_rpchead(cr, nqnfs, procid, auth_type, auth_len, auth_str, mrest,
154 mrest_len, mbp, xidp)
155 register struct ucred *cr;
156 int nqnfs;
157 int procid;
158 int auth_type;
159 int auth_len;
160 char *auth_str;
161 struct mbuf *mrest;
162 int mrest_len;
163 struct mbuf **mbp;
164 u_long *xidp;
165 {
166 register struct mbuf *mb;
167 register u_long *tl;
168 register caddr_t bpos;
169 register int i;
170 struct mbuf *mreq, *mb2;
171 int siz, grpsiz, authsiz;
172
173 authsiz = nfsm_rndup(auth_len);
174 if (auth_type == RPCAUTH_NQNFS)
175 authsiz += 2 * NFSX_UNSIGNED;
176 MGETHDR(mb, M_WAIT, MT_DATA);
177 if ((authsiz + 10*NFSX_UNSIGNED) >= MINCLSIZE) {
178 MCLGET(mb, M_WAIT);
179 } else if ((authsiz + 10*NFSX_UNSIGNED) < MHLEN) {
180 MH_ALIGN(mb, authsiz + 10*NFSX_UNSIGNED);
181 } else {
182 MH_ALIGN(mb, 8*NFSX_UNSIGNED);
183 }
184 mb->m_len = 0;
185 mreq = mb;
186 bpos = mtod(mb, caddr_t);
187
188 /*
189 * First the RPC header.
190 */
191 nfsm_build(tl, u_long *, 8*NFSX_UNSIGNED);
192 if (++nfs_xid == 0)
193 nfs_xid++;
194 *tl++ = *xidp = txdr_unsigned(nfs_xid);
195 *tl++ = rpc_call;
196 *tl++ = rpc_vers;
197 if (nqnfs) {
198 *tl++ = txdr_unsigned(NQNFS_PROG);
199 *tl++ = txdr_unsigned(NQNFS_VER1);
200 } else {
201 *tl++ = txdr_unsigned(NFS_PROG);
202 *tl++ = txdr_unsigned(NFS_VER2);
203 }
204 *tl++ = txdr_unsigned(procid);
205
206 /*
207 * And then the authorization cred.
208 */
209 *tl++ = txdr_unsigned(auth_type);
210 *tl = txdr_unsigned(authsiz);
211 switch (auth_type) {
212 case RPCAUTH_UNIX:
213 nfsm_build(tl, u_long *, auth_len);
214 *tl++ = 0; /* stamp ?? */
215 *tl++ = 0; /* NULL hostname */
216 *tl++ = txdr_unsigned(cr->cr_uid);
217 *tl++ = txdr_unsigned(cr->cr_groups[0]);
218 grpsiz = (auth_len >> 2) - 5;
219 *tl++ = txdr_unsigned(grpsiz);
220 for (i = 1; i <= grpsiz; i++)
221 *tl++ = txdr_unsigned(cr->cr_groups[i]);
222 break;
223 case RPCAUTH_NQNFS:
224 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
225 *tl++ = txdr_unsigned(cr->cr_uid);
226 *tl = txdr_unsigned(auth_len);
227 siz = auth_len;
228 while (siz > 0) {
229 if (M_TRAILINGSPACE(mb) == 0) {
230 MGET(mb2, M_WAIT, MT_DATA);
231 if (siz >= MINCLSIZE)
232 MCLGET(mb2, M_WAIT);
233 mb->m_next = mb2;
234 mb = mb2;
235 mb->m_len = 0;
236 bpos = mtod(mb, caddr_t);
237 }
238 i = min(siz, M_TRAILINGSPACE(mb));
239 bcopy(auth_str, bpos, i);
240 mb->m_len += i;
241 auth_str += i;
242 bpos += i;
243 siz -= i;
244 }
245 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
246 for (i = 0; i < siz; i++)
247 *bpos++ = '\0';
248 mb->m_len += siz;
249 }
250 break;
251 };
252 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
253 *tl++ = txdr_unsigned(RPCAUTH_NULL);
254 *tl = 0;
255 mb->m_next = mrest;
256 mreq->m_pkthdr.len = authsiz + 10*NFSX_UNSIGNED + mrest_len;
257 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
258 *mbp = mb;
259 return (mreq);
260 }
261
262 /*
263 * copies mbuf chain to the uio scatter/gather list
264 */
265 nfsm_mbuftouio(mrep, uiop, siz, dpos)
266 struct mbuf **mrep;
267 register struct uio *uiop;
268 int siz;
269 caddr_t *dpos;
270 {
271 register char *mbufcp, *uiocp;
272 register int xfer, left, len;
273 register struct mbuf *mp;
274 long uiosiz, rem;
275 int error = 0;
276
277 mp = *mrep;
278 mbufcp = *dpos;
279 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
280 rem = nfsm_rndup(siz)-siz;
281 while (siz > 0) {
282 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
283 return (EFBIG);
284 left = uiop->uio_iov->iov_len;
285 uiocp = uiop->uio_iov->iov_base;
286 if (left > siz)
287 left = siz;
288 uiosiz = left;
289 while (left > 0) {
290 while (len == 0) {
291 mp = mp->m_next;
292 if (mp == NULL)
293 return (EBADRPC);
294 mbufcp = mtod(mp, caddr_t);
295 len = mp->m_len;
296 }
297 xfer = (left > len) ? len : left;
298 #ifdef notdef
299 /* Not Yet.. */
300 if (uiop->uio_iov->iov_op != NULL)
301 (*(uiop->uio_iov->iov_op))
302 (mbufcp, uiocp, xfer);
303 else
304 #endif
305 if (uiop->uio_segflg == UIO_SYSSPACE)
306 bcopy(mbufcp, uiocp, xfer);
307 else
308 copyout(mbufcp, uiocp, xfer);
309 left -= xfer;
310 len -= xfer;
311 mbufcp += xfer;
312 uiocp += xfer;
313 uiop->uio_offset += xfer;
314 uiop->uio_resid -= xfer;
315 }
316 if (uiop->uio_iov->iov_len <= siz) {
317 uiop->uio_iovcnt--;
318 uiop->uio_iov++;
319 } else {
320 uiop->uio_iov->iov_base += uiosiz;
321 uiop->uio_iov->iov_len -= uiosiz;
322 }
323 siz -= uiosiz;
324 }
325 *dpos = mbufcp;
326 *mrep = mp;
327 if (rem > 0) {
328 if (len < rem)
329 error = nfs_adv(mrep, dpos, rem, len);
330 else
331 *dpos += rem;
332 }
333 return (error);
334 }
335
336 /*
337 * copies a uio scatter/gather list to an mbuf chain...
338 */
339 nfsm_uiotombuf(uiop, mq, siz, bpos)
340 register struct uio *uiop;
341 struct mbuf **mq;
342 int siz;
343 caddr_t *bpos;
344 {
345 register char *uiocp;
346 register struct mbuf *mp, *mp2;
347 register int xfer, left, mlen;
348 int uiosiz, clflg, rem;
349 char *cp;
350
351 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
352 clflg = 1;
353 else
354 clflg = 0;
355 rem = nfsm_rndup(siz)-siz;
356 mp = mp2 = *mq;
357 while (siz > 0) {
358 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
359 return (EINVAL);
360 left = uiop->uio_iov->iov_len;
361 uiocp = uiop->uio_iov->iov_base;
362 if (left > siz)
363 left = siz;
364 uiosiz = left;
365 while (left > 0) {
366 mlen = M_TRAILINGSPACE(mp);
367 if (mlen == 0) {
368 MGET(mp, M_WAIT, MT_DATA);
369 if (clflg)
370 MCLGET(mp, M_WAIT);
371 mp->m_len = 0;
372 mp2->m_next = mp;
373 mp2 = mp;
374 mlen = M_TRAILINGSPACE(mp);
375 }
376 xfer = (left > mlen) ? mlen : left;
377 #ifdef notdef
378 /* Not Yet.. */
379 if (uiop->uio_iov->iov_op != NULL)
380 (*(uiop->uio_iov->iov_op))
381 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
382 else
383 #endif
384 if (uiop->uio_segflg == UIO_SYSSPACE)
385 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
386 else
387 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
388 mp->m_len += xfer;
389 left -= xfer;
390 uiocp += xfer;
391 uiop->uio_offset += xfer;
392 uiop->uio_resid -= xfer;
393 }
394 if (uiop->uio_iov->iov_len <= siz) {
395 uiop->uio_iovcnt--;
396 uiop->uio_iov++;
397 } else {
398 uiop->uio_iov->iov_base += uiosiz;
399 uiop->uio_iov->iov_len -= uiosiz;
400 }
401 siz -= uiosiz;
402 }
403 if (rem > 0) {
404 if (rem > M_TRAILINGSPACE(mp)) {
405 MGET(mp, M_WAIT, MT_DATA);
406 mp->m_len = 0;
407 mp2->m_next = mp;
408 }
409 cp = mtod(mp, caddr_t)+mp->m_len;
410 for (left = 0; left < rem; left++)
411 *cp++ = '\0';
412 mp->m_len += rem;
413 *bpos = cp;
414 } else
415 *bpos = mtod(mp, caddr_t)+mp->m_len;
416 *mq = mp;
417 return (0);
418 }
419
420 /*
421 * Help break down an mbuf chain by setting the first siz bytes contiguous
422 * pointed to by returned val.
423 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
424 * cases. (The macros use the vars. dpos and dpos2)
425 */
426 nfsm_disct(mdp, dposp, siz, left, cp2)
427 struct mbuf **mdp;
428 caddr_t *dposp;
429 int siz;
430 int left;
431 caddr_t *cp2;
432 {
433 register struct mbuf *mp, *mp2;
434 register int siz2, xfer;
435 register caddr_t p;
436
437 mp = *mdp;
438 while (left == 0) {
439 *mdp = mp = mp->m_next;
440 if (mp == NULL)
441 return (EBADRPC);
442 left = mp->m_len;
443 *dposp = mtod(mp, caddr_t);
444 }
445 if (left >= siz) {
446 *cp2 = *dposp;
447 *dposp += siz;
448 } else if (mp->m_next == NULL) {
449 return (EBADRPC);
450 } else if (siz > MHLEN) {
451 panic("nfs S too big");
452 } else {
453 MGET(mp2, M_WAIT, MT_DATA);
454 mp2->m_next = mp->m_next;
455 mp->m_next = mp2;
456 mp->m_len -= left;
457 mp = mp2;
458 *cp2 = p = mtod(mp, caddr_t);
459 bcopy(*dposp, p, left); /* Copy what was left */
460 siz2 = siz-left;
461 p += left;
462 mp2 = mp->m_next;
463 /* Loop around copying up the siz2 bytes */
464 while (siz2 > 0) {
465 if (mp2 == NULL)
466 return (EBADRPC);
467 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
468 if (xfer > 0) {
469 bcopy(mtod(mp2, caddr_t), p, xfer);
470 NFSMADV(mp2, xfer);
471 mp2->m_len -= xfer;
472 p += xfer;
473 siz2 -= xfer;
474 }
475 if (siz2 > 0)
476 mp2 = mp2->m_next;
477 }
478 mp->m_len = siz;
479 *mdp = mp2;
480 *dposp = mtod(mp2, caddr_t);
481 }
482 return (0);
483 }
484
485 /*
486 * Advance the position in the mbuf chain.
487 */
488 nfs_adv(mdp, dposp, offs, left)
489 struct mbuf **mdp;
490 caddr_t *dposp;
491 int offs;
492 int left;
493 {
494 register struct mbuf *m;
495 register int s;
496
497 m = *mdp;
498 s = left;
499 while (s < offs) {
500 offs -= s;
501 m = m->m_next;
502 if (m == NULL)
503 return (EBADRPC);
504 s = m->m_len;
505 }
506 *mdp = m;
507 *dposp = mtod(m, caddr_t)+offs;
508 return (0);
509 }
510
511 /*
512 * Copy a string into mbufs for the hard cases...
513 */
514 nfsm_strtmbuf(mb, bpos, cp, siz)
515 struct mbuf **mb;
516 char **bpos;
517 char *cp;
518 long siz;
519 {
520 register struct mbuf *m1, *m2;
521 long left, xfer, len, tlen;
522 u_long *tl;
523 int putsize;
524
525 putsize = 1;
526 m2 = *mb;
527 left = M_TRAILINGSPACE(m2);
528 if (left > 0) {
529 tl = ((u_long *)(*bpos));
530 *tl++ = txdr_unsigned(siz);
531 putsize = 0;
532 left -= NFSX_UNSIGNED;
533 m2->m_len += NFSX_UNSIGNED;
534 if (left > 0) {
535 bcopy(cp, (caddr_t) tl, left);
536 siz -= left;
537 cp += left;
538 m2->m_len += left;
539 left = 0;
540 }
541 }
542 /* Loop around adding mbufs */
543 while (siz > 0) {
544 MGET(m1, M_WAIT, MT_DATA);
545 if (siz > MLEN)
546 MCLGET(m1, M_WAIT);
547 m1->m_len = NFSMSIZ(m1);
548 m2->m_next = m1;
549 m2 = m1;
550 tl = mtod(m1, u_long *);
551 tlen = 0;
552 if (putsize) {
553 *tl++ = txdr_unsigned(siz);
554 m1->m_len -= NFSX_UNSIGNED;
555 tlen = NFSX_UNSIGNED;
556 putsize = 0;
557 }
558 if (siz < m1->m_len) {
559 len = nfsm_rndup(siz);
560 xfer = siz;
561 if (xfer < len)
562 *(tl+(xfer>>2)) = 0;
563 } else {
564 xfer = len = m1->m_len;
565 }
566 bcopy(cp, (caddr_t) tl, xfer);
567 m1->m_len = len+tlen;
568 siz -= xfer;
569 cp += xfer;
570 }
571 *mb = m1;
572 *bpos = mtod(m1, caddr_t)+m1->m_len;
573 return (0);
574 }
575
576 /*
577 * Called once to initialize data structures...
578 */
579 nfs_init()
580 {
581 register int i;
582
583 nfsrtt.pos = 0;
584 rpc_vers = txdr_unsigned(RPC_VER2);
585 rpc_call = txdr_unsigned(RPC_CALL);
586 rpc_reply = txdr_unsigned(RPC_REPLY);
587 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
588 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
589 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
590 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
591 rpc_rejectedcred = txdr_unsigned(AUTH_REJECTCRED);
592 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
593 rpc_auth_kerb = txdr_unsigned(RPCAUTH_NQNFS);
594 nfs_vers = txdr_unsigned(NFS_VER2);
595 nfs_prog = txdr_unsigned(NFS_PROG);
596 nfs_true = txdr_unsigned(TRUE);
597 nfs_false = txdr_unsigned(FALSE);
598 /* Loop thru nfs procids */
599 for (i = 0; i < NFS_NPROCS; i++)
600 nfs_procids[i] = txdr_unsigned(i);
601 /* Ensure async daemons disabled */
602 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
603 nfs_iodwant[i] = (struct proc *)0;
604 TAILQ_INIT(&nfs_bufq);
605 nfs_xdrneg1 = txdr_unsigned(-1);
606 nfs_nhinit(); /* Init the nfsnode table */
607 nfsrv_init(0); /* Init server data structures */
608 nfsrv_initcache(); /* Init the server request cache */
609
610 /*
611 * Initialize the nqnfs server stuff.
612 */
613 if (nqnfsstarttime == 0) {
614 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
615 + nqsrv_clockskew + nqsrv_writeslack;
616 NQLOADNOVRAM(nqnfsstarttime);
617 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
618 nqnfs_vers = txdr_unsigned(NQNFS_VER1);
619 nqthead.th_head[0] = &nqthead;
620 nqthead.th_head[1] = &nqthead;
621 nqfhead = hashinit(NQLCHSZ, M_NQLEASE, &nqfheadhash);
622 }
623
624 /*
625 * Initialize reply list and start timer
626 */
627 nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
628 nfs_timer();
629 }
630
631 /*
632 * Attribute cache routines.
633 * nfs_loadattrcache() - loads or updates the cache contents from attributes
634 * that are on the mbuf list
635 * nfs_getattrcache() - returns valid attributes if found in cache, returns
636 * error otherwise
637 */
638
639 /*
640 * Load the attribute cache (that lives in the nfsnode entry) with
641 * the values on the mbuf list and
642 * Iff vap not NULL
643 * copy the attributes to *vaper
644 */
645 nfs_loadattrcache(vpp, mdp, dposp, vaper)
646 struct vnode **vpp;
647 struct mbuf **mdp;
648 caddr_t *dposp;
649 struct vattr *vaper;
650 {
651 register struct vnode *vp = *vpp;
652 register struct vattr *vap;
653 register struct nfsv2_fattr *fp;
654 extern int (**spec_nfsv2nodeop_p)();
655 register struct nfsnode *np, *nq, **nhpp;
656 register long t1;
657 caddr_t dpos, cp2;
658 int error = 0, isnq;
659 struct mbuf *md;
660 enum vtype vtyp;
661 u_short vmode;
662 long rdev;
663 struct timespec mtime;
664 struct vnode *nvp;
665
666 md = *mdp;
667 dpos = *dposp;
668 t1 = (mtod(md, caddr_t) + md->m_len) - dpos;
669 isnq = (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS);
670 if (error = nfsm_disct(&md, &dpos, NFSX_FATTR(isnq), t1, &cp2))
671 return (error);
672 fp = (struct nfsv2_fattr *)cp2;
673 vtyp = nfstov_type(fp->fa_type);
674 vmode = fxdr_unsigned(u_short, fp->fa_mode);
675 if (vtyp == VNON || vtyp == VREG)
676 vtyp = IFTOVT(vmode);
677 if (isnq) {
678 rdev = fxdr_unsigned(long, fp->fa_nqrdev);
679 fxdr_nqtime(&fp->fa_nqmtime, &mtime);
680 } else {
681 rdev = fxdr_unsigned(long, fp->fa_nfsrdev);
682 fxdr_nfstime(&fp->fa_nfsmtime, &mtime);
683 }
684 /*
685 * If v_type == VNON it is a new node, so fill in the v_type,
686 * n_mtime fields. Check to see if it represents a special
687 * device, and if so, check for a possible alias. Once the
688 * correct vnode has been obtained, fill in the rest of the
689 * information.
690 */
691 np = VTONFS(vp);
692 if (vp->v_type == VNON) {
693 if (vtyp == VCHR && rdev == 0xffffffff)
694 vp->v_type = vtyp = VFIFO;
695 else
696 vp->v_type = vtyp;
697 if (vp->v_type == VFIFO) {
698 #ifdef FIFO
699 extern int (**fifo_nfsv2nodeop_p)();
700 vp->v_op = fifo_nfsv2nodeop_p;
701 #else
702 return (EOPNOTSUPP);
703 #endif /* FIFO */
704 }
705 if (vp->v_type == VCHR || vp->v_type == VBLK) {
706 vp->v_op = spec_nfsv2nodeop_p;
707 if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) {
708 /*
709 * Discard unneeded vnode, but save its nfsnode.
710 */
711 if (nq = np->n_forw)
712 nq->n_back = np->n_back;
713 *np->n_back = nq;
714 nvp->v_data = vp->v_data;
715 vp->v_data = NULL;
716 vp->v_op = spec_vnodeop_p;
717 vrele(vp);
718 vgone(vp);
719 /*
720 * Reinitialize aliased node.
721 */
722 np->n_vnode = nvp;
723 nhpp = (struct nfsnode **)nfs_hash(&np->n_fh);
724 if (nq = *nhpp)
725 nq->n_back = &np->n_forw;
726 np->n_forw = nq;
727 np->n_back = nhpp;
728 *nhpp = np;
729 *vpp = vp = nvp;
730 }
731 }
732 np->n_mtime = mtime.ts_sec;
733 }
734 vap = &np->n_vattr;
735 vap->va_type = vtyp;
736 vap->va_mode = (vmode & 07777);
737 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
738 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
739 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
740 vap->va_rdev = (dev_t)rdev;
741 vap->va_mtime = mtime;
742 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
743 if (isnq) {
744 fxdr_hyper(&fp->fa_nqsize, &vap->va_size);
745 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nqblocksize);
746 fxdr_hyper(&fp->fa_nqbytes, &vap->va_bytes);
747 vap->va_fileid = fxdr_unsigned(long, fp->fa_nqfileid);
748 fxdr_nqtime(&fp->fa_nqatime, &vap->va_atime);
749 vap->va_flags = fxdr_unsigned(u_long, fp->fa_nqflags);
750 fxdr_nqtime(&fp->fa_nqctime, &vap->va_ctime);
751 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nqgen);
752 fxdr_hyper(&fp->fa_nqfilerev, &vap->va_filerev);
753 } else {
754 vap->va_size = fxdr_unsigned(u_long, fp->fa_nfssize);
755 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nfsblocksize);
756 vap->va_bytes = fxdr_unsigned(long, fp->fa_nfsblocks) * NFS_FABLKSIZE;
757 vap->va_fileid = fxdr_unsigned(long, fp->fa_nfsfileid);
758 fxdr_nfstime(&fp->fa_nfsatime, &vap->va_atime);
759 vap->va_flags = 0;
760 vap->va_ctime.ts_sec = fxdr_unsigned(long, fp->fa_nfsctime.nfs_sec);
761 vap->va_ctime.ts_nsec = 0;
762 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nfsctime.nfs_usec);
763 vap->va_filerev = 0;
764 }
765 if (vap->va_size != np->n_size) {
766 if (vap->va_type == VREG) {
767 if (np->n_flag & NMODIFIED) {
768 if (vap->va_size < np->n_size)
769 vap->va_size = np->n_size;
770 else
771 np->n_size = vap->va_size;
772 } else
773 np->n_size = vap->va_size;
774 vnode_pager_setsize(vp, (u_long)np->n_size);
775 } else
776 np->n_size = vap->va_size;
777 }
778 np->n_attrstamp = time.tv_sec;
779 *dposp = dpos;
780 *mdp = md;
781 if (vaper != NULL) {
782 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
783 #ifdef notdef
784 if ((np->n_flag & NMODIFIED) && np->n_size > vap->va_size)
785 if (np->n_size > vap->va_size)
786 vaper->va_size = np->n_size;
787 #endif
788 if (np->n_flag & NCHG) {
789 if (np->n_flag & NACC) {
790 vaper->va_atime.ts_sec = np->n_atim.tv_sec;
791 vaper->va_atime.ts_nsec =
792 np->n_atim.tv_usec * 1000;
793 }
794 if (np->n_flag & NUPD) {
795 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec;
796 vaper->va_mtime.ts_nsec =
797 np->n_mtim.tv_usec * 1000;
798 }
799 }
800 }
801 return (0);
802 }
803
804 /*
805 * Check the time stamp
806 * If the cache is valid, copy contents to *vap and return 0
807 * otherwise return an error
808 */
809 nfs_getattrcache(vp, vaper)
810 register struct vnode *vp;
811 struct vattr *vaper;
812 {
813 register struct nfsnode *np = VTONFS(vp);
814 register struct vattr *vap;
815
816 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQLOOKLEASE) {
817 if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) {
818 nfsstats.attrcache_misses++;
819 return (ENOENT);
820 }
821 } else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
822 nfsstats.attrcache_misses++;
823 return (ENOENT);
824 }
825 nfsstats.attrcache_hits++;
826 vap = &np->n_vattr;
827 if (vap->va_size != np->n_size) {
828 if (vap->va_type == VREG) {
829 if (np->n_flag & NMODIFIED) {
830 if (vap->va_size < np->n_size)
831 vap->va_size = np->n_size;
832 else
833 np->n_size = vap->va_size;
834 } else
835 np->n_size = vap->va_size;
836 vnode_pager_setsize(vp, (u_long)np->n_size);
837 } else
838 np->n_size = vap->va_size;
839 }
840 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
841 #ifdef notdef
842 if ((np->n_flag & NMODIFIED) == 0) {
843 np->n_size = vaper->va_size;
844 vnode_pager_setsize(vp, (u_long)np->n_size);
845 } else if (np->n_size > vaper->va_size)
846 if (np->n_size > vaper->va_size)
847 vaper->va_size = np->n_size;
848 #endif
849 if (np->n_flag & NCHG) {
850 if (np->n_flag & NACC) {
851 vaper->va_atime.ts_sec = np->n_atim.tv_sec;
852 vaper->va_atime.ts_nsec = np->n_atim.tv_usec * 1000;
853 }
854 if (np->n_flag & NUPD) {
855 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec;
856 vaper->va_mtime.ts_nsec = np->n_mtim.tv_usec * 1000;
857 }
858 }
859 return (0);
860 }
861
862 /*
863 * Set up nameidata for a lookup() call and do it
864 */
865 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p)
866 register struct nameidata *ndp;
867 fhandle_t *fhp;
868 int len;
869 struct nfssvc_sock *slp;
870 struct mbuf *nam;
871 struct mbuf **mdp;
872 caddr_t *dposp;
873 struct proc *p;
874 {
875 register int i, rem;
876 register struct mbuf *md;
877 register char *fromcp, *tocp;
878 struct vnode *dp;
879 int error, rdonly;
880 struct componentname *cnp = &ndp->ni_cnd;
881
882 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
883 /*
884 * Copy the name from the mbuf list to ndp->ni_pnbuf
885 * and set the various ndp fields appropriately.
886 */
887 fromcp = *dposp;
888 tocp = cnp->cn_pnbuf;
889 md = *mdp;
890 rem = mtod(md, caddr_t) + md->m_len - fromcp;
891 cnp->cn_hash = 0;
892 for (i = 0; i < len; i++) {
893 while (rem == 0) {
894 md = md->m_next;
895 if (md == NULL) {
896 error = EBADRPC;
897 goto out;
898 }
899 fromcp = mtod(md, caddr_t);
900 rem = md->m_len;
901 }
902 if (*fromcp == '\0' || *fromcp == '/') {
903 error = EINVAL;
904 goto out;
905 }
906 cnp->cn_hash += (unsigned char)*fromcp;
907 *tocp++ = *fromcp++;
908 rem--;
909 }
910 *tocp = '\0';
911 *mdp = md;
912 *dposp = fromcp;
913 len = nfsm_rndup(len)-len;
914 if (len > 0) {
915 if (rem >= len)
916 *dposp += len;
917 else if (error = nfs_adv(mdp, dposp, len, rem))
918 goto out;
919 }
920 ndp->ni_pathlen = tocp - cnp->cn_pnbuf;
921 cnp->cn_nameptr = cnp->cn_pnbuf;
922 /*
923 * Extract and set starting directory.
924 */
925 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
926 nam, &rdonly))
927 goto out;
928 if (dp->v_type != VDIR) {
929 vrele(dp);
930 error = ENOTDIR;
931 goto out;
932 }
933 ndp->ni_startdir = dp;
934 if (rdonly)
935 cnp->cn_flags |= (NOCROSSMOUNT | RDONLY);
936 else
937 cnp->cn_flags |= NOCROSSMOUNT;
938 /*
939 * And call lookup() to do the real work
940 */
941 cnp->cn_proc = p;
942 if (error = lookup(ndp))
943 goto out;
944 /*
945 * Check for encountering a symbolic link
946 */
947 if (cnp->cn_flags & ISSYMLINK) {
948 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
949 vput(ndp->ni_dvp);
950 else
951 vrele(ndp->ni_dvp);
952 vput(ndp->ni_vp);
953 ndp->ni_vp = NULL;
954 error = EINVAL;
955 goto out;
956 }
957 /*
958 * Check for saved name request
959 */
960 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
961 cnp->cn_flags |= HASBUF;
962 return (0);
963 }
964 out:
965 FREE(cnp->cn_pnbuf, M_NAMEI);
966 return (error);
967 }
968
969 /*
970 * A fiddled version of m_adj() that ensures null fill to a long
971 * boundary and only trims off the back end
972 */
973 void
974 nfsm_adj(mp, len, nul)
975 struct mbuf *mp;
976 register int len;
977 int nul;
978 {
979 register struct mbuf *m;
980 register int count, i;
981 register char *cp;
982
983 /*
984 * Trim from tail. Scan the mbuf chain,
985 * calculating its length and finding the last mbuf.
986 * If the adjustment only affects this mbuf, then just
987 * adjust and return. Otherwise, rescan and truncate
988 * after the remaining size.
989 */
990 count = 0;
991 m = mp;
992 for (;;) {
993 count += m->m_len;
994 if (m->m_next == (struct mbuf *)0)
995 break;
996 m = m->m_next;
997 }
998 if (m->m_len > len) {
999 m->m_len -= len;
1000 if (nul > 0) {
1001 cp = mtod(m, caddr_t)+m->m_len-nul;
1002 for (i = 0; i < nul; i++)
1003 *cp++ = '\0';
1004 }
1005 return;
1006 }
1007 count -= len;
1008 if (count < 0)
1009 count = 0;
1010 /*
1011 * Correct length for chain is "count".
1012 * Find the mbuf with last data, adjust its length,
1013 * and toss data from remaining mbufs on chain.
1014 */
1015 for (m = mp; m; m = m->m_next) {
1016 if (m->m_len >= count) {
1017 m->m_len = count;
1018 if (nul > 0) {
1019 cp = mtod(m, caddr_t)+m->m_len-nul;
1020 for (i = 0; i < nul; i++)
1021 *cp++ = '\0';
1022 }
1023 break;
1024 }
1025 count -= m->m_len;
1026 }
1027 while (m = m->m_next)
1028 m->m_len = 0;
1029 }
1030
1031 /*
1032 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1033 * - look up fsid in mount list (if not found ret error)
1034 * - get vp and export rights by calling VFS_FHTOVP()
1035 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1036 * - if not lockflag unlock it with VOP_UNLOCK()
1037 */
1038 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp)
1039 fhandle_t *fhp;
1040 int lockflag;
1041 struct vnode **vpp;
1042 struct ucred *cred;
1043 struct nfssvc_sock *slp;
1044 struct mbuf *nam;
1045 int *rdonlyp;
1046 {
1047 register struct mount *mp;
1048 register struct nfsuid *uidp;
1049 register int i;
1050 struct ucred *credanon;
1051 int error, exflags;
1052
1053 *vpp = (struct vnode *)0;
1054 if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
1055 return (ESTALE);
1056 if (error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon))
1057 return (error);
1058 /*
1059 * Check/setup credentials.
1060 */
1061 if (exflags & MNT_EXKERB) {
1062 uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)];
1063 while (uidp) {
1064 if (uidp->nu_uid == cred->cr_uid)
1065 break;
1066 uidp = uidp->nu_hnext;
1067 }
1068 if (uidp) {
1069 cred->cr_uid = uidp->nu_cr.cr_uid;
1070 for (i = 0; i < uidp->nu_cr.cr_ngroups; i++)
1071 cred->cr_groups[i] = uidp->nu_cr.cr_groups[i];
1072 } else {
1073 vput(*vpp);
1074 return (NQNFS_AUTHERR);
1075 }
1076 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1077 cred->cr_uid = credanon->cr_uid;
1078 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1079 cred->cr_groups[i] = credanon->cr_groups[i];
1080 }
1081 if (exflags & MNT_EXRDONLY)
1082 *rdonlyp = 1;
1083 else
1084 *rdonlyp = 0;
1085 if (!lockflag)
1086 VOP_UNLOCK(*vpp);
1087 return (0);
1088 }
1089
1090 /*
1091 * This function compares two net addresses by family and returns TRUE
1092 * if they are the same host.
1093 * If there is any doubt, return FALSE.
1094 * The AF_INET family is handled as a special case so that address mbufs
1095 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1096 */
1097 netaddr_match(family, haddr, nam)
1098 int family;
1099 union nethostaddr *haddr;
1100 struct mbuf *nam;
1101 {
1102 register struct sockaddr_in *inetaddr;
1103
1104 switch (family) {
1105 case AF_INET:
1106 inetaddr = mtod(nam, struct sockaddr_in *);
1107 if (inetaddr->sin_family == AF_INET &&
1108 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1109 return (1);
1110 break;
1111 #ifdef ISO
1112 case AF_ISO:
1113 {
1114 register struct sockaddr_iso *isoaddr1, *isoaddr2;
1115
1116 isoaddr1 = mtod(nam, struct sockaddr_iso *);
1117 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
1118 if (isoaddr1->siso_family == AF_ISO &&
1119 isoaddr1->siso_nlen > 0 &&
1120 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1121 SAME_ISOADDR(isoaddr1, isoaddr2))
1122 return (1);
1123 break;
1124 }
1125 #endif /* ISO */
1126 default:
1127 break;
1128 };
1129 return (0);
1130 }
1131