nfs_subs.c revision 1.3 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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 * from: @(#)nfs_subs.c 7.41 (Berkeley) 5/15/91
37 * $Id: nfs_subs.c,v 1.3 1993/05/21 07:38:05 cgd Exp $
38 */
39
40 /*
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
44 */
45 #include "param.h"
46 #include "proc.h"
47 #include "filedesc.h"
48 #include "systm.h"
49 #include "kernel.h"
50 #include "mount.h"
51 #include "file.h"
52 #include "vnode.h"
53 #include "namei.h"
54 #include "mbuf.h"
55
56 #include "../ufs/quota.h"
57 #include "../ufs/inode.h"
58
59 #include "rpcv2.h"
60 #include "nfsv2.h"
61 #include "nfsnode.h"
62 #include "nfs.h"
63 #include "nfsiom.h"
64 #include "xdr_subs.h"
65 #include "nfsm_subs.h"
66 #include "nfscompress.h"
67
68 #define TRUE 1
69 #define FALSE 0
70
71 /*
72 * Data items converted to xdr at startup, since they are constant
73 * This is kinda hokey, but may save a little time doing byte swaps
74 */
75 u_long nfs_procids[NFS_NPROCS];
76 u_long nfs_xdrneg1;
77 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
78 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
79 u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
80 /* And other global data */
81 static u_long *rpc_uidp = (u_long *)0;
82 static u_long nfs_xid = 1;
83 static char *rpc_unixauth;
84 extern long hostid;
85
86 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
87 extern struct nfsreq nfsreqh;
88
89 /* Function ret types */
90 static char *nfs_unixauth();
91
92 /*
93 * Maximum number of groups passed through to NFS server.
94 * According to RFC1057 it should be 16.
95 * For release 3.X systems, the maximum value is 8.
96 * For some other servers, the maximum value is 10.
97 */
98 int numgrps = 8;
99
100 /*
101 * Create the header for an rpc request packet
102 * The function nfs_unixauth() creates a unix style authorization string
103 * and returns a ptr to it.
104 * The hsiz is the size of the rest of the nfs request header.
105 * (just used to decide if a cluster is a good idea)
106 * nb: Note that the prog, vers and procid args are already in xdr byte order
107 */
108 struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
109 u_long prog;
110 u_long vers;
111 u_long procid;
112 struct ucred *cred;
113 int hsiz;
114 caddr_t *bpos;
115 struct mbuf **mb;
116 u_long *retxid;
117 {
118 register struct mbuf *mreq, *m;
119 register u_long *tl;
120 struct mbuf *m1;
121 char *ap;
122 int asiz, siz;
123
124 NFSMGETHDR(mreq);
125 asiz = ((((cred->cr_ngroups - 1) > numgrps) ? numgrps :
126 (cred->cr_ngroups - 1)) << 2);
127 #ifdef FILLINHOST
128 asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
129 #else
130 asiz += 9*NFSX_UNSIGNED;
131 #endif
132
133 /* If we need a lot, alloc a cluster ?? */
134 if ((asiz+hsiz+RPC_SIZ) > MHLEN)
135 MCLGET(mreq, M_WAIT);
136 mreq->m_len = NFSMSIZ(mreq);
137 siz = mreq->m_len;
138 m1 = mreq;
139 /*
140 * Alloc enough mbufs
141 * We do it now to avoid all sleeps after the call to nfs_unixauth()
142 */
143 while ((asiz+RPC_SIZ) > siz) {
144 MGET(m, M_WAIT, MT_DATA);
145 m1->m_next = m;
146 m->m_len = MLEN;
147 siz += MLEN;
148 m1 = m;
149 }
150 tl = mtod(mreq, u_long *);
151 *tl++ = *retxid = txdr_unsigned(++nfs_xid);
152 *tl++ = rpc_call;
153 *tl++ = rpc_vers;
154 *tl++ = prog;
155 *tl++ = vers;
156 *tl++ = procid;
157
158 /* Now we can call nfs_unixauth() and copy it in */
159 ap = nfs_unixauth(cred);
160 m = mreq;
161 siz = m->m_len-RPC_SIZ;
162 if (asiz <= siz) {
163 bcopy(ap, (caddr_t)tl, asiz);
164 m->m_len = asiz+RPC_SIZ;
165 } else {
166 bcopy(ap, (caddr_t)tl, siz);
167 ap += siz;
168 asiz -= siz;
169 while (asiz > 0) {
170 siz = (asiz > MLEN) ? MLEN : asiz;
171 m = m->m_next;
172 bcopy(ap, mtod(m, caddr_t), siz);
173 m->m_len = siz;
174 asiz -= siz;
175 ap += siz;
176 }
177 }
178
179 /* Finally, return values */
180 *mb = m;
181 *bpos = mtod(m, caddr_t)+m->m_len;
182 return (mreq);
183 }
184
185 /*
186 * copies mbuf chain to the uio scatter/gather list
187 */
188 nfsm_mbuftouio(mrep, uiop, siz, dpos)
189 struct mbuf **mrep;
190 register struct uio *uiop;
191 int siz;
192 caddr_t *dpos;
193 {
194 register char *mbufcp, *uiocp;
195 register int xfer, left, len;
196 register struct mbuf *mp;
197 long uiosiz, rem;
198 int error = 0;
199
200 mp = *mrep;
201 mbufcp = *dpos;
202 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
203 rem = nfsm_rndup(siz)-siz;
204 while (siz > 0) {
205 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
206 return (EFBIG);
207 left = uiop->uio_iov->iov_len;
208 uiocp = uiop->uio_iov->iov_base;
209 if (left > siz)
210 left = siz;
211 uiosiz = left;
212 while (left > 0) {
213 while (len == 0) {
214 mp = mp->m_next;
215 if (mp == NULL)
216 return (EBADRPC);
217 mbufcp = mtod(mp, caddr_t);
218 len = mp->m_len;
219 }
220 xfer = (left > len) ? len : left;
221 #ifdef notdef
222 /* Not Yet.. */
223 if (uiop->uio_iov->iov_op != NULL)
224 (*(uiop->uio_iov->iov_op))
225 (mbufcp, uiocp, xfer);
226 else
227 #endif
228 if (uiop->uio_segflg == UIO_SYSSPACE)
229 bcopy(mbufcp, uiocp, xfer);
230 else
231 copyout(mbufcp, uiocp, xfer);
232 left -= xfer;
233 len -= xfer;
234 mbufcp += xfer;
235 uiocp += xfer;
236 uiop->uio_offset += xfer;
237 uiop->uio_resid -= xfer;
238 }
239 if (uiop->uio_iov->iov_len <= siz) {
240 uiop->uio_iovcnt--;
241 uiop->uio_iov++;
242 } else {
243 uiop->uio_iov->iov_base += uiosiz;
244 uiop->uio_iov->iov_len -= uiosiz;
245 }
246 siz -= uiosiz;
247 }
248 *dpos = mbufcp;
249 *mrep = mp;
250 if (rem > 0) {
251 if (len < rem)
252 error = nfs_adv(mrep, dpos, rem, len);
253 else
254 *dpos += rem;
255 }
256 return (error);
257 }
258
259 /*
260 * copies a uio scatter/gather list to an mbuf chain...
261 */
262 nfsm_uiotombuf(uiop, mq, siz, bpos)
263 register struct uio *uiop;
264 struct mbuf **mq;
265 int siz;
266 caddr_t *bpos;
267 {
268 register char *uiocp;
269 register struct mbuf *mp, *mp2;
270 register int xfer, left, len;
271 int uiosiz, clflg, rem;
272 char *cp;
273
274 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
275 clflg = 1;
276 else
277 clflg = 0;
278 rem = nfsm_rndup(siz)-siz;
279 mp2 = *mq;
280 while (siz > 0) {
281 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
282 return (EINVAL);
283 left = uiop->uio_iov->iov_len;
284 uiocp = uiop->uio_iov->iov_base;
285 if (left > siz)
286 left = siz;
287 uiosiz = left;
288 while (left > 0) {
289 MGET(mp, M_WAIT, MT_DATA);
290 if (clflg)
291 MCLGET(mp, M_WAIT);
292 mp->m_len = NFSMSIZ(mp);
293 mp2->m_next = mp;
294 mp2 = mp;
295 xfer = (left > mp->m_len) ? mp->m_len : left;
296 #ifdef notdef
297 /* Not Yet.. */
298 if (uiop->uio_iov->iov_op != NULL)
299 (*(uiop->uio_iov->iov_op))
300 (uiocp, mtod(mp, caddr_t), xfer);
301 else
302 #endif
303 if (uiop->uio_segflg == UIO_SYSSPACE)
304 bcopy(uiocp, mtod(mp, caddr_t), xfer);
305 else
306 copyin(uiocp, mtod(mp, caddr_t), xfer);
307 len = mp->m_len;
308 mp->m_len = xfer;
309 left -= xfer;
310 uiocp += xfer;
311 uiop->uio_offset += xfer;
312 uiop->uio_resid -= xfer;
313 }
314 if (uiop->uio_iov->iov_len <= siz) {
315 uiop->uio_iovcnt--;
316 uiop->uio_iov++;
317 } else {
318 uiop->uio_iov->iov_base += uiosiz;
319 uiop->uio_iov->iov_len -= uiosiz;
320 }
321 siz -= uiosiz;
322 }
323 if (rem > 0) {
324 if (rem > (len-mp->m_len)) {
325 MGET(mp, M_WAIT, MT_DATA);
326 mp->m_len = 0;
327 mp2->m_next = mp;
328 }
329 cp = mtod(mp, caddr_t)+mp->m_len;
330 for (left = 0; left < rem; left++)
331 *cp++ = '\0';
332 mp->m_len += rem;
333 *bpos = cp;
334 } else
335 *bpos = mtod(mp, caddr_t)+mp->m_len;
336 *mq = mp;
337 return (0);
338 }
339
340 /*
341 * Help break down an mbuf chain by setting the first siz bytes contiguous
342 * pointed to by returned val.
343 * If Updateflg == True we can overwrite the first part of the mbuf data
344 * This is used by the macros nfsm_disect and nfsm_disecton for tough
345 * cases. (The macros use the vars. dpos and dpos2)
346 */
347 nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
348 struct mbuf **mdp;
349 caddr_t *dposp;
350 int siz;
351 int left;
352 int updateflg;
353 caddr_t *cp2;
354 {
355 register struct mbuf *mp, *mp2;
356 register int siz2, xfer;
357 register caddr_t tl;
358
359 mp = *mdp;
360 while (left == 0) {
361 *mdp = mp = mp->m_next;
362 if (mp == NULL)
363 return (EBADRPC);
364 left = mp->m_len;
365 *dposp = mtod(mp, caddr_t);
366 }
367 if (left >= siz) {
368 *cp2 = *dposp;
369 *dposp += siz;
370 } else if (mp->m_next == NULL) {
371 return (EBADRPC);
372 } else if (siz > MHLEN) {
373 panic("nfs S too big");
374 } else {
375 /* Iff update, you can overwrite, else must alloc new mbuf */
376 if (updateflg) {
377 NFSMINOFF(mp);
378 } else {
379 MGET(mp2, M_WAIT, MT_DATA);
380 mp2->m_next = mp->m_next;
381 mp->m_next = mp2;
382 mp->m_len -= left;
383 mp = mp2;
384 }
385 *cp2 = tl = mtod(mp, caddr_t);
386 bcopy(*dposp, tl, left); /* Copy what was left */
387 siz2 = siz-left;
388 tl += left;
389 mp2 = mp->m_next;
390 /* Loop around copying up the siz2 bytes */
391 while (siz2 > 0) {
392 if (mp2 == NULL)
393 return (EBADRPC);
394 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
395 if (xfer > 0) {
396 bcopy(mtod(mp2, caddr_t), tl, xfer);
397 NFSMADV(mp2, xfer);
398 mp2->m_len -= xfer;
399 tl += xfer;
400 siz2 -= xfer;
401 }
402 if (siz2 > 0)
403 mp2 = mp2->m_next;
404 }
405 mp->m_len = siz;
406 *mdp = mp2;
407 *dposp = mtod(mp2, caddr_t);
408 }
409 return (0);
410 }
411
412 /*
413 * Advance the position in the mbuf chain.
414 */
415 nfs_adv(mdp, dposp, offs, left)
416 struct mbuf **mdp;
417 caddr_t *dposp;
418 int offs;
419 int left;
420 {
421 register struct mbuf *m;
422 register int s;
423
424 m = *mdp;
425 s = left;
426 while (s < offs) {
427 offs -= s;
428 m = m->m_next;
429 if (m == NULL)
430 return (EBADRPC);
431 s = m->m_len;
432 }
433 *mdp = m;
434 *dposp = mtod(m, caddr_t)+offs;
435 return (0);
436 }
437
438 /*
439 * Copy a string into mbufs for the hard cases...
440 */
441 nfsm_strtmbuf(mb, bpos, cp, siz)
442 struct mbuf **mb;
443 char **bpos;
444 char *cp;
445 long siz;
446 {
447 register struct mbuf *m1, *m2;
448 long left, xfer, len, tlen;
449 u_long *tl;
450 int putsize;
451
452 putsize = 1;
453 m2 = *mb;
454 left = NFSMSIZ(m2)-m2->m_len;
455 if (left > 0) {
456 tl = ((u_long *)(*bpos));
457 *tl++ = txdr_unsigned(siz);
458 putsize = 0;
459 left -= NFSX_UNSIGNED;
460 m2->m_len += NFSX_UNSIGNED;
461 if (left > 0) {
462 bcopy(cp, (caddr_t) tl, left);
463 siz -= left;
464 cp += left;
465 m2->m_len += left;
466 left = 0;
467 }
468 }
469 /* Loop arround adding mbufs */
470 while (siz > 0) {
471 MGET(m1, M_WAIT, MT_DATA);
472 if (siz > MLEN)
473 MCLGET(m1, M_WAIT);
474 m1->m_len = NFSMSIZ(m1);
475 m2->m_next = m1;
476 m2 = m1;
477 tl = mtod(m1, u_long *);
478 tlen = 0;
479 if (putsize) {
480 *tl++ = txdr_unsigned(siz);
481 m1->m_len -= NFSX_UNSIGNED;
482 tlen = NFSX_UNSIGNED;
483 putsize = 0;
484 }
485 if (siz < m1->m_len) {
486 len = nfsm_rndup(siz);
487 xfer = siz;
488 if (xfer < len)
489 *(tl+(xfer>>2)) = 0;
490 } else {
491 xfer = len = m1->m_len;
492 }
493 bcopy(cp, (caddr_t) tl, xfer);
494 m1->m_len = len+tlen;
495 siz -= xfer;
496 cp += xfer;
497 }
498 *mb = m1;
499 *bpos = mtod(m1, caddr_t)+m1->m_len;
500 return (0);
501 }
502
503 /*
504 * Called once to initialize data structures...
505 */
506 nfs_init()
507 {
508 register int i;
509
510 rpc_vers = txdr_unsigned(RPC_VER2);
511 rpc_call = txdr_unsigned(RPC_CALL);
512 rpc_reply = txdr_unsigned(RPC_REPLY);
513 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
514 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
515 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
516 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
517 nfs_vers = txdr_unsigned(NFS_VER2);
518 nfs_prog = txdr_unsigned(NFS_PROG);
519 nfs_true = txdr_unsigned(TRUE);
520 nfs_false = txdr_unsigned(FALSE);
521 /* Loop thru nfs procids */
522 for (i = 0; i < NFS_NPROCS; i++)
523 nfs_procids[i] = txdr_unsigned(i);
524 /* Ensure async daemons disabled */
525 nfs_xdrneg1 = txdr_unsigned(-1);
526 #ifdef NFSCLIENT
527 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
528 nfs_iodwant[i] = (struct proc *)0;
529 nfs_nhinit(); /* Init the nfsnode table */
530 #endif /* NFSCLIENT */
531 #ifdef NFSSERVER
532 nfsrv_initcache(); /* Init the server request cache */
533 #endif /*NFSSERVER */
534 /*
535 * Initialize reply list and start timer
536 */
537 nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
538 nfs_timer();
539 }
540
541 /*
542 * Fill in the rest of the rpc_unixauth and return it
543 */
544 static char *nfs_unixauth(cr)
545 register struct ucred *cr;
546 {
547 register u_long *tl;
548 register int i;
549 int ngr;
550
551 /* Maybe someday there should be a cache of AUTH_SHORT's */
552 if ((tl = rpc_uidp) == NULL) {
553 #ifdef FILLINHOST
554 i = nfsm_rndup(hostnamelen)+(25*NFSX_UNSIGNED);
555 #else
556 i = 25*NFSX_UNSIGNED;
557 #endif
558 MALLOC(tl, u_long *, i, M_TEMP, M_WAITOK);
559 bzero((caddr_t)tl, i);
560 rpc_unixauth = (caddr_t)tl;
561 *tl++ = txdr_unsigned(RPCAUTH_UNIX);
562 tl++; /* Fill in size later */
563 *tl++ = hostid;
564 #ifdef FILLINHOST
565 *tl++ = txdr_unsigned(hostnamelen);
566 i = nfsm_rndup(hostnamelen);
567 bcopy(hostname, (caddr_t)tl, hostnamelen);
568 tl += (i>>2);
569 #else
570 *tl++ = 0;
571 #endif
572 rpc_uidp = tl;
573 }
574 *tl++ = txdr_unsigned(cr->cr_uid);
575 *tl++ = txdr_unsigned(cr->cr_groups[0]);
576 ngr = ((cr->cr_ngroups - 1) > numgrps) ? numgrps : (cr->cr_ngroups - 1);
577 *tl++ = txdr_unsigned(ngr);
578 for (i = 1; i <= ngr; i++)
579 *tl++ = txdr_unsigned(cr->cr_groups[i]);
580 /* And add the AUTH_NULL */
581 *tl++ = 0;
582 *tl = 0;
583 i = (((caddr_t)tl)-rpc_unixauth)-12;
584 tl = (u_long *)(rpc_unixauth+4);
585 *tl = txdr_unsigned(i);
586 return (rpc_unixauth);
587 }
588
589 /*
590 * Set up nameidata for a namei() call and do it
591 */
592 nfs_namei(ndp, fhp, len, mdp, dposp, p)
593 register struct nameidata *ndp;
594 fhandle_t *fhp;
595 int len;
596 struct mbuf **mdp;
597 caddr_t *dposp;
598 struct proc *p;
599 {
600 register int i, rem;
601 register struct mbuf *md;
602 register char *fromcp, *tocp;
603 struct vnode *dp;
604 int flag;
605 int error;
606
607 flag = ndp->ni_nameiop & OPMASK;
608 MALLOC(ndp->ni_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
609 /*
610 * Copy the name from the mbuf list to ndp->ni_pnbuf
611 * and set the various ndp fields appropriately.
612 */
613 fromcp = *dposp;
614 tocp = ndp->ni_pnbuf;
615 md = *mdp;
616 rem = mtod(md, caddr_t) + md->m_len - fromcp;
617 ndp->ni_hash = 0;
618 for (i = 0; i < len; i++) {
619 while (rem == 0) {
620 md = md->m_next;
621 if (md == NULL) {
622 error = EBADRPC;
623 goto out;
624 }
625 fromcp = mtod(md, caddr_t);
626 rem = md->m_len;
627 }
628 if (*fromcp == '\0' || *fromcp == '/') {
629 error = EINVAL;
630 goto out;
631 }
632 if (*fromcp & 0200)
633 if ((*fromcp&0377) == ('/'|0200) || flag != DELETE) {
634 error = EINVAL;
635 goto out;
636 }
637 ndp->ni_hash += (unsigned char)*fromcp;
638 *tocp++ = *fromcp++;
639 rem--;
640 }
641 *tocp = '\0';
642 *mdp = md;
643 *dposp = fromcp;
644 len = nfsm_rndup(len)-len;
645 if (len > 0) {
646 if (rem >= len)
647 *dposp += len;
648 else if (error = nfs_adv(mdp, dposp, len, rem))
649 goto out;
650 }
651 ndp->ni_pathlen = tocp - ndp->ni_pnbuf;
652 ndp->ni_ptr = ndp->ni_pnbuf;
653 /*
654 * Extract and set starting directory.
655 */
656 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred))
657 goto out;
658 if (dp->v_type != VDIR) {
659 vrele(dp);
660 error = ENOTDIR;
661 goto out;
662 }
663 ndp->ni_startdir = dp;
664 ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE);
665 /*
666 * And call lookup() to do the real work
667 */
668 if (error = lookup(ndp, p))
669 goto out;
670 /*
671 * Check for encountering a symbolic link
672 */
673 if (ndp->ni_more) {
674 if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1)
675 vput(ndp->ni_dvp);
676 else
677 vrele(ndp->ni_dvp);
678 vput(ndp->ni_vp);
679 ndp->ni_vp = NULL;
680 error = EINVAL;
681 goto out;
682 }
683 /*
684 * Check for saved name request
685 */
686 if (ndp->ni_nameiop & (SAVENAME | SAVESTART)) {
687 ndp->ni_nameiop |= HASBUF;
688 return (0);
689 }
690 out:
691 FREE(ndp->ni_pnbuf, M_NAMEI);
692 return (error);
693 }
694
695 /*
696 * A fiddled version of m_adj() that ensures null fill to a long
697 * boundary and only trims off the back end
698 */
699 nfsm_adj(mp, len, nul)
700 struct mbuf *mp;
701 register int len;
702 int nul;
703 {
704 register struct mbuf *m;
705 register int count, i;
706 register char *cp;
707
708 /*
709 * Trim from tail. Scan the mbuf chain,
710 * calculating its length and finding the last mbuf.
711 * If the adjustment only affects this mbuf, then just
712 * adjust and return. Otherwise, rescan and truncate
713 * after the remaining size.
714 */
715 count = 0;
716 m = mp;
717 for (;;) {
718 count += m->m_len;
719 if (m->m_next == (struct mbuf *)0)
720 break;
721 m = m->m_next;
722 }
723 if (m->m_len > len) {
724 m->m_len -= len;
725 if (nul > 0) {
726 cp = mtod(m, caddr_t)+m->m_len-nul;
727 for (i = 0; i < nul; i++)
728 *cp++ = '\0';
729 }
730 return;
731 }
732 count -= len;
733 if (count < 0)
734 count = 0;
735 /*
736 * Correct length for chain is "count".
737 * Find the mbuf with last data, adjust its length,
738 * and toss data from remaining mbufs on chain.
739 */
740 for (m = mp; m; m = m->m_next) {
741 if (m->m_len >= count) {
742 m->m_len = count;
743 if (nul > 0) {
744 cp = mtod(m, caddr_t)+m->m_len-nul;
745 for (i = 0; i < nul; i++)
746 *cp++ = '\0';
747 }
748 break;
749 }
750 count -= m->m_len;
751 }
752 while (m = m->m_next)
753 m->m_len = 0;
754 }
755
756 /*
757 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
758 * - look up fsid in mount list (if not found ret error)
759 * - check that it is exported
760 * - get vp by calling VFS_FHTOVP() macro
761 * - if not lockflag unlock it with VOP_UNLOCK()
762 * - if cred->cr_uid == 0 set it to m_exroot
763 */
764 nfsrv_fhtovp(fhp, lockflag, vpp, cred)
765 fhandle_t *fhp;
766 int lockflag;
767 struct vnode **vpp;
768 struct ucred *cred;
769 {
770 register struct mount *mp;
771
772 if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
773 return (ESTALE);
774 if ((mp->mnt_flag & MNT_EXPORTED) == 0)
775 return (EACCES);
776 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
777 return (ESTALE);
778 if (cred->cr_uid == 0)
779 cred->cr_uid = mp->mnt_exroot;
780 if (!lockflag)
781 VOP_UNLOCK(*vpp);
782 return (0);
783 }
784
785 /*
786 * These two functions implement nfs rpc compression.
787 * The algorithm is a trivial run length encoding of '\0' bytes. The high
788 * order nibble of hex "e" is or'd with the number of zeroes - 2 in four
789 * bits. (2 - 17 zeros) Any data byte with a high order nibble of hex "e"
790 * is byte stuffed.
791 * The compressed data is padded with 0x0 bytes to an even multiple of
792 * 4 bytes in length to avoid any weird long pointer alignments.
793 * If compression/uncompression is unsuccessful, the original mbuf list
794 * is returned.
795 * The first four bytes (the XID) are left uncompressed and the fifth
796 * byte is set to 0x1 for request and 0x2 for reply.
797 * An uncompressed RPC will always have the fifth byte == 0x0.
798 */
799 struct mbuf *
800 nfs_compress(m0)
801 struct mbuf *m0;
802 {
803 register u_char ch, nextch;
804 register int i, rlelast;
805 register u_char *ip, *op;
806 register int ileft, oleft, noteof;
807 register struct mbuf *m, *om;
808 struct mbuf **mp, *retm;
809 int olen, clget;
810
811 i = rlelast = 0;
812 noteof = 1;
813 m = m0;
814 if (m->m_len < 12)
815 return (m0);
816 if (m->m_pkthdr.len >= MINCLSIZE)
817 clget = 1;
818 else
819 clget = 0;
820 ileft = m->m_len - 9;
821 ip = mtod(m, u_char *);
822 MGETHDR(om, M_WAIT, MT_DATA);
823 if (clget)
824 MCLGET(om, M_WAIT);
825 retm = om;
826 mp = &om->m_next;
827 olen = om->m_len = 5;
828 oleft = M_TRAILINGSPACE(om);
829 op = mtod(om, u_char *);
830 *((u_long *)op) = *((u_long *)ip);
831 ip += 7;
832 op += 4;
833 *op++ = *ip++ + 1;
834 nextch = *ip++;
835 while (noteof) {
836 ch = nextch;
837 if (ileft == 0) {
838 do {
839 m = m->m_next;
840 } while (m && m->m_len == 0);
841 if (m) {
842 ileft = m->m_len;
843 ip = mtod(m, u_char *);
844 } else {
845 noteof = 0;
846 nextch = 0x1;
847 goto doit;
848 }
849 }
850 nextch = *ip++;
851 ileft--;
852 doit:
853 if (ch == '\0') {
854 if (++i == NFSC_MAX || nextch != '\0') {
855 if (i < 2) {
856 nfscput('\0');
857 } else {
858 if (rlelast == i) {
859 nfscput('\0');
860 i--;
861 }
862 if (NFSCRLE(i) == (nextch & 0xff)) {
863 i--;
864 if (i < 2) {
865 nfscput('\0');
866 } else {
867 nfscput(NFSCRLE(i));
868 }
869 nfscput('\0');
870 rlelast = 0;
871 } else {
872 nfscput(NFSCRLE(i));
873 rlelast = i;
874 }
875 }
876 i = 0;
877 }
878 } else {
879 if ((ch & NFSCRL) == NFSCRL) {
880 nfscput(ch);
881 }
882 nfscput(ch);
883 i = rlelast = 0;
884 }
885 }
886 if (olen < m0->m_pkthdr.len) {
887 m_freem(m0);
888 if (i = (olen & 0x3)) {
889 i = 4 - i;
890 while (i-- > 0) {
891 nfscput('\0');
892 }
893 }
894 retm->m_pkthdr.len = olen;
895 retm->m_pkthdr.rcvif = (struct ifnet *)0;
896 return (retm);
897 } else {
898 m_freem(retm);
899 return (m0);
900 }
901 }
902
903 struct mbuf *
904 nfs_uncompress(m0)
905 struct mbuf *m0;
906 {
907 register u_char cp, nextcp, *ip, *op;
908 register struct mbuf *m, *om;
909 struct mbuf *retm, **mp;
910 int i, j, noteof, clget, ileft, oleft, olen;
911
912 m = m0;
913 i = 0;
914 while (m && i < MINCLSIZE) {
915 i += m->m_len;
916 m = m->m_next;
917 }
918 if (i < 6)
919 return (m0);
920 if (i >= MINCLSIZE)
921 clget = 1;
922 else
923 clget = 0;
924 m = m0;
925 MGET(om, M_WAIT, MT_DATA);
926 if (clget)
927 MCLGET(om, M_WAIT);
928 olen = om->m_len = 8;
929 oleft = M_TRAILINGSPACE(om);
930 op = mtod(om, u_char *);
931 retm = om;
932 mp = &om->m_next;
933 if (m->m_len >= 6) {
934 ileft = m->m_len - 6;
935 ip = mtod(m, u_char *);
936 *((u_long *)op) = *((u_long *)ip);
937 bzero(op + 4, 3);
938 ip += 4;
939 op += 7;
940 if (*ip == '\0') {
941 m_freem(om);
942 return (m0);
943 }
944 *op++ = *ip++ - 1;
945 cp = *ip++;
946 } else {
947 ileft = m->m_len;
948 ip = mtod(m, u_char *);
949 nfscget(*op++);
950 nfscget(*op++);
951 nfscget(*op++);
952 nfscget(*op++);
953 bzero(op, 3);
954 op += 3;
955 nfscget(*op);
956 if (*op == '\0') {
957 m_freem(om);
958 return (m0);
959 }
960 (*op)--;
961 op++;
962 nfscget(cp);
963 }
964 noteof = 1;
965 while (noteof) {
966 if ((cp & NFSCRL) == NFSCRL) {
967 nfscget(nextcp);
968 if (cp == nextcp) {
969 nfscput(cp);
970 goto readit;
971 } else {
972 i = (cp & 0xf) + 2;
973 for (j = 0; j < i; j++) {
974 nfscput('\0');
975 }
976 cp = nextcp;
977 }
978 } else {
979 nfscput(cp);
980 readit:
981 nfscget(cp);
982 }
983 }
984 m_freem(m0);
985 if (i = (olen & 0x3))
986 om->m_len -= i;
987 return (retm);
988 }
989