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 * 838420Smckusick * Redistribution and use in source and binary forms are permitted 938420Smckusick * provided that the above copyright notice and this paragraph are 1038420Smckusick * duplicated in all such forms and that any documentation, 1138420Smckusick * advertising materials, and other materials related to such 1238420Smckusick * distribution and use acknowledge that the software was developed 1338420Smckusick * by the University of California, Berkeley. The name of the 1438420Smckusick * University may not be used to endorse or promote products derived 1538420Smckusick * from this software without specific prior written permission. 1638420Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1738420Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1838420Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1938420Smckusick * 20*39494Smckusick * @(#)nfs_subs.c 7.12 (Berkeley) 11/03/89 2138420Smckusick */ 2238420Smckusick 2338420Smckusick /* 2438420Smckusick * These functions support the macros and help fiddle mbuf chains for 2538420Smckusick * the nfs op functions. They do things like create the rpc header and 2638420Smckusick * copy data between mbuf chains and uio lists. 2738420Smckusick */ 2838420Smckusick #include "strings.h" 2938420Smckusick #include "param.h" 3039444Smckusick #include "systm.h" 3139345Smckusick #include "user.h" 3239345Smckusick #include "proc.h" 3338420Smckusick #include "mount.h" 3438425Smckusick #include "../ufs/dir.h" 3538420Smckusick #include "time.h" 3638420Smckusick #include "errno.h" 3738420Smckusick #include "kernel.h" 3838420Smckusick #include "malloc.h" 3938420Smckusick #include "mbuf.h" 4038420Smckusick #include "file.h" 4138420Smckusick #include "vnode.h" 4238420Smckusick #include "uio.h" 4338420Smckusick #include "namei.h" 4438420Smckusick #include "ucred.h" 4538884Smacklem #include "map.h" 4638420Smckusick #include "rpcv2.h" 4738420Smckusick #include "nfsv2.h" 4838420Smckusick #include "nfsnode.h" 4938420Smckusick #include "nfs.h" 5038884Smacklem #include "nfsiom.h" 5138420Smckusick #include "xdr_subs.h" 5238420Smckusick #include "nfsm_subs.h" 5338420Smckusick 5438420Smckusick #define TRUE 1 5538420Smckusick #define FALSE 0 5638420Smckusick 5738420Smckusick /* 5838420Smckusick * Data items converted to xdr at startup, since they are constant 5938420Smckusick * This is kinda hokey, but may save a little time doing byte swaps 6038420Smckusick */ 6138420Smckusick u_long nfs_procids[NFS_NPROCS]; 6238420Smckusick u_long nfs_xdrneg1; 6338420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, 6438420Smckusick rpc_mismatch, rpc_auth_unix, rpc_msgaccepted; 6538420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false; 6638420Smckusick /* And other global data */ 6738420Smckusick static u_long *rpc_uidp = (u_long *)0; 6838420Smckusick static u_long nfs_xid = 1; 6938420Smckusick static char *rpc_unixauth; 7038420Smckusick extern long hostid; 7138420Smckusick extern enum vtype v_type[NFLNK+1]; 7239345Smckusick extern struct proc *nfs_iodwant[MAX_ASYNCDAEMON]; 7338884Smacklem extern struct map nfsmap[NFS_MSIZ]; 7438420Smckusick 7538420Smckusick /* Function ret types */ 7638420Smckusick static char *nfs_unixauth(); 7738420Smckusick 7838420Smckusick /* 7938737Smckusick * Maximum number of groups passed through to NFS server. 8038737Smckusick * For release 3.X systems, the maximum value is 8. 8138737Smckusick * For release 4.X systems, the maximum value is 10. 8238737Smckusick */ 8338737Smckusick int numgrps = 8; 8438737Smckusick 8538737Smckusick /* 8638420Smckusick * Create the header for an rpc request packet 8738420Smckusick * The function nfs_unixauth() creates a unix style authorization string 8838420Smckusick * and returns a ptr to it. 8938420Smckusick * The hsiz is the size of the rest of the nfs request header. 9038420Smckusick * (just used to decide if a cluster is a good idea) 91*39494Smckusick * nb: Note that the prog, vers and procid args are already in xdr byte order 9238420Smckusick */ 93*39494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid) 9438420Smckusick u_long prog; 9538420Smckusick u_long vers; 96*39494Smckusick u_long procid; 9738420Smckusick struct ucred *cred; 9838420Smckusick int hsiz; 9938420Smckusick caddr_t *bpos; 10038420Smckusick struct mbuf **mb; 10138420Smckusick u_long *retxid; 10238420Smckusick { 10338420Smckusick register struct mbuf *mreq, *m; 10438420Smckusick register u_long *p; 10538420Smckusick struct mbuf *m1; 10638420Smckusick char *ap; 10738420Smckusick int asiz, siz; 10838420Smckusick 10938420Smckusick NFSMGETHDR(mreq); 11038737Smckusick asiz = (((cred->cr_ngroups > numgrps) ? numgrps : cred->cr_ngroups)<<2); 11138425Smckusick #ifdef FILLINHOST 11238420Smckusick asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED); 11338425Smckusick #else 11438425Smckusick asiz += 9*NFSX_UNSIGNED; 11538425Smckusick #endif 11638420Smckusick 11738420Smckusick /* If we need a lot, alloc a cluster ?? */ 11838420Smckusick if ((asiz+hsiz+RPC_SIZ) > MHLEN) 11938420Smckusick NFSMCLGET(mreq, M_WAIT); 12038420Smckusick mreq->m_len = NFSMSIZ(mreq); 12138420Smckusick siz = mreq->m_len; 12238420Smckusick m1 = mreq; 12338420Smckusick /* 12438420Smckusick * Alloc enough mbufs 12538420Smckusick * We do it now to avoid all sleeps after the call to nfs_unixauth() 12638420Smckusick */ 12738420Smckusick while ((asiz+RPC_SIZ) > siz) { 12838420Smckusick MGET(m, M_WAIT, MT_DATA); 12938420Smckusick m1->m_next = m; 13038420Smckusick m->m_len = MLEN; 13138420Smckusick siz += MLEN; 13238420Smckusick m1 = m; 13338420Smckusick } 13438420Smckusick p = mtod(mreq, u_long *); 13538420Smckusick *p++ = *retxid = txdr_unsigned(++nfs_xid); 13638420Smckusick *p++ = rpc_call; 13738420Smckusick *p++ = rpc_vers; 13838420Smckusick *p++ = prog; 13938420Smckusick *p++ = vers; 140*39494Smckusick *p++ = procid; 14138420Smckusick 14238420Smckusick /* Now we can call nfs_unixauth() and copy it in */ 14338420Smckusick ap = nfs_unixauth(cred); 14438420Smckusick m = mreq; 14538420Smckusick siz = m->m_len-RPC_SIZ; 14638420Smckusick if (asiz <= siz) { 14738420Smckusick bcopy(ap, (caddr_t)p, asiz); 14838420Smckusick m->m_len = asiz+RPC_SIZ; 14938420Smckusick } else { 15038420Smckusick bcopy(ap, (caddr_t)p, siz); 15138420Smckusick ap += siz; 15238420Smckusick asiz -= siz; 15338420Smckusick while (asiz > 0) { 15438420Smckusick siz = (asiz > MLEN) ? MLEN : asiz; 15538420Smckusick m = m->m_next; 15638420Smckusick bcopy(ap, mtod(m, caddr_t), siz); 15738420Smckusick m->m_len = siz; 15838420Smckusick asiz -= siz; 15938420Smckusick ap += siz; 16038420Smckusick } 16138420Smckusick } 16238420Smckusick 16338420Smckusick /* Finally, return values */ 16438420Smckusick *mb = m; 16538420Smckusick *bpos = mtod(m, caddr_t)+m->m_len; 16638420Smckusick return (mreq); 16738420Smckusick } 16838420Smckusick 16938420Smckusick /* 17038420Smckusick * copies mbuf chain to the uio scatter/gather list 17138420Smckusick */ 17238420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos) 17338420Smckusick struct mbuf **mrep; 17438420Smckusick struct uio *uiop; 17538420Smckusick int siz; 17638420Smckusick caddr_t *dpos; 17738420Smckusick { 17838420Smckusick register int xfer, left, len; 17938420Smckusick register struct mbuf *mp; 18038420Smckusick register char *mbufcp, *uiocp; 18138420Smckusick long uiosiz, rem; 18238420Smckusick 18338420Smckusick mp = *mrep; 18438420Smckusick mbufcp = *dpos; 18538420Smckusick len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 18638420Smckusick rem = nfsm_rndup(siz)-siz; 18738420Smckusick while (siz > 0) { 18838420Smckusick if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 18938420Smckusick return(EFBIG); 19038420Smckusick left = uiop->uio_iov->iov_len; 19138420Smckusick uiocp = uiop->uio_iov->iov_base; 19238420Smckusick if (left > siz) 19338420Smckusick left = siz; 19438420Smckusick uiosiz = left; 19538420Smckusick while (left > 0) { 19638420Smckusick while (len == 0) { 19738420Smckusick mp = mp->m_next; 19838420Smckusick if (mp == NULL) 19938420Smckusick return (EBADRPC); 20038420Smckusick mbufcp = mtod(mp, caddr_t); 20138420Smckusick len = mp->m_len; 20238420Smckusick } 20338420Smckusick xfer = (left > len) ? len : left; 20438420Smckusick #ifdef notdef 20538420Smckusick /* Not Yet.. */ 20638420Smckusick if (uiop->uio_iov->iov_op != NULL) 20738420Smckusick (*(uiop->uio_iov->iov_op)) 20838420Smckusick (mbufcp, uiocp, xfer); 20938420Smckusick else 21038420Smckusick #endif 21138420Smckusick if (uiop->uio_segflg == UIO_SYSSPACE) 21238420Smckusick bcopy(mbufcp, uiocp, xfer); 21338420Smckusick else 21438420Smckusick copyout(mbufcp, uiocp, xfer); 21538420Smckusick left -= xfer; 21638420Smckusick len -= xfer; 21738420Smckusick mbufcp += xfer; 21838420Smckusick uiocp += xfer; 21938420Smckusick uiop->uio_resid -= xfer; 22038420Smckusick } 22138420Smckusick if (uiop->uio_iov->iov_len <= siz) { 22238420Smckusick uiop->uio_iovcnt--; 22338420Smckusick uiop->uio_iov++; 22438420Smckusick } else { 22538420Smckusick uiop->uio_iov->iov_base += uiosiz; 22638420Smckusick uiop->uio_iov->iov_len -= uiosiz; 22738420Smckusick } 22838420Smckusick siz -= uiosiz; 22938420Smckusick } 23038420Smckusick if (rem > 0) 23138420Smckusick mbufcp += rem; 23238420Smckusick *dpos = mbufcp; 23338420Smckusick *mrep = mp; 23438420Smckusick return(0); 23538420Smckusick } 23638420Smckusick 23738420Smckusick /* 23838420Smckusick * copies a uio scatter/gather list to an mbuf chain... 23938420Smckusick */ 24038420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos) 24138420Smckusick register struct uio *uiop; 24238420Smckusick struct mbuf **mq; 24338420Smckusick int siz; 24438420Smckusick caddr_t *bpos; 24538420Smckusick { 24638420Smckusick register struct mbuf *mp; 24738420Smckusick struct mbuf *mp2; 248*39494Smckusick long xfer, left, uiosiz; 24938420Smckusick int clflg; 25038420Smckusick int rem, len; 25138420Smckusick char *cp, *uiocp; 25238420Smckusick 25338420Smckusick if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 25438420Smckusick clflg = 1; 25538420Smckusick else 25638420Smckusick clflg = 0; 25738420Smckusick rem = nfsm_rndup(siz)-siz; 25838420Smckusick mp2 = *mq; 25938420Smckusick while (siz > 0) { 26038420Smckusick if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 26138420Smckusick return(EINVAL); 26238420Smckusick left = uiop->uio_iov->iov_len; 26338420Smckusick uiocp = uiop->uio_iov->iov_base; 26438420Smckusick if (left > siz) 26538420Smckusick left = siz; 26638420Smckusick uiosiz = left; 26738420Smckusick while (left > 0) { 26838420Smckusick MGET(mp, M_WAIT, MT_DATA); 26938420Smckusick if (clflg) 27038420Smckusick NFSMCLGET(mp, M_WAIT); 27138420Smckusick mp->m_len = NFSMSIZ(mp); 27238420Smckusick mp2->m_next = mp; 27338420Smckusick mp2 = mp; 27438420Smckusick xfer = (left > mp->m_len) ? mp->m_len : left; 27538420Smckusick #ifdef notdef 27638420Smckusick /* Not Yet.. */ 27738420Smckusick if (uiop->uio_iov->iov_op != NULL) 27838420Smckusick (*(uiop->uio_iov->iov_op)) 27938420Smckusick (uiocp, mtod(mp, caddr_t), xfer); 28038420Smckusick else 28138420Smckusick #endif 28238420Smckusick if (uiop->uio_segflg == UIO_SYSSPACE) 28338420Smckusick bcopy(uiocp, mtod(mp, caddr_t), xfer); 28438420Smckusick else 28538420Smckusick copyin(uiocp, mtod(mp, caddr_t), xfer); 28638420Smckusick len = mp->m_len; 28738420Smckusick mp->m_len = xfer; 28838420Smckusick left -= xfer; 28938420Smckusick uiocp += xfer; 29038420Smckusick uiop->uio_resid -= xfer; 29138420Smckusick } 29238420Smckusick if (uiop->uio_iov->iov_len <= siz) { 29338420Smckusick uiop->uio_iovcnt--; 29438420Smckusick uiop->uio_iov++; 29538420Smckusick } else { 29638420Smckusick uiop->uio_iov->iov_base += uiosiz; 29738420Smckusick uiop->uio_iov->iov_len -= uiosiz; 29838420Smckusick } 29938420Smckusick siz -= uiosiz; 30038420Smckusick } 30138420Smckusick if (rem > 0) { 30238420Smckusick if (rem > (len-mp->m_len)) { 30338420Smckusick MGET(mp, M_WAIT, MT_DATA); 30438420Smckusick mp->m_len = 0; 30538420Smckusick mp2->m_next = mp; 30638420Smckusick } 30738420Smckusick cp = mtod(mp, caddr_t)+mp->m_len; 30838420Smckusick for (left = 0; left < rem; left++) 30938420Smckusick *cp++ = '\0'; 31038420Smckusick mp->m_len += rem; 31138420Smckusick *bpos = cp; 31238420Smckusick } else 31338420Smckusick *bpos = mtod(mp, caddr_t)+mp->m_len; 31438420Smckusick *mq = mp; 31538420Smckusick return(0); 31638420Smckusick } 31738420Smckusick 31838420Smckusick /* 31938420Smckusick * Help break down an mbuf chain by setting the first siz bytes contiguous 32038420Smckusick * pointed to by returned val. 32138420Smckusick * If Updateflg == True we can overwrite the first part of the mbuf data 32238420Smckusick * This is used by the macros nfsm_disect and nfsm_disecton for tough 32338420Smckusick * cases. (The macros use the vars. dpos and dpos2) 32438420Smckusick */ 32538420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2) 32638420Smckusick struct mbuf **mdp; 32738420Smckusick caddr_t *dposp; 32838420Smckusick int siz; 32938420Smckusick int left; 33038420Smckusick int updateflg; 33138420Smckusick caddr_t *cp2; 33238420Smckusick { 33338420Smckusick register struct mbuf *mp, *mp2; 33438420Smckusick register int siz2, xfer; 33538420Smckusick register caddr_t p; 33638420Smckusick 33738420Smckusick mp = *mdp; 33838420Smckusick while (left == 0) { 33938420Smckusick *mdp = mp = mp->m_next; 34038420Smckusick if (mp == NULL) 34138420Smckusick return(EBADRPC); 34238420Smckusick left = mp->m_len; 34338420Smckusick *dposp = mtod(mp, caddr_t); 34438420Smckusick } 34538420Smckusick if (left >= siz) { 34638420Smckusick *cp2 = *dposp; 34738420Smckusick *dposp += siz; 34838420Smckusick return(0); 34938420Smckusick } else if (mp->m_next == NULL) { 35038420Smckusick return(EBADRPC); 35138420Smckusick } else if (siz > MCLBYTES) { 35238420Smckusick panic("nfs S too big"); 35338420Smckusick } else { 35438420Smckusick /* Iff update, you can overwrite, else must alloc new mbuf */ 35538420Smckusick if (updateflg) { 35638420Smckusick NFSMINOFF(mp); 35738420Smckusick } else { 35838420Smckusick MGET(mp2, M_WAIT, MT_DATA); 35938420Smckusick mp2->m_next = mp->m_next; 36038420Smckusick mp->m_next = mp2; 36138420Smckusick mp->m_len -= left; 36238420Smckusick mp = mp2; 36338420Smckusick } 36438420Smckusick /* Alloc cluster iff we need it */ 36538420Smckusick if (!M_HASCL(mp) && siz > NFSMSIZ(mp)) { 36638420Smckusick NFSMCLGET(mp, M_WAIT); 36738420Smckusick if (!M_HASCL(mp)) 36838420Smckusick return(ENOBUFS); 36938420Smckusick } 37038420Smckusick *cp2 = p = mtod(mp, caddr_t); 37138420Smckusick bcopy(*dposp, p, left); /* Copy what was left */ 37238420Smckusick siz2 = siz-left; 37338420Smckusick p += left; 37438420Smckusick mp2 = mp->m_next; 37538420Smckusick /* Loop arround copying up the siz2 bytes */ 37638420Smckusick while (siz2 > 0) { 37738420Smckusick if (mp2 == NULL) 37838420Smckusick return (EBADRPC); 37938420Smckusick xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 38038420Smckusick bcopy(mtod(mp2, caddr_t), p, xfer); 38138420Smckusick NFSMADV(mp2, xfer); 38238420Smckusick mp2->m_len -= xfer; 38338420Smckusick siz2 -= xfer; 38438420Smckusick if (siz2 > 0) 38538420Smckusick mp2 = mp2->m_next; 38638420Smckusick } 38738420Smckusick mp->m_len = siz; 38838420Smckusick *mdp = mp2; 38938420Smckusick *dposp = mtod(mp2, caddr_t); 39038420Smckusick } 391*39494Smckusick return (0); 39238420Smckusick } 39338420Smckusick 39438420Smckusick /* 39538420Smckusick * Advance the position in the mbuf chain with/without freeing mbufs 39638420Smckusick */ 39738420Smckusick nfs_adv(mdp, dposp, offs, left) 39838420Smckusick struct mbuf **mdp; 39938420Smckusick caddr_t *dposp; 40038420Smckusick int offs; 40138420Smckusick int left; 40238420Smckusick { 40338420Smckusick register struct mbuf *m; 40438420Smckusick register int s; 40538420Smckusick 40638420Smckusick m = *mdp; 40738420Smckusick s = left; 40838420Smckusick while (s < offs) { 40938420Smckusick offs -= s; 41038420Smckusick m = m->m_next; 41138420Smckusick if (m == NULL) 41238420Smckusick return(EBADRPC); 41338420Smckusick s = m->m_len; 41438420Smckusick } 41538420Smckusick *mdp = m; 41638420Smckusick *dposp = mtod(m, caddr_t)+offs; 41738420Smckusick return(0); 41838420Smckusick } 41938420Smckusick 42038420Smckusick /* 42138420Smckusick * Copy a string into mbufs for the hard cases... 42238420Smckusick */ 42338420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz) 42438420Smckusick struct mbuf **mb; 42538420Smckusick char **bpos; 42638420Smckusick char *cp; 42738420Smckusick long siz; 42838420Smckusick { 42938420Smckusick register struct mbuf *m1, *m2; 43038420Smckusick long left, xfer, len, tlen; 43138420Smckusick u_long *p; 43238420Smckusick int putsize; 43338420Smckusick 43438420Smckusick putsize = 1; 43538420Smckusick m2 = *mb; 43638420Smckusick left = NFSMSIZ(m2)-m2->m_len; 43738420Smckusick if (left > 0) { 43838420Smckusick p = ((u_long *)(*bpos)); 43938420Smckusick *p++ = txdr_unsigned(siz); 44038420Smckusick putsize = 0; 44138420Smckusick left -= NFSX_UNSIGNED; 44238420Smckusick m2->m_len += NFSX_UNSIGNED; 44338420Smckusick if (left > 0) { 44438420Smckusick bcopy(cp, (caddr_t) p, left); 44538420Smckusick siz -= left; 44638420Smckusick cp += left; 44738420Smckusick m2->m_len += left; 44838420Smckusick left = 0; 44938420Smckusick } 45038420Smckusick } 45138420Smckusick /* Loop arround adding mbufs */ 45238420Smckusick while (siz > 0) { 45338420Smckusick MGET(m1, M_WAIT, MT_DATA); 45438420Smckusick if (siz > MLEN) 45538420Smckusick NFSMCLGET(m1, M_WAIT); 45638420Smckusick m1->m_len = NFSMSIZ(m1); 45738420Smckusick m2->m_next = m1; 45838420Smckusick m2 = m1; 45938420Smckusick p = mtod(m1, u_long *); 46038420Smckusick tlen = 0; 46138420Smckusick if (putsize) { 46238420Smckusick *p++ = txdr_unsigned(siz); 46338420Smckusick m1->m_len -= NFSX_UNSIGNED; 46438420Smckusick tlen = NFSX_UNSIGNED; 46538420Smckusick putsize = 0; 46638420Smckusick } 46738420Smckusick if (siz < m1->m_len) { 46838420Smckusick len = nfsm_rndup(siz); 46938420Smckusick xfer = siz; 47038420Smckusick if (xfer < len) 47138420Smckusick *(p+(xfer>>2)) = 0; 47238420Smckusick } else { 47338420Smckusick xfer = len = m1->m_len; 47438420Smckusick } 47538420Smckusick bcopy(cp, (caddr_t) p, xfer); 47638420Smckusick m1->m_len = len+tlen; 47738420Smckusick siz -= xfer; 47838420Smckusick cp += xfer; 47938420Smckusick } 48038420Smckusick *mb = m1; 48138420Smckusick *bpos = mtod(m1, caddr_t)+m1->m_len; 48238420Smckusick return(0); 48338420Smckusick } 48438420Smckusick 48538420Smckusick /* 48638420Smckusick * Called once to initialize data structures... 48738420Smckusick */ 48839444Smckusick nfs_init() 48938420Smckusick { 49038420Smckusick register int i; 49138420Smckusick 49238420Smckusick rpc_vers = txdr_unsigned(RPC_VER2); 49338420Smckusick rpc_call = txdr_unsigned(RPC_CALL); 49438420Smckusick rpc_reply = txdr_unsigned(RPC_REPLY); 49538420Smckusick rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 49638420Smckusick rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 49738420Smckusick rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 49838420Smckusick rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 49938420Smckusick nfs_vers = txdr_unsigned(NFS_VER2); 50038420Smckusick nfs_prog = txdr_unsigned(NFS_PROG); 50138420Smckusick nfs_true = txdr_unsigned(TRUE); 50238420Smckusick nfs_false = txdr_unsigned(FALSE); 50338420Smckusick /* Loop thru nfs procids */ 50438420Smckusick for (i = 0; i < NFS_NPROCS; i++) 50538420Smckusick nfs_procids[i] = txdr_unsigned(i); 50639345Smckusick /* Ensure async daemons disabled */ 50739345Smckusick for (i = 0; i < MAX_ASYNCDAEMON; i++) 50839345Smckusick nfs_iodwant[i] = (struct proc *)0; 50938420Smckusick v_type[0] = VNON; 51038420Smckusick v_type[1] = VREG; 51138420Smckusick v_type[2] = VDIR; 51238420Smckusick v_type[3] = VBLK; 51338420Smckusick v_type[4] = VCHR; 51438420Smckusick v_type[5] = VLNK; 51538420Smckusick nfs_xdrneg1 = txdr_unsigned(-1); 51638420Smckusick nfs_nhinit(); /* Init the nfsnode table */ 51738884Smacklem rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ); 51838420Smckusick /* And start timer */ 51938420Smckusick nfs_timer(); 52038420Smckusick } 52138420Smckusick 52238420Smckusick /* 52338420Smckusick * Fill in the rest of the rpc_unixauth and return it 52438420Smckusick */ 52538420Smckusick static char *nfs_unixauth(cr) 52638420Smckusick register struct ucred *cr; 52738420Smckusick { 52838420Smckusick register u_long *p; 52938420Smckusick register int i; 53038420Smckusick int ngr; 53138420Smckusick 53238420Smckusick /* Maybe someday there should be a cache of AUTH_SHORT's */ 53338420Smckusick if ((p = rpc_uidp) == NULL) { 53438425Smckusick #ifdef FILLINHOST 53538420Smckusick i = nfsm_rndup(hostnamelen)+(19*NFSX_UNSIGNED); 53638425Smckusick #else 53738425Smckusick i = 19*NFSX_UNSIGNED; 53838425Smckusick #endif 53938420Smckusick MALLOC(p, u_long *, i, M_TEMP, M_WAITOK); 54038420Smckusick bzero((caddr_t)p, i); 54138420Smckusick rpc_unixauth = (caddr_t)p; 54238420Smckusick *p++ = txdr_unsigned(RPCAUTH_UNIX); 54338420Smckusick p++; /* Fill in size later */ 54438420Smckusick *p++ = hostid; 54538425Smckusick #ifdef FILLINHOST 54638420Smckusick *p++ = txdr_unsigned(hostnamelen); 54738420Smckusick i = nfsm_rndup(hostnamelen); 54838420Smckusick bcopy(hostname, (caddr_t)p, hostnamelen); 54938420Smckusick p += (i>>2); 55038425Smckusick #else 55138425Smckusick *p++ = 0; 55238425Smckusick #endif 55338420Smckusick rpc_uidp = p; 55438420Smckusick } 55538420Smckusick *p++ = txdr_unsigned(cr->cr_uid); 55638420Smckusick *p++ = txdr_unsigned(cr->cr_groups[0]); 55738737Smckusick ngr = (cr->cr_ngroups > numgrps) ? numgrps : cr->cr_ngroups; 55838420Smckusick *p++ = txdr_unsigned(ngr); 55938420Smckusick for (i = 0; i < ngr; i++) 56038420Smckusick *p++ = txdr_unsigned(cr->cr_groups[i]); 56138420Smckusick /* And add the AUTH_NULL */ 56238420Smckusick *p++ = 0; 56338420Smckusick *p = 0; 56438420Smckusick i = (((caddr_t)p)-rpc_unixauth)-12; 56538420Smckusick p = (u_long *)(rpc_unixauth+4); 56638420Smckusick *p = txdr_unsigned(i); 56738420Smckusick return(rpc_unixauth); 56838420Smckusick } 56938420Smckusick 57038420Smckusick /* 57138420Smckusick * Attribute cache routines. 57238420Smckusick * nfs_loadattrcache() - loads or updates the cache contents from attributes 57338420Smckusick * that are on the mbuf list 57438420Smckusick * nfs_getattrcache() - returns valid attributes if found in cache, returns 57538420Smckusick * error otherwise 57638420Smckusick */ 57738420Smckusick 57838420Smckusick /* 57939444Smckusick * Load the attribute cache (that lives in the nfsnode entry) with 58038420Smckusick * the values on the mbuf list and 58138420Smckusick * Iff vap not NULL 58238420Smckusick * copy the attributes to *vaper 58338420Smckusick */ 58439457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper) 58539457Smckusick struct vnode **vpp; 58638420Smckusick struct mbuf **mdp; 58738420Smckusick caddr_t *dposp; 58838420Smckusick struct vattr *vaper; 58938420Smckusick { 59039457Smckusick register struct vnode *vp = *vpp; 59138420Smckusick register struct vattr *vap; 59238884Smacklem register struct nfsv2_fattr *fp; 59339444Smckusick extern struct vnodeops spec_nfsv2nodeops; 59439457Smckusick register struct nfsnode *np; 595*39494Smckusick register long t1; 596*39494Smckusick caddr_t dpos, cp2; 597*39494Smckusick int error = 0; 598*39494Smckusick struct mbuf *md; 59939444Smckusick enum vtype type; 60039444Smckusick dev_t rdev; 60139444Smckusick struct timeval mtime; 60239444Smckusick struct vnode *nvp; 60338420Smckusick 60438420Smckusick md = *mdp; 60538420Smckusick dpos = *dposp; 60638420Smckusick t1 = (mtod(md, caddr_t)+md->m_len)-dpos; 60738420Smckusick if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2)) 60838420Smckusick return (error); 60938884Smacklem fp = (struct nfsv2_fattr *)cp2; 61039444Smckusick type = nfstov_type(fp->fa_type); 61139444Smckusick rdev = fxdr_unsigned(dev_t, fp->fa_rdev); 61239444Smckusick fxdr_time(&fp->fa_mtime, &mtime); 61339444Smckusick /* 61439444Smckusick * If v_type == VNON it is a new node, so fill in the v_type, 61539444Smckusick * n_mtime fields. Check to see if it represents a special 61639444Smckusick * device, and if so, check for a possible alias. Once the 61739444Smckusick * correct vnode has been obtained, fill in the rest of the 61839444Smckusick * information. 61939444Smckusick */ 62038420Smckusick np = VTONFS(vp); 62139444Smckusick if (vp->v_type == VNON) { 62239444Smckusick vp->v_type = type; 62339444Smckusick if (vp->v_type == VCHR || vp->v_type == VBLK) { 62439444Smckusick vp->v_rdev = rdev; 62539444Smckusick vp->v_op = &spec_nfsv2nodeops; 62639444Smckusick if (nvp = checkalias(vp, vp->v_mount)) { 62739444Smckusick /* 62839444Smckusick * Reinitialize aliased node. 62939444Smckusick */ 63039444Smckusick np = VTONFS(nvp); 63139444Smckusick np->n_vnode = nvp; 63239444Smckusick np->n_flag = NLOCKED; 63339444Smckusick bcopy((caddr_t)&VTONFS(vp)->n_fh, 63439444Smckusick (caddr_t)&np->n_fh, NFSX_FH); 63539444Smckusick insque(np, nfs_hash(&np->n_fh)); 63639444Smckusick np->n_attrstamp = 0; 63739444Smckusick np->n_sillyrename = (struct sillyrename *)0; 63839444Smckusick /* 63939457Smckusick * Discard unneeded vnode and update actual one 64039444Smckusick */ 64139444Smckusick vput(vp); 64239457Smckusick *vpp = nvp; 64339444Smckusick } 64439444Smckusick } 64539444Smckusick np->n_mtime = mtime.tv_sec; 64639444Smckusick } 64738420Smckusick vap = &np->n_vattr; 64839444Smckusick vap->va_type = type; 64938884Smacklem vap->va_mode = nfstov_mode(fp->fa_mode); 65038884Smacklem vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 65138884Smacklem vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 65238884Smacklem vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 65338884Smacklem vap->va_size = fxdr_unsigned(u_long, fp->fa_size); 65438884Smacklem if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size) 65538884Smacklem np->n_size = vap->va_size; 65638420Smckusick vap->va_size1 = 0; /* OR -1 ?? */ 65738884Smacklem vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize); 65839444Smckusick vap->va_rdev = rdev; 65938884Smacklem vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * vap->va_blocksize; 66038884Smacklem vap->va_bytes1 = 0; 66138884Smacklem vap->va_fsid = fxdr_unsigned(long, fp->fa_fsid); 66238884Smacklem vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid); 66338884Smacklem fxdr_time(&fp->fa_atime, &vap->va_atime); 66438884Smacklem fxdr_time(&fp->fa_ctime, &vap->va_ctime); 66539444Smckusick vap->va_mtime = mtime; 66638579Smckusick vap->va_gen = 0; 66738579Smckusick vap->va_flags = 0; 66838420Smckusick np->n_attrstamp = time.tv_sec; 66938420Smckusick *dposp = dpos; 67038420Smckusick *mdp = md; 67138884Smacklem if (vaper != NULL) { 67238420Smckusick bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 67338884Smacklem if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size)) 67438884Smacklem vaper->va_size = np->n_size; 67538884Smacklem } 67638420Smckusick return (0); 67738420Smckusick } 67838420Smckusick 67938420Smckusick /* 68038420Smckusick * Check the time stamp 68138420Smckusick * If the cache is valid, copy contents to *vap and return 0 68238420Smckusick * otherwise return an error 68338420Smckusick */ 68438420Smckusick nfs_getattrcache(vp, vap) 68538420Smckusick register struct vnode *vp; 68638420Smckusick struct vattr *vap; 68738420Smckusick { 68838420Smckusick register struct nfsnode *np; 68938420Smckusick 69038420Smckusick np = VTONFS(vp); 69138420Smckusick if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) { 69238420Smckusick nfsstats.attrcache_hits++; 69338420Smckusick bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr)); 69439361Smckusick if ((np->n_flag & NMODIFIED) == 0) 69539361Smckusick np->n_size = vap->va_size; 69639361Smckusick else if (np->n_size > vap->va_size) 69738884Smacklem vap->va_size = np->n_size; 69838420Smckusick return (0); 69938420Smckusick } else { 70038420Smckusick nfsstats.attrcache_misses++; 70138420Smckusick return (ENOENT); 70238420Smckusick } 70338420Smckusick } 70438420Smckusick 70538420Smckusick /* 70638420Smckusick * nfs_namei - a liitle like namei(), but for one element only 70738420Smckusick * essentially look up file handle, fill in ndp and call VOP_LOOKUP() 70838420Smckusick */ 70938420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp) 71038420Smckusick register struct nameidata *ndp; 71138420Smckusick fhandle_t *fhp; 71238420Smckusick int len; 71338420Smckusick struct mbuf **mdp; 71438420Smckusick caddr_t *dposp; 71538420Smckusick { 71638420Smckusick register int i, rem; 71738420Smckusick register struct mbuf *md; 71838420Smckusick register char *cp; 71938420Smckusick struct vnode *dp = (struct vnode *)0; 72038420Smckusick int flag; 72138420Smckusick int docache; 72238420Smckusick int wantparent; 72338420Smckusick int lockparent; 72438420Smckusick int error = 0; 72538420Smckusick 72638420Smckusick ndp->ni_vp = ndp->ni_dvp = (struct vnode *)0; 72738420Smckusick flag = ndp->ni_nameiop & OPFLAG; 72838420Smckusick wantparent = ndp->ni_nameiop & (LOCKPARENT | WANTPARENT); 72938420Smckusick lockparent = ndp->ni_nameiop & LOCKPARENT; 73038420Smckusick docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 73138420Smckusick if (flag == DELETE || wantparent) 73238420Smckusick docache = 0; 73338420Smckusick 73438420Smckusick /* Fill in the nameidata and call lookup */ 73538420Smckusick cp = *dposp; 73638420Smckusick md = *mdp; 73738420Smckusick rem = mtod(md, caddr_t)+md->m_len-cp; 73838420Smckusick ndp->ni_hash = 0; 73938420Smckusick for (i = 0; i < len;) { 74038420Smckusick if (rem == 0) { 74138420Smckusick md = md->m_next; 74238420Smckusick if (md == NULL) 74338420Smckusick return (EBADRPC); 74438420Smckusick cp = mtod(md, caddr_t); 74538420Smckusick rem = md->m_len; 74638420Smckusick } 74738420Smckusick if (*cp == '\0' || *cp == '/') 74838420Smckusick return (EINVAL); 74938420Smckusick if (*cp & 0200) 75038420Smckusick if ((*cp&0377) == ('/'|0200) || flag != DELETE) 75138420Smckusick return (EINVAL); 75238420Smckusick ndp->ni_dent.d_name[i++] = *cp; 75338420Smckusick ndp->ni_hash += (unsigned char)*cp * i; 75438420Smckusick cp++; 75538420Smckusick rem--; 75638420Smckusick } 75738420Smckusick *mdp = md; 75838420Smckusick len = nfsm_rndup(len)-len; 75938420Smckusick if (len > 0) 76038420Smckusick *dposp = cp+len; 76138420Smckusick else 76238420Smckusick *dposp = cp; 76338420Smckusick ndp->ni_namelen = i; 76438420Smckusick ndp->ni_dent.d_namlen = i; 76538420Smckusick ndp->ni_dent.d_name[i] = '\0'; 76638425Smckusick ndp->ni_pathlen = 1; 76738420Smckusick ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0]; 76838420Smckusick ndp->ni_next = &ndp->ni_dent.d_name[i]; 76938420Smckusick ndp->ni_loopcnt = 0; /* Not actually used for now */ 77038420Smckusick ndp->ni_endoff = 0; 77138420Smckusick if (docache) 77238420Smckusick ndp->ni_makeentry = 1; 77338420Smckusick else 77438420Smckusick ndp->ni_makeentry = 0; 77538420Smckusick ndp->ni_isdotdot = (i == 2 && 77638420Smckusick ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.'); 77738420Smckusick 77838420Smckusick if (error = nfsrv_fhtovp(fhp, TRUE, &dp, ndp->ni_cred)) 77938420Smckusick return (error); 78038425Smckusick if (dp->v_type != VDIR) { 78138425Smckusick vput(dp); 78238425Smckusick return (ENOTDIR); 78338425Smckusick } 78439345Smckusick /* 78539345Smckusick * Must set current directory here to avoid confusion in namei() 78639345Smckusick * called from rename() 78739345Smckusick */ 78838425Smckusick ndp->ni_cdir = dp; 78938425Smckusick ndp->ni_rdir = (struct vnode *)0; 79038420Smckusick 79138420Smckusick /* 79238425Smckusick * Handle "..": 79338425Smckusick * If this vnode is the root of the mounted 79438425Smckusick * file system, then ignore it so can't get out 79538420Smckusick */ 79638425Smckusick if (ndp->ni_isdotdot && (dp->v_flag & VROOT)) { 79738425Smckusick ndp->ni_dvp = dp; 79838425Smckusick ndp->ni_vp = dp; 79938425Smckusick VREF(dp); 80038425Smckusick goto nextname; 80138420Smckusick } 80238420Smckusick 80338420Smckusick /* 80438420Smckusick * We now have a segment name to search for, and a directory to search. 80538420Smckusick */ 80638420Smckusick if (error = VOP_LOOKUP(dp, ndp)) { 80738420Smckusick if (ndp->ni_vp != NULL) 80838420Smckusick panic("leaf should be empty"); 80938420Smckusick /* 81038420Smckusick * If creating and at end of pathname, then can consider 81138420Smckusick * allowing file to be created. 81238420Smckusick */ 81338420Smckusick if (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY | M_EXRDONLY)) 81438420Smckusick error = EROFS; 81538420Smckusick if (flag == LOOKUP || flag == DELETE || error != ENOENT) 81638420Smckusick goto bad; 81738420Smckusick /* 81838420Smckusick * We return with ni_vp NULL to indicate that the entry 81938420Smckusick * doesn't currently exist, leaving a pointer to the 82038420Smckusick * (possibly locked) directory inode in ndp->ni_dvp. 82138420Smckusick */ 82238420Smckusick return (0); /* should this be ENOENT? */ 82338420Smckusick } 82438420Smckusick 82538420Smckusick dp = ndp->ni_vp; 82638420Smckusick 82738420Smckusick nextname: 82838425Smckusick ndp->ni_ptr = ndp->ni_next; 82938420Smckusick /* 83038425Smckusick * Check for read-only file systems 83138420Smckusick */ 83238421Smckusick if (flag == DELETE || flag == RENAME) { 83338421Smckusick /* 83438421Smckusick * Disallow directory write attempts on read-only 83538421Smckusick * file systems. 83638421Smckusick */ 83738421Smckusick if ((dp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)) || 83838425Smckusick (wantparent && (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)))) { 83938421Smckusick error = EROFS; 84038421Smckusick goto bad2; 84138421Smckusick } 84238421Smckusick } 84338420Smckusick 84438420Smckusick if (!wantparent) 84538420Smckusick vrele(ndp->ni_dvp); 84638420Smckusick 84738420Smckusick if ((ndp->ni_nameiop & LOCKLEAF) == 0) 84838420Smckusick VOP_UNLOCK(dp); 84938420Smckusick return (0); 85038420Smckusick 85138420Smckusick bad2: 85238420Smckusick if (lockparent) 85338420Smckusick VOP_UNLOCK(ndp->ni_dvp); 85438420Smckusick vrele(ndp->ni_dvp); 85538420Smckusick bad: 85638420Smckusick vput(dp); 85738420Smckusick ndp->ni_vp = NULL; 85838420Smckusick return (error); 85938420Smckusick } 86038420Smckusick 86138420Smckusick /* 86238420Smckusick * A fiddled version of m_adj() that ensures null fill to a long 86338420Smckusick * boundary and only trims off the back end 86438420Smckusick */ 86538420Smckusick nfsm_adj(mp, len, nul) 86638420Smckusick struct mbuf *mp; 86738420Smckusick register int len; 86838420Smckusick int nul; 86938420Smckusick { 87038420Smckusick register struct mbuf *m; 87138420Smckusick register int count, i; 87238420Smckusick register char *cp; 87338420Smckusick 87438420Smckusick /* 87538420Smckusick * Trim from tail. Scan the mbuf chain, 87638420Smckusick * calculating its length and finding the last mbuf. 87738420Smckusick * If the adjustment only affects this mbuf, then just 87838420Smckusick * adjust and return. Otherwise, rescan and truncate 87938420Smckusick * after the remaining size. 88038420Smckusick */ 88138420Smckusick count = 0; 88238420Smckusick m = mp; 88338420Smckusick for (;;) { 88438420Smckusick count += m->m_len; 88538420Smckusick if (m->m_next == (struct mbuf *)0) 88638420Smckusick break; 88738420Smckusick m = m->m_next; 88838420Smckusick } 88938579Smckusick if (m->m_len > len) { 89038420Smckusick m->m_len -= len; 89138420Smckusick if (nul > 0) { 89238420Smckusick cp = mtod(m, caddr_t)+m->m_len-nul; 89338420Smckusick for (i = 0; i < nul; i++) 89438420Smckusick *cp++ = '\0'; 89538420Smckusick } 89638420Smckusick return; 89738420Smckusick } 89838420Smckusick count -= len; 89938420Smckusick if (count < 0) 90038420Smckusick count = 0; 90138420Smckusick /* 90238420Smckusick * Correct length for chain is "count". 90338420Smckusick * Find the mbuf with last data, adjust its length, 90438420Smckusick * and toss data from remaining mbufs on chain. 90538420Smckusick */ 90638420Smckusick for (m = mp; m; m = m->m_next) { 90738420Smckusick if (m->m_len >= count) { 90838420Smckusick m->m_len = count; 90938420Smckusick if (nul > 0) { 91038420Smckusick cp = mtod(m, caddr_t)+m->m_len-nul; 91138420Smckusick for (i = 0; i < nul; i++) 91238420Smckusick *cp++ = '\0'; 91338420Smckusick } 91438420Smckusick break; 91538420Smckusick } 91638420Smckusick count -= m->m_len; 91738420Smckusick } 91838420Smckusick while (m = m->m_next) 91938420Smckusick m->m_len = 0; 92038420Smckusick } 92138420Smckusick 92238420Smckusick /* 92338420Smckusick * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 92438420Smckusick * - look up fsid in mount list (if not found ret error) 92538420Smckusick * - check that it is exported 92638420Smckusick * - get vp by calling VFS_FHTOVP() macro 92738420Smckusick * - if not lockflag unlock it with VOP_UNLOCK() 92838420Smckusick * - if cred->cr_uid == 0 set it to m_exroot 92938420Smckusick */ 93038420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred) 93138420Smckusick fhandle_t *fhp; 93238420Smckusick int lockflag; 93338420Smckusick struct vnode **vpp; 93438420Smckusick struct ucred *cred; 93538420Smckusick { 93638420Smckusick register struct mount *mp; 93738420Smckusick 93838420Smckusick if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 93938420Smckusick return (ESTALE); 94038420Smckusick if ((mp->m_flag & M_EXPORTED) == 0) 94138420Smckusick return (EACCES); 94238420Smckusick if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 94338420Smckusick return (ESTALE); 94438420Smckusick if (cred->cr_uid == 0) 94538420Smckusick cred->cr_uid = mp->m_exroot; 94638420Smckusick if (!lockflag) 94738420Smckusick VOP_UNLOCK(*vpp); 94838420Smckusick return (0); 94938420Smckusick } 950