138420Smckusick /* 238420Smckusick * Copyright (c) 1989 The Regents of the University of California. 338420Smckusick * All rights reserved. 438420Smckusick * 538420Smckusick * This code is derived from software contributed to Berkeley by 638420Smckusick * Rick Macklem at The University of Guelph. 738420Smckusick * 844513Sbostic * %sccs.include.redist.c% 938420Smckusick * 10*48049Smckusick * @(#)nfs_subs.c 7.39 (Berkeley) 04/16/91 1138420Smckusick */ 1238420Smckusick 1338420Smckusick /* 1438420Smckusick * These functions support the macros and help fiddle mbuf chains for 1538420Smckusick * the nfs op functions. They do things like create the rpc header and 1638420Smckusick * copy data between mbuf chains and uio lists. 1738420Smckusick */ 1838420Smckusick #include "param.h" 1939345Smckusick #include "proc.h" 2045921Smckusick #include "filedesc.h" 2140136Smckusick #include "systm.h" 2240136Smckusick #include "kernel.h" 2338420Smckusick #include "mount.h" 2438420Smckusick #include "file.h" 2538420Smckusick #include "vnode.h" 26*48049Smckusick #include "namei.h" 2740136Smckusick #include "mbuf.h" 2838884Smacklem #include "map.h" 2947573Skarels 3046988Smckusick #include "../ufs/quota.h" 3146988Smckusick #include "../ufs/inode.h" 3247573Skarels 3338420Smckusick #include "rpcv2.h" 3438420Smckusick #include "nfsv2.h" 3538420Smckusick #include "nfsnode.h" 3638420Smckusick #include "nfs.h" 3738884Smacklem #include "nfsiom.h" 3838420Smckusick #include "xdr_subs.h" 3938420Smckusick #include "nfsm_subs.h" 4045282Smckusick #include "nfscompress.h" 4138420Smckusick 4238420Smckusick #define TRUE 1 4338420Smckusick #define FALSE 0 4438420Smckusick 4538420Smckusick /* 4638420Smckusick * Data items converted to xdr at startup, since they are constant 4738420Smckusick * This is kinda hokey, but may save a little time doing byte swaps 4838420Smckusick */ 4938420Smckusick u_long nfs_procids[NFS_NPROCS]; 5038420Smckusick u_long nfs_xdrneg1; 5138420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, 5238420Smckusick rpc_mismatch, rpc_auth_unix, rpc_msgaccepted; 5338420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false; 5438420Smckusick /* And other global data */ 5538420Smckusick static u_long *rpc_uidp = (u_long *)0; 5638420Smckusick static u_long nfs_xid = 1; 5738420Smckusick static char *rpc_unixauth; 5838420Smckusick extern long hostid; 5942244Smckusick enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON }; 6041902Smckusick extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 6138884Smacklem extern struct map nfsmap[NFS_MSIZ]; 6241902Smckusick extern struct nfsreq nfsreqh; 6338420Smckusick 6438420Smckusick /* Function ret types */ 6538420Smckusick static char *nfs_unixauth(); 6638420Smckusick 6738420Smckusick /* 6838737Smckusick * Maximum number of groups passed through to NFS server. 6941902Smckusick * According to RFC1057 it should be 16. 7038737Smckusick * For release 3.X systems, the maximum value is 8. 7144987Smckusick * For some other servers, the maximum value is 10. 7238737Smckusick */ 7338737Smckusick int numgrps = 8; 7438737Smckusick 7538737Smckusick /* 7638420Smckusick * Create the header for an rpc request packet 7738420Smckusick * The function nfs_unixauth() creates a unix style authorization string 7838420Smckusick * and returns a ptr to it. 7938420Smckusick * The hsiz is the size of the rest of the nfs request header. 8038420Smckusick * (just used to decide if a cluster is a good idea) 8139494Smckusick * nb: Note that the prog, vers and procid args are already in xdr byte order 8238420Smckusick */ 8339494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid) 8438420Smckusick u_long prog; 8538420Smckusick u_long vers; 8639494Smckusick u_long procid; 8738420Smckusick struct ucred *cred; 8838420Smckusick int hsiz; 8938420Smckusick caddr_t *bpos; 9038420Smckusick struct mbuf **mb; 9138420Smckusick u_long *retxid; 9238420Smckusick { 9338420Smckusick register struct mbuf *mreq, *m; 94*48049Smckusick register u_long *tl; 9538420Smckusick struct mbuf *m1; 9638420Smckusick char *ap; 9738420Smckusick int asiz, siz; 9838420Smckusick 9938420Smckusick NFSMGETHDR(mreq); 10044987Smckusick asiz = ((((cred->cr_ngroups - 1) > numgrps) ? numgrps : 10144987Smckusick (cred->cr_ngroups - 1)) << 2); 10238425Smckusick #ifdef FILLINHOST 10338420Smckusick asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED); 10438425Smckusick #else 10538425Smckusick asiz += 9*NFSX_UNSIGNED; 10638425Smckusick #endif 10738420Smckusick 10838420Smckusick /* If we need a lot, alloc a cluster ?? */ 10938420Smckusick if ((asiz+hsiz+RPC_SIZ) > MHLEN) 11041902Smckusick MCLGET(mreq, M_WAIT); 11138420Smckusick mreq->m_len = NFSMSIZ(mreq); 11238420Smckusick siz = mreq->m_len; 11338420Smckusick m1 = mreq; 11438420Smckusick /* 11538420Smckusick * Alloc enough mbufs 11638420Smckusick * We do it now to avoid all sleeps after the call to nfs_unixauth() 11738420Smckusick */ 11838420Smckusick while ((asiz+RPC_SIZ) > siz) { 11938420Smckusick MGET(m, M_WAIT, MT_DATA); 12038420Smckusick m1->m_next = m; 12138420Smckusick m->m_len = MLEN; 12238420Smckusick siz += MLEN; 12338420Smckusick m1 = m; 12438420Smckusick } 125*48049Smckusick tl = mtod(mreq, u_long *); 126*48049Smckusick *tl++ = *retxid = txdr_unsigned(++nfs_xid); 127*48049Smckusick *tl++ = rpc_call; 128*48049Smckusick *tl++ = rpc_vers; 129*48049Smckusick *tl++ = prog; 130*48049Smckusick *tl++ = vers; 131*48049Smckusick *tl++ = procid; 13238420Smckusick 13338420Smckusick /* Now we can call nfs_unixauth() and copy it in */ 13438420Smckusick ap = nfs_unixauth(cred); 13538420Smckusick m = mreq; 13638420Smckusick siz = m->m_len-RPC_SIZ; 13738420Smckusick if (asiz <= siz) { 138*48049Smckusick bcopy(ap, (caddr_t)tl, asiz); 13938420Smckusick m->m_len = asiz+RPC_SIZ; 14038420Smckusick } else { 141*48049Smckusick bcopy(ap, (caddr_t)tl, siz); 14238420Smckusick ap += siz; 14338420Smckusick asiz -= siz; 14438420Smckusick while (asiz > 0) { 14538420Smckusick siz = (asiz > MLEN) ? MLEN : asiz; 14638420Smckusick m = m->m_next; 14738420Smckusick bcopy(ap, mtod(m, caddr_t), siz); 14838420Smckusick m->m_len = siz; 14938420Smckusick asiz -= siz; 15038420Smckusick ap += siz; 15138420Smckusick } 15238420Smckusick } 15338420Smckusick 15438420Smckusick /* Finally, return values */ 15538420Smckusick *mb = m; 15638420Smckusick *bpos = mtod(m, caddr_t)+m->m_len; 15738420Smckusick return (mreq); 15838420Smckusick } 15938420Smckusick 16038420Smckusick /* 16138420Smckusick * copies mbuf chain to the uio scatter/gather list 16238420Smckusick */ 16338420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos) 16438420Smckusick struct mbuf **mrep; 16543353Smckusick register struct uio *uiop; 16638420Smckusick int siz; 16738420Smckusick caddr_t *dpos; 16838420Smckusick { 16943353Smckusick register char *mbufcp, *uiocp; 17038420Smckusick register int xfer, left, len; 17138420Smckusick register struct mbuf *mp; 17238420Smckusick long uiosiz, rem; 17341902Smckusick int error = 0; 17438420Smckusick 17538420Smckusick mp = *mrep; 17638420Smckusick mbufcp = *dpos; 17738420Smckusick len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 17838420Smckusick rem = nfsm_rndup(siz)-siz; 17938420Smckusick while (siz > 0) { 18038420Smckusick if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 18141902Smckusick return (EFBIG); 18238420Smckusick left = uiop->uio_iov->iov_len; 18338420Smckusick uiocp = uiop->uio_iov->iov_base; 18438420Smckusick if (left > siz) 18538420Smckusick left = siz; 18638420Smckusick uiosiz = left; 18738420Smckusick while (left > 0) { 18838420Smckusick while (len == 0) { 18938420Smckusick mp = mp->m_next; 19038420Smckusick if (mp == NULL) 19138420Smckusick return (EBADRPC); 19238420Smckusick mbufcp = mtod(mp, caddr_t); 19338420Smckusick len = mp->m_len; 19438420Smckusick } 19538420Smckusick xfer = (left > len) ? len : left; 19638420Smckusick #ifdef notdef 19738420Smckusick /* Not Yet.. */ 19838420Smckusick if (uiop->uio_iov->iov_op != NULL) 19938420Smckusick (*(uiop->uio_iov->iov_op)) 20038420Smckusick (mbufcp, uiocp, xfer); 20138420Smckusick else 20238420Smckusick #endif 20338420Smckusick if (uiop->uio_segflg == UIO_SYSSPACE) 20438420Smckusick bcopy(mbufcp, uiocp, xfer); 20538420Smckusick else 20638420Smckusick copyout(mbufcp, uiocp, xfer); 20738420Smckusick left -= xfer; 20838420Smckusick len -= xfer; 20938420Smckusick mbufcp += xfer; 21038420Smckusick uiocp += xfer; 21139585Smckusick uiop->uio_offset += xfer; 21238420Smckusick uiop->uio_resid -= xfer; 21338420Smckusick } 21438420Smckusick if (uiop->uio_iov->iov_len <= siz) { 21538420Smckusick uiop->uio_iovcnt--; 21638420Smckusick uiop->uio_iov++; 21738420Smckusick } else { 21838420Smckusick uiop->uio_iov->iov_base += uiosiz; 21938420Smckusick uiop->uio_iov->iov_len -= uiosiz; 22038420Smckusick } 22138420Smckusick siz -= uiosiz; 22238420Smckusick } 22338420Smckusick *dpos = mbufcp; 22438420Smckusick *mrep = mp; 22541902Smckusick if (rem > 0) { 22641902Smckusick if (len < rem) 22741902Smckusick error = nfs_adv(mrep, dpos, rem, len); 22841902Smckusick else 22941902Smckusick *dpos += rem; 23041902Smckusick } 23141902Smckusick return (error); 23238420Smckusick } 23338420Smckusick 23438420Smckusick /* 23538420Smckusick * copies a uio scatter/gather list to an mbuf chain... 23638420Smckusick */ 23738420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos) 23838420Smckusick register struct uio *uiop; 23938420Smckusick struct mbuf **mq; 24038420Smckusick int siz; 24138420Smckusick caddr_t *bpos; 24238420Smckusick { 24343353Smckusick register char *uiocp; 24443353Smckusick register struct mbuf *mp, *mp2; 24543353Smckusick register int xfer, left, len; 24643353Smckusick int uiosiz, clflg, rem; 24743353Smckusick char *cp; 24838420Smckusick 24938420Smckusick if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 25038420Smckusick clflg = 1; 25138420Smckusick else 25238420Smckusick clflg = 0; 25338420Smckusick rem = nfsm_rndup(siz)-siz; 25438420Smckusick mp2 = *mq; 25538420Smckusick while (siz > 0) { 25638420Smckusick if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 25741902Smckusick return (EINVAL); 25838420Smckusick left = uiop->uio_iov->iov_len; 25938420Smckusick uiocp = uiop->uio_iov->iov_base; 26038420Smckusick if (left > siz) 26138420Smckusick left = siz; 26238420Smckusick uiosiz = left; 26338420Smckusick while (left > 0) { 26438420Smckusick MGET(mp, M_WAIT, MT_DATA); 26538420Smckusick if (clflg) 26641902Smckusick MCLGET(mp, M_WAIT); 26738420Smckusick mp->m_len = NFSMSIZ(mp); 26838420Smckusick mp2->m_next = mp; 26938420Smckusick mp2 = mp; 27038420Smckusick xfer = (left > mp->m_len) ? mp->m_len : left; 27138420Smckusick #ifdef notdef 27238420Smckusick /* Not Yet.. */ 27338420Smckusick if (uiop->uio_iov->iov_op != NULL) 27438420Smckusick (*(uiop->uio_iov->iov_op)) 27538420Smckusick (uiocp, mtod(mp, caddr_t), xfer); 27638420Smckusick else 27738420Smckusick #endif 27838420Smckusick if (uiop->uio_segflg == UIO_SYSSPACE) 27938420Smckusick bcopy(uiocp, mtod(mp, caddr_t), xfer); 28038420Smckusick else 28138420Smckusick copyin(uiocp, mtod(mp, caddr_t), xfer); 28238420Smckusick len = mp->m_len; 28338420Smckusick mp->m_len = xfer; 28438420Smckusick left -= xfer; 28538420Smckusick uiocp += xfer; 28639585Smckusick uiop->uio_offset += xfer; 28738420Smckusick uiop->uio_resid -= xfer; 28838420Smckusick } 28938420Smckusick if (uiop->uio_iov->iov_len <= siz) { 29038420Smckusick uiop->uio_iovcnt--; 29138420Smckusick uiop->uio_iov++; 29238420Smckusick } else { 29338420Smckusick uiop->uio_iov->iov_base += uiosiz; 29438420Smckusick uiop->uio_iov->iov_len -= uiosiz; 29538420Smckusick } 29638420Smckusick siz -= uiosiz; 29738420Smckusick } 29838420Smckusick if (rem > 0) { 29938420Smckusick if (rem > (len-mp->m_len)) { 30038420Smckusick MGET(mp, M_WAIT, MT_DATA); 30138420Smckusick mp->m_len = 0; 30238420Smckusick mp2->m_next = mp; 30338420Smckusick } 30438420Smckusick cp = mtod(mp, caddr_t)+mp->m_len; 30538420Smckusick for (left = 0; left < rem; left++) 30638420Smckusick *cp++ = '\0'; 30738420Smckusick mp->m_len += rem; 30838420Smckusick *bpos = cp; 30938420Smckusick } else 31038420Smckusick *bpos = mtod(mp, caddr_t)+mp->m_len; 31138420Smckusick *mq = mp; 31241902Smckusick return (0); 31338420Smckusick } 31438420Smckusick 31538420Smckusick /* 31638420Smckusick * Help break down an mbuf chain by setting the first siz bytes contiguous 31738420Smckusick * pointed to by returned val. 31838420Smckusick * If Updateflg == True we can overwrite the first part of the mbuf data 31938420Smckusick * This is used by the macros nfsm_disect and nfsm_disecton for tough 32038420Smckusick * cases. (The macros use the vars. dpos and dpos2) 32138420Smckusick */ 32238420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2) 32338420Smckusick struct mbuf **mdp; 32438420Smckusick caddr_t *dposp; 32538420Smckusick int siz; 32638420Smckusick int left; 32738420Smckusick int updateflg; 32838420Smckusick caddr_t *cp2; 32938420Smckusick { 33038420Smckusick register struct mbuf *mp, *mp2; 33138420Smckusick register int siz2, xfer; 332*48049Smckusick register caddr_t tl; 33338420Smckusick 33438420Smckusick mp = *mdp; 33538420Smckusick while (left == 0) { 33638420Smckusick *mdp = mp = mp->m_next; 33738420Smckusick if (mp == NULL) 33841902Smckusick return (EBADRPC); 33938420Smckusick left = mp->m_len; 34038420Smckusick *dposp = mtod(mp, caddr_t); 34138420Smckusick } 34238420Smckusick if (left >= siz) { 34338420Smckusick *cp2 = *dposp; 34438420Smckusick *dposp += siz; 34538420Smckusick } else if (mp->m_next == NULL) { 34641902Smckusick return (EBADRPC); 34741902Smckusick } else if (siz > MHLEN) { 34838420Smckusick panic("nfs S too big"); 34938420Smckusick } else { 35038420Smckusick /* Iff update, you can overwrite, else must alloc new mbuf */ 35138420Smckusick if (updateflg) { 35238420Smckusick NFSMINOFF(mp); 35338420Smckusick } else { 35438420Smckusick MGET(mp2, M_WAIT, MT_DATA); 35538420Smckusick mp2->m_next = mp->m_next; 35638420Smckusick mp->m_next = mp2; 35738420Smckusick mp->m_len -= left; 35838420Smckusick mp = mp2; 35938420Smckusick } 360*48049Smckusick *cp2 = tl = mtod(mp, caddr_t); 361*48049Smckusick bcopy(*dposp, tl, left); /* Copy what was left */ 36238420Smckusick siz2 = siz-left; 363*48049Smckusick tl += left; 36438420Smckusick mp2 = mp->m_next; 36541902Smckusick /* Loop around copying up the siz2 bytes */ 36638420Smckusick while (siz2 > 0) { 36738420Smckusick if (mp2 == NULL) 36838420Smckusick return (EBADRPC); 36938420Smckusick xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 37041902Smckusick if (xfer > 0) { 371*48049Smckusick bcopy(mtod(mp2, caddr_t), tl, xfer); 37241902Smckusick NFSMADV(mp2, xfer); 37341902Smckusick mp2->m_len -= xfer; 374*48049Smckusick tl += xfer; 37541902Smckusick siz2 -= xfer; 37641902Smckusick } 37738420Smckusick if (siz2 > 0) 37838420Smckusick mp2 = mp2->m_next; 37938420Smckusick } 38038420Smckusick mp->m_len = siz; 38138420Smckusick *mdp = mp2; 38238420Smckusick *dposp = mtod(mp2, caddr_t); 38338420Smckusick } 38439494Smckusick return (0); 38538420Smckusick } 38638420Smckusick 38738420Smckusick /* 38841902Smckusick * Advance the position in the mbuf chain. 38938420Smckusick */ 39038420Smckusick nfs_adv(mdp, dposp, offs, left) 39138420Smckusick struct mbuf **mdp; 39238420Smckusick caddr_t *dposp; 39338420Smckusick int offs; 39438420Smckusick int left; 39538420Smckusick { 39638420Smckusick register struct mbuf *m; 39738420Smckusick register int s; 39838420Smckusick 39938420Smckusick m = *mdp; 40038420Smckusick s = left; 40138420Smckusick while (s < offs) { 40238420Smckusick offs -= s; 40338420Smckusick m = m->m_next; 40438420Smckusick if (m == NULL) 40541902Smckusick return (EBADRPC); 40638420Smckusick s = m->m_len; 40738420Smckusick } 40838420Smckusick *mdp = m; 40938420Smckusick *dposp = mtod(m, caddr_t)+offs; 41041902Smckusick return (0); 41138420Smckusick } 41238420Smckusick 41338420Smckusick /* 41438420Smckusick * Copy a string into mbufs for the hard cases... 41538420Smckusick */ 41638420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz) 41738420Smckusick struct mbuf **mb; 41838420Smckusick char **bpos; 41938420Smckusick char *cp; 42038420Smckusick long siz; 42138420Smckusick { 42238420Smckusick register struct mbuf *m1, *m2; 42338420Smckusick long left, xfer, len, tlen; 424*48049Smckusick u_long *tl; 42538420Smckusick int putsize; 42638420Smckusick 42738420Smckusick putsize = 1; 42838420Smckusick m2 = *mb; 42938420Smckusick left = NFSMSIZ(m2)-m2->m_len; 43038420Smckusick if (left > 0) { 431*48049Smckusick tl = ((u_long *)(*bpos)); 432*48049Smckusick *tl++ = txdr_unsigned(siz); 43338420Smckusick putsize = 0; 43438420Smckusick left -= NFSX_UNSIGNED; 43538420Smckusick m2->m_len += NFSX_UNSIGNED; 43638420Smckusick if (left > 0) { 437*48049Smckusick bcopy(cp, (caddr_t) tl, left); 43838420Smckusick siz -= left; 43938420Smckusick cp += left; 44038420Smckusick m2->m_len += left; 44138420Smckusick left = 0; 44238420Smckusick } 44338420Smckusick } 44438420Smckusick /* Loop arround adding mbufs */ 44538420Smckusick while (siz > 0) { 44638420Smckusick MGET(m1, M_WAIT, MT_DATA); 44738420Smckusick if (siz > MLEN) 44841902Smckusick MCLGET(m1, M_WAIT); 44938420Smckusick m1->m_len = NFSMSIZ(m1); 45038420Smckusick m2->m_next = m1; 45138420Smckusick m2 = m1; 452*48049Smckusick tl = mtod(m1, u_long *); 45338420Smckusick tlen = 0; 45438420Smckusick if (putsize) { 455*48049Smckusick *tl++ = txdr_unsigned(siz); 45638420Smckusick m1->m_len -= NFSX_UNSIGNED; 45738420Smckusick tlen = NFSX_UNSIGNED; 45838420Smckusick putsize = 0; 45938420Smckusick } 46038420Smckusick if (siz < m1->m_len) { 46138420Smckusick len = nfsm_rndup(siz); 46238420Smckusick xfer = siz; 46338420Smckusick if (xfer < len) 464*48049Smckusick *(tl+(xfer>>2)) = 0; 46538420Smckusick } else { 46638420Smckusick xfer = len = m1->m_len; 46738420Smckusick } 468*48049Smckusick bcopy(cp, (caddr_t) tl, xfer); 46938420Smckusick m1->m_len = len+tlen; 47038420Smckusick siz -= xfer; 47138420Smckusick cp += xfer; 47238420Smckusick } 47338420Smckusick *mb = m1; 47438420Smckusick *bpos = mtod(m1, caddr_t)+m1->m_len; 47541902Smckusick return (0); 47638420Smckusick } 47738420Smckusick 47838420Smckusick /* 47938420Smckusick * Called once to initialize data structures... 48038420Smckusick */ 48139444Smckusick nfs_init() 48238420Smckusick { 48338420Smckusick register int i; 48438420Smckusick 48538420Smckusick rpc_vers = txdr_unsigned(RPC_VER2); 48638420Smckusick rpc_call = txdr_unsigned(RPC_CALL); 48738420Smckusick rpc_reply = txdr_unsigned(RPC_REPLY); 48838420Smckusick rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 48938420Smckusick rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 49038420Smckusick rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 49138420Smckusick rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 49238420Smckusick nfs_vers = txdr_unsigned(NFS_VER2); 49338420Smckusick nfs_prog = txdr_unsigned(NFS_PROG); 49438420Smckusick nfs_true = txdr_unsigned(TRUE); 49538420Smckusick nfs_false = txdr_unsigned(FALSE); 49638420Smckusick /* Loop thru nfs procids */ 49738420Smckusick for (i = 0; i < NFS_NPROCS; i++) 49838420Smckusick nfs_procids[i] = txdr_unsigned(i); 49939345Smckusick /* Ensure async daemons disabled */ 50041902Smckusick for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 50139345Smckusick nfs_iodwant[i] = (struct proc *)0; 50238420Smckusick nfs_xdrneg1 = txdr_unsigned(-1); 50338420Smckusick nfs_nhinit(); /* Init the nfsnode table */ 50439755Smckusick nfsrv_initcache(); /* Init the server request cache */ 50538884Smacklem rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ); 50641902Smckusick 50741902Smckusick /* 50841902Smckusick * Initialize reply list and start timer 50941902Smckusick */ 51041902Smckusick nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh; 51138420Smckusick nfs_timer(); 51238420Smckusick } 51338420Smckusick 51438420Smckusick /* 51538420Smckusick * Fill in the rest of the rpc_unixauth and return it 51638420Smckusick */ 51738420Smckusick static char *nfs_unixauth(cr) 51838420Smckusick register struct ucred *cr; 51938420Smckusick { 520*48049Smckusick register u_long *tl; 52138420Smckusick register int i; 52238420Smckusick int ngr; 52338420Smckusick 52438420Smckusick /* Maybe someday there should be a cache of AUTH_SHORT's */ 525*48049Smckusick if ((tl = rpc_uidp) == NULL) { 52638425Smckusick #ifdef FILLINHOST 52744987Smckusick i = nfsm_rndup(hostnamelen)+(25*NFSX_UNSIGNED); 52838425Smckusick #else 52944987Smckusick i = 25*NFSX_UNSIGNED; 53038425Smckusick #endif 531*48049Smckusick MALLOC(tl, u_long *, i, M_TEMP, M_WAITOK); 532*48049Smckusick bzero((caddr_t)tl, i); 533*48049Smckusick rpc_unixauth = (caddr_t)tl; 534*48049Smckusick *tl++ = txdr_unsigned(RPCAUTH_UNIX); 535*48049Smckusick tl++; /* Fill in size later */ 536*48049Smckusick *tl++ = hostid; 53738425Smckusick #ifdef FILLINHOST 538*48049Smckusick *tl++ = txdr_unsigned(hostnamelen); 53938420Smckusick i = nfsm_rndup(hostnamelen); 540*48049Smckusick bcopy(hostname, (caddr_t)tl, hostnamelen); 541*48049Smckusick tl += (i>>2); 54238425Smckusick #else 543*48049Smckusick *tl++ = 0; 54438425Smckusick #endif 545*48049Smckusick rpc_uidp = tl; 54638420Smckusick } 547*48049Smckusick *tl++ = txdr_unsigned(cr->cr_uid); 548*48049Smckusick *tl++ = txdr_unsigned(cr->cr_groups[0]); 54944987Smckusick ngr = ((cr->cr_ngroups - 1) > numgrps) ? numgrps : (cr->cr_ngroups - 1); 550*48049Smckusick *tl++ = txdr_unsigned(ngr); 55144987Smckusick for (i = 1; i <= ngr; i++) 552*48049Smckusick *tl++ = txdr_unsigned(cr->cr_groups[i]); 55338420Smckusick /* And add the AUTH_NULL */ 554*48049Smckusick *tl++ = 0; 555*48049Smckusick *tl = 0; 556*48049Smckusick i = (((caddr_t)tl)-rpc_unixauth)-12; 557*48049Smckusick tl = (u_long *)(rpc_unixauth+4); 558*48049Smckusick *tl = txdr_unsigned(i); 55941902Smckusick return (rpc_unixauth); 56038420Smckusick } 56138420Smckusick 56238420Smckusick /* 56338420Smckusick * Attribute cache routines. 56438420Smckusick * nfs_loadattrcache() - loads or updates the cache contents from attributes 56538420Smckusick * that are on the mbuf list 56638420Smckusick * nfs_getattrcache() - returns valid attributes if found in cache, returns 56738420Smckusick * error otherwise 56838420Smckusick */ 56938420Smckusick 57038420Smckusick /* 57139444Smckusick * Load the attribute cache (that lives in the nfsnode entry) with 57238420Smckusick * the values on the mbuf list and 57338420Smckusick * Iff vap not NULL 57438420Smckusick * copy the attributes to *vaper 57538420Smckusick */ 57639457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper) 57739457Smckusick struct vnode **vpp; 57838420Smckusick struct mbuf **mdp; 57938420Smckusick caddr_t *dposp; 58038420Smckusick struct vattr *vaper; 58138420Smckusick { 58239457Smckusick register struct vnode *vp = *vpp; 58338420Smckusick register struct vattr *vap; 58438884Smacklem register struct nfsv2_fattr *fp; 58539444Smckusick extern struct vnodeops spec_nfsv2nodeops; 58639457Smckusick register struct nfsnode *np; 58739494Smckusick register long t1; 58839494Smckusick caddr_t dpos, cp2; 58939494Smckusick int error = 0; 59039494Smckusick struct mbuf *md; 59139444Smckusick enum vtype type; 59246988Smckusick u_short mode; 59342244Smckusick long rdev; 59439444Smckusick struct timeval mtime; 59539444Smckusick struct vnode *nvp; 59638420Smckusick 59738420Smckusick md = *mdp; 59838420Smckusick dpos = *dposp; 59938420Smckusick t1 = (mtod(md, caddr_t)+md->m_len)-dpos; 60038420Smckusick if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2)) 60138420Smckusick return (error); 60238884Smacklem fp = (struct nfsv2_fattr *)cp2; 60339444Smckusick type = nfstov_type(fp->fa_type); 60446988Smckusick mode = fxdr_unsigned(u_short, fp->fa_mode); 60546988Smckusick if (type == VNON) 60646988Smckusick type = IFTOVT(mode); 60742244Smckusick rdev = fxdr_unsigned(long, fp->fa_rdev); 60839444Smckusick fxdr_time(&fp->fa_mtime, &mtime); 60939444Smckusick /* 61039444Smckusick * If v_type == VNON it is a new node, so fill in the v_type, 61139444Smckusick * n_mtime fields. Check to see if it represents a special 61239444Smckusick * device, and if so, check for a possible alias. Once the 61339444Smckusick * correct vnode has been obtained, fill in the rest of the 61439444Smckusick * information. 61539444Smckusick */ 61638420Smckusick np = VTONFS(vp); 61739444Smckusick if (vp->v_type == VNON) { 61842244Smckusick if (type == VCHR && rdev == 0xffffffff) 61942244Smckusick vp->v_type = type = VFIFO; 62042244Smckusick else 62142244Smckusick vp->v_type = type; 62240295Smckusick if (vp->v_type == VFIFO) { 62340295Smckusick #ifdef FIFO 62440295Smckusick extern struct vnodeops fifo_nfsv2nodeops; 62540295Smckusick vp->v_op = &fifo_nfsv2nodeops; 62640295Smckusick #else 62740295Smckusick return (EOPNOTSUPP); 62840295Smckusick #endif /* FIFO */ 62940295Smckusick } 63039444Smckusick if (vp->v_type == VCHR || vp->v_type == VBLK) { 63139444Smckusick vp->v_op = &spec_nfsv2nodeops; 63242244Smckusick if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) { 63339444Smckusick /* 63439444Smckusick * Reinitialize aliased node. 63539444Smckusick */ 63639444Smckusick np = VTONFS(nvp); 63739444Smckusick np->n_vnode = nvp; 63839907Smckusick np->n_flag = 0; 63939907Smckusick nfs_lock(nvp); 64039444Smckusick bcopy((caddr_t)&VTONFS(vp)->n_fh, 64139444Smckusick (caddr_t)&np->n_fh, NFSX_FH); 64239444Smckusick insque(np, nfs_hash(&np->n_fh)); 64339444Smckusick np->n_attrstamp = 0; 64439444Smckusick np->n_sillyrename = (struct sillyrename *)0; 64539444Smckusick /* 64639457Smckusick * Discard unneeded vnode and update actual one 64739444Smckusick */ 64839444Smckusick vput(vp); 64941902Smckusick *vpp = nvp; 65039444Smckusick } 65139444Smckusick } 65239444Smckusick np->n_mtime = mtime.tv_sec; 65339444Smckusick } 65438420Smckusick vap = &np->n_vattr; 65539444Smckusick vap->va_type = type; 65646988Smckusick vap->va_mode = (mode & 07777); 65738884Smacklem vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 65838884Smacklem vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 65938884Smacklem vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 66038884Smacklem vap->va_size = fxdr_unsigned(u_long, fp->fa_size); 66145716Smckusick if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size) { 66238884Smacklem np->n_size = vap->va_size; 66345907Smckusick #ifdef NVM 66445716Smckusick vnode_pager_setsize(vp, np->n_size); 66545907Smckusick #endif 66645716Smckusick } 66740642Smckusick vap->va_size_rsv = 0; 66838884Smacklem vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize); 66942244Smckusick vap->va_rdev = (dev_t)rdev; 67042244Smckusick vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE; 67140642Smckusick vap->va_bytes_rsv = 0; 67242878Smckusick vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 67338884Smacklem vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid); 67439755Smckusick vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec); 67539755Smckusick vap->va_atime.tv_usec = 0; 67639755Smckusick vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec); 67739444Smckusick vap->va_mtime = mtime; 67839755Smckusick vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec); 67939755Smckusick vap->va_ctime.tv_usec = 0; 68039755Smckusick vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec); 68138420Smckusick np->n_attrstamp = time.tv_sec; 68238420Smckusick *dposp = dpos; 68338420Smckusick *mdp = md; 68438884Smacklem if (vaper != NULL) { 68538420Smckusick bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 68638884Smacklem if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size)) 68738884Smacklem vaper->va_size = np->n_size; 68838884Smacklem } 68938420Smckusick return (0); 69038420Smckusick } 69138420Smckusick 69238420Smckusick /* 69338420Smckusick * Check the time stamp 69438420Smckusick * If the cache is valid, copy contents to *vap and return 0 69538420Smckusick * otherwise return an error 69638420Smckusick */ 69738420Smckusick nfs_getattrcache(vp, vap) 69838420Smckusick register struct vnode *vp; 69938420Smckusick struct vattr *vap; 70038420Smckusick { 70138420Smckusick register struct nfsnode *np; 70238420Smckusick 70338420Smckusick np = VTONFS(vp); 70438420Smckusick if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) { 70538420Smckusick nfsstats.attrcache_hits++; 70638420Smckusick bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr)); 70745716Smckusick if ((np->n_flag & NMODIFIED) == 0) { 70839361Smckusick np->n_size = vap->va_size; 70945907Smckusick #ifdef NVM 71045716Smckusick vnode_pager_setsize(vp, np->n_size); 71145907Smckusick #endif 71245716Smckusick } else if (np->n_size > vap->va_size) 71338884Smacklem vap->va_size = np->n_size; 71438420Smckusick return (0); 71538420Smckusick } else { 71638420Smckusick nfsstats.attrcache_misses++; 71738420Smckusick return (ENOENT); 71838420Smckusick } 71938420Smckusick } 72038420Smckusick 72138420Smckusick /* 72241902Smckusick * Set up nameidata for a namei() call and do it 72338420Smckusick */ 72438420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp) 72538420Smckusick register struct nameidata *ndp; 72638420Smckusick fhandle_t *fhp; 72738420Smckusick int len; 72838420Smckusick struct mbuf **mdp; 72938420Smckusick caddr_t *dposp; 73038420Smckusick { 73138420Smckusick register int i, rem; 73238420Smckusick register struct mbuf *md; 73338420Smckusick register char *cp; 73446514Smckusick struct vnode *dp; 73538420Smckusick int flag; 73641902Smckusick int error; 73738420Smckusick 73842244Smckusick if ((ndp->ni_nameiop & HASBUF) == 0) { 73946514Smckusick flag = ndp->ni_nameiop & OPMASK; 74042244Smckusick /* 74142244Smckusick * Copy the name from the mbuf list to the d_name field of ndp 74242244Smckusick * and set the various ndp fields appropriately. 74342244Smckusick */ 74442244Smckusick cp = *dposp; 74542244Smckusick md = *mdp; 74642244Smckusick rem = mtod(md, caddr_t)+md->m_len-cp; 74742244Smckusick ndp->ni_hash = 0; 74842244Smckusick for (i = 0; i < len;) { 74942244Smckusick while (rem == 0) { 75042244Smckusick md = md->m_next; 75142244Smckusick if (md == NULL) 75242244Smckusick return (EBADRPC); 75342244Smckusick cp = mtod(md, caddr_t); 75442244Smckusick rem = md->m_len; 75542244Smckusick } 75642244Smckusick if (*cp == '\0' || *cp == '/') 75742244Smckusick return (EINVAL); 75842244Smckusick if (*cp & 0200) 75942244Smckusick if ((*cp&0377) == ('/'|0200) || flag != DELETE) 76042244Smckusick return (EINVAL); 76142244Smckusick ndp->ni_dent.d_name[i++] = *cp; 76242244Smckusick ndp->ni_hash += (unsigned char)*cp * i; 76342244Smckusick cp++; 76442244Smckusick rem--; 76538420Smckusick } 76642244Smckusick *mdp = md; 76742244Smckusick *dposp = cp; 76842244Smckusick len = nfsm_rndup(len)-len; 76942244Smckusick if (len > 0) { 77042244Smckusick if (rem < len) { 77142244Smckusick if (error = nfs_adv(mdp, dposp, len, rem)) 77242244Smckusick return (error); 77342244Smckusick } else 77442244Smckusick *dposp += len; 77542244Smckusick } 77642244Smckusick } else 77742244Smckusick i = len; 77838420Smckusick ndp->ni_namelen = i; 77938420Smckusick ndp->ni_dent.d_namlen = i; 78038420Smckusick ndp->ni_dent.d_name[i] = '\0'; 78142244Smckusick ndp->ni_segflg = UIO_SYSSPACE; 78238425Smckusick ndp->ni_pathlen = 1; 78341902Smckusick ndp->ni_pnbuf = ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0]; 78438420Smckusick ndp->ni_next = &ndp->ni_dent.d_name[i]; 78546514Smckusick ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE | HASBUF | STARTDIR); 78646514Smckusick /* 78746514Smckusick * Extract and set starting directory. 78846514Smckusick */ 78941902Smckusick if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred)) 79038420Smckusick return (error); 79138425Smckusick if (dp->v_type != VDIR) { 79241902Smckusick vrele(dp); 79338425Smckusick return (ENOTDIR); 79438425Smckusick } 79546514Smckusick ndp->ni_startdir = dp; 79639345Smckusick /* 79741902Smckusick * And call namei() to do the real work 79838420Smckusick */ 799*48049Smckusick error = namei(ndp, curproc); /* XXX XXX XXX */ 80046514Smckusick if (error || (ndp->ni_nameiop & SAVESTARTDIR) == 0) 80146514Smckusick vrele(dp); 80238420Smckusick return (error); 80338420Smckusick } 80438420Smckusick 80538420Smckusick /* 80638420Smckusick * A fiddled version of m_adj() that ensures null fill to a long 80738420Smckusick * boundary and only trims off the back end 80838420Smckusick */ 80938420Smckusick nfsm_adj(mp, len, nul) 81038420Smckusick struct mbuf *mp; 81138420Smckusick register int len; 81238420Smckusick int nul; 81338420Smckusick { 81438420Smckusick register struct mbuf *m; 81538420Smckusick register int count, i; 81638420Smckusick register char *cp; 81738420Smckusick 81838420Smckusick /* 81938420Smckusick * Trim from tail. Scan the mbuf chain, 82038420Smckusick * calculating its length and finding the last mbuf. 82138420Smckusick * If the adjustment only affects this mbuf, then just 82238420Smckusick * adjust and return. Otherwise, rescan and truncate 82338420Smckusick * after the remaining size. 82438420Smckusick */ 82538420Smckusick count = 0; 82638420Smckusick m = mp; 82738420Smckusick for (;;) { 82838420Smckusick count += m->m_len; 82938420Smckusick if (m->m_next == (struct mbuf *)0) 83038420Smckusick break; 83138420Smckusick m = m->m_next; 83238420Smckusick } 83338579Smckusick if (m->m_len > len) { 83438420Smckusick m->m_len -= len; 83538420Smckusick if (nul > 0) { 83638420Smckusick cp = mtod(m, caddr_t)+m->m_len-nul; 83738420Smckusick for (i = 0; i < nul; i++) 83838420Smckusick *cp++ = '\0'; 83938420Smckusick } 84038420Smckusick return; 84138420Smckusick } 84238420Smckusick count -= len; 84338420Smckusick if (count < 0) 84438420Smckusick count = 0; 84538420Smckusick /* 84638420Smckusick * Correct length for chain is "count". 84738420Smckusick * Find the mbuf with last data, adjust its length, 84838420Smckusick * and toss data from remaining mbufs on chain. 84938420Smckusick */ 85038420Smckusick for (m = mp; m; m = m->m_next) { 85138420Smckusick if (m->m_len >= count) { 85238420Smckusick m->m_len = count; 85338420Smckusick if (nul > 0) { 85438420Smckusick cp = mtod(m, caddr_t)+m->m_len-nul; 85538420Smckusick for (i = 0; i < nul; i++) 85638420Smckusick *cp++ = '\0'; 85738420Smckusick } 85838420Smckusick break; 85938420Smckusick } 86038420Smckusick count -= m->m_len; 86138420Smckusick } 86238420Smckusick while (m = m->m_next) 86338420Smckusick m->m_len = 0; 86438420Smckusick } 86538420Smckusick 86638420Smckusick /* 86738420Smckusick * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 86838420Smckusick * - look up fsid in mount list (if not found ret error) 86938420Smckusick * - check that it is exported 87038420Smckusick * - get vp by calling VFS_FHTOVP() macro 87138420Smckusick * - if not lockflag unlock it with VOP_UNLOCK() 87241902Smckusick * - if cred->cr_uid == 0 set it to m_exroot 87338420Smckusick */ 87438420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred) 87538420Smckusick fhandle_t *fhp; 87638420Smckusick int lockflag; 87738420Smckusick struct vnode **vpp; 87838420Smckusick struct ucred *cred; 87938420Smckusick { 88038420Smckusick register struct mount *mp; 88138420Smckusick 88238420Smckusick if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 88338420Smckusick return (ESTALE); 88441398Smckusick if ((mp->mnt_flag & MNT_EXPORTED) == 0) 88538420Smckusick return (EACCES); 88638420Smckusick if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 88738420Smckusick return (ESTALE); 88838420Smckusick if (cred->cr_uid == 0) 88941398Smckusick cred->cr_uid = mp->mnt_exroot; 89038420Smckusick if (!lockflag) 89138420Smckusick VOP_UNLOCK(*vpp); 89238420Smckusick return (0); 89338420Smckusick } 89445282Smckusick 89545282Smckusick /* 89645282Smckusick * These two functions implement nfs rpc compression. 89745282Smckusick * The algorithm is a trivial run length encoding of '\0' bytes. The high 89845282Smckusick * order nibble of hex "e" is or'd with the number of zeroes - 2 in four 89945282Smckusick * bits. (2 - 17 zeros) Any data byte with a high order nibble of hex "e" 90045282Smckusick * is byte stuffed. 90145282Smckusick * The compressed data is padded with 0x0 bytes to an even multiple of 90245282Smckusick * 4 bytes in length to avoid any weird long pointer alignments. 90345282Smckusick * If compression/uncompression is unsuccessful, the original mbuf list 90445282Smckusick * is returned. 90545282Smckusick * The first four bytes (the XID) are left uncompressed and the fifth 90645282Smckusick * byte is set to 0x1 for request and 0x2 for reply. 90745282Smckusick * An uncompressed RPC will always have the fifth byte == 0x0. 90845282Smckusick */ 90945282Smckusick struct mbuf * 91045282Smckusick nfs_compress(m0) 91145282Smckusick struct mbuf *m0; 91245282Smckusick { 91345282Smckusick register u_char ch, nextch; 91445282Smckusick register int i, rlelast; 91545282Smckusick register u_char *ip, *op; 91645282Smckusick register int ileft, oleft, noteof; 91745282Smckusick register struct mbuf *m, *om; 91845282Smckusick struct mbuf **mp, *retm; 91945282Smckusick int olen, clget; 92045282Smckusick 92145282Smckusick i = rlelast = 0; 92245282Smckusick noteof = 1; 92345282Smckusick m = m0; 92445282Smckusick if (m->m_len < 12) 92545282Smckusick return (m0); 92645282Smckusick if (m->m_pkthdr.len >= MINCLSIZE) 92745282Smckusick clget = 1; 92845282Smckusick else 92945282Smckusick clget = 0; 93045282Smckusick ileft = m->m_len - 9; 93145282Smckusick ip = mtod(m, u_char *); 93245288Smckusick MGETHDR(om, M_WAIT, MT_DATA); 93345282Smckusick if (clget) 93445282Smckusick MCLGET(om, M_WAIT); 93545282Smckusick retm = om; 93645282Smckusick mp = &om->m_next; 93745282Smckusick olen = om->m_len = 5; 93845282Smckusick oleft = M_TRAILINGSPACE(om); 93945282Smckusick op = mtod(om, u_char *); 94045298Smckusick *((u_long *)op) = *((u_long *)ip); 94145282Smckusick ip += 7; 94245282Smckusick op += 4; 94345282Smckusick *op++ = *ip++ + 1; 94445282Smckusick nextch = *ip++; 94545282Smckusick while (noteof) { 94645282Smckusick ch = nextch; 94745282Smckusick if (ileft == 0) { 94845282Smckusick do { 94945282Smckusick m = m->m_next; 95045282Smckusick } while (m && m->m_len == 0); 95145282Smckusick if (m) { 95245282Smckusick ileft = m->m_len; 95345282Smckusick ip = mtod(m, u_char *); 95445282Smckusick } else { 95545282Smckusick noteof = 0; 95645282Smckusick nextch = 0x1; 95745282Smckusick goto doit; 95845282Smckusick } 95945282Smckusick } 96045282Smckusick nextch = *ip++; 96145282Smckusick ileft--; 96245282Smckusick doit: 96345282Smckusick if (ch == '\0') { 96445282Smckusick if (++i == NFSC_MAX || nextch != '\0') { 96545282Smckusick if (i < 2) { 96645282Smckusick nfscput('\0'); 96745282Smckusick } else { 96845282Smckusick if (rlelast == i) { 96945282Smckusick nfscput('\0'); 97045282Smckusick i--; 97145282Smckusick } 97245282Smckusick if (NFSCRLE(i) == (nextch & 0xff)) { 97345282Smckusick i--; 97445282Smckusick if (i < 2) { 97545282Smckusick nfscput('\0'); 97645282Smckusick } else { 97745282Smckusick nfscput(NFSCRLE(i)); 97845282Smckusick } 97945282Smckusick nfscput('\0'); 98045282Smckusick rlelast = 0; 98145282Smckusick } else { 98245282Smckusick nfscput(NFSCRLE(i)); 98345282Smckusick rlelast = i; 98445282Smckusick } 98545282Smckusick } 98645282Smckusick i = 0; 98745282Smckusick } 98845282Smckusick } else { 98945282Smckusick if ((ch & NFSCRL) == NFSCRL) { 99045282Smckusick nfscput(ch); 99145282Smckusick } 99245282Smckusick nfscput(ch); 99345282Smckusick i = rlelast = 0; 99445282Smckusick } 99545282Smckusick } 99645282Smckusick if (olen < m0->m_pkthdr.len) { 99745282Smckusick m_freem(m0); 99845282Smckusick if (i = (olen & 0x3)) { 99945282Smckusick i = 4 - i; 100045298Smckusick while (i-- > 0) { 100145282Smckusick nfscput('\0'); 100245298Smckusick } 100345282Smckusick } 100445282Smckusick retm->m_pkthdr.len = olen; 100545298Smckusick retm->m_pkthdr.rcvif = (struct ifnet *)0; 100645282Smckusick return (retm); 100745282Smckusick } else { 100845282Smckusick m_freem(retm); 100945282Smckusick return (m0); 101045282Smckusick } 101145282Smckusick } 101245282Smckusick 101345282Smckusick struct mbuf * 101445282Smckusick nfs_uncompress(m0) 101545282Smckusick struct mbuf *m0; 101645282Smckusick { 101745282Smckusick register u_char cp, nextcp, *ip, *op; 101845282Smckusick register struct mbuf *m, *om; 101945282Smckusick struct mbuf *retm, **mp; 102045282Smckusick int i, j, noteof, clget, ileft, oleft, olen; 102145282Smckusick 102245282Smckusick m = m0; 102345298Smckusick i = 0; 102445298Smckusick while (m && i < MINCLSIZE) { 102545298Smckusick i += m->m_len; 102645298Smckusick m = m->m_next; 102745298Smckusick } 102845298Smckusick if (i < 6) 102945282Smckusick return (m0); 103045298Smckusick if (i >= MINCLSIZE) 103145282Smckusick clget = 1; 103245282Smckusick else 103345282Smckusick clget = 0; 103445298Smckusick m = m0; 103545298Smckusick MGET(om, M_WAIT, MT_DATA); 103645282Smckusick if (clget) 103745282Smckusick MCLGET(om, M_WAIT); 103845282Smckusick olen = om->m_len = 8; 103945282Smckusick oleft = M_TRAILINGSPACE(om); 104045282Smckusick op = mtod(om, u_char *); 104145282Smckusick retm = om; 104245282Smckusick mp = &om->m_next; 104345282Smckusick if (m->m_len >= 6) { 104445282Smckusick ileft = m->m_len - 6; 104545282Smckusick ip = mtod(m, u_char *); 104645282Smckusick *((u_long *)op) = *((u_long *)ip); 104745282Smckusick bzero(op + 4, 3); 104845282Smckusick ip += 4; 104945282Smckusick op += 7; 105045282Smckusick if (*ip == '\0') { 105145282Smckusick m_freem(om); 105245282Smckusick return (m0); 105345282Smckusick } 105445282Smckusick *op++ = *ip++ - 1; 105545282Smckusick cp = *ip++; 105645282Smckusick } else { 105745282Smckusick ileft = m->m_len; 105845282Smckusick ip = mtod(m, u_char *); 105945282Smckusick nfscget(*op++); 106045282Smckusick nfscget(*op++); 106145282Smckusick nfscget(*op++); 106245282Smckusick nfscget(*op++); 106345282Smckusick bzero(op, 3); 106445282Smckusick op += 3; 106545282Smckusick nfscget(*op); 106645282Smckusick if (*op == '\0') { 106745282Smckusick m_freem(om); 106845282Smckusick return (m0); 106945282Smckusick } 107045282Smckusick (*op)--; 107145282Smckusick op++; 107245282Smckusick nfscget(cp); 107345282Smckusick } 107445282Smckusick noteof = 1; 107545282Smckusick while (noteof) { 107645282Smckusick if ((cp & NFSCRL) == NFSCRL) { 107745282Smckusick nfscget(nextcp); 107845282Smckusick if (cp == nextcp) { 107945282Smckusick nfscput(cp); 108045282Smckusick goto readit; 108145282Smckusick } else { 108245282Smckusick i = (cp & 0xf) + 2; 108345282Smckusick for (j = 0; j < i; j++) { 108445282Smckusick nfscput('\0'); 108545282Smckusick } 108645282Smckusick cp = nextcp; 108745282Smckusick } 108845282Smckusick } else { 108945282Smckusick nfscput(cp); 109045282Smckusick readit: 109145282Smckusick nfscget(cp); 109245282Smckusick } 109345282Smckusick } 109445282Smckusick m_freem(m0); 109545298Smckusick if (i = (olen & 0x3)) 109645282Smckusick om->m_len -= i; 109745282Smckusick return (retm); 109845282Smckusick } 1099