rearrange for kirk
[unix-history] / usr / src / sys / kern / vfs_vnops.c
index f63b48b..67693a4 100644 (file)
@@ -1,4 +1,4 @@
-/*     vfs_vnops.c     4.4     %G%     */
+/*     vfs_vnops.c     4.22    82/02/27        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
 #include "../h/reg.h"
 #include "../h/acct.h"
 #include "../h/mount.h"
 #include "../h/reg.h"
 #include "../h/acct.h"
 #include "../h/mount.h"
+#include "../h/socket.h"
+#include "../h/socketvar.h"
+#include "../h/proc.h"
 
 /*
 
 /*
- * Convert a user supplied
- * file descriptor into a pointer
- * to a file structure.
- * Only task is to check range
- * of the descriptor.
+ * Convert a user supplied file descriptor into a pointer
+ * to a file structure.  Only task is to check range of the descriptor.
+ * Critical paths should use the GETF macro, defined in inline.h.
  */
 struct file *
 getf(f)
  */
 struct file *
 getf(f)
-register int f;
+       register int f;
 {
        register struct file *fp;
 
 {
        register struct file *fp;
 
@@ -29,12 +30,6 @@ register int f;
                u.u_error = EBADF;
                return (NULL);
        }
                u.u_error = EBADF;
                return (NULL);
        }
-#ifdef UCBIPC
-       if (u.u_pofile[f] & ISPORT) {
-               u.u_error = EISPORT;
-               return (NULL);
-       }
-#endif
        return (fp);
 }
 
        return (fp);
 }
 
@@ -48,9 +43,11 @@ register int f;
  * Decrement reference count on the inode following
  * removal to the referencing file structure.
  * Call device handler on last close.
  * Decrement reference count on the inode following
  * removal to the referencing file structure.
  * Call device handler on last close.
+ * Nouser indicates that the user isn't available to present
+ * errors to.
  */
  */
-closef(fp)
-register struct file *fp;
+closef(fp, nouser)
+       register struct file *fp;
 {
        register struct inode *ip;
        register struct mount *mp;
 {
        register struct inode *ip;
        register struct mount *mp;
@@ -58,30 +55,32 @@ register struct file *fp;
        dev_t dev;
        register int (*cfunc)();
 
        dev_t dev;
        register int (*cfunc)();
 
-       if(fp == NULL)
+       if (fp == NULL)
                return;
        if (fp->f_count > 1) {
                fp->f_count--;
                return;
        }
                return;
        if (fp->f_count > 1) {
                fp->f_count--;
                return;
        }
-       ip = fp->f_inode;
        flag = fp->f_flag;
        flag = fp->f_flag;
+       if (flag & FSOCKET) {
+               u.u_error = 0;                  /* XXX */
+               soclose(fp->f_socket, nouser);
+               if (nouser == 0 && u.u_error)
+                       return;
+               fp->f_socket = 0;
+               fp->f_count = 0;
+               return;
+       }
+       ip = fp->f_inode;
        dev = (dev_t)ip->i_un.i_rdev;
        mode = ip->i_mode & IFMT;
        dev = (dev_t)ip->i_un.i_rdev;
        mode = ip->i_mode & IFMT;
-
-       plock(ip);
-       fp->f_count = 0;
-       if(flag & FPIPE) {
-               ip->i_mode &= ~(IREAD|IWRITE);
-               wakeup((caddr_t)ip+1);
-               wakeup((caddr_t)ip+2);
-       }
+       ilock(ip);
        iput(ip);
        iput(ip);
+       fp->f_count = 0;
 
 
-       switch(mode) {
+       switch (mode) {
 
        case IFCHR:
 
        case IFCHR:
-       case IFMPC:
                cfunc = cdevsw[major(dev)].d_close;
                break;
 
                cfunc = cdevsw[major(dev)].d_close;
                break;
 
@@ -92,55 +91,53 @@ register struct file *fp;
                for (mp = mount; mp < &mount[NMOUNT]; mp++)
                        if (mp->m_bufp != NULL && mp->m_dev == dev)
                                return;
                for (mp = mount; mp < &mount[NMOUNT]; mp++)
                        if (mp->m_bufp != NULL && mp->m_dev == dev)
                                return;
-       case IFMPB:
                cfunc = bdevsw[major(dev)].d_close;
                break;
                cfunc = bdevsw[major(dev)].d_close;
                break;
+
        default:
                return;
        }
        default:
                return;
        }
-
-       if ((flag & FMP) == 0) {
-               for(fp=file; fp < &file[NFILE]; fp++)
-                       if (fp->f_count && (ip=fp->f_inode)->i_un.i_rdev==dev &&
-                           (ip->i_mode&IFMT) == mode)
-                               return;
-               if (mode == IFBLK) {
-                       /*
-                        * on last close of a block device (that isn't mounted)
-                        * we must invalidate any in core blocks
-                        */
-                       bflush(dev);
-                       binval(dev);
-               }
+       for (fp = file; fp < fileNFILE; fp++) {
+               if (fp->f_flag & FSOCKET)
+                       continue;
+               if (fp->f_count && (ip = fp->f_inode) &&
+                   ip->i_un.i_rdev == dev && (ip->i_mode&IFMT) == mode)
+                       return;
+       }
+       if (mode == IFBLK) {
+               /*
+                * On last close of a block device (that isn't mounted)
+                * we must invalidate any in core blocks
+                */
+               bflush(dev);
+               binval(dev);
        }
        (*cfunc)(dev, flag, fp);
 }
 
 /*
        }
        (*cfunc)(dev, flag, fp);
 }
 
 /*
- * openi called to allow handler
+ * Openi called to allow handler
  * of special files to initialize and
  * validate before actual IO.
  */
 openi(ip, rw)
  * of special files to initialize and
  * validate before actual IO.
  */
 openi(ip, rw)
-register struct inode *ip;
+       register struct inode *ip;
 {
        dev_t dev;
        register unsigned int maj;
 
        dev = (dev_t)ip->i_un.i_rdev;
        maj = major(dev);
 {
        dev_t dev;
        register unsigned int maj;
 
        dev = (dev_t)ip->i_un.i_rdev;
        maj = major(dev);
-       switch(ip->i_mode&IFMT) {
+       switch (ip->i_mode&IFMT) {
 
        case IFCHR:
 
        case IFCHR:
-       case IFMPC:
-               if(maj >= nchrdev)
+               if (maj >= nchrdev)
                        goto bad;
                (*cdevsw[maj].d_open)(dev, rw);
                break;
 
        case IFBLK:
                        goto bad;
                (*cdevsw[maj].d_open)(dev, rw);
                break;
 
        case IFBLK:
-       case IFMPB:
-               if(maj >= nblkdev)
+               if (maj >= nblkdev)
                        goto bad;
                (*bdevsw[maj].d_open)(dev, rw);
        }
                        goto bad;
                (*bdevsw[maj].d_open)(dev, rw);
        }
@@ -164,35 +161,37 @@ bad:
  * permissions.
  */
 access(ip, mode)
  * permissions.
  */
 access(ip, mode)
-register struct inode *ip;
+       register struct inode *ip;
+       int mode;
 {
        register m;
 
        m = mode;
 {
        register m;
 
        m = mode;
-       if(m == IWRITE) {
-               if(getfs(ip->i_dev)->s_ronly != 0) {
+       if (m == IWRITE) {
+               if (getfs(ip->i_dev)->s_ronly != 0) {
                        u.u_error = EROFS;
                        u.u_error = EROFS;
-                       return(1);
+                       return (1);
                }
                if (ip->i_flag&ITEXT)           /* try to free text */
                        xrele(ip);
                }
                if (ip->i_flag&ITEXT)           /* try to free text */
                        xrele(ip);
-               if(ip->i_flag & ITEXT) {
+               if (ip->i_flag & ITEXT) {
                        u.u_error = ETXTBSY;
                        u.u_error = ETXTBSY;
-                       return(1);
+                       return (1);
                }
        }
                }
        }
-       if(u.u_uid == 0)
-               return(0);
-       if(u.u_uid != ip->i_uid) {
+       if (u.u_uid == 0)
+               return (0);
+       if (u.u_uid != ip->i_uid) {
                m >>= 3;
                m >>= 3;
-               if(u.u_gid != ip->i_gid)
+               if (ip->i_gid >= NGRPS ||
+                   (u.u_grps[ip->i_gid/(sizeof(int)*8)] &
+                    (1 << ip->i_gid%(sizeof(int)*8)) == 0))
                        m >>= 3;
        }
                        m >>= 3;
        }
-       if((ip->i_mode&m) != 0)
-               return(0);
-
+       if ((ip->i_mode&m) != 0)
+               return (0);
        u.u_error = EACCES;
        u.u_error = EACCES;
-       return(1);
+       return (1);
 }
 
 /*
 }
 
 /*
@@ -204,19 +203,20 @@ register struct inode *ip;
  * return inode pointer.
  */
 struct inode *
  * return inode pointer.
  */
 struct inode *
-owner()
+owner(follow)
+       int follow;
 {
        register struct inode *ip;
 
 {
        register struct inode *ip;
 
-       ip = namei(uchar, 0);
-       if(ip == NULL)
-               return(NULL);
-       if(u.u_uid == ip->i_uid)
-               return(ip);
-       if(suser())
-               return(ip);
+       ip = namei(uchar, 0, follow);
+       if (ip == NULL)
+               return (NULL);
+       if (u.u_uid == ip->i_uid)
+               return (ip);
+       if (suser())
+               return (ip);
        iput(ip);
        iput(ip);
-       return(NULL);
+       return (NULL);
 }
 
 /*
 }
 
 /*
@@ -226,12 +226,12 @@ owner()
 suser()
 {
 
 suser()
 {
 
-       if(u.u_uid == 0) {
+       if (u.u_uid == 0) {
                u.u_acflag |= ASU;
                u.u_acflag |= ASU;
-               return(1);
+               return (1);
        }
        u.u_error = EPERM;
        }
        u.u_error = EPERM;
-       return(0);
+       return (0);
 }
 
 /*
 }
 
 /*
@@ -241,25 +241,22 @@ ufalloc()
 {
        register i;
 
 {
        register i;
 
-       for(i=0; i<NOFILE; i++)
-               if(u.u_ofile[i] == NULL) {
+       for (i=0; i<NOFILE; i++)
+               if (u.u_ofile[i] == NULL) {
                        u.u_r.r_val1 = i;
                        u.u_pofile[i] = 0;
                        u.u_r.r_val1 = i;
                        u.u_pofile[i] = 0;
-                       return(i);
+                       return (i);
                }
        u.u_error = EMFILE;
                }
        u.u_error = EMFILE;
-       return(-1);
+       return (-1);
 }
 
 }
 
-struct file *lastf = &file[0];
+struct file *lastf;
 /*
  * Allocate a user file descriptor
  * and a file structure.
  * Initialize the descriptor
  * to point at the file structure.
 /*
  * Allocate a user file descriptor
  * and a file structure.
  * Initialize the descriptor
  * to point at the file structure.
- *
- * no file -- if there are no available
- *     file structures.
  */
 struct file *
 falloc()
  */
 struct file *
 falloc()
@@ -268,21 +265,24 @@ falloc()
        register i;
 
        i = ufalloc();
        register i;
 
        i = ufalloc();
-       if(i < 0)
-               return(NULL);
-       for(fp = lastf; fp < &file[NFILE]; fp++)
-               if(fp->f_count == 0)
+       if (i < 0)
+               return (NULL);
+       if (lastf == 0)
+               lastf = file;
+       for (fp = lastf; fp < fileNFILE; fp++)
+               if (fp->f_count == 0)
                        goto slot;
                        goto slot;
-       for(fp = &file[0]; fp < lastf; fp++)
-               if(fp->f_count == 0)
+       for (fp = file; fp < lastf; fp++)
+               if (fp->f_count == 0)
                        goto slot;
                        goto slot;
-       printf("no file\n");
+       tablefull("file");
        u.u_error = ENFILE;
        u.u_error = ENFILE;
-       return(NULL);
+       return (NULL);
 slot:
        u.u_ofile[i] = fp;
        fp->f_count++;
 slot:
        u.u_ofile[i] = fp;
        fp->f_count++;
-       fp->f_un.f_offset = 0;
+       fp->f_offset = 0;
+       fp->f_inode = 0;
        lastf = fp + 1;
        lastf = fp + 1;
-       return(fp);
+       return (fp);
 }
 }