add memory filesystem initialization
[unix-history] / usr / src / sys / kern / sys_generic.c
index 351a1e2..27ba545 100644 (file)
@@ -1,28 +1,35 @@
-/*     sys_generic.c   5.31    83/02/18        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/ioctl.h"
-#include "../h/tty.h"
-#include "../h/file.h"
-#include "../h/inode.h"
-#include "../h/buf.h"
-#include "../h/proc.h"
-#include "../h/conf.h"
-#include "../h/socket.h"
-#include "../h/socketvar.h"
-#include "../h/fs.h"
-#ifdef MUSH
-#include "../h/quota.h"
-#include "../h/share.h"
-#else
-#define        CHARGE(nothing)
+/*
+ * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
+ * All rights reserved.
+ *
+ * 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.
+ *
+ *     @(#)sys_generic.c       7.12 (Berkeley) %G%
+ */
+
+#include "param.h"
+#include "systm.h"
+#include "syscontext.h"
+#include "ioctl.h"
+#include "file.h"
+#include "proc.h"
+#include "uio.h"
+#include "kernel.h"
+#include "stat.h"
+#include "malloc.h"
+#ifdef KTRACE
+#include "ktrace.h"
 #endif
 #endif
-#include "../h/descrip.h"
-#include "../h/uio.h"
-#include "../h/cmap.h"
 
 /*
  * Read system call.
 
 /*
  * Read system call.
@@ -34,14 +41,51 @@ read()
                char    *cbuf;
                unsigned count;
        } *uap = (struct a *)u.u_ap;
                char    *cbuf;
                unsigned count;
        } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
        struct uio auio;
        struct iovec aiov;
        struct uio auio;
        struct iovec aiov;
+       long cnt, error = 0;
+#ifdef KTRACE
+       struct iovec ktriov;
+#endif
 
 
+       if (((unsigned)uap->fdes) >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL ||
+           (fp->f_flag & FREAD) == 0)
+               RETURN (EBADF);
+       if (uap->count < 0)
+               RETURN (EINVAL);
        aiov.iov_base = (caddr_t)uap->cbuf;
        aiov.iov_len = uap->count;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
        aiov.iov_base = (caddr_t)uap->cbuf;
        aiov.iov_len = uap->count;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
-       rwuio(&auio, UIO_READ);
+       auio.uio_resid = uap->count;
+       auio.uio_rw = UIO_READ;
+       auio.uio_segflg = UIO_USERSPACE;
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(u.u_procp, KTR_GENIO))
+               ktriov = aiov;
+#endif
+       cnt = uap->count;
+       if (setjmp(&u.u_qsave)) {
+               if (auio.uio_resid == cnt) {
+                       if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
+                               error = EINTR;
+                       else
+                               u.u_eosys = RESTARTSYS;
+               }
+       } else
+               error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (KTRPOINT(u.u_procp, KTR_GENIO))
+               ktrgenio(u.u_procp->p_tracep, uap->fdes, UIO_READ, &ktriov, cnt);
+#endif
+       u.u_r.r_val1 = cnt;
+       RETURN (error);
 }
 
 readv()
 }
 
 readv()
@@ -49,22 +93,81 @@ readv()
        register struct a {
                int     fdes;
                struct  iovec *iovp;
        register struct a {
                int     fdes;
                struct  iovec *iovp;
-               int     iovcnt;
+               unsigned iovcnt;
        } *uap = (struct a *)u.u_ap;
        } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
        struct uio auio;
        struct uio auio;
-       struct iovec aiov[16];          /* XXX */
+       register struct iovec *iov;
+       struct iovec aiov[UIO_SMALLIOV];
+       long i, cnt, error = 0;
+#ifdef KTRACE
+       struct iovec *ktriov = NULL;
+#endif
 
 
-       if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) {
-               u.u_error = EINVAL;
-               return;
-       }
-       auio.uio_iov = aiov;
+       if (((unsigned)uap->fdes) >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL ||
+           (fp->f_flag & FREAD) == 0)
+               RETURN (EBADF);
+       if (uap->iovcnt > UIO_SMALLIOV) {
+               if (uap->iovcnt > UIO_MAXIOV)
+                       RETURN (EINVAL);
+               MALLOC(iov, struct iovec *, 
+                     sizeof(struct iovec) * uap->iovcnt, M_IOV, M_WAITOK);
+       } else
+               iov = aiov;
+       auio.uio_iov = iov;
        auio.uio_iovcnt = uap->iovcnt;
        auio.uio_iovcnt = uap->iovcnt;
-       u.u_error = copyin((caddr_t)uap->iovp, (caddr_t)aiov,
-           (unsigned)(uap->iovcnt * sizeof (struct iovec)));
-       if (u.u_error)
-               return;
-       rwuio(&auio, UIO_READ);
+       auio.uio_rw = UIO_READ;
+       auio.uio_segflg = UIO_USERSPACE;
+       if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov,
+           uap->iovcnt * sizeof (struct iovec)))
+               goto done;
+       auio.uio_resid = 0;
+       for (i = 0; i < uap->iovcnt; i++) {
+               if (iov->iov_len < 0) {
+                       error = EINVAL;
+                       goto done;
+               }
+               auio.uio_resid += iov->iov_len;
+               if (auio.uio_resid < 0) {
+                       error = EINVAL;
+                       goto done;
+               }
+               iov++;
+       }
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(u.u_procp, KTR_GENIO))  {
+               int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
+
+               MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
+               bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
+       }
+#endif
+       cnt = auio.uio_resid;
+       if (setjmp(&u.u_qsave)) {
+               if (auio.uio_resid == cnt) {
+                       if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
+                               error = EINTR;
+                       else
+                               u.u_eosys = RESTARTSYS;
+               }
+       } else
+               error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (ktriov != NULL) {
+               ktrgenio(u.u_procp->p_tracep, uap->fdes, UIO_READ, ktriov, cnt);
+               FREE(ktriov, M_TEMP);
+       }
+#endif
+       u.u_r.r_val1 = cnt;
+done:
+       if (uap->iovcnt > UIO_SMALLIOV)
+               FREE(iov, M_IOV);
+       RETURN (error);
 }
 
 /*
 }
 
 /*
@@ -75,16 +178,54 @@ write()
        register struct a {
                int     fdes;
                char    *cbuf;
        register struct a {
                int     fdes;
                char    *cbuf;
-               int     count;
+               unsigned count;
        } *uap = (struct a *)u.u_ap;
        } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
        struct uio auio;
        struct iovec aiov;
        struct uio auio;
        struct iovec aiov;
+       long cnt, error = 0;
+#ifdef KTRACE
+       struct iovec ktriov;
+#endif
 
 
+       if (((unsigned)uap->fdes) >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL ||
+           (fp->f_flag & FWRITE) == 0)
+               RETURN (EBADF);
+       if (uap->count < 0)
+               RETURN (EINVAL);
+       aiov.iov_base = (caddr_t)uap->cbuf;
+       aiov.iov_len = uap->count;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
-       aiov.iov_base = uap->cbuf;
-       aiov.iov_len = uap->count;
-       rwuio(&auio, UIO_WRITE);
+       auio.uio_resid = uap->count;
+       auio.uio_rw = UIO_WRITE;
+       auio.uio_segflg = UIO_USERSPACE;
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(u.u_procp, KTR_GENIO))
+               ktriov = aiov;
+#endif
+       cnt = uap->count;
+       if (setjmp(&u.u_qsave)) {
+               if (auio.uio_resid == cnt) {
+                       if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
+                               error = EINTR;
+                       else
+                               u.u_eosys = RESTARTSYS;
+               }
+       } else
+               error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (KTRPOINT(u.u_procp, KTR_GENIO))
+               ktrgenio(u.u_procp->p_tracep, uap->fdes, UIO_WRITE,
+                   &ktriov, cnt);
+#endif
+       u.u_r.r_val1 = cnt;
+       RETURN (error);
 }
 
 writev()
 }
 
 writev()
@@ -92,372 +233,86 @@ writev()
        register struct a {
                int     fdes;
                struct  iovec *iovp;
        register struct a {
                int     fdes;
                struct  iovec *iovp;
-               int     iovcnt;
+               unsigned iovcnt;
        } *uap = (struct a *)u.u_ap;
        } *uap = (struct a *)u.u_ap;
-       struct uio auio;
-       struct iovec aiov[16];          /* XXX */
-
-       if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) {
-               u.u_error = EINVAL;
-               return;
-       }
-       auio.uio_iov = aiov;
-       auio.uio_iovcnt = uap->iovcnt;
-       u.u_error = copyin((caddr_t)uap->iovp, (caddr_t)aiov,
-           (unsigned)(uap->iovcnt * sizeof (struct iovec)));
-       if (u.u_error)
-               return;
-       rwuio(&auio, UIO_WRITE);
-}
-
-rwuio(uio, rw)
-       register struct uio *uio;
-       enum uio_rw rw;
-{
-       struct a {
-               int     fdes;
-       };
        register struct file *fp;
        register struct file *fp;
+       struct uio auio;
        register struct iovec *iov;
        register struct iovec *iov;
-       register struct inode *ip;
-       int i, count;
+       struct iovec aiov[UIO_SMALLIOV];
+       long i, cnt, error = 0;
+#ifdef KTRACE
+       struct iovec *ktriov = NULL;
+#endif
 
 
-       GETF(fp, ((struct a *)u.u_ap)->fdes);
-       if ((fp->f_flag&(rw==UIO_READ ? FREAD : FWRITE)) == 0) {
-               u.u_error = EBADF;
-               return;
-       }
-       uio->uio_resid = 0;
-       uio->uio_segflg = 0;
-       iov = uio->uio_iov;
-       for (i = 0; i < uio->uio_iovcnt; i++) {
+       if (((unsigned)uap->fdes) >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL ||
+           (fp->f_flag & FWRITE) == 0)
+               RETURN (EBADF);
+       if (uap->iovcnt > UIO_SMALLIOV) {
+               if (uap->iovcnt > UIO_MAXIOV)
+                       RETURN (EINVAL);
+               MALLOC(iov, struct iovec *, 
+                     sizeof(struct iovec) * uap->iovcnt, M_IOV, M_WAITOK);
+       } else
+               iov = aiov;
+       auio.uio_iov = iov;
+       auio.uio_iovcnt = uap->iovcnt;
+       auio.uio_rw = UIO_WRITE;
+       auio.uio_segflg = UIO_USERSPACE;
+       if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov,
+           uap->iovcnt * sizeof (struct iovec)))
+               goto done;
+       auio.uio_resid = 0;
+       for (i = 0; i < uap->iovcnt; i++) {
                if (iov->iov_len < 0) {
                if (iov->iov_len < 0) {
-                       u.u_error = EINVAL;
-                       return;
+                       error = EINVAL;
+                       goto done;
                }
                }
-               uio->uio_resid += iov->iov_len;
-               if (uio->uio_resid < 0) {
-                       u.u_error = EINVAL;
-                       return;
+               auio.uio_resid += iov->iov_len;
+               if (auio.uio_resid < 0) {
+                       error = EINVAL;
+                       goto done;
                }
                }
+               iov++;
        }
        }
-       count = uio->uio_resid;
-       if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) {
-               if (uio->uio_resid == count)
-                       u.u_eosys = RESTARTSYS;
-       } else if (fp->f_type == DTYPE_SOCKET) {
-               int sosend(), soreceive();
-               u.u_error = 
-                   (*(rw==UIO_READ?soreceive:sosend))
-                     (fp->f_socket, (struct sockaddr *)0, uio, 0);
-       } else {
-               ip = fp->f_inode;
-               uio->uio_offset = fp->f_offset;
-               if ((ip->i_mode&IFMT) == IFREG) {
-                       ILOCK(ip);
-                       if (fp->f_flag&FAPPEND && rw == UIO_WRITE)
-                               uio->uio_offset = fp->f_offset = ip->i_size;
-                       u.u_error = rwip(ip, uio, rw);
-                       IUNLOCK(ip);
-               } else
-                       u.u_error = rwip(ip, uio, rw);
-               fp->f_offset += count - uio->uio_resid;
-       }
-       u.u_r.r_val1 = count - uio->uio_resid;
-}
-
-rdwri(rw, ip, base, len, offset, segflg, aresid)
-       struct inode *ip;
-       caddr_t base;
-       int len, offset, segflg;
-       int *aresid;
-       enum uio_rw rw;
-{
-       struct uio auio;
-       struct iovec aiov;
-       int error;
-
-       auio.uio_iov = &aiov;
-       auio.uio_iovcnt = 1;
-       aiov.iov_base = base;
-       aiov.iov_len = len;
-       auio.uio_resid = len;
-       auio.uio_offset = offset;
-       auio.uio_segflg = segflg;
-       error = rwip(ip, &auio, rw);
-       if (aresid)
-               *aresid = auio.uio_resid;
-       else
-               if (auio.uio_resid)
-                       error = EIO;
-       return (error);
-}
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(u.u_procp, KTR_GENIO))  {
+               int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
 
 
-rwip(ip, uio, rw)
-       register struct inode *ip;
-       register struct uio *uio;
-       enum uio_rw rw;
-{
-       dev_t dev = (dev_t)ip->i_rdev;
-       struct buf *bp;
-       struct fs *fs;
-       daddr_t lbn, bn;
-       register int n, on, type;
-       int size;
-       long bsize;
-       extern int mem_no;
-       int error = 0;
-
-       if (rw != UIO_READ && rw != UIO_WRITE)
-               panic("rwip");
-       if (rw == UIO_READ && uio->uio_resid == 0)
-               return (0);
-       if (uio->uio_offset < 0 &&
-           ((ip->i_mode&IFMT) != IFCHR || mem_no != major(dev)))
-               return (EINVAL);
-       if (rw == UIO_READ)
-               ip->i_flag |= IACC;
-       type = ip->i_mode&IFMT;
-       if (type == IFCHR) {
-#ifdef QUOTA
-               register c = uio->uio_resid;
-#endif
-               if (rw == UIO_READ)
-                       u.u_error = (*cdevsw[major(dev)].d_read)(dev, uio);
-               else {
-                       ip->i_flag |= IUPD|ICHG;
-                       u.u_error = (*cdevsw[major(dev)].d_write)(dev, uio);
-               }
-               CHARGE(sc_tio * (c - uio->uio_resid));
-               return (u.u_error);
-       }
-       if (uio->uio_resid == 0)
-               return (0);
-       if (rw == UIO_WRITE && type == IFREG &&
-           uio->uio_offset + uio->uio_resid >
-             u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
-               psignal(u.u_procp, SIGXFSZ);
-               return (EMFILE);
+               MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
+               bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
        }
        }
-       if (type != IFBLK) {
-               dev = ip->i_dev;
-               fs = ip->i_fs;
-               bsize = fs->fs_bsize;
-       } else
-               bsize = BLKDEV_IOSIZE;
-       do {
-               lbn = uio->uio_offset / bsize;
-               on = uio->uio_offset % bsize;
-               n = MIN((unsigned)(bsize - on), uio->uio_resid);
-               if (type != IFBLK) {
-                       if (rw == UIO_READ) {
-                               int diff = ip->i_size - uio->uio_offset;
-                               if (diff <= 0)
-                                       return (0);
-                               if (diff < n)
-                                       n = diff;
-                       }
-                       bn = fsbtodb(fs,
-                           bmap(ip, lbn, rw == UIO_WRITE ? B_WRITE: B_READ, (int)(on+n)));
-                       if (u.u_error || rw == UIO_WRITE && (long)bn<0)
-                               return (u.u_error);
-                       if (rw == UIO_WRITE && uio->uio_offset + n > ip->i_size &&
-                          (type == IFDIR || type == IFREG || type == IFLNK))
-                               ip->i_size = uio->uio_offset + n;
-                       size = blksize(fs, ip, lbn);
-               } else {
-                       bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE);
-                       rablock = bn + (BLKDEV_IOSIZE/DEV_BSIZE);
-                       rasize = size = bsize;
-               }
-               if (rw == UIO_READ) {
-                       if ((long)bn<0) {
-                               bp = geteblk(size);
-                               clrbuf(bp);
-                       } else if (ip->i_lastr + 1 == lbn)
-                               bp = breada(dev, bn, size, rablock, rasize);
-                       else
-                               bp = bread(dev, bn, size);
-                       ip->i_lastr = lbn;
-               } else {
-                       int i, count;
-                       extern struct cmap *mfind();
-
-                       count = howmany(size, DEV_BSIZE);
-                       for (i = 0; i < count; i += CLSIZE)
-                               if (mfind(dev, bn + i))
-                                       munhash(dev, bn + i);
-                       if (n == bsize) 
-                               bp = getblk(dev, bn, size);
-                       else
-                               bp = bread(dev, bn, size);
-               }
-               n = MIN(n, size - bp->b_resid);
-               if (bp->b_flags & B_ERROR) {
-                       error = EIO;
-                       brelse(bp);
-                       goto bad;
-               }
-               u.u_error =
-                   uiomove(bp->b_un.b_addr+on, n, rw, uio);
-               if (rw == UIO_READ) {
-                       if (n + on == bsize || uio->uio_offset == ip->i_size)
-                               bp->b_flags |= B_AGE;
-                       brelse(bp);
-               } else {
-                       if ((ip->i_mode&IFMT) == IFDIR)
-                               bwrite(bp);
-                       else if (n + on == bsize) {
-                               bp->b_flags |= B_AGE;
-                               bawrite(bp);
-                       } else
-                               bdwrite(bp);
-                       ip->i_flag |= IUPD|ICHG;
-                       if (u.u_ruid != 0)
-                               ip->i_mode &= ~(ISUID|ISGID);
-               }
-       } while (u.u_error == 0 && uio->uio_resid > 0 && n != 0);
-bad:
-       return (error);
-}
-
-uiomove(cp, n, rw, uio)
-       register caddr_t cp;
-       register int n;
-       enum uio_rw rw;
-       register struct uio *uio;
-{
-       register struct iovec *iov;
-       u_int cnt;
-       int error = 0;
-
-       while (n > 0 && uio->uio_resid) {
-               iov = uio->uio_iov;
-               cnt = iov->iov_len;
-               if (cnt == 0) {
-                       uio->uio_iov++;
-                       uio->uio_iovcnt--;
-                       continue;
-               }
-               if (cnt > n)
-                       cnt = n;
-               switch (uio->uio_segflg) {
-
-               case 0:
-               case 2:
-                       if (rw == UIO_READ)
-                               error = copyout(cp, iov->iov_base, cnt);
-                       else
-                               error = copyin(iov->iov_base, cp, cnt);
-                       if (error)
-                               return (error);
-                       break;
-
-               case 1:
-                       if (rw == UIO_READ)
-                               bcopy((caddr_t)cp, iov->iov_base, cnt);
+#endif
+       cnt = auio.uio_resid;
+       if (setjmp(&u.u_qsave)) {
+               if (auio.uio_resid == cnt) {
+                       if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
+                               error = EINTR;
                        else
                        else
-                               bcopy(iov->iov_base, (caddr_t)cp, cnt);
-                       break;
+                               u.u_eosys = RESTARTSYS;
                }
                }
-               iov->iov_base += cnt;
-               iov->iov_len -= cnt;
-               uio->uio_resid -= cnt;
-               uio->uio_offset += cnt;
-               cp += cnt;
-               n -= cnt;
-       }
-       return (error);
-}
-
-/*
- * Give next character to user as result of read.
- */
-ureadc(c, uio)
-       register int c;
-       register struct uio *uio;
-{
-       register struct iovec *iov;
-
-again:
-       if (uio->uio_iovcnt == 0)
-               panic("ureadc");
-       iov = uio->uio_iov;
-       if (iov->iov_len <= 0 || uio->uio_resid <= 0) {
-               uio->uio_iovcnt--;
-               uio->uio_iov++;
-               goto again;
-       }
-       switch (uio->uio_segflg) {
-
-       case 0:
-               if (subyte(iov->iov_base, c) < 0)
-                       return (EFAULT);
-               break;
-
-       case 1:
-               *iov->iov_base = c;
-               break;
-
-       case 2:
-               if (suibyte(iov->iov_base, c) < 0)
-                       return (EFAULT);
-               break;
-       }
-       iov->iov_base++;
-       iov->iov_len--;
-       uio->uio_resid--;
-       uio->uio_offset++;
-       return (0);
-}
-
-#ifdef notdef
-/*
- * Get next character written in by user from uio.
- */
-uwritec(uio)
-       struct uio *uio;
-{
-       register struct iovec *iov;
-       register int c;
-
-again:
-       if (uio->uio_iovcnt <= 0 || uio->uio_resid <= 0)
-               panic("uwritec");
-       iov = uio->uio_iov;
-       if (iov->iov_len == 0) {
-               uio->uio_iovcnt--;
-               uio->uio_iov++;
-               goto again;
-       }
-       switch (uio->uio_segflg) {
-
-       case 0:
-               c = fubyte(iov->iov_base);
-               break;
-
-       case 1:
-               c = *iov->iov_base & 0377;
-               break;
-
-       case 2:
-               c = fuibyte(iov->iov_base);
-               break;
+       } else
+               error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (ktriov != NULL) {
+               ktrgenio(u.u_procp->p_tracep, uap->fdes, UIO_WRITE,
+                   ktriov, cnt);
+               FREE(ktriov, M_TEMP);
        }
        }
-       if (c < 0)
-               return (-1);
-       iov->iov_base++;
-       iov->iov_len--;
-       uio->uio_resid--;
-       uio->uio_offset++;
-       return (c & 0377);
-}
 #endif
 #endif
+       u.u_r.r_val1 = cnt;
+done:
+       if (uap->iovcnt > UIO_SMALLIOV)
+               FREE(iov, M_IOV);
+       RETURN (error);
+}
 
 /*
  * Ioctl system call
 
 /*
  * Ioctl system call
- * Check legality, execute common code,
- * and switch out to individual device routine.
  */
 ioctl()
 {
  */
 ioctl()
 {
@@ -466,33 +321,23 @@ ioctl()
                int     fdes;
                int     cmd;
                caddr_t cmarg;
                int     fdes;
                int     cmd;
                caddr_t cmarg;
-       } *uap;
+       } *uap = (struct a *)u.u_ap;
        register int com;
        register u_int size;
        register int com;
        register u_int size;
-       char data[IOCPARM_MASK+1];
-
-       uap = (struct a *)u.u_ap;
-       if ((fp = getf(uap->fdes)) == NULL)
-               return;
+       caddr_t memp = 0;
+#define STK_PARAMS     128
+       char stkbuf[STK_PARAMS];
+       caddr_t data = stkbuf;
+
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               RETURN (EBADF);
        if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
                u.u_error = EBADF;
                return;
        }
        com = uap->cmd;
 
        if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
                u.u_error = EBADF;
                return;
        }
        com = uap->cmd;
 
-#ifndef NOCOMPAT
-       /*
-        * Map old style ioctl's into new for the
-        * sake of backwards compatibility (sigh).
-        */
-       if ((com&~0xffff) == 0) {
-               com = mapioctl(com);
-               if (com == 0) {
-                       u.u_error = EINVAL;
-                       return;
-               }
-       }
-#endif
        if (com == FIOCLEX) {
                u.u_pofile[uap->fdes] |= UF_EXCLOSE;
                return;
        if (com == FIOCLEX) {
                u.u_pofile[uap->fdes] |= UF_EXCLOSE;
                return;
@@ -507,83 +352,253 @@ ioctl()
         * amount of data to be copied to/from the
         * user's address space.
         */
         * amount of data to be copied to/from the
         * user's address space.
         */
-       size = (com &~ (IOC_INOUT|IOC_VOID)) >> 16;
-       if (size > sizeof (data)) {
-               u.u_error = EFAULT;
+       size = IOCPARM_LEN(com);
+       if (size > IOCPARM_MAX) {
+               u.u_error = ENOTTY;
                return;
        }
                return;
        }
+       if (size > sizeof (stkbuf)) {
+               memp = (caddr_t)malloc((u_long)IOCPARM_LEN(com), M_IOCTLOPS,
+                   M_WAITOK);
+               data = memp;
+       }
        if (com&IOC_IN) {
                if (size) {
        if (com&IOC_IN) {
                if (size) {
-                       u.u_error =
-                           copyin(uap->cmarg, (caddr_t)data, (u_int)size);
-                       if (u.u_error)
+                       u.u_error = copyin(uap->cmarg, data, (u_int)size);
+                       if (u.u_error) {
+                               if (memp)
+                                       free(memp, M_IOCTLOPS);
                                return;
                                return;
+                       }
                } else
                        *(caddr_t *)data = uap->cmarg;
        } else if ((com&IOC_OUT) && size)
                /*
                } else
                        *(caddr_t *)data = uap->cmarg;
        } else if ((com&IOC_OUT) && size)
                /*
-                * Zero the buffer on the stack so the user
-                * always gets back something deterministic.
+                * Zero the buffer so the user always
+                * gets back something deterministic.
                 */
                 */
-               bzero((caddr_t)data, size);
-
-       if (fp->f_type == DTYPE_SOCKET)
-               u.u_error = soioctl(fp->f_socket, com, data);
-       else {
-               register struct inode *ip = fp->f_inode;
-               int fmt = ip->i_mode & IFMT;
-               dev_t dev;
-
-               if (fmt != IFCHR) {
-                       if (com == FIONREAD && (fmt == IFREG || fmt == IFDIR)) {
-                               *(off_t *)data = ip->i_size - fp->f_offset;
-                               goto returndata;
-                       }
-                       if (com != FIONBIO && com != FIOASYNC)
-                               u.u_error = ENOTTY;
-                       return;
+               bzero(data, size);
+       else if (com&IOC_VOID)
+               *(caddr_t *)data = uap->cmarg;
+
+       switch (com) {
+
+       case FIONBIO:
+               u.u_error = fset(fp, FNDELAY, *(int *)data);
+               break;
+
+       case FIOASYNC:
+               u.u_error = fset(fp, FASYNC, *(int *)data);
+               break;
+
+       case FIOSETOWN:
+               u.u_error = fsetown(fp, *(int *)data);
+               break;
+
+       case FIOGETOWN:
+               u.u_error = fgetown(fp, (int *)data);
+               break;
+       default:
+               if (setjmp(&u.u_qsave))
+                       u.u_error = EINTR;
+               else
+                       u.u_error = (*fp->f_ops->fo_ioctl)(fp, com, data);
+               /*
+                * Copy any data to user, size was
+                * already set and checked above.
+                */
+               if (u.u_error == 0 && (com&IOC_OUT) && size)
+                       u.u_error = copyout(data, uap->cmarg, (u_int)size);
+               break;
+       }
+       if (memp)
+               free(memp, M_IOCTLOPS);
+}
+
+int    unselect();
+int    nselcoll;
+
+/*
+ * Select system call.
+ */
+select()
+{
+       register struct uap  {
+               int     nd;
+               fd_set  *in, *ou, *ex;
+               struct  timeval *tv;
+       } *uap = (struct uap *)u.u_ap;
+       fd_set ibits[3], obits[3];
+       struct timeval atv;
+       int s, ncoll, ni;
+       label_t lqsave;
+
+       bzero((caddr_t)ibits, sizeof(ibits));
+       bzero((caddr_t)obits, sizeof(obits));
+       if (uap->nd > NOFILE)
+               uap->nd = NOFILE;       /* forgiving, if slightly wrong */
+       ni = howmany(uap->nd, NFDBITS);
+
+#define        getbits(name, x) \
+       if (uap->name) { \
+               u.u_error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], \
+                   (unsigned)(ni * sizeof(fd_mask))); \
+               if (u.u_error) \
+                       goto done; \
+       }
+       getbits(in, 0);
+       getbits(ou, 1);
+       getbits(ex, 2);
+#undef getbits
+
+       if (uap->tv) {
+               u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
+                       sizeof (atv));
+               if (u.u_error)
+                       goto done;
+               if (itimerfix(&atv)) {
+                       u.u_error = EINVAL;
+                       goto done;
                }
                }
-               dev = ip->i_rdev;
-               u.u_r.r_val1 = 0;
-               if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) {
-                       u.u_eosys = RESTARTSYS;
-                       return;
+               s = splhigh(); timevaladd(&atv, &time); splx(s);
+       }
+retry:
+       ncoll = nselcoll;
+       u.u_procp->p_flag |= SSEL;
+       u.u_r.r_val1 = selscan(ibits, obits, uap->nd);
+       if (u.u_error || u.u_r.r_val1)
+               goto done;
+       s = splhigh();
+       /* this should be timercmp(&time, &atv, >=) */
+       if (uap->tv && (time.tv_sec > atv.tv_sec ||
+           time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec)) {
+               splx(s);
+               goto done;
+       }
+       if ((u.u_procp->p_flag & SSEL) == 0 || nselcoll != ncoll) {
+               splx(s);
+               goto retry;
+       }
+       u.u_procp->p_flag &= ~SSEL;
+       if (uap->tv) {
+               lqsave = u.u_qsave;
+               if (setjmp(&u.u_qsave)) {
+                       untimeout(unselect, (caddr_t)u.u_procp);
+                       u.u_error = EINTR;
+                       splx(s);
+                       goto done;
                }
                }
-               u.u_error = (*cdevsw[major(dev)].d_ioctl)(dev, com, data, 0);
+               timeout(unselect, (caddr_t)u.u_procp, hzto(&atv));
        }
        }
+       sleep((caddr_t)&selwait, PZERO+1);
+       if (uap->tv) {
+               u.u_qsave = lqsave;
+               untimeout(unselect, (caddr_t)u.u_procp);
+       }
+       splx(s);
+       goto retry;
+done:
+       u.u_procp->p_flag &= ~SSEL;
+#define        putbits(name, x) \
+       if (uap->name) { \
+               int error = copyout((caddr_t)&obits[x], (caddr_t)uap->name, \
+                   (unsigned)(ni * sizeof(fd_mask))); \
+               if (error) \
+                       u.u_error = error; \
+       }
+       if (u.u_error == 0) {
+               putbits(in, 0);
+               putbits(ou, 1);
+               putbits(ex, 2);
+#undef putbits
+       }
+}
 
 
-returndata:
-       /*
-        * Copy any data to user, size was
-        * already set and checked above.
-        */
-       if (u.u_error == 0 && (com&IOC_OUT) && size)
-               u.u_error = copyout(data, uap->cmarg, (u_int)size);
+unselect(p)
+       register struct proc *p;
+{
+       register int s = splhigh();
+
+       switch (p->p_stat) {
+
+       case SSLEEP:
+               setrun(p);
+               break;
+
+       case SSTOP:
+               unsleep(p);
+               break;
+       }
+       splx(s);
 }
 
 }
 
-/*
- * Do nothing specific version of line
- * discipline specific ioctl command.
- */
-/*ARGSUSED*/
-nullioctl(tp, cmd, data, flags)
-       struct tty *tp;
-       char *data;
-       int flags;
+selscan(ibits, obits, nfd)
+       fd_set *ibits, *obits;
 {
 {
+       register int which, i, j;
+       register fd_mask bits;
+       int flag;
+       struct file *fp;
+       int n = 0;
 
 
-#ifdef lint
-       tp = tp; data = data; flags = flags;
-#endif
-       return (-1);
+       for (which = 0; which < 3; which++) {
+               switch (which) {
+
+               case 0:
+                       flag = FREAD; break;
+
+               case 1:
+                       flag = FWRITE; break;
+
+               case 2:
+                       flag = 0; break;
+               }
+               for (i = 0; i < nfd; i += NFDBITS) {
+                       bits = ibits[which].fds_bits[i/NFDBITS];
+                       while ((j = ffs(bits)) && i + --j < nfd) {
+                               bits &= ~(1 << j);
+                               fp = u.u_ofile[i + j];
+                               if (fp == NULL) {
+                                       u.u_error = EBADF;
+                                       break;
+                               }
+                               if ((*fp->f_ops->fo_select)(fp, flag)) {
+                                       FD_SET(i + j, &obits[which]);
+                                       n++;
+                               }
+                       }
+               }
+       }
+       return (n);
 }
 
 }
 
-ostty()
+/*ARGSUSED*/
+seltrue(dev, flag)
+       dev_t dev;
+       int flag;
 {
 
 {
 
+       return (1);
 }
 
 }
 
-ogtty()
+selwakeup(p, coll)
+       register struct proc *p;
+       int coll;
 {
 
 {
 
+       if (coll) {
+               nselcoll++;
+               wakeup((caddr_t)&selwait);
+       }
+       if (p) {
+               int s = splhigh();
+               if (p->p_wchan == (caddr_t)&selwait) {
+                       if (p->p_stat == SSLEEP)
+                               setrun(p);
+                       else
+                               unsleep(p);
+               } else if (p->p_flag & SSEL)
+                       p->p_flag &= ~SSEL;
+               splx(s);
+       }
 }
 }