Home | History | Annotate | Line # | Download | only in coda
coda_psdev.c revision 1.16
      1  1.16   thorpej /*	$NetBSD: coda_psdev.c,v 1.16 2001/04/16 22:41:09 thorpej Exp $	*/
      2   1.2       rvb 
      3   1.1       rvb /*
      4   1.2       rvb  *
      5   1.2       rvb  *             Coda: an Experimental Distributed File System
      6   1.2       rvb  *                              Release 3.1
      7   1.2       rvb  *
      8   1.2       rvb  *           Copyright (c) 1987-1998 Carnegie Mellon University
      9   1.2       rvb  *                          All Rights Reserved
     10   1.2       rvb  *
     11   1.2       rvb  * Permission  to  use, copy, modify and distribute this software and its
     12   1.2       rvb  * documentation is hereby granted,  provided  that  both  the  copyright
     13   1.2       rvb  * notice  and  this  permission  notice  appear  in  all  copies  of the
     14   1.2       rvb  * software, derivative works or  modified  versions,  and  any  portions
     15   1.2       rvb  * thereof, and that both notices appear in supporting documentation, and
     16   1.2       rvb  * that credit is given to Carnegie Mellon University  in  all  documents
     17   1.2       rvb  * and publicity pertaining to direct or indirect use of this code or its
     18   1.2       rvb  * derivatives.
     19   1.2       rvb  *
     20   1.2       rvb  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
     21   1.2       rvb  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
     22   1.2       rvb  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
     23   1.2       rvb  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
     24   1.2       rvb  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
     25   1.2       rvb  * ANY DERIVATIVE WORK.
     26   1.2       rvb  *
     27   1.2       rvb  * Carnegie  Mellon  encourages  users  of  this  software  to return any
     28   1.2       rvb  * improvements or extensions that  they  make,  and  to  grant  Carnegie
     29   1.2       rvb  * Mellon the rights to redistribute these changes without encumbrance.
     30   1.2       rvb  *
     31   1.4       rvb  * 	@(#) coda/coda_psdev.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
     32   1.2       rvb  */
     33   1.1       rvb 
     34   1.1       rvb /*
     35   1.1       rvb  * Mach Operating System
     36   1.1       rvb  * Copyright (c) 1989 Carnegie-Mellon University
     37   1.1       rvb  * All rights reserved.  The CMU software License Agreement specifies
     38   1.1       rvb  * the terms and conditions for use and redistribution.
     39   1.1       rvb  */
     40   1.1       rvb 
     41   1.1       rvb /*
     42   1.1       rvb  * This code was written for the Coda file system at Carnegie Mellon
     43   1.1       rvb  * University.  Contributers include David Steere, James Kistler, and
     44   1.1       rvb  * M. Satyanarayanan.  */
     45   1.1       rvb 
     46   1.1       rvb /* These routines define the psuedo device for communication between
     47   1.1       rvb  * Coda's Venus and Minicache in Mach 2.6. They used to be in cfs_subr.c,
     48   1.1       rvb  * but I moved them to make it easier to port the Minicache without
     49   1.1       rvb  * porting coda. -- DCS 10/12/94
     50   1.1       rvb  */
     51   1.1       rvb 
     52   1.1       rvb /* These routines are the device entry points for Venus. */
     53   1.1       rvb 
     54   1.3       rvb extern int coda_nc_initialized;    /* Set if cache has been initialized */
     55   1.1       rvb 
     56   1.5       rvb #ifdef	_LKM
     57   1.5       rvb #define	NVCODA 4
     58   1.5       rvb #else
     59   1.3       rvb #include <vcoda.h>
     60   1.5       rvb #endif
     61   1.5       rvb 
     62   1.1       rvb #include <sys/param.h>
     63   1.1       rvb #include <sys/systm.h>
     64   1.1       rvb #include <sys/kernel.h>
     65   1.1       rvb #include <sys/malloc.h>
     66   1.1       rvb #include <sys/proc.h>
     67   1.1       rvb #include <sys/mount.h>
     68   1.1       rvb #include <sys/file.h>
     69   1.1       rvb #include <sys/ioctl.h>
     70   1.1       rvb #include <sys/poll.h>
     71   1.1       rvb #include <sys/select.h>
     72   1.1       rvb 
     73  1.16   thorpej #include <miscfs/syncfs/syncfs.h>
     74  1.16   thorpej 
     75   1.4       rvb #include <coda/coda.h>
     76   1.4       rvb #include <coda/cnode.h>
     77   1.4       rvb #include <coda/coda_namecache.h>
     78   1.4       rvb #include <coda/coda_io.h>
     79   1.5       rvb #include <coda/coda_psdev.h>
     80   1.1       rvb 
     81   1.2       rvb #define CTL_C
     82   1.2       rvb 
     83   1.3       rvb int coda_psdev_print_entry = 0;
     84   1.8       rvb static
     85   1.8       rvb int outstanding_upcalls = 0;
     86   1.8       rvb int coda_call_sleep = PZERO - 1;
     87   1.8       rvb #ifdef	CTL_C
     88   1.8       rvb int coda_pcatch = PCATCH;
     89   1.8       rvb #else
     90   1.8       rvb #endif
     91   1.3       rvb 
     92   1.3       rvb #define ENTRY if(coda_psdev_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
     93   1.1       rvb 
     94   1.3       rvb void vcodaattach(int n);
     95   1.1       rvb 
     96   1.1       rvb struct vmsg {
     97   1.1       rvb     struct queue vm_chain;
     98   1.1       rvb     caddr_t	 vm_data;
     99   1.1       rvb     u_short	 vm_flags;
    100   1.1       rvb     u_short      vm_inSize;	/* Size is at most 5000 bytes */
    101   1.1       rvb     u_short	 vm_outSize;
    102   1.1       rvb     u_short	 vm_opcode; 	/* copied from data to save ptr lookup */
    103   1.1       rvb     int		 vm_unique;
    104   1.1       rvb     caddr_t	 vm_sleep;	/* Not used by Mach. */
    105   1.1       rvb };
    106   1.1       rvb 
    107   1.1       rvb #define	VM_READ	    1
    108   1.1       rvb #define	VM_WRITE    2
    109   1.1       rvb #define	VM_INTR	    4
    110   1.1       rvb 
    111   1.3       rvb /* vcodaattach: do nothing */
    112   1.1       rvb void
    113   1.3       rvb vcodaattach(n)
    114   1.1       rvb     int n;
    115   1.1       rvb {
    116   1.1       rvb }
    117   1.1       rvb 
    118   1.1       rvb /*
    119   1.1       rvb  * These functions are written for NetBSD.
    120   1.1       rvb  */
    121   1.1       rvb int
    122   1.1       rvb vc_nb_open(dev, flag, mode, p)
    123   1.1       rvb     dev_t        dev;
    124   1.1       rvb     int          flag;
    125   1.1       rvb     int          mode;
    126   1.1       rvb     struct proc *p;             /* NetBSD only */
    127   1.1       rvb {
    128  1.13  augustss     struct vcomm *vcp;
    129   1.1       rvb 
    130   1.1       rvb     ENTRY;
    131   1.1       rvb 
    132   1.3       rvb     if (minor(dev) >= NVCODA || minor(dev) < 0)
    133   1.1       rvb 	return(ENXIO);
    134   1.1       rvb 
    135   1.3       rvb     if (!coda_nc_initialized)
    136   1.3       rvb 	coda_nc_init();
    137   1.1       rvb 
    138   1.3       rvb     vcp = &coda_mnttbl[minor(dev)].mi_vcomm;
    139   1.1       rvb     if (VC_OPEN(vcp))
    140   1.1       rvb 	return(EBUSY);
    141   1.1       rvb 
    142   1.1       rvb     bzero(&(vcp->vc_selproc), sizeof (struct selinfo));
    143   1.1       rvb     INIT_QUEUE(vcp->vc_requests);
    144   1.1       rvb     INIT_QUEUE(vcp->vc_replys);
    145   1.1       rvb     MARK_VC_OPEN(vcp);
    146   1.1       rvb 
    147   1.3       rvb     coda_mnttbl[minor(dev)].mi_vfsp = NULL;
    148   1.3       rvb     coda_mnttbl[minor(dev)].mi_rootvp = NULL;
    149   1.1       rvb 
    150   1.1       rvb     return(0);
    151   1.1       rvb }
    152   1.1       rvb 
    153   1.1       rvb int
    154   1.1       rvb vc_nb_close (dev, flag, mode, p)
    155   1.1       rvb     dev_t        dev;
    156   1.1       rvb     int          flag;
    157   1.1       rvb     int          mode;
    158   1.1       rvb     struct proc *p;
    159   1.1       rvb {
    160  1.13  augustss     struct vcomm *vcp;
    161  1.13  augustss     struct vmsg *vmp, *nvmp = NULL;
    162   1.3       rvb     struct coda_mntinfo *mi;
    163   1.1       rvb     int                 err;
    164   1.1       rvb 
    165   1.1       rvb     ENTRY;
    166   1.1       rvb 
    167   1.3       rvb     if (minor(dev) >= NVCODA || minor(dev) < 0)
    168   1.1       rvb 	return(ENXIO);
    169   1.1       rvb 
    170   1.3       rvb     mi = &coda_mnttbl[minor(dev)];
    171   1.1       rvb     vcp = &(mi->mi_vcomm);
    172   1.1       rvb 
    173   1.1       rvb     if (!VC_OPEN(vcp))
    174   1.1       rvb 	panic("vcclose: not open");
    175   1.1       rvb 
    176   1.1       rvb     /* prevent future operations on this vfs from succeeding by auto-
    177   1.1       rvb      * unmounting any vfs mounted via this device. This frees user or
    178   1.1       rvb      * sysadm from having to remember where all mount points are located.
    179   1.1       rvb      * Put this before WAKEUPs to avoid queuing new messages between
    180   1.1       rvb      * the WAKEUP and the unmount (which can happen if we're unlucky)
    181   1.1       rvb      */
    182   1.8       rvb     if (!mi->mi_rootvp) {
    183   1.8       rvb 	/* just a simple open/close w no mount */
    184   1.8       rvb 	MARK_VC_CLOSED(vcp);
    185   1.8       rvb 	return 0;
    186   1.1       rvb     }
    187   1.8       rvb 
    188   1.8       rvb     /* Let unmount know this is for real */
    189  1.16   thorpej     /*
    190  1.16   thorpej      * XXX Freeze syncer.  Must do this before locking the
    191  1.16   thorpej      * mount point.  See dounmount for details().
    192  1.16   thorpej      */
    193  1.16   thorpej     lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    194   1.8       rvb     VTOC(mi->mi_rootvp)->c_flags |= C_UNMOUNTING;
    195  1.16   thorpej     if (vfs_busy(mi->mi_vfsp, 0, 0)) {
    196  1.16   thorpej 	lockmgr(&syncer_lock, LK_RELEASE, NULL);
    197   1.8       rvb 	return (EBUSY);
    198  1.16   thorpej     }
    199   1.8       rvb     coda_unmounting(mi->mi_vfsp);
    200   1.1       rvb 
    201   1.1       rvb     /* Wakeup clients so they can return. */
    202   1.1       rvb     for (vmp = (struct vmsg *)GETNEXT(vcp->vc_requests);
    203   1.1       rvb 	 !EOQ(vmp, vcp->vc_requests);
    204  1.10       rvb 	 vmp = nvmp)
    205   1.1       rvb     {
    206   1.9       rvb     	nvmp = (struct vmsg *)GETNEXT(vmp->vm_chain);
    207   1.1       rvb 	/* Free signal request messages and don't wakeup cause
    208   1.1       rvb 	   no one is waiting. */
    209   1.3       rvb 	if (vmp->vm_opcode == CODA_SIGNAL) {
    210   1.3       rvb 	    CODA_FREE((caddr_t)vmp->vm_data, (u_int)VC_IN_NO_DATA);
    211   1.3       rvb 	    CODA_FREE((caddr_t)vmp, (u_int)sizeof(struct vmsg));
    212   1.1       rvb 	    continue;
    213   1.1       rvb 	}
    214   1.8       rvb 	outstanding_upcalls++;
    215   1.1       rvb 	wakeup(&vmp->vm_sleep);
    216   1.1       rvb     }
    217   1.8       rvb 
    218   1.1       rvb     for (vmp = (struct vmsg *)GETNEXT(vcp->vc_replys);
    219   1.1       rvb 	 !EOQ(vmp, vcp->vc_replys);
    220   1.1       rvb 	 vmp = (struct vmsg *)GETNEXT(vmp->vm_chain))
    221   1.1       rvb     {
    222   1.8       rvb 	outstanding_upcalls++;
    223   1.1       rvb 	wakeup(&vmp->vm_sleep);
    224   1.1       rvb     }
    225   1.8       rvb 
    226   1.1       rvb     MARK_VC_CLOSED(vcp);
    227   1.8       rvb 
    228   1.8       rvb     if (outstanding_upcalls) {
    229   1.8       rvb #ifdef	CODA_VERBOSE
    230   1.8       rvb 	printf("presleep: outstanding_upcalls = %d\n", outstanding_upcalls);
    231   1.8       rvb     	(void) tsleep(&outstanding_upcalls, coda_call_sleep, "coda_umount", 0);
    232   1.8       rvb 	printf("postsleep: outstanding_upcalls = %d\n", outstanding_upcalls);
    233   1.8       rvb #else
    234   1.8       rvb     	(void) tsleep(&outstanding_upcalls, coda_call_sleep, "coda_umount", 0);
    235   1.8       rvb #endif
    236   1.8       rvb     }
    237   1.8       rvb 
    238   1.8       rvb     err = dounmount(mi->mi_vfsp, flag, p);
    239   1.8       rvb     if (err)
    240   1.8       rvb 	myprintf(("Error %d unmounting vfs in vcclose(%d)\n",
    241   1.8       rvb 	           err, minor(dev)));
    242   1.1       rvb     return 0;
    243   1.1       rvb }
    244   1.1       rvb 
    245   1.1       rvb int
    246   1.1       rvb vc_nb_read(dev, uiop, flag)
    247   1.1       rvb     dev_t        dev;
    248   1.1       rvb     struct uio  *uiop;
    249   1.1       rvb     int          flag;
    250   1.1       rvb {
    251  1.13  augustss     struct vcomm *	vcp;
    252  1.13  augustss     struct vmsg *vmp;
    253   1.1       rvb     int error = 0;
    254   1.1       rvb 
    255   1.1       rvb     ENTRY;
    256   1.1       rvb 
    257   1.3       rvb     if (minor(dev) >= NVCODA || minor(dev) < 0)
    258   1.1       rvb 	return(ENXIO);
    259   1.1       rvb 
    260   1.3       rvb     vcp = &coda_mnttbl[minor(dev)].mi_vcomm;
    261   1.1       rvb     /* Get message at head of request queue. */
    262   1.1       rvb     if (EMPTY(vcp->vc_requests))
    263   1.1       rvb 	return(0);	/* Nothing to read */
    264   1.1       rvb 
    265   1.1       rvb     vmp = (struct vmsg *)GETNEXT(vcp->vc_requests);
    266   1.1       rvb 
    267   1.1       rvb     /* Move the input args into userspace */
    268   1.1       rvb     uiop->uio_rw = UIO_READ;
    269   1.1       rvb     error = uiomove(vmp->vm_data, vmp->vm_inSize, uiop);
    270   1.1       rvb     if (error) {
    271   1.1       rvb 	myprintf(("vcread: error (%d) on uiomove\n", error));
    272   1.1       rvb 	error = EINVAL;
    273   1.1       rvb     }
    274   1.1       rvb 
    275   1.5       rvb #ifdef OLD_DIAGNOSTIC
    276   1.1       rvb     if (vmp->vm_chain.forw == 0 || vmp->vm_chain.back == 0)
    277   1.1       rvb 	panic("vc_nb_read: bad chain");
    278   1.1       rvb #endif
    279   1.1       rvb 
    280   1.1       rvb     REMQUE(vmp->vm_chain);
    281   1.1       rvb 
    282   1.1       rvb     /* If request was a signal, free up the message and don't
    283   1.1       rvb        enqueue it in the reply queue. */
    284   1.3       rvb     if (vmp->vm_opcode == CODA_SIGNAL) {
    285   1.3       rvb 	if (codadebug)
    286   1.1       rvb 	    myprintf(("vcread: signal msg (%d, %d)\n",
    287   1.1       rvb 		      vmp->vm_opcode, vmp->vm_unique));
    288   1.3       rvb 	CODA_FREE((caddr_t)vmp->vm_data, (u_int)VC_IN_NO_DATA);
    289   1.3       rvb 	CODA_FREE((caddr_t)vmp, (u_int)sizeof(struct vmsg));
    290   1.1       rvb 	return(error);
    291   1.1       rvb     }
    292   1.1       rvb 
    293   1.1       rvb     vmp->vm_flags |= VM_READ;
    294   1.1       rvb     INSQUE(vmp->vm_chain, vcp->vc_replys);
    295   1.1       rvb 
    296   1.1       rvb     return(error);
    297   1.1       rvb }
    298   1.1       rvb 
    299   1.1       rvb int
    300   1.1       rvb vc_nb_write(dev, uiop, flag)
    301   1.1       rvb     dev_t        dev;
    302   1.1       rvb     struct uio  *uiop;
    303   1.1       rvb     int          flag;
    304   1.1       rvb {
    305  1.13  augustss     struct vcomm *	vcp;
    306  1.13  augustss     struct vmsg *vmp;
    307   1.3       rvb     struct coda_out_hdr *out;
    308   1.1       rvb     u_long seq;
    309   1.1       rvb     u_long opcode;
    310   1.1       rvb     int buf[2];
    311   1.1       rvb     int error = 0;
    312   1.1       rvb 
    313   1.1       rvb     ENTRY;
    314   1.1       rvb 
    315   1.3       rvb     if (minor(dev) >= NVCODA || minor(dev) < 0)
    316   1.1       rvb 	return(ENXIO);
    317   1.1       rvb 
    318   1.3       rvb     vcp = &coda_mnttbl[minor(dev)].mi_vcomm;
    319   1.1       rvb 
    320   1.1       rvb     /* Peek at the opcode, unique without transfering the data. */
    321   1.1       rvb     uiop->uio_rw = UIO_WRITE;
    322   1.1       rvb     error = uiomove((caddr_t)buf, sizeof(int) * 2, uiop);
    323   1.1       rvb     if (error) {
    324   1.1       rvb 	myprintf(("vcwrite: error (%d) on uiomove\n", error));
    325   1.1       rvb 	return(EINVAL);
    326   1.1       rvb     }
    327   1.1       rvb 
    328   1.1       rvb     opcode = buf[0];
    329   1.1       rvb     seq = buf[1];
    330   1.1       rvb 
    331   1.3       rvb     if (codadebug)
    332   1.1       rvb 	myprintf(("vcwrite got a call for %ld.%ld\n", opcode, seq));
    333   1.1       rvb 
    334   1.1       rvb     if (DOWNCALL(opcode)) {
    335   1.1       rvb 	union outputArgs pbuf;
    336   1.1       rvb 
    337   1.1       rvb 	/* get the rest of the data. */
    338   1.1       rvb 	uiop->uio_rw = UIO_WRITE;
    339   1.3       rvb 	error = uiomove((caddr_t)&pbuf.coda_purgeuser.oh.result, sizeof(pbuf) - (sizeof(int)*2), uiop);
    340   1.1       rvb 	if (error) {
    341   1.1       rvb 	    myprintf(("vcwrite: error (%d) on uiomove (Op %ld seq %ld)\n",
    342   1.1       rvb 		      error, opcode, seq));
    343   1.1       rvb 	    return(EINVAL);
    344   1.1       rvb 	    }
    345   1.1       rvb 
    346   1.1       rvb 	return handleDownCall(opcode, &pbuf);
    347   1.1       rvb     }
    348   1.1       rvb 
    349   1.1       rvb     /* Look for the message on the (waiting for) reply queue. */
    350   1.1       rvb     for (vmp = (struct vmsg *)GETNEXT(vcp->vc_replys);
    351   1.1       rvb 	 !EOQ(vmp, vcp->vc_replys);
    352   1.1       rvb 	 vmp = (struct vmsg *)GETNEXT(vmp->vm_chain))
    353   1.1       rvb     {
    354   1.1       rvb 	if (vmp->vm_unique == seq) break;
    355   1.1       rvb     }
    356   1.1       rvb 
    357   1.1       rvb     if (EOQ(vmp, vcp->vc_replys)) {
    358   1.3       rvb 	if (codadebug)
    359   1.1       rvb 	    myprintf(("vcwrite: msg (%ld, %ld) not found\n", opcode, seq));
    360   1.1       rvb 
    361   1.1       rvb 	return(ESRCH);
    362   1.1       rvb 	}
    363   1.1       rvb 
    364   1.1       rvb     /* Remove the message from the reply queue */
    365   1.1       rvb     REMQUE(vmp->vm_chain);
    366   1.1       rvb 
    367   1.1       rvb     /* move data into response buffer. */
    368   1.3       rvb     out = (struct coda_out_hdr *)vmp->vm_data;
    369   1.1       rvb     /* Don't need to copy opcode and uniquifier. */
    370   1.1       rvb 
    371   1.1       rvb     /* get the rest of the data. */
    372   1.1       rvb     if (vmp->vm_outSize < uiop->uio_resid) {
    373  1.11      matt 	myprintf(("vcwrite: more data than asked for (%d < %lu)\n",
    374  1.11      matt 		  vmp->vm_outSize, (unsigned long) uiop->uio_resid));
    375   1.1       rvb 	wakeup(&vmp->vm_sleep); 	/* Notify caller of the error. */
    376   1.1       rvb 	return(EINVAL);
    377   1.1       rvb     }
    378   1.1       rvb 
    379   1.1       rvb     buf[0] = uiop->uio_resid; 	/* Save this value. */
    380   1.1       rvb     uiop->uio_rw = UIO_WRITE;
    381   1.1       rvb     error = uiomove((caddr_t) &out->result, vmp->vm_outSize - (sizeof(int) * 2), uiop);
    382   1.1       rvb     if (error) {
    383   1.1       rvb 	myprintf(("vcwrite: error (%d) on uiomove (op %ld seq %ld)\n",
    384   1.1       rvb 		  error, opcode, seq));
    385   1.1       rvb 	return(EINVAL);
    386   1.1       rvb     }
    387   1.1       rvb 
    388   1.1       rvb     /* I don't think these are used, but just in case. */
    389   1.1       rvb     /* XXX - aren't these two already correct? -bnoble */
    390   1.1       rvb     out->opcode = opcode;
    391   1.1       rvb     out->unique = seq;
    392   1.1       rvb     vmp->vm_outSize	= buf[0];	/* Amount of data transferred? */
    393   1.1       rvb     vmp->vm_flags |= VM_WRITE;
    394   1.1       rvb     wakeup(&vmp->vm_sleep);
    395   1.1       rvb 
    396   1.1       rvb     return(0);
    397   1.1       rvb }
    398   1.1       rvb 
    399   1.1       rvb int
    400   1.1       rvb vc_nb_ioctl(dev, cmd, addr, flag, p)
    401   1.1       rvb     dev_t         dev;
    402   1.5       rvb     u_long        cmd;
    403   1.1       rvb     caddr_t       addr;
    404   1.1       rvb     int           flag;
    405   1.1       rvb     struct proc  *p;
    406   1.1       rvb {
    407   1.1       rvb     ENTRY;
    408   1.1       rvb 
    409   1.1       rvb     switch(cmd) {
    410   1.3       rvb     case CODARESIZE: {
    411   1.3       rvb 	struct coda_resize *data = (struct coda_resize *)addr;
    412   1.3       rvb 	return(coda_nc_resize(data->hashsize, data->heapsize, IS_DOWNCALL));
    413   1.1       rvb 	break;
    414   1.1       rvb     }
    415   1.3       rvb     case CODASTATS:
    416   1.3       rvb 	if (coda_nc_use) {
    417   1.3       rvb 	    coda_nc_gather_stats();
    418   1.1       rvb 	    return(0);
    419   1.1       rvb 	} else {
    420   1.1       rvb 	    return(ENODEV);
    421   1.1       rvb 	}
    422   1.1       rvb 	break;
    423   1.3       rvb     case CODAPRINT:
    424   1.3       rvb 	if (coda_nc_use) {
    425   1.3       rvb 	    print_coda_nc();
    426   1.1       rvb 	    return(0);
    427   1.1       rvb 	} else {
    428   1.1       rvb 	    return(ENODEV);
    429   1.1       rvb 	}
    430   1.1       rvb 	break;
    431   1.9       rvb     case CIOC_KERNEL_VERSION:
    432   1.9       rvb 	switch (*(u_int *)addr) {
    433   1.9       rvb 	case 0:
    434   1.9       rvb 		*(u_int *)addr = coda_kernel_version;
    435   1.9       rvb 		return 0;
    436   1.9       rvb 		break;
    437   1.9       rvb 	case 1:
    438   1.9       rvb 	case 2:
    439   1.9       rvb 		if (coda_kernel_version != *(u_int *)addr)
    440   1.9       rvb 		    return ENOENT;
    441   1.9       rvb 		else
    442   1.9       rvb 		    return 0;
    443   1.9       rvb 	default:
    444   1.9       rvb 		return ENOENT;
    445   1.9       rvb 	}
    446   1.9       rvb     	break;
    447   1.1       rvb     default :
    448   1.1       rvb 	return(EINVAL);
    449   1.1       rvb 	break;
    450   1.1       rvb     }
    451   1.1       rvb }
    452   1.1       rvb 
    453   1.1       rvb int
    454   1.1       rvb vc_nb_poll(dev, events, p)
    455   1.1       rvb     dev_t         dev;
    456   1.1       rvb     int           events;
    457   1.1       rvb     struct proc  *p;
    458   1.1       rvb {
    459  1.13  augustss     struct vcomm *vcp;
    460   1.1       rvb     int event_msk = 0;
    461   1.1       rvb 
    462   1.1       rvb     ENTRY;
    463   1.1       rvb 
    464   1.3       rvb     if (minor(dev) >= NVCODA || minor(dev) < 0)
    465   1.1       rvb 	return(ENXIO);
    466   1.1       rvb 
    467   1.3       rvb     vcp = &coda_mnttbl[minor(dev)].mi_vcomm;
    468   1.1       rvb 
    469   1.1       rvb     event_msk = events & (POLLIN|POLLRDNORM);
    470   1.1       rvb     if (!event_msk)
    471   1.1       rvb 	return(0);
    472   1.1       rvb 
    473   1.1       rvb     if (!EMPTY(vcp->vc_requests))
    474   1.1       rvb 	return(events & (POLLIN|POLLRDNORM));
    475   1.1       rvb 
    476   1.1       rvb     selrecord(p, &(vcp->vc_selproc));
    477   1.1       rvb 
    478   1.1       rvb     return(0);
    479   1.1       rvb }
    480   1.1       rvb 
    481   1.1       rvb /*
    482   1.1       rvb  * Statistics
    483   1.1       rvb  */
    484   1.3       rvb struct coda_clstat coda_clstat;
    485   1.1       rvb 
    486   1.1       rvb /*
    487   1.1       rvb  * Key question: whether to sleep interuptably or uninteruptably when
    488   1.1       rvb  * waiting for Venus.  The former seems better (cause you can ^C a
    489   1.1       rvb  * job), but then GNU-EMACS completion breaks. Use tsleep with no
    490   1.1       rvb  * timeout, and no longjmp happens. But, when sleeping
    491   1.1       rvb  * "uninterruptibly", we don't get told if it returns abnormally
    492   1.1       rvb  * (e.g. kill -9).
    493   1.1       rvb  */
    494   1.1       rvb 
    495   1.1       rvb int
    496   1.3       rvb coda_call(mntinfo, inSize, outSize, buffer)
    497   1.3       rvb      struct coda_mntinfo *mntinfo; int inSize; int *outSize; caddr_t buffer;
    498   1.1       rvb {
    499   1.1       rvb 	struct vcomm *vcp;
    500   1.1       rvb 	struct vmsg *vmp;
    501   1.1       rvb 	int error;
    502   1.1       rvb #ifdef	CTL_C
    503   1.1       rvb 	struct proc *p = curproc;
    504   1.4       rvb 	sigset_t psig_omask;
    505   1.1       rvb 	int i;
    506  1.14  jdolecek 	psig_omask = p->p_sigctx.ps_siglist;	/* array assignment */
    507   1.1       rvb #endif
    508   1.1       rvb 	if (mntinfo == NULL) {
    509   1.1       rvb 	    /* Unlikely, but could be a race condition with a dying warden */
    510   1.1       rvb 	    return ENODEV;
    511   1.1       rvb 	}
    512   1.1       rvb 
    513   1.1       rvb 	vcp = &(mntinfo->mi_vcomm);
    514   1.1       rvb 
    515   1.3       rvb 	coda_clstat.ncalls++;
    516   1.3       rvb 	coda_clstat.reqs[((struct coda_in_hdr *)buffer)->opcode]++;
    517   1.1       rvb 
    518   1.1       rvb 	if (!VC_OPEN(vcp))
    519   1.1       rvb 	    return(ENODEV);
    520   1.1       rvb 
    521   1.3       rvb 	CODA_ALLOC(vmp,struct vmsg *,sizeof(struct vmsg));
    522   1.1       rvb 	/* Format the request message. */
    523   1.1       rvb 	vmp->vm_data = buffer;
    524   1.1       rvb 	vmp->vm_flags = 0;
    525   1.1       rvb 	vmp->vm_inSize = inSize;
    526   1.1       rvb 	vmp->vm_outSize
    527   1.1       rvb 	    = *outSize ? *outSize : inSize; /* |buffer| >= inSize */
    528   1.3       rvb 	vmp->vm_opcode = ((struct coda_in_hdr *)buffer)->opcode;
    529   1.1       rvb 	vmp->vm_unique = ++vcp->vc_seq;
    530   1.3       rvb 	if (codadebug)
    531   1.1       rvb 	    myprintf(("Doing a call for %d.%d\n",
    532   1.1       rvb 		      vmp->vm_opcode, vmp->vm_unique));
    533   1.1       rvb 
    534   1.1       rvb 	/* Fill in the common input args. */
    535   1.3       rvb 	((struct coda_in_hdr *)buffer)->unique = vmp->vm_unique;
    536   1.1       rvb 
    537   1.1       rvb 	/* Append msg to request queue and poke Venus. */
    538   1.1       rvb 	INSQUE(vmp->vm_chain, vcp->vc_requests);
    539   1.1       rvb 	selwakeup(&(vcp->vc_selproc));
    540   1.1       rvb 
    541   1.1       rvb 	/* We can be interrupted while we wait for Venus to process
    542   1.1       rvb 	 * our request.  If the interrupt occurs before Venus has read
    543   1.1       rvb 	 * the request, we dequeue and return. If it occurs after the
    544   1.1       rvb 	 * read but before the reply, we dequeue, send a signal
    545   1.1       rvb 	 * message, and return. If it occurs after the reply we ignore
    546   1.1       rvb 	 * it. In no case do we want to restart the syscall.  If it
    547   1.1       rvb 	 * was interrupted by a venus shutdown (vcclose), return
    548   1.1       rvb 	 * ENODEV.  */
    549   1.1       rvb 
    550   1.1       rvb 	/* Ignore return, We have to check anyway */
    551   1.1       rvb #ifdef	CTL_C
    552   1.3       rvb 	/* This is work in progress.  Setting coda_pcatch lets tsleep reawaken
    553   1.1       rvb 	   on a ^c or ^z.  The problem is that emacs sets certain interrupts
    554   1.1       rvb 	   as SA_RESTART.  This means that we should exit sleep handle the
    555   1.1       rvb 	   "signal" and then go to sleep again.  Mostly this is done by letting
    556   1.1       rvb 	   the syscall complete and be restarted.  We are not idempotent and
    557   1.1       rvb 	   can not do this.  A better solution is necessary.
    558   1.1       rvb 	 */
    559   1.1       rvb 	i = 0;
    560   1.1       rvb 	do {
    561   1.3       rvb 	    error = tsleep(&vmp->vm_sleep, (coda_call_sleep|coda_pcatch), "coda_call", hz*2);
    562   1.1       rvb 	    if (error == 0)
    563   1.1       rvb 	    	break;
    564   1.1       rvb 	    else if (error == EWOULDBLOCK) {
    565   1.7       rvb #ifdef	CODA_VERBOSE
    566   1.3       rvb 		    printf("coda_call: tsleep TIMEOUT %d sec\n", 2+2*i);
    567   1.5       rvb #endif
    568  1.14  jdolecek     	    } else if (sigismember(&p->p_sigctx.ps_siglist, SIGIO)) {
    569  1.14  jdolecek 		    sigaddset(&p->p_sigctx.ps_sigmask, SIGIO);
    570   1.7       rvb #ifdef	CODA_VERBOSE
    571   1.3       rvb 		    printf("coda_call: tsleep returns %d SIGIO, cnt %d\n", error, i);
    572   1.5       rvb #endif
    573  1.14  jdolecek     	    } else if (sigismember(&p->p_sigctx.ps_siglist, SIGALRM)) {
    574  1.14  jdolecek 		    sigaddset(&p->p_sigctx.ps_sigmask, SIGALRM);
    575   1.8       rvb #ifdef	CODA_VERBOSE
    576   1.8       rvb 		    printf("coda_call: tsleep returns %d SIGALRM, cnt %d\n", error, i);
    577   1.8       rvb #endif
    578   1.1       rvb 	    } else {
    579   1.4       rvb 		    sigset_t tmp;
    580  1.14  jdolecek 		    tmp = p->p_sigctx.ps_siglist;	/* array assignment */
    581  1.14  jdolecek 		    sigminusset(&p->p_sigctx.ps_sigmask, &tmp);
    582   1.4       rvb 
    583   1.7       rvb #ifdef	CODA_VERBOSE
    584   1.3       rvb 		    printf("coda_call: tsleep returns %d, cnt %d\n", error, i);
    585   1.4       rvb 		    printf("coda_call: siglist = %x.%x.%x.%x, sigmask = %x.%x.%x.%x, mask %x.%x.%x.%x\n",
    586  1.14  jdolecek 			    p->p_sigctx.ps_siglist.__bits[0], p->p_sigctx.ps_siglist.__bits[1],
    587  1.14  jdolecek 			    p->p_sigctx.ps_siglist.__bits[2], p->p_sigctx.ps_siglist.__bits[3],
    588  1.14  jdolecek 			    p->p_sigctx.ps_sigmask.__bits[0], p->p_sigctx.ps_sigmask.__bits[1],
    589  1.14  jdolecek 			    p->p_sigctx.ps_sigmask.__bits[2], p->p_sigctx.ps_sigmask.__bits[3],
    590   1.4       rvb 			    tmp.__bits[0], tmp.__bits[1], tmp.__bits[2], tmp.__bits[3]);
    591   1.5       rvb #endif
    592   1.1       rvb 		    break;
    593   1.5       rvb #ifdef	notyet
    594  1.14  jdolecek 		    sigminusset(&p->p_sigctx.ps_sigmask, &p->p_sigctx.ps_siglist);
    595   1.4       rvb 		    printf("coda_call: siglist = %x.%x.%x.%x, sigmask = %x.%x.%x.%x\n",
    596  1.15  jdolecek 			    p->p_sigctx.ps_siglist.__bits[0], p->p_sigctx.ps_siglist.__bits[1],
    597  1.15  jdolecek 			    p->p_sigctx.ps_siglist.__bits[2], p->p_sigctx.ps_siglist.__bits[3],
    598  1.15  jdolecek 			    p->p_sigctx.ps_sigmask.__bits[0], p->p_sigctx.ps_sigmask.__bits[1],
    599  1.15  jdolecek 			    p->p_sigctx.ps_sigmask.__bits[2], p->p_sigctx.ps_sigmask.__bits[3]);
    600   1.5       rvb #endif
    601   1.1       rvb 	    }
    602   1.8       rvb 	} while (error && i++ < 128 && VC_OPEN(vcp));
    603  1.14  jdolecek 	p->p_sigctx.ps_siglist = psig_omask;	/* array assignment */
    604   1.1       rvb #else
    605   1.3       rvb 	(void) tsleep(&vmp->vm_sleep, coda_call_sleep, "coda_call", 0);
    606   1.1       rvb #endif
    607   1.1       rvb 	if (VC_OPEN(vcp)) {	/* Venus is still alive */
    608   1.1       rvb  	/* Op went through, interrupt or not... */
    609   1.1       rvb 	    if (vmp->vm_flags & VM_WRITE) {
    610   1.1       rvb 		error = 0;
    611   1.1       rvb 		*outSize = vmp->vm_outSize;
    612   1.1       rvb 	    }
    613   1.1       rvb 
    614   1.1       rvb 	    else if (!(vmp->vm_flags & VM_READ)) {
    615   1.1       rvb 		/* Interrupted before venus read it. */
    616   1.7       rvb #ifdef	CODA_VERBOSE
    617   1.7       rvb 		if (1)
    618   1.7       rvb #else
    619   1.5       rvb 		if (codadebug)
    620   1.5       rvb #endif
    621   1.1       rvb 		    myprintf(("interrupted before read: op = %d.%d, flags = %x\n",
    622   1.1       rvb 			   vmp->vm_opcode, vmp->vm_unique, vmp->vm_flags));
    623   1.1       rvb 		REMQUE(vmp->vm_chain);
    624   1.1       rvb 		error = EINTR;
    625   1.1       rvb 	    }
    626   1.1       rvb 
    627   1.1       rvb 	    else {
    628   1.1       rvb 		/* (!(vmp->vm_flags & VM_WRITE)) means interrupted after
    629   1.1       rvb                    upcall started */
    630   1.1       rvb 		/* Interrupted after start of upcall, send venus a signal */
    631   1.3       rvb 		struct coda_in_hdr *dog;
    632   1.1       rvb 		struct vmsg *svmp;
    633   1.1       rvb 
    634   1.7       rvb #ifdef	CODA_VERBOSE
    635   1.7       rvb 		if (1)
    636   1.7       rvb #else
    637   1.5       rvb 		if (codadebug)
    638   1.5       rvb #endif
    639   1.1       rvb 		    myprintf(("Sending Venus a signal: op = %d.%d, flags = %x\n",
    640   1.1       rvb 			   vmp->vm_opcode, vmp->vm_unique, vmp->vm_flags));
    641   1.1       rvb 
    642   1.1       rvb 		REMQUE(vmp->vm_chain);
    643   1.1       rvb 		error = EINTR;
    644   1.1       rvb 
    645   1.3       rvb 		CODA_ALLOC(svmp, struct vmsg *, sizeof (struct vmsg));
    646   1.1       rvb 
    647   1.3       rvb 		CODA_ALLOC((svmp->vm_data), char *, sizeof (struct coda_in_hdr));
    648   1.3       rvb 		dog = (struct coda_in_hdr *)svmp->vm_data;
    649   1.1       rvb 
    650   1.1       rvb 		svmp->vm_flags = 0;
    651   1.3       rvb 		dog->opcode = svmp->vm_opcode = CODA_SIGNAL;
    652   1.1       rvb 		dog->unique = svmp->vm_unique = vmp->vm_unique;
    653   1.3       rvb 		svmp->vm_inSize = sizeof (struct coda_in_hdr);
    654   1.3       rvb /*??? rvb */	svmp->vm_outSize = sizeof (struct coda_in_hdr);
    655   1.1       rvb 
    656   1.3       rvb 		if (codadebug)
    657   1.3       rvb 		    myprintf(("coda_call: enqueing signal msg (%d, %d)\n",
    658   1.1       rvb 			   svmp->vm_opcode, svmp->vm_unique));
    659   1.1       rvb 
    660   1.1       rvb 		/* insert at head of queue! */
    661   1.1       rvb 		INSQUE(svmp->vm_chain, vcp->vc_requests);
    662   1.1       rvb 		selwakeup(&(vcp->vc_selproc));
    663   1.1       rvb 	    }
    664   1.1       rvb 	}
    665   1.1       rvb 
    666   1.1       rvb 	else {	/* If venus died (!VC_OPEN(vcp)) */
    667   1.3       rvb 	    if (codadebug)
    668   1.1       rvb 		myprintf(("vcclose woke op %d.%d flags %d\n",
    669   1.1       rvb 		       vmp->vm_opcode, vmp->vm_unique, vmp->vm_flags));
    670   1.1       rvb 
    671   1.1       rvb 		error = ENODEV;
    672   1.1       rvb 	}
    673   1.1       rvb 
    674   1.3       rvb 	CODA_FREE(vmp, sizeof(struct vmsg));
    675   1.8       rvb 
    676   1.8       rvb 	if (outstanding_upcalls > 0 && (--outstanding_upcalls == 0))
    677   1.8       rvb 		wakeup(&outstanding_upcalls);
    678   1.1       rvb 
    679   1.1       rvb 	if (!error)
    680   1.3       rvb 		error = ((struct coda_out_hdr *)buffer)->result;
    681   1.1       rvb 	return(error);
    682   1.1       rvb }
    683   1.4       rvb 
    684