Home | History | Annotate | Line # | Download | only in coda
coda_subr.c revision 1.26
      1 /*	$NetBSD: coda_subr.c,v 1.26 2012/04/26 03:04:54 christos Exp $	*/
      2 
      3 /*
      4  *
      5  *             Coda: an Experimental Distributed File System
      6  *                              Release 3.1
      7  *
      8  *           Copyright (c) 1987-1998 Carnegie Mellon University
      9  *                          All Rights Reserved
     10  *
     11  * Permission  to  use, copy, modify and distribute this software and its
     12  * documentation is hereby granted,  provided  that  both  the  copyright
     13  * notice  and  this  permission  notice  appear  in  all  copies  of the
     14  * software, derivative works or  modified  versions,  and  any  portions
     15  * thereof, and that both notices appear in supporting documentation, and
     16  * that credit is given to Carnegie Mellon University  in  all  documents
     17  * and publicity pertaining to direct or indirect use of this code or its
     18  * derivatives.
     19  *
     20  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
     21  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
     22  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
     23  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
     24  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
     25  * ANY DERIVATIVE WORK.
     26  *
     27  * Carnegie  Mellon  encourages  users  of  this  software  to return any
     28  * improvements or extensions that  they  make,  and  to  grant  Carnegie
     29  * Mellon the rights to redistribute these changes without encumbrance.
     30  *
     31  * 	@(#) coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
     32  */
     33 
     34 /*
     35  * Mach Operating System
     36  * Copyright (c) 1989 Carnegie-Mellon University
     37  * All rights reserved.  The CMU software License Agreement specifies
     38  * the terms and conditions for use and redistribution.
     39  */
     40 
     41 /*
     42  * This code was written for the Coda file system at Carnegie Mellon
     43  * University.  Contributers include David Steere, James Kistler, and
     44  * M. Satyanarayanan.  */
     45 
     46 /* NOTES: rvb
     47  * 1.	Added coda_unmounting to mark all cnodes as being UNMOUNTING.  This has to
     48  *	 be done before dounmount is called.  Because some of the routines that
     49  *	 dounmount calls before coda_unmounted might try to force flushes to venus.
     50  *	 The vnode pager does this.
     51  * 2.	coda_unmounting marks all cnodes scanning coda_cache.
     52  * 3.	cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
     53  *	 under the /coda mount point.
     54  * 4.	coda_cacheprint (under DEBUG) prints names with vnode/cnode address
     55  */
     56 
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: coda_subr.c,v 1.26 2012/04/26 03:04:54 christos Exp $");
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/malloc.h>
     63 #include <sys/proc.h>
     64 #include <sys/select.h>
     65 #include <sys/mount.h>
     66 
     67 #include <coda/coda.h>
     68 #include <coda/cnode.h>
     69 #include <coda/coda_subr.h>
     70 #include <coda/coda_namecache.h>
     71 
     72 #ifdef _KERNEL_OPT
     73 #include "opt_coda_compat.h"
     74 #endif
     75 
     76 int coda_active = 0;
     77 int coda_reuse = 0;
     78 int coda_new = 0;
     79 
     80 struct cnode *coda_freelist = NULL;
     81 struct cnode *coda_cache[CODA_CACHESIZE];
     82 MALLOC_DEFINE(M_CODA, "coda", "Coda file system structures and tables");
     83 
     84 int codadebug = 0;
     85 int coda_printf_delay = 0;  /* in microseconds */
     86 int coda_vnop_print_entry = 0;
     87 int coda_vfsop_print_entry = 0;
     88 
     89 #define	CNODE_NEXT(cp)	((cp)->c_next)
     90 
     91 #ifdef CODA_COMPAT_5
     92 #define coda_hash(fid) \
     93     (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
     94 #define IS_DIR(cnode)        (cnode.Vnode & 0x1)
     95 #else
     96 #define coda_hash(fid) \
     97     (coda_f2i(fid) & (CODA_CACHESIZE-1))
     98 #define IS_DIR(cnode)        (cnode.opaque[2] & 0x1)
     99 #endif
    100 
    101 /*
    102  * Allocate a cnode.
    103  */
    104 struct cnode *
    105 coda_alloc(void)
    106 {
    107     struct cnode *cp;
    108 
    109     if (coda_freelist) {
    110 	cp = coda_freelist;
    111 	coda_freelist = CNODE_NEXT(cp);
    112 	coda_reuse++;
    113     }
    114     else {
    115 	CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
    116 	/* NetBSD vnodes don't have any Pager info in them ('cause there are
    117 	   no external pagers, duh!) */
    118 #define VNODE_VM_INFO_INIT(vp)         /* MT */
    119 	VNODE_VM_INFO_INIT(CTOV(cp));
    120 	coda_new++;
    121     }
    122     memset(cp, 0, sizeof (struct cnode));
    123 
    124     return(cp);
    125 }
    126 
    127 /*
    128  * Deallocate a cnode.
    129  */
    130 void
    131 coda_free(struct cnode *cp)
    132 {
    133 
    134     CNODE_NEXT(cp) = coda_freelist;
    135     coda_freelist = cp;
    136 }
    137 
    138 /*
    139  * Put a cnode in the hash table
    140  */
    141 void
    142 coda_save(struct cnode *cp)
    143 {
    144 	CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
    145 	coda_cache[coda_hash(&cp->c_fid)] = cp;
    146 }
    147 
    148 /*
    149  * Remove a cnode from the hash table
    150  */
    151 void
    152 coda_unsave(struct cnode *cp)
    153 {
    154     struct cnode *ptr;
    155     struct cnode *ptrprev = NULL;
    156 
    157     ptr = coda_cache[coda_hash(&cp->c_fid)];
    158     while (ptr != NULL) {
    159 	if (ptr == cp) {
    160 	    if (ptrprev == NULL) {
    161 		coda_cache[coda_hash(&cp->c_fid)]
    162 		    = CNODE_NEXT(ptr);
    163 	    } else {
    164 		CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
    165 	    }
    166 	    CNODE_NEXT(cp) = NULL;
    167 
    168 	    return;
    169 	}
    170 	ptrprev = ptr;
    171 	ptr = CNODE_NEXT(ptr);
    172     }
    173 }
    174 
    175 /*
    176  * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
    177  * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
    178  */
    179 struct cnode *
    180 coda_find(CodaFid *fid)
    181 {
    182     struct cnode *cp;
    183 
    184     cp = coda_cache[coda_hash(fid)];
    185     while (cp) {
    186     	if (coda_fid_eq(&(cp->c_fid), fid) &&
    187 	    (!IS_UNMOUNTING(cp)))
    188 	    {
    189 		coda_active++;
    190 		return(cp);
    191 	    }
    192 	cp = CNODE_NEXT(cp);
    193     }
    194     return(NULL);
    195 }
    196 
    197 /*
    198  * coda_kill is called as a side effect to vcopen. To prevent any
    199  * cnodes left around from an earlier run of a venus or warden from
    200  * causing problems with the new instance, mark any outstanding cnodes
    201  * as dying. Future operations on these cnodes should fail (excepting
    202  * coda_inactive of course!). Since multiple venii/wardens can be
    203  * running, only kill the cnodes for a particular entry in the
    204  * coda_mnttbl. -- DCS 12/1/94 */
    205 
    206 int
    207 coda_kill(struct mount *whoIam, enum dc_status dcstat)
    208 {
    209 	int hash, count = 0;
    210 	struct cnode *cp;
    211 
    212 	/*
    213 	 * Algorithm is as follows:
    214 	 *     Second, flush whatever vnodes we can from the name cache.
    215 	 *
    216 	 *     Finally, step through whatever is left and mark them dying.
    217 	 *        This prevents any operation at all.
    218 
    219 	 */
    220 
    221 	/* This is slightly overkill, but should work. Eventually it'd be
    222 	 * nice to only flush those entries from the namecache that
    223 	 * reference a vnode in this vfs.  */
    224 	coda_nc_flush(dcstat);
    225 
    226 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    227 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    228 			if (CTOV(cp)->v_mount == whoIam) {
    229 #ifdef	DEBUG
    230 				printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
    231 #endif
    232 				count++;
    233 				CODADEBUG(CODA_FLUSH,
    234 					 myprintf(("Live cnode fid %s flags %d count %d\n",
    235 						   coda_f2s(&cp->c_fid),
    236 						   cp->c_flags,
    237 						   CTOV(cp)->v_usecount)); );
    238 			}
    239 		}
    240 	}
    241 	return count;
    242 }
    243 
    244 /*
    245  * There are two reasons why a cnode may be in use, it may be in the
    246  * name cache or it may be executing.
    247  */
    248 void
    249 coda_flush(enum dc_status dcstat)
    250 {
    251     int hash;
    252     struct cnode *cp;
    253 
    254     coda_clstat.ncalls++;
    255     coda_clstat.reqs[CODA_FLUSH]++;
    256 
    257     coda_nc_flush(dcstat);	    /* flush files from the name cache */
    258 
    259     for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    260 	for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    261 	    if (!IS_DIR(cp->c_fid)) /* only files can be executed */
    262 		coda_vmflush(cp);
    263 	}
    264     }
    265 }
    266 
    267 /*
    268  * As a debugging measure, print out any cnodes that lived through a
    269  * name cache flush.
    270  */
    271 void
    272 coda_testflush(void)
    273 {
    274     int hash;
    275     struct cnode *cp;
    276 
    277     for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    278 	for (cp = coda_cache[hash];
    279 	     cp != NULL;
    280 	     cp = CNODE_NEXT(cp)) {
    281 	    myprintf(("Live cnode fid %s count %d\n",
    282 		      coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
    283 	}
    284     }
    285 }
    286 
    287 /*
    288  *     First, step through all cnodes and mark them unmounting.
    289  *         NetBSD kernels may try to fsync them now that venus
    290  *         is dead, which would be a bad thing.
    291  *
    292  */
    293 void
    294 coda_unmounting(struct mount *whoIam)
    295 {
    296 	int hash;
    297 	struct cnode *cp;
    298 
    299 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    300 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    301 			if (CTOV(cp)->v_mount == whoIam) {
    302 				if (cp->c_flags & (C_LOCKED|C_WANTED)) {
    303 					printf("coda_unmounting: Unlocking %p\n", cp);
    304 					cp->c_flags &= ~(C_LOCKED|C_WANTED);
    305 					wakeup((void *) cp);
    306 				}
    307 				cp->c_flags |= C_UNMOUNTING;
    308 			}
    309 		}
    310 	}
    311 }
    312 
    313 #ifdef	DEBUG
    314 void
    315 coda_checkunmounting(struct mount *mp)
    316 {
    317 	struct vnode *vp;
    318 	struct cnode *cp;
    319 	int count = 0, bad = 0;
    320 loop:
    321 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    322 		if (vp->v_mount != mp)
    323 			goto loop;
    324 		cp = VTOC(vp);
    325 		count++;
    326 		if (!(cp->c_flags & C_UNMOUNTING)) {
    327 			bad++;
    328 			printf("vp %p, cp %p missed\n", vp, cp);
    329 			cp->c_flags |= C_UNMOUNTING;
    330 		}
    331 	}
    332 }
    333 
    334 void
    335 coda_cacheprint(struct mount *whoIam)
    336 {
    337 	int hash;
    338 	struct cnode *cp;
    339 	int count = 0;
    340 
    341 	printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
    342 	coda_nc_name(VTOC(coda_ctlvp));
    343 	printf("\n");
    344 
    345 	for (hash = 0; hash < CODA_CACHESIZE; hash++) {
    346 		for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
    347 			if (CTOV(cp)->v_mount == whoIam) {
    348 				printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
    349 				coda_nc_name(cp);
    350 				printf("\n");
    351 				count++;
    352 			}
    353 		}
    354 	}
    355 	printf("coda_cacheprint: count %d\n", count);
    356 }
    357 #endif
    358 
    359 /*
    360  * There are 6 cases where invalidations occur. The semantics of each
    361  * is listed here.
    362  *
    363  * CODA_FLUSH     -- flush all entries from the name cache and the cnode cache.
    364  * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
    365  *                  This call is a result of token expiration.
    366  *
    367  * The next two are the result of callbacks on a file or directory.
    368  * CODA_ZAPDIR    -- flush the attributes for the dir from its cnode.
    369  *                  Zap all children of this directory from the namecache.
    370  * CODA_ZAPFILE   -- flush the attributes for a file.
    371  *
    372  * The fifth is a result of Venus detecting an inconsistent file.
    373  * CODA_PURGEFID  -- flush the attribute for the file
    374  *                  If it is a dir (odd vnode), purge its
    375  *                  children from the namecache
    376  *                  remove the file from the namecache.
    377  *
    378  * The sixth allows Venus to replace local fids with global ones
    379  * during reintegration.
    380  *
    381  * CODA_REPLACE -- replace one CodaFid with another throughout the name cache
    382  */
    383 
    384 int handleDownCall(int opcode, union outputArgs *out)
    385 {
    386     int error;
    387 
    388     /* Handle invalidate requests. */
    389     switch (opcode) {
    390       case CODA_FLUSH : {
    391 
    392 	  coda_flush(IS_DOWNCALL);
    393 
    394 	  CODADEBUG(CODA_FLUSH,coda_testflush();)    /* print remaining cnodes */
    395 	      return(0);
    396       }
    397 
    398       case CODA_PURGEUSER : {
    399 	  coda_clstat.ncalls++;
    400 	  coda_clstat.reqs[CODA_PURGEUSER]++;
    401 
    402 	  /* XXX - need to prevent fsync's */
    403 #ifdef CODA_COMPAT_5
    404 	  coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
    405 #else
    406 	  coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
    407 #endif
    408 	  return(0);
    409       }
    410 
    411       case CODA_ZAPFILE : {
    412 	  struct cnode *cp;
    413 
    414 	  error = 0;
    415 	  coda_clstat.ncalls++;
    416 	  coda_clstat.reqs[CODA_ZAPFILE]++;
    417 
    418 	  cp = coda_find(&out->coda_zapfile.Fid);
    419 	  if (cp != NULL) {
    420 	      vref(CTOV(cp));
    421 
    422 	      cp->c_flags &= ~C_VATTR;
    423 	      if (CTOV(cp)->v_iflag & VI_TEXT)
    424 		  error = coda_vmflush(cp);
    425 	      CODADEBUG(CODA_ZAPFILE, myprintf((
    426 		    "zapfile: fid = %s, refcnt = %d, error = %d\n",
    427 		    coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
    428 	      if (CTOV(cp)->v_usecount == 1) {
    429 		  cp->c_flags |= C_PURGING;
    430 	      }
    431 	      vrele(CTOV(cp));
    432 	  }
    433 
    434 	  return(error);
    435       }
    436 
    437       case CODA_ZAPDIR : {
    438 	  struct cnode *cp;
    439 
    440 	  coda_clstat.ncalls++;
    441 	  coda_clstat.reqs[CODA_ZAPDIR]++;
    442 
    443 	  cp = coda_find(&out->coda_zapdir.Fid);
    444 	  if (cp != NULL) {
    445 	      vref(CTOV(cp));
    446 
    447 	      cp->c_flags &= ~C_VATTR;
    448 	      coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);
    449 
    450 	      CODADEBUG(CODA_ZAPDIR, myprintf((
    451 		    "zapdir: fid = %s, refcnt = %d\n",
    452 		    coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
    453 	      if (CTOV(cp)->v_usecount == 1) {
    454 		  cp->c_flags |= C_PURGING;
    455 	      }
    456 	      vrele(CTOV(cp));
    457 	  }
    458 
    459 	  return(0);
    460       }
    461 
    462       case CODA_PURGEFID : {
    463 	  struct cnode *cp;
    464 
    465 	  error = 0;
    466 	  coda_clstat.ncalls++;
    467 	  coda_clstat.reqs[CODA_PURGEFID]++;
    468 
    469 	  cp = coda_find(&out->coda_purgefid.Fid);
    470 	  if (cp != NULL) {
    471 	      vref(CTOV(cp));
    472 	      if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
    473 		  coda_nc_zapParentfid(&out->coda_purgefid.Fid,
    474 				     IS_DOWNCALL);
    475 	      }
    476 	      cp->c_flags &= ~C_VATTR;
    477 	      coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
    478 	      if (!(IS_DIR(out->coda_purgefid.Fid))
    479 		  && (CTOV(cp)->v_iflag & VI_TEXT)) {
    480 
    481 		  error = coda_vmflush(cp);
    482 	      }
    483 	      CODADEBUG(CODA_PURGEFID, myprintf((
    484 			 "purgefid: fid = %s, refcnt = %d, error = %d\n",
    485 			 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
    486 	      if (CTOV(cp)->v_usecount == 1) {
    487 		  cp->c_flags |= C_PURGING;
    488 	      }
    489 	      vrele(CTOV(cp));
    490 	  }
    491 	  return(error);
    492       }
    493 
    494       case CODA_REPLACE : {
    495 	  struct cnode *cp = NULL;
    496 
    497 	  coda_clstat.ncalls++;
    498 	  coda_clstat.reqs[CODA_REPLACE]++;
    499 
    500 	  cp = coda_find(&out->coda_replace.OldFid);
    501 	  if (cp != NULL) {
    502 	      /* remove the cnode from the hash table, replace the fid, and reinsert */
    503 	      vref(CTOV(cp));
    504 	      coda_unsave(cp);
    505 	      cp->c_fid = out->coda_replace.NewFid;
    506 	      coda_save(cp);
    507 
    508 	      CODADEBUG(CODA_REPLACE, myprintf((
    509 			"replace: oldfid = %s, newfid = %s, cp = %p\n",
    510 			coda_f2s(&out->coda_replace.OldFid),
    511 			coda_f2s(&cp->c_fid), cp));)
    512 	      vrele(CTOV(cp));
    513 	  }
    514 	  return (0);
    515       }
    516       default:
    517       	myprintf(("handleDownCall: unknown opcode %d\n", opcode));
    518 	return (EINVAL);
    519     }
    520 }
    521 
    522 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
    523 
    524 int
    525 coda_vmflush(struct cnode *cp)
    526 {
    527     return 0;
    528 }
    529 
    530 
    531 /*
    532  * kernel-internal debugging switches
    533  */
    534 
    535 void coda_debugon(void)
    536 {
    537     codadebug = -1;
    538     coda_nc_debug = -1;
    539     coda_vnop_print_entry = 1;
    540     coda_psdev_print_entry = 1;
    541     coda_vfsop_print_entry = 1;
    542 }
    543 
    544 void coda_debugoff(void)
    545 {
    546     codadebug = 0;
    547     coda_nc_debug = 0;
    548     coda_vnop_print_entry = 0;
    549     coda_psdev_print_entry = 0;
    550     coda_vfsop_print_entry = 0;
    551 }
    552 
    553 /*
    554  * Utilities used by both client and server
    555  * Standard levels:
    556  * 0) no debugging
    557  * 1) hard failures
    558  * 2) soft failures
    559  * 3) current test software
    560  * 4) main procedure entry points
    561  * 5) main procedure exit points
    562  * 6) utility procedure entry points
    563  * 7) utility procedure exit points
    564  * 8) obscure procedure entry points
    565  * 9) obscure procedure exit points
    566  * 10) random stuff
    567  * 11) all <= 1
    568  * 12) all <= 2
    569  * 13) all <= 3
    570  * ...
    571  */
    572