add prototypes for NFS vnode ops
[unix-history] / usr / src / sys / nfs / nfs_syscalls.c
index 59e84ca..f0e30c2 100644 (file)
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)nfs_syscalls.c      7.7 (Berkeley) %G%
+ *     @(#)nfs_syscalls.c      7.25 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "user.h"
 #include "kernel.h"
 #include "file.h"
 #include "stat.h"
 #include "vnode.h"
 #include "mount.h"
 #include "proc.h"
 #include "kernel.h"
 #include "file.h"
 #include "stat.h"
 #include "vnode.h"
 #include "mount.h"
 #include "proc.h"
-#include "uio.h"
 #include "malloc.h"
 #include "buf.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "malloc.h"
 #include "buf.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
+#include "domain.h"
+#include "protosw.h"
+
+#include "../netinet/in.h"
+#include "../netinet/tcp.h"
+
 #include "nfsv2.h"
 #include "nfs.h"
 #include "nfsrvcache.h"
 #include "nfsv2.h"
 #include "nfs.h"
 #include "nfsrvcache.h"
-#include "tsleep.h"
 
 /* Global defs. */
 extern u_long nfs_prog, nfs_vers;
 extern int (*nfsrv_procs[NFS_NPROCS])();
 extern struct buf nfs_bqueue;
 
 /* Global defs. */
 extern u_long nfs_prog, nfs_vers;
 extern int (*nfsrv_procs[NFS_NPROCS])();
 extern struct buf nfs_bqueue;
-extern int nfs_asyncdaemons;
-extern struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
-struct file *getsock();
+extern int nfs_numasync;
+extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
+extern int nfs_tcpnodelay;
+struct mbuf *nfs_compress();
 
 
+#define        TRUE    1
+#define        FALSE   0
+
+static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
+static int compressreply[NFS_NPROCS] = {
+       FALSE,
+       TRUE,
+       TRUE,
+       FALSE,
+       TRUE,
+       TRUE,
+       FALSE,
+       FALSE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+       TRUE,
+};
 /*
  * NFS server system calls
  * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
  */
 /*
  * NFS server system calls
  * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
  */
-#define RETURN(value)  { u.u_error = (value); return; }
 
 /*
  * Get file handle system call
  */
 
 /*
  * Get file handle system call
  */
-getfh()
-{
-       register struct a {
+/* ARGSUSED */
+getfh(p, uap, retval)
+       struct proc *p;
+       register struct args {
                char    *fname;
                fhandle_t *fhp;
                char    *fname;
                fhandle_t *fhp;
-       } *uap = (struct a *)u.u_ap;
-       register struct nameidata *ndp = &u.u_nd;
+       } *uap;
+       int *retval;
+{
+       register struct nameidata *ndp;
        register struct vnode *vp;
        fhandle_t fh;
        int error;
        register struct vnode *vp;
        fhandle_t fh;
        int error;
+       struct nameidata nd;
 
        /*
         * Must be super user
         */
 
        /*
         * Must be super user
         */
-       if (error = suser(u.u_cred, &u.u_acflag))
-               RETURN (error);
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
+       ndp = &nd;
        ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
        ndp->ni_segflg = UIO_USERSPACE;
        ndp->ni_dirp = uap->fname;
        ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
        ndp->ni_segflg = UIO_USERSPACE;
        ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp))
-               RETURN (error);
+       if (error = namei(ndp, p))
+               return (error);
        vp = ndp->ni_vp;
        bzero((caddr_t)&fh, sizeof(fh));
        vp = ndp->ni_vp;
        bzero((caddr_t)&fh, sizeof(fh));
-       fh.fh_fsid = vp->v_mount->m_fsid;
+       fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
        error = VFS_VPTOFH(vp, &fh.fh_fid);
        vput(vp);
        if (error)
        error = VFS_VPTOFH(vp, &fh.fh_fid);
        vput(vp);
        if (error)
-               RETURN (error);
+               return (error);
        error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
        error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
-       RETURN (error);
-}
-
-/*
- * Mark a mount point in the filesystem exported
- */
-exportfs()
-{
-       register struct a {
-               char    *fname;
-               int     rootuid;
-               int     exflags;
-       } *uap = (struct a *)u.u_ap;
-       register struct nameidata *ndp = &u.u_nd;
-       register struct vnode *vp;
-       register struct mount *mp;
-       int error;
-
-       /*
-        * Must be super user
-        */
-       if (error = suser(u.u_cred, &u.u_acflag))
-               RETURN (error);
-       ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;   /* Or NOFOLLOW ?? */
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp))
-               RETURN (error);
-       vp = ndp->ni_vp;
-       if (vp->v_type != VDIR) {
-               vput(vp);
-               RETURN (ENOENT);
-       }
-       mp = vp->v_mount;
-
-       /*
-        * If the filesystem has already been exported, just relax
-        * security as required.
-        * Otherwise export it with the given security
-        */
-       if (mp->m_flag & M_EXPORTED) {
-               if (uap->rootuid == 0)
-                       mp->m_exroot = 0;
-               if ((uap->exflags & M_EXRDONLY) == 0)
-                       mp->m_flag &= ~M_EXRDONLY;
-       } else {
-               mp->m_exroot = uap->rootuid;
-               if (uap->exflags & M_EXRDONLY)
-                       mp->m_flag |= M_EXRDONLY;
-               mp->m_flag |= M_EXPORTED;
-       }
-       vput(vp);
-       RETURN (0);
+       return (error);
 }
 
 /*
  * Nfs server psuedo system call for the nfsd's
  * Never returns unless it fails or gets killed
  */
 }
 
 /*
  * Nfs server psuedo system call for the nfsd's
  * Never returns unless it fails or gets killed
  */
-nfssvc()
-{
-       register struct a {
+/* ARGSUSED */
+nfssvc(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int s;
                int s;
-               u_long ormask;
-               u_long matchbits;
-       } *uap = (struct a *)u.u_ap;
+               caddr_t mskval;
+               int msklen;
+               caddr_t mtchval;
+               int mtchlen;
+       } *uap;
+       int *retval;
+{
        register struct mbuf *m;
        register int siz;
        register struct ucred *cr;
        struct file *fp;
        struct mbuf *mreq, *mrep, *nam, *md;
        register struct mbuf *m;
        register int siz;
        register struct ucred *cr;
        struct file *fp;
        struct mbuf *mreq, *mrep, *nam, *md;
+       struct mbuf msk, mtch;
        struct socket *so;
        caddr_t dpos;
        struct socket *so;
        caddr_t dpos;
-       int procid;
+       int procid, repstat, error, cacherep, wascomp;
        u_long retxid;
        u_long retxid;
-       u_long msk, mtch;
-       int repstat;
-       int error;
 
        /*
         * Must be super user
         */
 
        /*
         * Must be super user
         */
-       if (error = suser(u.u_cred, &u.u_acflag))
-               RETURN (error);
-       fp = getsock(uap->s);
-       if (fp == 0)
-               return;
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
        so = (struct socket *)fp->f_data;
        so = (struct socket *)fp->f_data;
-       cr = u.u_cred = crcopy(u.u_cred);       /* Copy it so others don't see changes */
-       msk = uap->ormask;
-       mtch = uap->matchbits;
+       if (sosendallatonce(so))
+               siz = NFS_MAXPACKET;
+       else
+               siz = NFS_MAXPACKET + sizeof(u_long);
+       if (error = soreserve(so, siz, siz))
+               goto bad;
+       if (error = sockargs(&nam, uap->mskval, uap->msklen, MT_SONAME))
+               goto bad;
+       bcopy((caddr_t)nam, (caddr_t)&msk, sizeof (struct mbuf));
+       msk.m_data = msk.m_dat;
+       m_freem(nam);
+       if (error = sockargs(&nam, uap->mtchval, uap->mtchlen, MT_SONAME))
+               goto bad;
+       bcopy((caddr_t)nam, (caddr_t)&mtch, sizeof (struct mbuf));
+       mtch.m_data = mtch.m_dat;
+       m_freem(nam);
+
+       /* Copy the cred so others don't see changes */
+       cr = p->p_ucred = crcopy(p->p_ucred);
+
+       /*
+        * Set protocol specific options { for now TCP only } and
+        * reserve some space. For datagram sockets, this can get called
+        * repeatedly for the same socket, but that isn't harmful.
+        */
+       if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
+               MGET(m, M_WAIT, MT_SOOPTS);
+               *mtod(m, int *) = 1;
+               m->m_len = sizeof(int);
+               sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
+       }
+       if (so->so_proto->pr_domain->dom_family == AF_INET &&
+           so->so_proto->pr_protocol == IPPROTO_TCP &&
+           nfs_tcpnodelay) {
+               MGET(m, M_WAIT, MT_SOOPTS);
+               *mtod(m, int *) = 1;
+               m->m_len = sizeof(int);
+               sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
+       }
+       so->so_rcv.sb_flags &= ~SB_NOINTR;
+       so->so_rcv.sb_timeo = 0;
+       so->so_snd.sb_flags &= ~SB_NOINTR;
+       so->so_snd.sb_timeo = 0;
+
        /*
         * Just loop around doin our stuff until SIGKILL
         */
        for (;;) {
                if (error = nfs_getreq(so, nfs_prog, nfs_vers, NFS_NPROCS-1,
        /*
         * Just loop around doin our stuff until SIGKILL
         */
        for (;;) {
                if (error = nfs_getreq(so, nfs_prog, nfs_vers, NFS_NPROCS-1,
-                  &nam, &mrep, &md, &dpos, &retxid, &procid, cr, msk, mtch)) {
-                       m_freem(nam);
+                  &nam, &mrep, &md, &dpos, &retxid, &procid, cr,
+                  &msk, &mtch, &wascomp)) {
+                       if (nam)
+                               m_freem(nam);
+                       if (error == EPIPE || error == EINTR ||
+                           error == ERESTART) {
+                               error = 0;
+                               goto bad;
+                       }
+                       so->so_error = 0;
                        continue;
                }
                        continue;
                }
-               switch (nfsrv_getcache(nam, retxid, procid, &mreq)) {
+
+               if (nam)
+                       cacherep = nfsrv_getcache(nam, retxid, procid, &mreq);
+               else
+                       cacherep = RC_DOIT;
+               switch (cacherep) {
                case RC_DOIT:
                        if (error = (*(nfsrv_procs[procid]))(mrep, md, dpos,
                                cr, retxid, &mreq, &repstat)) {
                case RC_DOIT:
                        if (error = (*(nfsrv_procs[procid]))(mrep, md, dpos,
                                cr, retxid, &mreq, &repstat)) {
-                               m_freem(nam);
                                nfsstats.srv_errs++;
                                nfsstats.srv_errs++;
+                               if (nam) {
+                                       nfsrv_updatecache(nam, retxid, procid,
+                                               FALSE, repstat, mreq);
+                                       m_freem(nam);
+                               }
                                break;
                        }
                        nfsstats.srvrpccnt[procid]++;
                                break;
                        }
                        nfsstats.srvrpccnt[procid]++;
-                       nfsrv_updatecache(nam, retxid, procid, repstat, mreq);
+                       if (nam)
+                               nfsrv_updatecache(nam, retxid, procid, TRUE,
+                                       repstat, mreq);
                        mrep = (struct mbuf *)0;
                case RC_REPLY:
                        m = mreq;
                        mrep = (struct mbuf *)0;
                case RC_REPLY:
                        m = mreq;
@@ -204,14 +236,35 @@ nfssvc()
                                siz += m->m_len;
                                m = m->m_next;
                        }
                                siz += m->m_len;
                                m = m->m_next;
                        }
-                       if (siz <= 0 || siz > 9216) {
+                       if (siz <= 0 || siz > NFS_MAXPACKET) {
                                printf("mbuf siz=%d\n",siz);
                                panic("Bad nfs svc reply");
                        }
                                printf("mbuf siz=%d\n",siz);
                                panic("Bad nfs svc reply");
                        }
-                       error = nfs_send(so, nam, mreq, 0, siz);
-                       m_freem(nam);
+                       mreq->m_pkthdr.len = siz;
+                       mreq->m_pkthdr.rcvif = (struct ifnet *)0;
+                       if (wascomp && compressreply[procid]) {
+                               mreq = nfs_compress(mreq);
+                               siz = mreq->m_pkthdr.len;
+                       }
+                       /*
+                        * For non-atomic protocols, prepend a Sun RPC
+                        * Record Mark.
+                        */
+                       if (!sosendallatonce(so)) {
+                               M_PREPEND(mreq, sizeof(u_long), M_WAIT);
+                               *mtod(mreq, u_long *) = htonl(0x80000000 | siz);
+                       }
+                       error = nfs_send(so, nam, mreq, (struct nfsreq *)0);
+                       if (nam)
+                               m_freem(nam);
                        if (mrep)
                                m_freem(mrep);
                        if (mrep)
                                m_freem(mrep);
+                       if (error) {
+                               if (error == EPIPE || error == EINTR ||
+                                   error == ERESTART)
+                                       goto bad;
+                               so->so_error = 0;
+                       }
                        break;
                case RC_DROPIT:
                        m_freem(mrep);
                        break;
                case RC_DROPIT:
                        m_freem(mrep);
@@ -219,6 +272,8 @@ nfssvc()
                        break;
                };
        }
                        break;
                };
        }
+bad:
+       return (error);
 }
 
 /*
 }
 
 /*
@@ -227,41 +282,60 @@ nfssvc()
  * for client nfs. They are mainly here for read ahead/write behind.
  * Never returns unless it fails or gets killed
  */
  * for client nfs. They are mainly here for read ahead/write behind.
  * Never returns unless it fails or gets killed
  */
-async_daemon()
+/* ARGSUSED */
+async_daemon(p, uap, retval)
+       struct proc *p;
+       struct args *uap;
+       int *retval;
 {
        register struct buf *bp, *dp;
 {
        register struct buf *bp, *dp;
+       register int i, myiod;
        int error;
        int error;
-       int myiod;
 
        /*
         * Must be super user
         */
 
        /*
         * Must be super user
         */
-       if (error = suser(u.u_cred, &u.u_acflag))
-               RETURN (error);
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
        /*
         * Assign my position or return error if too many already running
         */
        /*
         * Assign my position or return error if too many already running
         */
-       if (nfs_asyncdaemons > MAX_ASYNCDAEMON)
-               RETURN (EBUSY);
-       myiod = nfs_asyncdaemons++;
+       myiod = -1;
+       for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
+               if (nfs_asyncdaemon[i] == 0) {
+                       nfs_asyncdaemon[i]++;
+                       myiod = i;
+                       break;
+               }
+       if (myiod == -1)
+               return (EBUSY);
+       nfs_numasync++;
        dp = &nfs_bqueue;
        /*
         * Just loop around doin our stuff until SIGKILL
         */
        for (;;) {
        dp = &nfs_bqueue;
        /*
         * Just loop around doin our stuff until SIGKILL
         */
        for (;;) {
-               while (dp->b_actf == NULL) {
-                       nfs_iodwant[myiod] = u.u_procp;
-                       tsleep((caddr_t)&nfs_iodwant[myiod], PZERO+1, 
-                               SLP_NFS_IOD, 0);
+               while (dp->b_actf == NULL && error == 0) {
+                       nfs_iodwant[myiod] = curproc;
+                       error = tsleep((caddr_t)&nfs_iodwant[myiod],
+                               PWAIT | PCATCH, "nfsidl", 0);
+                       nfs_iodwant[myiod] = (struct proc *)0;
+               }
+               while (dp->b_actf != NULL) {
+                       /* Take one off the end of the list */
+                       bp = dp->b_actl;
+                       if (bp->b_actl == dp) {
+                               dp->b_actf = dp->b_actl = (struct buf *)0;
+                       } else {
+                               dp->b_actl = bp->b_actl;
+                               bp->b_actl->b_actf = dp;
+                       }
+                       (void) nfs_doio(bp);
                }
                }
-               /* Take one off the end of the list */
-               bp = dp->b_actl;
-               if (bp->b_actl == dp) {
-                       dp->b_actf = dp->b_actl = (struct buf *)0;
-               } else {
-                       dp->b_actl = bp->b_actl;
-                       bp->b_actl->b_actf = dp;
+               if (error) {
+                       nfs_asyncdaemon[myiod] = 0;
+                       nfs_numasync--;
+                       return (error);
                }
                }
-               (void) nfs_doio(bp);
        }
 }
        }
 }