Home | History | Annotate | Line # | Download | only in coda
coda_subr.c revision 1.5
      1  1.5  rvb /*	$NetBSD: coda_subr.c,v 1.5 1998/09/25 15:01:13 rvb 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_subr.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 /*
     47  1.1  rvb  * HISTORY
     48  1.4  rvb  * $Log: coda_subr.c,v $
     49  1.5  rvb  * Revision 1.5  1998/09/25 15:01:13  rvb
     50  1.5  rvb  * Conditionalize "stray" printouts under DIAGNOSTIC and DEBUG.
     51  1.5  rvb  * Make files compile if DEBUG is on (from  Alan Barrett).  Finally,
     52  1.4  rvb  * make coda an lkm.
     53  1.4  rvb  *
     54  1.4  rvb  * Revision 1.4  1998/09/15 02:02:59  rvb
     55  1.4  rvb  * Final piece of rename cfs->coda
     56  1.3  rvb  *
     57  1.3  rvb  * Revision 1.3  1998/09/12 15:05:49  rvb
     58  1.3  rvb  * Change cfs/CFS in symbols, strings and constants to coda/CODA
     59  1.2  rvb  * to avoid fs conflicts.
     60  1.2  rvb  *
     61  1.2  rvb  * Revision 1.2  1998/09/08 17:12:47  rvb
     62  1.1  rvb  * Pass2 complete
     63  1.1  rvb  *
     64  1.1  rvb  * Revision 1.1.1.1  1998/08/29 21:26:45  rvb
     65  1.1  rvb  * Very Preliminary Coda
     66  1.1  rvb  *
     67  1.1  rvb  * Revision 1.11  1998/08/28 18:12:18  rvb
     68  1.1  rvb  * Now it also works on FreeBSD -current.  This code will be
     69  1.1  rvb  * committed to the FreeBSD -current and NetBSD -current
     70  1.1  rvb  * trees.  It will then be tailored to the particular platform
     71  1.1  rvb  * by flushing conditional code.
     72  1.1  rvb  *
     73  1.1  rvb  * Revision 1.10  1998/08/18 17:05:16  rvb
     74  1.1  rvb  * Don't use __RCSID now
     75  1.1  rvb  *
     76  1.1  rvb  * Revision 1.9  1998/08/18 16:31:41  rvb
     77  1.1  rvb  * Sync the code for NetBSD -current; test on 1.3 later
     78  1.3  rvb  *
     79  1.1  rvb  * Revision 1.8  98/01/31  20:53:12  rvb
     80  1.1  rvb  * First version that works on FreeBSD 2.2.5
     81  1.1  rvb  *
     82  1.1  rvb  * Revision 1.7  98/01/23  11:53:42  rvb
     83  1.1  rvb  * Bring RVB_CODA1_1 to HEAD
     84  1.1  rvb  *
     85  1.1  rvb  * Revision 1.6.2.3  98/01/23  11:21:05  rvb
     86  1.1  rvb  * Sync with 2.2.5
     87  1.1  rvb  *
     88  1.1  rvb  * Revision 1.6.2.2  97/12/16  12:40:06  rvb
     89  1.1  rvb  * Sync with 1.3
     90  1.1  rvb  *
     91  1.1  rvb  * Revision 1.6.2.1  97/12/06  17:41:21  rvb
     92  1.1  rvb  * Sync with peters coda.h
     93  1.1  rvb  *
     94  1.1  rvb  * Revision 1.6  97/12/05  10:39:17  rvb
     95  1.1  rvb  * Read CHANGES
     96  1.1  rvb  *
     97  1.1  rvb  * Revision 1.5.4.8  97/11/26  15:28:58  rvb
     98  1.1  rvb  * Cant make downcall pbuf == union cfs_downcalls yet
     99  1.1  rvb  *
    100  1.1  rvb  * Revision 1.5.4.7  97/11/20  11:46:42  rvb
    101  1.1  rvb  * Capture current cfs_venus
    102  1.1  rvb  *
    103  1.1  rvb  * Revision 1.5.4.6  97/11/18  10:27:16  rvb
    104  1.1  rvb  * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
    105  1.1  rvb  * cfs_nb_foo and cfs_foo are joined
    106  1.1  rvb  *
    107  1.1  rvb  * Revision 1.5.4.5  97/11/13  22:03:00  rvb
    108  1.1  rvb  * pass2 cfs_NetBSD.h mt
    109  1.1  rvb  *
    110  1.1  rvb  * Revision 1.5.4.4  97/11/12  12:09:39  rvb
    111  1.1  rvb  * reorg pass1
    112  1.1  rvb  *
    113  1.1  rvb  * Revision 1.5.4.3  97/11/06  21:02:38  rvb
    114  1.1  rvb  * first pass at ^c ^z
    115  1.1  rvb  *
    116  1.1  rvb  * Revision 1.5.4.2  97/10/29  16:06:27  rvb
    117  1.1  rvb  * Kill DYING
    118  1.3  rvb  *
    119  1.1  rvb  * Revision 1.5.4.1  97/10/28 23:10:16  rvb
    120  1.1  rvb  * >64Meg; venus can be killed!
    121  1.1  rvb  *
    122  1.1  rvb  * Revision 1.5  97/08/05  11:08:17  lily
    123  1.1  rvb  * Removed cfsnc_replace, replaced it with a coda_find, unhash, and
    124  1.1  rvb  * rehash.  This fixes a cnode leak and a bug in which the fid is
    125  1.1  rvb  * not actually replaced.  (cfs_namecache.c, cfsnc.h, cfs_subr.c)
    126  1.1  rvb  *
    127  1.1  rvb  * Revision 1.4  96/12/12  22:10:59  bnoble
    128  1.1  rvb  * Fixed the "downcall invokes venus operation" deadlock in all known cases.
    129  1.1  rvb  * There may be more
    130  1.1  rvb  *
    131  1.1  rvb  * Revision 1.3  1996/12/05 16:20:15  bnoble
    132  1.1  rvb  * Minor debugging aids
    133  1.3  rvb  *
    134  1.1  rvb  * Revision 1.2  1996/01/02 16:57:01  bnoble
    135  1.1  rvb  * Added support for Coda MiniCache and raw inode calls (final commit)
    136  1.1  rvb  *
    137  1.1  rvb  * Revision 1.1.2.1  1995/12/20 01:57:27  bnoble
    138  1.1  rvb  * Added CODA-specific files
    139  1.1  rvb  *
    140  1.1  rvb  * Revision 3.1.1.1  1995/03/04  19:07:59  bnoble
    141  1.1  rvb  * Branch for NetBSD port revisions
    142  1.1  rvb  *
    143  1.1  rvb  * Revision 3.1  1995/03/04  19:07:58  bnoble
    144  1.1  rvb  * Bump to major revision 3 to prepare for NetBSD port
    145  1.1  rvb  *
    146  1.1  rvb  * Revision 2.8  1995/03/03  17:00:04  dcs
    147  1.1  rvb  * Fixed kernel bug involving sleep and upcalls. Basically if you killed
    148  1.1  rvb  * a job waiting on venus, the venus upcall queues got trashed. Depending
    149  1.1  rvb  * on luck, you could kill the kernel or not.
    150  1.1  rvb  * (mods to cfs_subr.c and cfs_mach.d)
    151  1.1  rvb  *
    152  1.1  rvb  * Revision 2.7  95/03/02  22:45:21  dcs
    153  1.1  rvb  * Sun4 compatibility
    154  1.1  rvb  *
    155  1.1  rvb  * Revision 2.6  95/02/17  16:25:17  dcs
    156  1.1  rvb  * These versions represent several changes:
    157  1.1  rvb  * 1. Allow venus to restart even if outstanding references exist.
    158  1.1  rvb  * 2. Have only one ctlvp per client, as opposed to one per mounted cfs device.d
    159  1.1  rvb  * 3. Allow ody_expand to return many members, not just one.
    160  1.1  rvb  *
    161  1.1  rvb  * Revision 2.5  94/11/09  15:56:26  dcs
    162  1.1  rvb  * Had the thread sleeping on the wrong thing!
    163  1.1  rvb  *
    164  1.1  rvb  * Revision 2.4  94/10/14  09:57:57  dcs
    165  1.1  rvb  * Made changes 'cause sun4s have braindead compilers
    166  1.1  rvb  *
    167  1.1  rvb  * Revision 2.3  94/10/12  16:46:26  dcs
    168  1.1  rvb  * Cleaned kernel/venus interface by removing XDR junk, plus
    169  1.1  rvb  * so cleanup to allow this code to be more easily ported.
    170  1.1  rvb  *
    171  1.3  rvb  * Revision 1.2  92/10/27  17:58:22  lily
    172  1.1  rvb  * merge kernel/latest and alpha/src/cfs
    173  1.1  rvb  *
    174  1.1  rvb  * Revision 2.4  92/09/30  14:16:26  mja
    175  1.1  rvb  * 	Incorporated Dave Steere's fix for the GNU-Emacs bug.
    176  1.1  rvb  * 	Also, included his coda_flush routine in place of the former coda_nc_flush.
    177  1.1  rvb  * 	[91/02/07            jjk]
    178  1.1  rvb  *
    179  1.1  rvb  * 	Added contributors blurb.
    180  1.1  rvb  * 	[90/12/13            jjk]
    181  1.1  rvb  *
    182  1.1  rvb  * 	Hack to allow users to keep coda venus calls uninterruptible. THis
    183  1.1  rvb  * 	basically prevents the Gnu-emacs bug from appearing, in which a call
    184  1.1  rvb  * 	was being interrupted, and return EINTR, but gnu didn't check for the
    185  1.1  rvb  * 	error and figured the file was buggered.
    186  1.1  rvb  * 	[90/12/09            dcs]
    187  1.1  rvb  *
    188  1.1  rvb  * Revision 2.3  90/08/10  10:23:20  mrt
    189  1.1  rvb  * 	Removed include of vm/vm_page.h as it no longer exists.
    190  1.1  rvb  * 	[90/08/10            mrt]
    191  1.1  rvb  *
    192  1.1  rvb  * Revision 2.2  90/07/05  11:26:35  mrt
    193  1.1  rvb  * 	Initialize name cache on first call to vcopen.
    194  1.1  rvb  * 	[90/05/23            dcs]
    195  1.1  rvb  *
    196  1.1  rvb  * 	Created for the Coda File System.
    197  1.1  rvb  * 	[90/05/23            dcs]
    198  1.1  rvb  *
    199  1.1  rvb  * Revision 1.5  90/05/31  17:01:35  dcs
    200  1.1  rvb  * Prepare for merge with facilities kernel.
    201  1.1  rvb  *
    202  1.1  rvb  * Revision 1.2  90/03/19  15:56:25  dcs
    203  1.1  rvb  * Initialize name cache on first call to vcopen.
    204  1.1  rvb  *
    205  1.1  rvb  * Revision 1.1  90/03/15  10:43:26  jjk
    206  1.3  rvb  * Initial revision
    207  1.1  rvb  *
    208  1.3  rvb  */
    209  1.1  rvb 
    210  1.3  rvb /* NOTES: rvb
    211  1.1  rvb  * 1.	Added coda_unmounting to mark all cnodes as being UNMOUNTING.  This has to
    212  1.1  rvb  *	 be done before dounmount is called.  Because some of the routines that
    213  1.3  rvb  *	 dounmount calls before coda_unmounted might try to force flushes to venus.
    214  1.1  rvb  *	 The vnode pager does this.
    215  1.1  rvb  * 2.	coda_unmounting marks all cnodes scanning coda_cache.
    216  1.5  rvb  * 3.	cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
    217  1.5  rvb  *	 under the /coda mount point.
    218  1.5  rvb  * 4.	coda_cacheprint (under DEBUG) prints names with vnode/cnode address
    219  1.3  rvb  */
    220  1.5  rvb 
    221  1.1  rvb #ifdef	_LKM
    222  1.1  rvb #define	NVCODA 4
    223  1.1  rvb #else
    224  1.1  rvb #include <vcoda.h>
    225  1.1  rvb #endif
    226  1.1  rvb 
    227  1.1  rvb #include <sys/param.h>
    228  1.1  rvb #include <sys/systm.h>
    229  1.4  rvb #include <sys/malloc.h>
    230  1.4  rvb #include <sys/proc.h>
    231  1.4  rvb #include <sys/select.h>
    232  1.4  rvb #include <sys/mount.h>
    233  1.1  rvb 
    234  1.3  rvb #include <coda/coda.h>
    235  1.3  rvb #include <coda/cnode.h>
    236  1.3  rvb #include <coda/coda_subr.h>
    237  1.1  rvb #include <coda/coda_namecache.h>
    238  1.3  rvb 
    239  1.3  rvb int coda_active = 0;
    240  1.1  rvb int coda_reuse = 0;
    241  1.3  rvb int coda_new = 0;
    242  1.3  rvb 
    243  1.1  rvb struct cnode *coda_freelist = NULL;
    244  1.1  rvb struct cnode *coda_cache[CODA_CACHESIZE];
    245  1.1  rvb 
    246  1.1  rvb #define coda_hash(fid) \
    247  1.1  rvb     (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
    248  1.1  rvb 
    249  1.1  rvb #define	CNODE_NEXT(cp)	((cp)->c_next)
    250  1.1  rvb 
    251  1.1  rvb #define ODD(vnode)        ((vnode) & 0x1)
    252  1.3  rvb 
    253  1.1  rvb /*
    254  1.1  rvb  * Allocate a cnode.
    255  1.1  rvb  */
    256  1.3  rvb struct cnode *
    257  1.3  rvb coda_alloc(void)
    258  1.3  rvb {
    259  1.3  rvb     struct cnode *cp;
    260  1.1  rvb 
    261  1.1  rvb     if (coda_freelist) {
    262  1.3  rvb 	cp = coda_freelist;
    263  1.1  rvb 	coda_freelist = CNODE_NEXT(cp);
    264  1.1  rvb 	coda_reuse++;
    265  1.1  rvb     }
    266  1.1  rvb     else {
    267  1.3  rvb 	CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
    268  1.1  rvb 	/* NetBSD vnodes don't have any Pager info in them ('cause there are
    269  1.1  rvb 	   no external pagers, duh!) */
    270  1.1  rvb #define VNODE_VM_INFO_INIT(vp)         /* MT */
    271  1.1  rvb 	VNODE_VM_INFO_INIT(CTOV(cp));
    272  1.1  rvb 	coda_new++;
    273  1.1  rvb     }
    274  1.1  rvb     bzero(cp, sizeof (struct cnode));
    275  1.1  rvb 
    276  1.1  rvb     return(cp);
    277  1.1  rvb }
    278  1.3  rvb 
    279  1.1  rvb /*
    280  1.1  rvb  * Deallocate a cnode.
    281  1.1  rvb  */
    282  1.3  rvb void
    283  1.3  rvb coda_free(cp)
    284  1.1  rvb      register struct cnode *cp;
    285  1.1  rvb {
    286  1.1  rvb 
    287  1.1  rvb     CNODE_NEXT(cp) = coda_freelist;
    288  1.1  rvb     coda_freelist = cp;
    289  1.1  rvb }
    290  1.3  rvb 
    291  1.1  rvb /*
    292  1.1  rvb  * Put a cnode in the hash table
    293  1.3  rvb  */
    294  1.3  rvb void
    295  1.1  rvb coda_save(cp)
    296  1.1  rvb      struct cnode *cp;
    297  1.1  rvb {
    298  1.1  rvb 	CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
    299  1.1  rvb 	coda_cache[coda_hash(&cp->c_fid)] = cp;
    300  1.1  rvb }
    301  1.3  rvb 
    302  1.1  rvb /*
    303  1.1  rvb  * Remove a cnode from the hash table
    304  1.1  rvb  */
    305  1.1  rvb void
    306  1.1  rvb coda_unsave(cp)
    307  1.3  rvb      struct cnode *cp;
    308  1.1  rvb {
    309  1.1  rvb     struct cnode *ptr;
    310  1.1  rvb     struct cnode *ptrprev = NULL;
    311  1.3  rvb 
    312  1.1  rvb     ptr = coda_cache[coda_hash(&cp->c_fid)];
    313  1.1  rvb     while (ptr != NULL) {
    314  1.1  rvb 	if (ptr == cp) {
    315  1.1  rvb 	    if (ptrprev == NULL) {
    316  1.1  rvb 		coda_cache[coda_hash(&cp->c_fid)]
    317  1.1  rvb 		    = CNODE_NEXT(ptr);
    318  1.1  rvb 	    } else {
    319  1.1  rvb 		CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
    320  1.1  rvb 	    }
    321  1.1  rvb 	    CNODE_NEXT(cp) = (struct cnode *)NULL;
    322  1.1  rvb 
    323  1.1  rvb 	    return;
    324  1.1  rvb 	}
    325  1.1  rvb 	ptrprev = ptr;
    326  1.1  rvb 	ptr = CNODE_NEXT(ptr);
    327  1.1  rvb     }
    328  1.1  rvb }
    329  1.1  rvb 
    330  1.3  rvb /*
    331  1.1  rvb  * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
    332  1.1  rvb  * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
    333  1.1  rvb  */
    334  1.1  rvb struct cnode *
    335  1.3  rvb coda_find(fid)
    336  1.1  rvb      ViceFid *fid;
    337  1.1  rvb {
    338  1.1  rvb     struct cnode *cp;
    339  1.1  rvb 
    340  1.1  rvb     cp = coda_cache[coda_hash(fid)];
    341  1.1  rvb     while (cp) {
    342  1.3  rvb 	if ((cp->c_fid.Vnode == fid->Vnode) &&
    343  1.1  rvb 	    (cp->c_fid.Volume == fid->Volume) &&
    344  1.1  rvb 	    (cp->c_fid.Unique == fid->Unique) &&
    345  1.1  rvb 	    (!IS_UNMOUNTING(cp)))
    346  1.1  rvb 	    {
    347  1.1  rvb 		coda_active++;
    348  1.1  rvb 		return(cp);
    349  1.1  rvb 	    }
    350  1.1  rvb 	cp = CNODE_NEXT(cp);
    351  1.3  rvb     }
    352  1.1  rvb     return(NULL);
    353  1.1  rvb }
    354  1.1  rvb 
    355  1.3  rvb /*
    356  1.1  rvb  * coda_kill is called as a side effect to vcopen. To prevent any
    357  1.3  rvb  * cnodes left around from an earlier run of a venus or warden from
    358  1.1  rvb  * causing problems with the new instance, mark any outstanding cnodes
    359  1.1  rvb  * as dying. Future operations on these cnodes should fail (excepting
    360  1.3  rvb  * coda_inactive of course!). Since multiple venii/wardens can be
    361  1.1  rvb  * running, only kill the cnodes for a particular entry in the
    362  1.1  rvb  * coda_mnttbl. -- DCS 12/1/94 */
    363  1.1  rvb 
    364  1.1  rvb int
    365  1.1  rvb coda_kill(whoIam, dcstat)
    366  1.1  rvb 	struct mount *whoIam;
    367  1.1  rvb 	enum dc_status dcstat;
    368  1.1  rvb {
    369  1.1  rvb 	int hash, count = 0;
    370  1.1  rvb 	struct cnode *cp;
    371  1.1  rvb 
    372  1.1  rvb 	/*
    373  1.2  rvb 	 * Algorithm is as follows:
    374  1.1  rvb 	 *     Second, flush whatever vnodes we can from the name cache.
    375  1.1  rvb 	 *
    376  1.1  rvb 	 *     Finally, step through whatever is left and mark them dying.
    377  1.1  rvb 	 *        This prevents any operation at all.
    378  1.1  rvb 
    379  1.3  rvb 	 */
    380  1.1  rvb 
    381  1.3  rvb 	/* This is slightly overkill, but should work. Eventually it'd be
    382  1.3  rvb 	 * nice to only flush those entries from the namecache that
    383  1.1  rvb 	 * reference a vnode in this vfs.  */
    384  1.1  rvb 	coda_nc_flush(dcstat);
    385  1.3  rvb 
    386  1.1  rvb 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    387  1.1  rvb 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    388  1.3  rvb 			if (CTOV(cp)->v_mount == whoIam) {
    389  1.1  rvb #ifdef	DEBUG
    390  1.1  rvb 				printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
    391  1.1  rvb #endif
    392  1.1  rvb 				count++;
    393  1.1  rvb 				CODADEBUG(CODA_FLUSH,
    394  1.1  rvb 					 myprintf(("Live cnode fid %lx.%lx.%lx flags %d count %d\n",
    395  1.1  rvb 						   (cp->c_fid).Volume,
    396  1.1  rvb 						   (cp->c_fid).Vnode,
    397  1.1  rvb 						   (cp->c_fid).Unique,
    398  1.1  rvb 						   cp->c_flags,
    399  1.1  rvb 						   CTOV(cp)->v_usecount)); );
    400  1.1  rvb 			}
    401  1.1  rvb 		}
    402  1.1  rvb 	}
    403  1.1  rvb 	return count;
    404  1.1  rvb }
    405  1.1  rvb 
    406  1.3  rvb /*
    407  1.1  rvb  * There are two reasons why a cnode may be in use, it may be in the
    408  1.1  rvb  * name cache or it may be executing.
    409  1.1  rvb  */
    410  1.1  rvb void
    411  1.1  rvb coda_flush(dcstat)
    412  1.3  rvb 	enum dc_status dcstat;
    413  1.3  rvb {
    414  1.1  rvb     int hash;
    415  1.3  rvb     struct cnode *cp;
    416  1.1  rvb 
    417  1.3  rvb     coda_clstat.ncalls++;
    418  1.3  rvb     coda_clstat.reqs[CODA_FLUSH]++;
    419  1.1  rvb 
    420  1.3  rvb     coda_nc_flush(dcstat);	    /* flush files from the name cache */
    421  1.1  rvb 
    422  1.1  rvb     for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    423  1.1  rvb 	for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    424  1.1  rvb 	    if (!ODD(cp->c_fid.Vnode)) /* only files can be executed */
    425  1.1  rvb 		coda_vmflush(cp);
    426  1.1  rvb 	}
    427  1.1  rvb     }
    428  1.1  rvb }
    429  1.1  rvb 
    430  1.3  rvb /*
    431  1.1  rvb  * As a debugging measure, print out any cnodes that lived through a
    432  1.1  rvb  * name cache flush.
    433  1.1  rvb  */
    434  1.1  rvb void
    435  1.3  rvb coda_testflush(void)
    436  1.3  rvb {
    437  1.1  rvb     int hash;
    438  1.1  rvb     struct cnode *cp;
    439  1.1  rvb 
    440  1.1  rvb     for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    441  1.1  rvb 	for (cp = coda_cache[hash];
    442  1.1  rvb 	     cp != NULL;
    443  1.1  rvb 	     cp = CNODE_NEXT(cp)) {
    444  1.1  rvb 	    myprintf(("Live cnode fid %lx.%lx.%lx count %d\n",
    445  1.1  rvb 		      (cp->c_fid).Volume,(cp->c_fid).Vnode,
    446  1.1  rvb 		      (cp->c_fid).Unique, CTOV(cp)->v_usecount));
    447  1.1  rvb 	}
    448  1.1  rvb     }
    449  1.1  rvb }
    450  1.1  rvb 
    451  1.1  rvb /*
    452  1.1  rvb  *     First, step through all cnodes and mark them unmounting.
    453  1.3  rvb  *         NetBSD kernels may try to fsync them now that venus
    454  1.1  rvb  *         is dead, which would be a bad thing.
    455  1.1  rvb  *
    456  1.1  rvb  */
    457  1.1  rvb void
    458  1.1  rvb coda_unmounting(whoIam)
    459  1.3  rvb 	struct mount *whoIam;
    460  1.3  rvb {
    461  1.1  rvb 	int hash;
    462  1.1  rvb 	struct cnode *cp;
    463  1.3  rvb 
    464  1.1  rvb 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    465  1.1  rvb 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    466  1.1  rvb 			if (CTOV(cp)->v_mount == whoIam) {
    467  1.1  rvb 				if (cp->c_flags & (C_LOCKED|C_WANTED)) {
    468  1.1  rvb 					printf("coda_unmounting: Unlocking %p\n", cp);
    469  1.1  rvb 					cp->c_flags &= ~(C_LOCKED|C_WANTED);
    470  1.1  rvb 					wakeup((caddr_t) cp);
    471  1.1  rvb 				}
    472  1.1  rvb 				cp->c_flags |= C_UNMOUNTING;
    473  1.1  rvb 			}
    474  1.5  rvb 		}
    475  1.3  rvb 	}
    476  1.1  rvb }
    477  1.1  rvb 
    478  1.1  rvb #ifdef	DEBUG
    479  1.1  rvb void
    480  1.1  rvb coda_checkunmounting(mp)
    481  1.1  rvb 	struct mount *mp;
    482  1.1  rvb {
    483  1.1  rvb 	register struct vnode *vp, *nvp;
    484  1.1  rvb 	struct cnode *cp;
    485  1.1  rvb 	int count = 0, bad = 0;
    486  1.1  rvb loop:
    487  1.1  rvb 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
    488  1.1  rvb 		if (vp->v_mount != mp)
    489  1.1  rvb 			goto loop;
    490  1.1  rvb 		nvp = vp->v_mntvnodes.le_next;
    491  1.1  rvb 		cp = VTOC(vp);
    492  1.1  rvb 		count++;
    493  1.1  rvb 		if (!(cp->c_flags & C_UNMOUNTING)) {
    494  1.1  rvb 			bad++;
    495  1.1  rvb 			printf("vp %p, cp %p missed\n", vp, cp);
    496  1.5  rvb 			cp->c_flags |= C_UNMOUNTING;
    497  1.3  rvb 		}
    498  1.1  rvb 	}
    499  1.1  rvb }
    500  1.1  rvb 
    501  1.1  rvb void
    502  1.1  rvb coda_cacheprint(whoIam)
    503  1.1  rvb 	struct mount *whoIam;
    504  1.3  rvb {
    505  1.5  rvb 	int hash;
    506  1.1  rvb 	struct cnode *cp;
    507  1.1  rvb 	int count = 0;
    508  1.3  rvb 
    509  1.3  rvb 	printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
    510  1.1  rvb 	coda_nc_name(VTOC(coda_ctlvp));
    511  1.3  rvb 	printf("\n");
    512  1.3  rvb 
    513  1.1  rvb 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    514  1.1  rvb 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    515  1.1  rvb 			if (CTOV(cp)->v_mount == whoIam) {
    516  1.1  rvb 				printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
    517  1.1  rvb 				coda_nc_name(cp);
    518  1.3  rvb 				printf("\n");
    519  1.1  rvb 				count++;
    520  1.1  rvb 			}
    521  1.1  rvb 		}
    522  1.1  rvb 	}
    523  1.1  rvb 	printf("coda_cacheprint: count %d\n", count);
    524  1.1  rvb }
    525  1.1  rvb #endif
    526  1.3  rvb 
    527  1.3  rvb /*
    528  1.1  rvb  * There are 6 cases where invalidations occur. The semantics of each
    529  1.1  rvb  * is listed here.
    530  1.1  rvb  *
    531  1.3  rvb  * CODA_FLUSH     -- flush all entries from the name cache and the cnode cache.
    532  1.1  rvb  * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
    533  1.3  rvb  *                  This call is a result of token expiration.
    534  1.1  rvb  *
    535  1.1  rvb  * The next two are the result of callbacks on a file or directory.
    536  1.3  rvb  * CODA_ZAPDIR    -- flush the attributes for the dir from its cnode.
    537  1.1  rvb  *                  Zap all children of this directory from the namecache.
    538  1.1  rvb  * CODA_ZAPFILE   -- flush the attributes for a file.
    539  1.1  rvb  *
    540  1.1  rvb  * The fifth is a result of Venus detecting an inconsistent file.
    541  1.1  rvb  * CODA_PURGEFID  -- flush the attribute for the file
    542  1.1  rvb  *                  If it is a dir (odd vnode), purge its
    543  1.1  rvb  *                  children from the namecache
    544  1.3  rvb  *                  remove the file from the namecache.
    545  1.1  rvb  *
    546  1.1  rvb  * The sixth allows Venus to replace local fids with global ones
    547  1.1  rvb  * during reintegration.
    548  1.1  rvb  *
    549  1.1  rvb  * CODA_REPLACE -- replace one ViceFid with another throughout the name cache
    550  1.1  rvb  */
    551  1.1  rvb 
    552  1.1  rvb int handleDownCall(opcode, out)
    553  1.1  rvb      int opcode; union outputArgs *out;
    554  1.3  rvb {
    555  1.1  rvb     int error;
    556  1.3  rvb 
    557  1.1  rvb     /* Handle invalidate requests. */
    558  1.3  rvb     switch (opcode) {
    559  1.1  rvb       case CODA_FLUSH : {
    560  1.1  rvb 
    561  1.1  rvb 	  coda_flush(IS_DOWNCALL);
    562  1.3  rvb 
    563  1.3  rvb 	  CODADEBUG(CODA_FLUSH,coda_testflush();)    /* print remaining cnodes */
    564  1.3  rvb 	      return(0);
    565  1.1  rvb       }
    566  1.1  rvb 
    567  1.3  rvb       case CODA_PURGEUSER : {
    568  1.1  rvb 	  coda_clstat.ncalls++;
    569  1.1  rvb 	  coda_clstat.reqs[CODA_PURGEUSER]++;
    570  1.1  rvb 
    571  1.3  rvb 	  /* XXX - need to prevent fsync's */
    572  1.1  rvb 	  coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
    573  1.1  rvb 	  return(0);
    574  1.1  rvb       }
    575  1.3  rvb 
    576  1.3  rvb       case CODA_ZAPFILE : {
    577  1.1  rvb 	  struct cnode *cp;
    578  1.3  rvb 
    579  1.1  rvb 	  error = 0;
    580  1.1  rvb 	  coda_clstat.ncalls++;
    581  1.1  rvb 	  coda_clstat.reqs[CODA_ZAPFILE]++;
    582  1.1  rvb 
    583  1.1  rvb 	  cp = coda_find(&out->coda_zapfile.CodaFid);
    584  1.3  rvb 	  if (cp != NULL) {
    585  1.3  rvb 	      vref(CTOV(cp));
    586  1.1  rvb 
    587  1.1  rvb 	      cp->c_flags &= ~C_VATTR;
    588  1.1  rvb 	      if (CTOV(cp)->v_flag & VTEXT)
    589  1.1  rvb 		  error = coda_vmflush(cp);
    590  1.1  rvb 	      CODADEBUG(CODA_ZAPFILE, myprintf(("zapfile: fid = (%lx.%lx.%lx),
    591  1.1  rvb                                               refcnt = %d, error = %d\n",
    592  1.1  rvb 					      cp->c_fid.Volume,
    593  1.1  rvb 					      cp->c_fid.Vnode,
    594  1.1  rvb 					      cp->c_fid.Unique,
    595  1.1  rvb 					      CTOV(cp)->v_usecount - 1, error)););
    596  1.1  rvb 	      if (CTOV(cp)->v_usecount == 1) {
    597  1.1  rvb 		  cp->c_flags |= C_PURGING;
    598  1.1  rvb 	      }
    599  1.1  rvb 	      vrele(CTOV(cp));
    600  1.3  rvb 	  }
    601  1.1  rvb 
    602  1.1  rvb 	  return(error);
    603  1.3  rvb       }
    604  1.3  rvb 
    605  1.1  rvb       case CODA_ZAPDIR : {
    606  1.3  rvb 	  struct cnode *cp;
    607  1.1  rvb 
    608  1.1  rvb 	  coda_clstat.ncalls++;
    609  1.1  rvb 	  coda_clstat.reqs[CODA_ZAPDIR]++;
    610  1.1  rvb 
    611  1.3  rvb 	  cp = coda_find(&out->coda_zapdir.CodaFid);
    612  1.1  rvb 	  if (cp != NULL) {
    613  1.3  rvb 	      vref(CTOV(cp));
    614  1.1  rvb 
    615  1.1  rvb 	      cp->c_flags &= ~C_VATTR;
    616  1.1  rvb 	      coda_nc_zapParentfid(&out->coda_zapdir.CodaFid, IS_DOWNCALL);
    617  1.1  rvb 
    618  1.1  rvb 	      CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = (%lx.%lx.%lx),
    619  1.1  rvb                                           refcnt = %d\n",cp->c_fid.Volume,
    620  1.1  rvb 					     cp->c_fid.Vnode,
    621  1.1  rvb 					     cp->c_fid.Unique,
    622  1.1  rvb 					     CTOV(cp)->v_usecount - 1)););
    623  1.1  rvb 	      if (CTOV(cp)->v_usecount == 1) {
    624  1.1  rvb 		  cp->c_flags |= C_PURGING;
    625  1.1  rvb 	      }
    626  1.1  rvb 	      vrele(CTOV(cp));
    627  1.3  rvb 	  }
    628  1.3  rvb 
    629  1.3  rvb 	  return(0);
    630  1.1  rvb       }
    631  1.3  rvb 
    632  1.1  rvb       case CODA_ZAPVNODE : {
    633  1.1  rvb 	  coda_clstat.ncalls++;
    634  1.1  rvb 	  coda_clstat.reqs[CODA_ZAPVNODE]++;
    635  1.1  rvb 
    636  1.1  rvb 	  myprintf(("CODA_ZAPVNODE: Called, but uniplemented\n"));
    637  1.1  rvb 	  /*
    638  1.3  rvb 	   * Not that below we must really translate the returned coda_cred to
    639  1.1  rvb 	   * a netbsd cred.  This is a bit muddled at present and the cfsnc_zapnode
    640  1.1  rvb 	   * is further unimplemented, so punt!
    641  1.1  rvb 	   * I suppose we could use just the uid.
    642  1.1  rvb 	   */
    643  1.3  rvb 	  /* coda_nc_zapvnode(&out->coda_zapvnode.VFid, &out->coda_zapvnode.cred,
    644  1.1  rvb 			 IS_DOWNCALL); */
    645  1.1  rvb 	  return(0);
    646  1.1  rvb       }
    647  1.3  rvb 
    648  1.3  rvb       case CODA_PURGEFID : {
    649  1.1  rvb 	  struct cnode *cp;
    650  1.3  rvb 
    651  1.1  rvb 	  error = 0;
    652  1.1  rvb 	  coda_clstat.ncalls++;
    653  1.3  rvb 	  coda_clstat.reqs[CODA_PURGEFID]++;
    654  1.3  rvb 
    655  1.1  rvb 	  cp = coda_find(&out->coda_purgefid.CodaFid);
    656  1.1  rvb 	  if (cp != NULL) {
    657  1.1  rvb 	      vref(CTOV(cp));
    658  1.3  rvb 	      if (ODD(out->coda_purgefid.CodaFid.Vnode)) { /* Vnode is a directory */
    659  1.3  rvb 		  coda_nc_zapParentfid(&out->coda_purgefid.CodaFid,
    660  1.1  rvb 				     IS_DOWNCALL);
    661  1.1  rvb 	      }
    662  1.3  rvb 	      cp->c_flags &= ~C_VATTR;
    663  1.1  rvb 	      coda_nc_zapfid(&out->coda_purgefid.CodaFid, IS_DOWNCALL);
    664  1.3  rvb 	      if (!(ODD(out->coda_purgefid.CodaFid.Vnode))
    665  1.1  rvb 		  && (CTOV(cp)->v_flag & VTEXT)) {
    666  1.1  rvb 
    667  1.1  rvb 		  error = coda_vmflush(cp);
    668  1.1  rvb 	      }
    669  1.1  rvb 	      CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
    670  1.1  rvb                                             cp->c_fid.Volume, cp->c_fid.Vnode,
    671  1.1  rvb                                             cp->c_fid.Unique,
    672  1.1  rvb 					    CTOV(cp)->v_usecount - 1, error)););
    673  1.1  rvb 	      if (CTOV(cp)->v_usecount == 1) {
    674  1.1  rvb 		  cp->c_flags |= C_PURGING;
    675  1.1  rvb 	      }
    676  1.3  rvb 	      vrele(CTOV(cp));
    677  1.1  rvb 	  }
    678  1.1  rvb 	  return(error);
    679  1.3  rvb       }
    680  1.3  rvb 
    681  1.1  rvb       case CODA_REPLACE : {
    682  1.3  rvb 	  struct cnode *cp = NULL;
    683  1.1  rvb 
    684  1.1  rvb 	  coda_clstat.ncalls++;
    685  1.1  rvb 	  coda_clstat.reqs[CODA_REPLACE]++;
    686  1.3  rvb 
    687  1.3  rvb 	  cp = coda_find(&out->coda_replace.OldFid);
    688  1.3  rvb 	  if (cp != NULL) {
    689  1.3  rvb 	      /* remove the cnode from the hash table, replace the fid, and reinsert */
    690  1.3  rvb 	      vref(CTOV(cp));
    691  1.3  rvb 	      coda_unsave(cp);
    692  1.3  rvb 	      cp->c_fid = out->coda_replace.NewFid;
    693  1.3  rvb 	      coda_save(cp);
    694  1.1  rvb 
    695  1.1  rvb 	      CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid = (%lx.%lx.%lx), newfid = (%lx.%lx.%lx), cp = %p\n",
    696  1.1  rvb 					   out->coda_replace.OldFid.Volume,
    697  1.1  rvb 					   out->coda_replace.OldFid.Vnode,
    698  1.1  rvb 					   out->coda_replace.OldFid.Unique,
    699  1.1  rvb 					   cp->c_fid.Volume, cp->c_fid.Vnode,
    700  1.1  rvb 					   cp->c_fid.Unique, cp));)
    701  1.1  rvb 	      vrele(CTOV(cp));
    702  1.1  rvb 	  }
    703  1.1  rvb 	  return (0);
    704  1.1  rvb       }
    705  1.1  rvb       default:
    706  1.3  rvb       	myprintf(("handleDownCall: unknown opcode %d\n", opcode));
    707  1.1  rvb 	return (EINVAL);
    708  1.1  rvb     }
    709  1.3  rvb }
    710  1.1  rvb 
    711  1.1  rvb /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
    712  1.1  rvb 
    713  1.1  rvb int
    714  1.1  rvb coda_vmflush(cp)
    715  1.1  rvb      struct cnode *cp;
    716  1.1  rvb {
    717  1.1  rvb     return 0;
    718  1.1  rvb }
    719  1.1  rvb 
    720  1.3  rvb 
    721  1.1  rvb /*
    722  1.3  rvb  * kernel-internal debugging switches
    723  1.3  rvb  */
    724  1.3  rvb 
    725  1.3  rvb void coda_debugon(void)
    726  1.3  rvb {
    727  1.3  rvb     codadebug = -1;
    728  1.3  rvb     coda_nc_debug = -1;
    729  1.3  rvb     coda_vnop_print_entry = 1;
    730  1.3  rvb     coda_psdev_print_entry = 1;
    731  1.3  rvb     coda_vfsop_print_entry = 1;
    732  1.3  rvb }
    733  1.3  rvb 
    734  1.3  rvb void coda_debugoff(void)
    735  1.3  rvb {
    736  1.1  rvb     codadebug = 0;
    737  1.1  rvb     coda_nc_debug = 0;
    738  1.1  rvb     coda_vnop_print_entry = 0;
    739  1.1  rvb     coda_psdev_print_entry = 0;
    740  1.1  rvb     coda_vfsop_print_entry = 0;
    741  1.1  rvb }
    742  1.1  rvb 
    743  1.1  rvb /*
    744  1.1  rvb  * Utilities used by both client and server
    745  1.1  rvb  * Standard levels:
    746  1.1  rvb  * 0) no debugging
    747  1.1  rvb  * 1) hard failures
    748  1.1  rvb  * 2) soft failures
    749  1.1  rvb  * 3) current test software
    750  1.1  rvb  * 4) main procedure entry points
    751  1.1  rvb  * 5) main procedure exit points
    752  1.1  rvb  * 6) utility procedure entry points
    753  1.1  rvb  * 7) utility procedure exit points
    754  1.1  rvb  * 8) obscure procedure entry points
    755  1.1  rvb  * 9) obscure procedure exit points
    756  1.1  rvb  * 10) random stuff
    757            * 11) all <= 1
    758            * 12) all <= 2
    759            * 13) all <= 3
    760            * ...
    761            */
    762