afs: Free rx/rxevent resources during shutdown
[openafs.git] / src / afs / afs_init.c
index c4fd031..f2414f7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include "afs/stds.h"
 #include "afs/sysincludes.h"   /* Standard vendor system headers */
 #include "afsincludes.h"       /* Afs-based standard headers */
 #include "afs/afs_stats.h"     /* afs statistics */
+#include "rx/rxstat.h"
+#if defined(AFS_LINUX26_ENV) && defined(STRUCT_TASK_STRUCT_HAS_CRED)
+#include <linux/cred.h>
+#endif
+
+#define FSINT_COMMON_XG
+#include "afs/afscbint.h"
 
 /* Exported variables */
 struct osi_dev cacheDev;       /*Cache device */
 afs_int32 cacheInfoModTime;    /*Last time cache info modified */
-#if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_NBSD_ENV)
 struct mount *afs_cacheVfsp = 0;
 #elif defined(AFS_LINUX20_ENV)
 struct super_block *afs_cacheSBp = 0;
@@ -45,11 +50,15 @@ int afs_memvolumes = 0;
 #if defined(AFS_XBSD_ENV)
 static struct vnode *volumeVnode;
 #endif
-#if defined(AFS_DISCON_ENV)
 afs_rwlock_t afs_discon_lock;
-extern afs_rwlock_t afs_DDirtyVCListLock;
+extern afs_rwlock_t afs_disconDirtyLock;
+#if defined(AFS_LINUX26_ENV) && defined(STRUCT_TASK_STRUCT_HAS_CRED)
+const struct cred *cache_creds;
 #endif
 
+/* This is the kernel side of the dynamic vcache setting */
+int afsd_dynamic_vcaches = 0;  /* Enable dynamic-vcache support */
+
 /*
  * Initialization order is important.  Must first call afs_CacheInit,
  * then cache file and volume file initialization routines.  Next, the
@@ -86,10 +95,12 @@ static int afs_cacheinit_flag = 0;
 int
 afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
              afs_int32 aDentries, afs_int32 aVolumes, afs_int32 achunk,
-             afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers)
+             afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers,
+             afs_int32 dynamic_vcaches)
 {
-    register afs_int32 i;
-    register struct volume *tv;
+    afs_int32 i;
+    int code;
+    struct volume *tv;
 
     AFS_STATCNT(afs_CacheInit);
     /*
@@ -103,32 +114,36 @@ afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
     afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
 #endif /* SYS_NAME_ID */
 
-    printf("Starting AFS cache scan...");
+#ifdef AFS_MAXVCOUNT_ENV
+    afsd_dynamic_vcaches = dynamic_vcaches;
+    afs_warn("%s dynamically allocated vcaches\n",
+            ( afsd_dynamic_vcaches ? "enabling" : "disabling" ));
+#endif
+
+    afs_warn("Starting AFS cache scan...");
     if (afs_cacheinit_flag)
        return 0;
     afs_cacheinit_flag = 1;
     cacheInfoModTime = 0;
-    maxIHint = ninodes;
-    nihints = 0;
-    usedihint = 0;
 
     LOCK_INIT(&afs_ftf, "afs_ftf");
-    RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
-#ifdef AFS_DISCON_ENV
-    RWLOCK_INIT(&afs_discon_lock, "afs_discon_lock");
-    RWLOCK_INIT(&afs_DDirtyVCListLock, "afs_DDirtyVCListLock");
-#endif
+    AFS_RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
+    AFS_RWLOCK_INIT(&afs_discon_lock, "afs_discon_lock");
+    AFS_RWLOCK_INIT(&afs_disconDirtyLock, "afs_disconDirtyLock");
+    QInit(&afs_disconDirty);
+    QInit(&afs_disconShadow);
     osi_dnlc_init();
 
-    /* 
-     * create volume list structure 
+    /*
+     * create volume list structure
      */
     if (aVolumes < 50)
        aVolumes = 50;
     else if (aVolumes > 32767)
        aVolumes = 32767;
 
-    tv = (struct volume *)afs_osi_Alloc(aVolumes * sizeof(struct volume));
+    tv = afs_osi_Alloc(aVolumes * sizeof(struct volume));
+    osi_Assert(tv != NULL);
     for (i = 0; i < aVolumes - 1; i++)
        tv[i].next = &tv[i + 1];
     tv[aVolumes - 1].next = NULL;
@@ -138,7 +153,20 @@ afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
     afs_cacheFiles = afiles;
     afs_cacheStats = astatSize;
     afs_vcacheInit(astatSize);
-    afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
+    code = afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
+    if (code) {
+        return code;
+    }
+#if defined(AFS_LINUX26_ENV) && defined(STRUCT_TASK_STRUCT_HAS_CRED)
+    /*
+     * Save current credentials for later access to disk cache files.
+     * If selinux, apparmor or other security modules are enabled,
+     * they might deny access to cache files if the userspace process
+     * is restricted.  Save the credentials used at cache initialisation
+     * for later use when opening cache files.
+     */
+    cache_creds = get_current_cred();
+#endif
 #ifdef AFS_64BIT_CLIENT
 #ifdef AFS_VM_RDWR_ENV
     afs_vmMappingEnd = AFS_CHUNKBASE(0x7fffffff);
@@ -162,7 +190,7 @@ afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
     cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
     cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
     cm_initParams.cmi_cacheSize = afs_cacheBlocks;
-    cm_initParams.cmi_setTime = afs_setTime;
+    cm_initParams.cmi_setTime = 0;
     cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
 
     return 0;
@@ -183,7 +211,7 @@ afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
 void
 afs_ComputeCacheParms(void)
 {
-    register afs_int32 i;
+    afs_int32 i;
     afs_int32 afs_maxCacheDirty;
 
     /*
@@ -213,36 +241,41 @@ afs_ComputeCacheParms(void)
 
 
 /*
- * LookupInodeByPath
+ * afs_LookupInodeByPath
  *
  * Look up inode given a file name.
  * Optionally return the vnode too.
  * If the vnode is not returned, we rele it.
  */
-static int
-LookupInodeByPath(char *filename, ino_t * inode, struct vnode **fvpp)
+int
+afs_LookupInodeByPath(char *filename, afs_ufs_dcache_id_t *inode,
+                     struct vnode **fvpp)
 {
     afs_int32 code;
 
-#ifdef AFS_LINUX22_ENV
+#if defined(AFS_LINUX22_ENV)
     struct dentry *dp;
     code = gop_lookupname(filename, AFS_UIOSYS, 0, &dp);
     if (code)
        return code;
-    *inode = dp->d_inode->i_ino;
+    osi_get_fh(dp, inode);
     dput(dp);
 #else
     struct vnode *filevp;
     code = gop_lookupname(filename, AFS_UIOSYS, 0, &filevp);
     if (code)
        return code;
+#ifdef AFS_CACHE_VNODE_PATH
+    *inode = afs_strdup(filename);
+#else
     *inode = afs_vnodeToInumber(filevp);
+#endif
     if (fvpp)
        *fvpp = filevp;
     else {
        AFS_RELE(filevp);
     }
-#endif /* AFS_LINUX22_ENV */
+#endif
 
     return 0;
 }
@@ -250,15 +283,11 @@ LookupInodeByPath(char *filename, ino_t * inode, struct vnode **fvpp)
 int
 afs_InitCellInfo(char *afile)
 {
-    ino_t inode;
-    int code;
+    afs_dcache_id_t inode;
+    int code = 0;
 
-#ifdef AFS_CACHE_VNODE_PATH
-    return afs_cellname_init(AFS_CACHE_CELLS_INODE, code);
-#else
-    code = LookupInodeByPath(afile, &inode, NULL);
-    return afs_cellname_init(inode, code);
-#endif
+    code = afs_LookupInodeByPath(afile, &inode.ufs, NULL);
+    return afs_cellname_init(&inode, code);
 }
 
 /*
@@ -280,7 +309,7 @@ afs_InitCellInfo(char *afile)
 int
 afs_InitVolumeInfo(char *afile)
 {
-    int code;
+    int code = 0;
     struct osi_file *tfile;
 
     AFS_STATCNT(afs_InitVolumeInfo);
@@ -298,20 +327,30 @@ afs_InitVolumeInfo(char *afile)
      * are things which try to get the volumeInode, and since we keep
      * it in the cache...
      */
-    code = LookupInodeByPath(afile, &volumeInode, &volumeVnode);
-#elif defined(AFS_CACHE_VNODE_PATH)
-    volumeInode = AFS_CACHE_VOLUME_INODE;
+    code = afs_LookupInodeByPath(afile, &volumeInode.ufs, &volumeVnode);
 #else
-    code = LookupInodeByPath(afile, &volumeInode, NULL);
+    code = afs_LookupInodeByPath(afile, &volumeInode.ufs, NULL);
 #endif
     if (code)
        return code;
-    tfile = afs_CFileOpen(volumeInode);
+    tfile = afs_CFileOpen(&volumeInode);
+    osi_Assert(tfile);
     afs_CFileTruncate(tfile, 0);
     afs_CFileClose(tfile);
     return 0;
 }
 
+void
+afs_InitFHeader(struct afs_fheader *aheader)
+{
+    memset(aheader, 0, sizeof(*aheader));
+    aheader->magic = AFS_FHMAGIC;
+    aheader->version = AFS_CI_VERSION;
+    aheader->dataSize = sizeof(struct fcache);
+    aheader->firstCSize = AFS_FIRSTCSIZE;
+    aheader->otherCSize = AFS_OTHERCSIZE;
+}
+
 /*
  * afs_InitCacheInfo
  *
@@ -321,12 +360,12 @@ afs_InitVolumeInfo(char *afile)
  * Parameters:
  *     afile : Name of the file assumed to be the cache info file
  *             for the Cache Manager; it will be used as such.
- * Side Effects:  This sets afs_fragsize, which is used in the cache usage 
+ * Side Effects:  This sets afs_fragsize, which is used in the cache usage
  *                calculations such as in afs_adjustsize()
  *
  * Environment:
  *     This function is called only during initialization.  The given
- *     file should NOT be truncated to 0 lenght; its contents descrebe
+ *     file should NOT be truncated to 0 length; its contents describe
  *     what data is really in the cache.
  *
  *     WARNING: data will be written to this file over time by AFS.
@@ -336,11 +375,11 @@ afs_InitVolumeInfo(char *afile)
  *
  */
 int
-afs_InitCacheInfo(register char *afile)
+afs_InitCacheInfo(char *afile)
 {
-    register afs_int32 code;
+    afs_int32 code;
     struct osi_stat tstat;
-    register struct osi_file *tfile;
+    struct osi_file *tfile;
     struct afs_fheader theader;
 #ifndef AFS_LINUX22_ENV
     struct vnode *filevp;
@@ -359,19 +398,17 @@ afs_InitCacheInfo(register char *afile)
     if (code || !filevp)
        return ENOENT;
     {
-#if    defined(AFS_SUN56_ENV)
+#if    defined(AFS_SUN5_ENV)
        struct statvfs64 st;
 #elif  defined(AFS_HPUX102_ENV)
        struct k_statvfs st;
-#elif  defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
+#elif  defined(AFS_SGI_ENV) || defined(AFS_HPUX100_ENV) || defined(AFS_NBSD40_ENV)
        struct statvfs st;
-#elif defined(AFS_DUX40_ENV)
-       struct nstatfs st;
 #elif defined(AFS_DARWIN80_ENV)
        struct vfsstatfs st;
 #else
        struct statfs st;
-#endif /* SUN56 */
+#endif /* SUN5 */
 
 #if    defined(AFS_SGI_ENV)
 #ifdef AFS_SGI65_ENV
@@ -382,12 +419,6 @@ afs_InitCacheInfo(register char *afile)
 #endif /* AFS_SGI65_ENV */
 #elif  defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
        if (!VFS_STATVFS(filevp->v_vfsp, &st))
-#elif defined(AFS_OSF_ENV)
-
-       VFS_STATFS(filevp->v_vfsp, code);
-       /* struct copy */
-       st = filevp->v_vfsp->m_stat;
-       if (code == 0)
 #elif defined(AFS_AIX41_ENV)
        if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
 #elif defined(AFS_LINUX20_ENV)
@@ -401,26 +432,36 @@ afs_InitCacheInfo(register char *afile)
 #elif defined(AFS_DARWIN80_ENV)
         afs_cacheVfsp = vnode_mount(filevp);
        if (afs_cacheVfsp && ((st = *(vfs_statfs(afs_cacheVfsp))),1))
-#elif defined(AFS_DARWIN_ENV)
-       if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
-#elif defined(AFS_FBSD50_ENV)
-       if (!VFS_STATFS(filevp->v_mount, &st, curthread))
-#elif defined(AFS_XBSD_ENV)
-       if (!VFS_STATFS(filevp->v_mount, &st, curproc))
+#elif defined(AFS_FBSD_ENV)
+       if (!VFS_STATFS(filevp->v_mount, &st))
+#elif defined(AFS_NBSD50_ENV)
+       if (!VFS_STATVFS(filevp->v_vfsp, &st))
+#elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+       if (!VFS_STATFS(filevp->v_mount, &st, osi_curproc()))
 #else
        if (!VFS_STATFS(filevp->v_vfsp, &st))
 #endif /* SGI... */
 #if    defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
-           afs_fsfragsize = st.f_frsize - 1;
+           if (strcmp("zfs", st.f_basetype) == 0) {
+               /*
+                * Files in ZFS can take up to around the next
+                * recordsize boundary after being truncated. recordsize
+                * is reported in statvfs by f_bsize, so use that
+                * instead.
+                */
+               afs_fsfragsize = st.f_bsize - 1;
+           } else {
+               afs_fsfragsize = st.f_frsize - 1;
+           }
 #else
            afs_fsfragsize = st.f_bsize - 1;
 #endif
     }
 #if defined(AFS_LINUX20_ENV)
-    cacheInode = filevp->i_ino;
+    cacheInode.ufs = filevp->i_ino;
     afs_cacheSBp = filevp->i_sb;
 #elif defined(AFS_XBSD_ENV)
-    cacheInode = VTOI(filevp)->i_number;
+    cacheInode.ufs = VTOI(filevp)->i_number;
     cacheDev.mp = filevp->v_mount;
     cacheDev.held_vnode = filevp;
     vref(filevp);              /* Make sure mount point stays busy. XXX */
@@ -428,22 +469,28 @@ afs_InitCacheInfo(register char *afile)
     afs_cacheVfsp = filevp->v_vfsp;
 #endif
 #else
-#if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
+#if defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
     afs_InitDualFSCacheOps(filevp);
 #endif
 #ifndef AFS_CACHE_VNODE_PATH
 #ifndef AFS_DARWIN80_ENV
     afs_cacheVfsp = filevp->v_vfsp;
 #endif
-    cacheInode = afs_vnodeToInumber(filevp);
+    cacheInode.ufs = afs_vnodeToInumber(filevp);
 #else
-    cacheInode = AFS_CACHE_ITEMS_INODE;
+    afs_LookupInodeByPath(afile, &cacheInode.ufs, NULL);
 #endif
     cacheDev.dev = afs_vnodeToDev(filevp);
 #endif /* AFS_LINUX20_ENV */
     AFS_RELE(filevp);
 #endif /* AFS_LINUX22_ENV */
-    tfile = osi_UFSOpen(cacheInode);
+    if (afs_fsfragsize < AFS_MIN_FRAGSIZE) {
+       afs_fsfragsize = AFS_MIN_FRAGSIZE;
+    }
+    tfile = osi_UFSOpen(&cacheInode);
+    if (!tfile)
+       return ENOENT;
+
     afs_osi_Stat(tfile, &tstat);
     cacheInfoModTime = tstat.mtime;
     code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
@@ -453,15 +500,13 @@ afs_InitCacheInfo(register char *afile)
        if (theader.magic == AFS_FHMAGIC
            && theader.firstCSize == AFS_FIRSTCSIZE
            && theader.otherCSize == AFS_OTHERCSIZE
+           && theader.dataSize == sizeof(struct fcache)
            && theader.version == AFS_CI_VERSION)
            goodFile = 1;
     }
     if (!goodFile) {
        /* write out a good file label */
-       theader.magic = AFS_FHMAGIC;
-       theader.firstCSize = AFS_FIRSTCSIZE;
-       theader.otherCSize = AFS_OTHERCSIZE;
-       theader.version = AFS_CI_VERSION;
+       afs_InitFHeader(&theader);
        afs_osi_Write(tfile, 0, &theader, sizeof(theader));
        /*
         * Truncate the rest of the file, since it may be arbitrarily
@@ -482,22 +527,22 @@ int afs_resourceinit_flag = 0;
 int
 afs_ResourceInit(int preallocs)
 {
-    register afs_int32 i;
+    afs_int32 i;
     static struct rx_securityClass *secobj;
 
     AFS_STATCNT(afs_ResourceInit);
-    RWLOCK_INIT(&afs_xuser, "afs_xuser");
-    RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
-    RWLOCK_INIT(&afs_xserver, "afs_xserver");
-    RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
-    RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
-    RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
+    AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
+    AFS_RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
+    AFS_RWLOCK_INIT(&afs_xserver, "afs_xserver");
+    AFS_RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
+    AFS_RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
+    AFS_RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
     LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
-#ifndef AFS_FBSD_ENV
+#ifndef AFS_PRIVATE_OSI_ALLOCSPACES
     LOCK_INIT(&osi_fsplock, "osi_fsplock");
     LOCK_INIT(&osi_flplock, "osi_flplock");
 #endif
-    RWLOCK_INIT(&afs_xconn, "afs_xconn");
+    AFS_RWLOCK_INIT(&afs_xconn, "afs_xconn");
 
     afs_CellInit();
     afs_InitCBQueue(1);                /* initialize callback queues */
@@ -506,8 +551,10 @@ afs_ResourceInit(int preallocs)
        afs_resourceinit_flag = 1;
        for (i = 0; i < NFENTRIES; i++)
            fvTable[i] = 0;
-       for (i = 0; i < MAXNUMSYSNAMES; i++)
+       for (i = 0; i < MAXNUMSYSNAMES; i++) {
            afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
+           osi_Assert(afs_sysnamelist[i] != NULL);
+       }
        afs_sysname = afs_sysnamelist[0];
        strcpy(afs_sysname, SYS_NAME);
        afs_sysnamecount = 1;
@@ -571,8 +618,8 @@ afs_ResourceInit(int preallocs)
 static void
 afs_procsize_init(void)
 {
-    struct proc *p0;           /* pointer to process 0 */
-    struct proc *pN;           /* pointer to process 0's first child */
+    afs_proc_t *p0;            /* pointer to process 0 */
+    afs_proc_t *pN;            /* pointer to process 0's first child */
 #ifdef AFS_AIX51_ENV
     struct pvproc *pV;
 #endif
@@ -580,13 +627,13 @@ afs_procsize_init(void)
     ptrdiff_t pN_offset;
     int procsize;
 
-    p0 = (struct proc *)v.vb_proc;
+    p0 = (afs_proc_t *)v.vb_proc;
     if (!p0) {
        afs_gcpags = AFS_GCPAGS_EPROC0;
        return;
     }
 #ifdef AFS_AIX51_ENV
-    pN = (struct proc *)0;
+    pN = NULL;
     pV = p0->p_pvprocp;
     if (pV) {
        pV = pV->pv_child;
@@ -650,178 +697,190 @@ void
 shutdown_cache(void)
 {
     AFS_STATCNT(shutdown_cache);
-    afs_WriteThroughDSlots();
-    if (afs_cold_shutdown) {
+    osi_Assert(afs_WriteThroughDSlots() == 0);
+    if (1/*afs_cold_shutdown*/) {
        afs_cacheinit_flag = 0;
        shutdown_dcache();
        shutdown_vcache();
 
        afs_cacheStats = 0;
        afs_cacheFiles = afs_cacheBlocks = 0;
-       pag_epoch = maxIHint = nihints = usedihint = 0;
+       pag_epoch = 0;
        pagCounter = 0;
 #if defined(AFS_XBSD_ENV)
-       vrele(volumeVnode);     /* let it go, finally. */
-       volumeVnode = NULL;
+       /* memcache never sets this, so don't panic on shutdown */
+       if (volumeVnode != NULL) {
+           vrele(volumeVnode); /* let it go, finally. */
+           volumeVnode = NULL;
+       }
        if (cacheDev.held_vnode) {
            vrele(cacheDev.held_vnode);
            cacheDev.held_vnode = NULL;
        }
 #endif
-       cacheInode = volumeInode = (ino_t) 0;
-
+#ifdef AFS_CACHE_VNODE_PATH
+       if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
+           afs_osi_FreeStr(cacheInode.ufs);
+           afs_osi_FreeStr(volumeInode.ufs);
+       }
+#endif
+       afs_reset_inode(&cacheInode);
+       afs_reset_inode(&volumeInode);
        cacheInfoModTime = 0;
 
        afs_fsfragsize = 1023;
-       memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
-       memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
+       memset(&cacheDev, 0, sizeof(struct osi_dev));
        osi_dnlc_shutdown();
     }
+#if defined(AFS_LINUX26_ENV) && defined(STRUCT_TASK_STRUCT_HAS_CRED)
+    put_cred(cache_creds);
+#endif
 }                              /*shutdown_cache */
 
 
 void
 shutdown_vnodeops(void)
 {
-#if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
-    struct buf *afs_bread_freebp = 0;
-#endif
-
-
     AFS_STATCNT(shutdown_vnodeops);
     if (afs_cold_shutdown) {
-#ifndef        AFS_SUN5_ENV            /* XXX */
-       lastWarnTime = 0;
-#endif
 #ifndef AFS_LINUX20_ENV
        afs_rd_stash_i = 0;
 #endif
-#if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
-       afs_bread_freebp = 0;
-#endif
        shutdown_mariner();
     }
 }
 
 
-void
-shutdown_AFS(void)
+static void
+shutdown_server(void)
 {
     int i;
-    register struct srvAddr *sa;
-
-    AFS_STATCNT(shutdown_AFS);
-    if (afs_cold_shutdown) {
-       afs_resourceinit_flag = 0;
-       /* 
-        * Free Volumes table allocations 
-        */
-       {
-           struct volume *tv;
-           for (i = 0; i < NVOLS; i++) {
-               for (tv = afs_volumes[i]; tv; tv = tv->next) {
-                   if (tv->name) {
-                       afs_osi_Free(tv->name, strlen(tv->name) + 1);
-                       tv->name = 0;
-                   }
+    struct afs_cbr *tcbrp, *tbrp;
+    struct srvAddr *sa;
+
+    for (i = 0; i < NSERVERS; i++) {
+       struct server *ts, *next;
+
+        ts = afs_servers[i];
+        while(ts) {
+           next = ts->next;
+           for (sa = ts->addr; sa; sa = sa->next_sa) {
+               if (sa->conns) {
+                    /* afs_ReleaseConns has been updated to
+                     * defer rx_DestroyConnection to Rx
+                     * shutdown, as most recently was done
+                     * here */
+                    afs_ReleaseConns(sa->conns);
                }
-               afs_volumes[i] = 0;
            }
-       }
+           for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
+               /*
+                * Free all server's callback structs
+                */
+               tbrp = tcbrp->next;
+               afs_FreeCBR(tcbrp);
+           }
+           afs_osi_Free(ts, sizeof(struct server));
+           ts = next;
+        }
+    }
 
-       /* 
-        * Free FreeVolList allocations 
-        */
-       afs_osi_Free(Initialafs_freeVolList,
-                    afs_memvolumes * sizeof(struct volume));
-       afs_freeVolList = Initialafs_freeVolList = 0;
-
-       /* XXX HACK fort MEM systems XXX 
-        *
-        * For -memcache cache managers when we run out of free in memory volumes
-        * we simply malloc more; we won't be able to free those additional volumes.
-        */
+    for (i = 0; i < NSERVERS; i++) {
+       struct srvAddr *sa, *next;
 
+        sa = afs_srvAddrs[i];
+        while(sa) {
+           next = sa->next_bkt;
+           afs_osi_Free(sa, sizeof(struct srvAddr));
+           sa = next;
+        }
+    }
+}
 
+static void
+shutdown_volume(void)
+{
+    struct volume *tv;
+    int i;
 
-       /* 
-        * Free Users table allocation 
-        */
-       {
-           struct unixuser *tu, *ntu;
-           for (i = 0; i < NUSERS; i++) {
-               for (tu = afs_users[i]; tu; tu = ntu) {
-                   ntu = tu->next;
-                   if (tu->stp)
-                       afs_osi_Free(tu->stp, tu->stLen);
-                   if (tu->exporter)
-                       EXP_RELE(tu->exporter);
-                   afs_osi_Free(tu, sizeof(struct unixuser));
-               }
-               afs_users[i] = 0;
+    for (i = 0; i < NVOLS; i++) {
+       for (tv = afs_volumes[i]; tv; tv = tv->next) {
+           if (tv->name) {
+               afs_osi_Free(tv->name, strlen(tv->name) + 1);
+               tv->name = 0;
            }
        }
+       afs_volumes[i] = 0;
+    }
+}
 
-       /* 
-        * Free Servers table allocation 
-        */
-       {
-           struct server *ts, *nts;
-           struct conn *tc, *ntc;
-           register struct afs_cbr *tcbrp, *tbrp;
-
-           for (i = 0; i < NSERVERS; i++) {
-               for (ts = afs_servers[i]; ts; ts = nts) {
-                   nts = ts->next;
-                   for (sa = ts->addr; sa; sa = sa->next_sa) {
-                       if (sa->conns) {
-                           /*
-                            * Free all server's connection structs
-                            */
-                           tc = sa->conns;
-                           while (tc) {
-                               ntc = tc->next;
-                               AFS_GUNLOCK();
-                               rx_DestroyConnection(tc->id);
-                               AFS_GLOCK();
-                               afs_osi_Free(tc, sizeof(struct conn));
-                               tc = ntc;
-                           }
-                       }
-                   }
-                   for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
-                       /*
-                        * Free all server's callback structs
-                        */
-                       tbrp = tcbrp->next;
-                       afs_FreeCBR(tcbrp);
-                   }
-                   afs_osi_Free(ts, sizeof(struct server));
-               }
-               afs_servers[i] = 0;
+void
+shutdown_AFS(void)
+{
+    int i;
+
+    AFS_STATCNT(shutdown_AFS);
+
+    afs_resourceinit_flag = 0;
+
+    shutdown_volume();
+
+    /*
+     * Free FreeVolList allocations
+     */
+    afs_osi_Free(Initialafs_freeVolList,
+                afs_memvolumes * sizeof(struct volume));
+    afs_freeVolList = Initialafs_freeVolList = NULL;
+
+    /* XXX HACK for MEM systems XXX
+     *
+     * For -memcache cache managers when we run out of free in memory volumes
+     * we simply malloc more; we won't be able to free those additional volumes.
+     */
+
+    /*
+     * Free Users table allocation
+     */
+    {
+       struct unixuser *tu, *ntu;
+       for (i = 0; i < NUSERS; i++) {
+           for (tu = afs_users[i]; tu; tu = ntu) {
+               ntu = tu->next;
+               if (tu->tokens)
+                   afs_FreeTokens(&tu->tokens);
+               if (tu->exporter)
+                   EXP_RELE(tu->exporter);
+               afs_osi_Free(tu, sizeof(struct unixuser));
            }
+           afs_users[i] = NULL;
        }
-       for (i = 0; i < NFENTRIES; i++)
-           fvTable[i] = 0;
-       /* Reinitialize local globals to defaults */
-       for (i = 0; i < MAXNUMSYSNAMES; i++)
-           afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
-       afs_sysname = 0;
-       afs_sysnamecount = 0;
-       afs_marinerHost = 0;
-       afs_setTimeHost = NULL;
-       afs_volCounter = 1;
-       afs_waitForever = afs_waitForeverCount = 0;
-       afs_FVIndex = -1;
-       afs_server = (struct rx_service *)0;
-       RWLOCK_INIT(&afs_xconn, "afs_xconn");
-       memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
-       RWLOCK_INIT(&afs_xuser, "afs_xuser");
-       RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
-       RWLOCK_INIT(&afs_xserver, "afs_xserver");
-       LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
+    }
+
+    for (i = 0; i < NFENTRIES; i++)
+       fvTable[i] = 0;
+    /* Reinitialize local globals to defaults */
+    for (i = 0; i < MAXNUMSYSNAMES; i++) {
+       afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
+       afs_sysnamelist[i] = NULL;
+    }
+    afs_sysname = NULL;
+    afs_sysnamecount = 0;
+    afs_marinerHost = 0;
+    afs_volCounter = 1;
+    afs_waitForever = afs_waitForeverCount = 0;
+    afs_FVIndex = -1;
+    afs_server = NULL;
 
-       shutdown_cell();
-       shutdown_server();
+    if (afs_cold_shutdown) {
+       AFS_RWLOCK_INIT(&afs_xconn, "afs_xconn");
+       memset(&afs_rootFid, 0, sizeof(struct VenusFid));
+       AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
+       AFS_RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
+       AFS_RWLOCK_INIT(&afs_xserver, "afs_xserver");
+       LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
     }
+    shutdown_cell();
+    shutdown_server();
+    shutdown_rx();
+    shutdown_rxevent();
 }