darwin port was unhappy because of this
[openafs.git] / src / afs / afs_init.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * afs_init.c - initialize AFS client.
12  *
13  * Implements:
14  */
15
16 #include "../afs/param.h"       /* Should be always first */
17 #include "../afs/stds.h"
18 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include "../afs/afsincludes.h" /* Afs-based standard headers */
20 #include "../afs/afs_stats.h"   /* afs statistics */
21
22
23 /* Imported variables */
24 extern afs_int32 afs_waitForever;
25 extern short afs_waitForeverCount;
26 extern afs_int32 afs_FVIndex;
27 extern struct server *afs_setTimeHost;
28 extern struct server *afs_servers[NSERVERS];
29 extern struct unixuser *afs_users[NUSERS];
30 extern struct volume *afs_freeVolList;
31 extern struct volume *afs_volumes[NVOLS];
32 extern afs_int32 afs_volCounter;
33
34 extern afs_rwlock_t afs_xaxs;
35 extern afs_rwlock_t afs_xvolume;
36 extern afs_rwlock_t afs_xuser;
37 extern afs_rwlock_t afs_xserver;
38 #ifndef AFS_AIX41_ENV
39 extern afs_lock_t osi_fsplock;
40 #endif
41 extern afs_lock_t osi_flplock;
42 extern afs_int32 fvTable[NFENTRIES];
43
44 /* afs_cell.c */
45 extern afs_rwlock_t afs_xcell;
46 extern struct afs_q CellLRU;
47 extern afs_int32 afs_cellindex;
48
49 /* afs_conn.c */
50 extern afs_rwlock_t afs_xconn;
51 extern afs_rwlock_t afs_xinterface;
52
53 /* afs_mariner.c */
54 extern struct rx_service *afs_server;
55
56
57 /* afs_mariner.c */
58 extern afs_int32 afs_mariner;
59 extern afs_int32 afs_marinerHost;
60
61 /* afs_volume.c */
62 extern ino_t volumeInode;
63
64 /* afs_osi_pag.c */
65 extern afs_uint32 pag_epoch;
66
67 /* afs_dcache.c */
68 extern afs_rwlock_t afs_xdcache;
69 extern int cacheDiskType;
70 extern afs_int32 afs_fsfragsize;
71 extern ino_t cacheInode;
72 extern struct osi_file *afs_cacheInodep;
73 extern afs_int32 afs_freeDCList;                /*Free list for disk cache entries*/
74
75
76 /* afs_vcache.c */
77 extern afs_rwlock_t afs_xvcache;
78 extern afs_rwlock_t afs_xvcb;
79
80 /* VNOPS/afs_vnop_read.c */
81 extern afs_int32 maxIHint;
82 extern afs_int32 nihints;                   /* # of above actually in-use */
83 extern afs_int32 usedihint;
84
85 /* afs_server.c */
86 extern afs_int32 afs_setTime;
87
88 /* Imported functions. */
89 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
90 extern int RXAFSCB_ExecuteRequest();
91 extern int RXSTATS_ExecuteRequest();
92
93
94 /* afs_osi.c */
95 extern afs_lock_t afs_ftf;
96
97 /* Exported variables */
98 struct osi_dev cacheDev;           /*Cache device*/
99 afs_int32 cacheInfoModTime;                     /*Last time cache info modified*/
100 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV) || defined(AFS_DARWIN_ENV)
101 struct mount *afs_cacheVfsp=0;
102 #elif defined(AFS_LINUX20_ENV)
103 struct super_block *afs_cacheSBp = 0;
104 #else
105 struct vfs *afs_cacheVfsp=0;
106 #endif
107 afs_rwlock_t afs_puttofileLock; /* not used */
108 char *afs_sysname = 0;                  /* So that superuser may change the
109                                          * local value of @sys */
110 struct volume *Initialafs_freeVolList;
111 int afs_memvolumes = 0;
112
113 /* Local variables */
114
115
116 /*
117  * Initialization order is important.  Must first call afs_CacheInit,
118  * then cache file and volume file initialization routines.  Next, the
119  * individual cache entry initialization routines are called.
120  */
121
122
123 /*
124  * afs_CacheInit
125  *
126  * Description:
127  *
128  * Parameters:
129  *      astatSize : The number of stat cache (vnode) entries to
130  *                  allocate.
131  *      afiles    : The number of disk files to allocate to the cache
132  *      ablocks   : The max number of 1 Kbyte blocks that all of
133  *                  the files in the cache may occupy.
134  *      aDentries : Number of dcache entries to allocate.
135  *      aVolumes  : Number of volume cache entries to allocate.
136  *      achunk    : Power of 2 to make the chunks.
137  *      aflags    : Flags passed in.
138  *      inodes    : max inodes to pin down in inode[]
139  *      users     : what should size of per-user access cache be?
140  *
141  * Environment:
142  *      This routine should only be called at initialization time, since
143  *      it reclaims no resources and doesn't sufficiently synchronize
144  *      with other processes.
145  */
146
147 struct cm_initparams cm_initParams;
148 static int afs_cacheinit_flag = 0;
149 int
150 afs_CacheInit(astatSize, afiles, ablocks, aDentries, aVolumes, achunk, aflags,
151               ninodes, nusers)
152     afs_int32 afiles;
153     afs_int32 astatSize, ablocks; 
154     afs_int32 achunk, aflags, ninodes, nusers;
155     afs_int32 aDentries;
156 { /*afs_CacheInit*/
157     extern int afs_memvolumes;
158     register afs_int32 i, preallocs;
159     register struct volume *tv;
160     long code;
161
162     AFS_STATCNT(afs_CacheInit);
163     /*
164      * Jot down the epoch time, namely when this incarnation of the
165      * Cache Manager started.
166      */
167     afs_stats_cmperf.epoch = pag_epoch = osi_Time();
168 #ifdef SYS_NAME_ID
169     afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
170 #else
171     afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
172 #endif /* SYS_NAME_ID */
173
174     printf("Starting AFS cache scan...");
175     if (afs_cacheinit_flag)
176         return 0;
177     afs_cacheinit_flag = 1;
178     cacheInfoModTime = 0;
179     maxIHint = ninodes;
180     nihints = 0;
181     usedihint = 0;
182
183     LOCK_INIT(&afs_ftf, "afs_ftf");
184     RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
185     osi_dnlc_init();
186
187
188 #if     defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
189     /*
190      * We want to also reserve space for the gnode struct which is associated
191      * with each vnode (vcache) one; we want to use the pinned pool for them   
192      * since they're referenced at interrupt level.
193      */
194     if (afs_stats_cmperf.SmallBlocksAlloced + astatSize < 3600)
195       preallocs = astatSize;
196     else {
197       preallocs = 3600 - afs_stats_cmperf.SmallBlocksAlloced;
198       if (preallocs <= 0) preallocs = 10;
199     }
200     osi_AllocMoreSSpace(preallocs);
201 #endif
202     /* 
203      * create volume list structure 
204      */
205     if ( aVolumes < 50 )     aVolumes = 50;  
206     if (aVolumes > 3000) aVolumes = 3000;
207
208     tv = (struct volume *) afs_osi_Alloc(aVolumes * sizeof(struct volume));
209     for (i=0;i<aVolumes-1;i++)
210         tv[i].next = &tv[i+1];
211     tv[aVolumes-1].next = (struct volume *) 0;
212     afs_freeVolList = Initialafs_freeVolList = tv;
213     afs_memvolumes = aVolumes;
214
215     afs_cacheFiles = afiles;
216     afs_cacheStats = astatSize;
217     afs_vcacheInit(astatSize);
218     afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
219
220 #if defined(AFS_AIX_ENV)
221     {
222         static void afs_procsize_init(void);
223
224         afs_procsize_init();
225     }
226 #endif
227
228     /* Save the initialization parameters for later pioctl queries. */
229     cm_initParams.cmi_version = CMI_VERSION;
230     cm_initParams.cmi_nChunkFiles = afiles;
231     cm_initParams.cmi_nStatCaches = astatSize;
232     cm_initParams.cmi_nDataCaches = aDentries;
233     cm_initParams.cmi_nVolumeCaches = aVolumes;
234     cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
235     cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
236     cm_initParams.cmi_cacheSize = ablocks;
237     cm_initParams.cmi_setTime = afs_setTime;
238     cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
239
240     return 0;
241
242 } /*afs_CacheInit*/
243
244
245 /*
246   * afs_ComputeCacheParams
247   *
248   * Description:
249   *     Set some cache parameters.
250   *
251   * Parameters:
252   *     None.
253   */
254
255 void
256 afs_ComputeCacheParms()
257
258 { /*afs_ComputeCacheParms*/
259
260     register afs_int32 i;
261     afs_int32 afs_maxCacheDirty;
262
263     /*
264      * Don't allow more than 2/3 of the files in the cache to be dirty.
265      */
266     afs_maxCacheDirty = (2*afs_cacheFiles) / 3;
267
268     /*
269      * Also, don't allow more than 2/3 of the total space get filled
270      * with dirty chunks.  Compute the total number of chunks required
271      * to fill the cache, make sure we don't set out limit above 2/3 of
272      * that. If the cache size is greater than 1G, avoid overflow at
273      * the expense of precision on the chunk size.
274      */
275     if (afs_cacheBlocks & 0xffe00000) {
276         i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
277     }
278     else {
279         i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
280     }
281     i = (2*i) / 3;
282     if (afs_maxCacheDirty > i)
283         afs_maxCacheDirty = i;
284     if (afs_maxCacheDirty < 1)
285         afs_maxCacheDirty = 1;
286     afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
287 } /*afs_ComputeCacheParms*/
288
289
290 /*
291  * afs_InitVolumeInfo
292  *
293  * Description:
294  *      Set up the volume info storage file.
295  *
296  * Parameters:
297  *      afile : the file to be declared to be the volume info storage
298  *              file for AFS.  It must be already truncated to 0 length.
299  *
300  * Environment:
301  *      This function is called only during initialization.
302  *
303  *      WARNING: Data will be written to this file over time by AFS.
304  */
305
306 afs_InitVolumeInfo(afile)
307     register char *afile;
308
309 { /*afs_InitVolumeInfo*/
310
311     afs_int32 code;
312     struct osi_file *tfile;
313     struct vnode *filevp;
314     struct fcache fce;
315
316     AFS_STATCNT(afs_InitVolumeInfo);
317 #ifdef AFS_LINUX22_ENV
318     {
319         struct dentry *dp;
320         code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &dp);
321         if (code) return ENOENT;
322         fce.inode = volumeInode = dp->d_inode->i_ino;
323         dput(dp);
324     }
325 #else
326     code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
327     if (code) return ENOENT;
328     fce.inode = volumeInode = afs_vnodeToInumber(filevp);
329 #ifdef AFS_DEC_ENV
330     grele(filevp);
331 #else
332     AFS_RELE((struct vnode *)filevp);
333 #endif
334 #endif /* AFS_LINUX22_ENV */
335     tfile = afs_CFileOpen(fce.inode);
336     afs_CFileTruncate(tfile, 0);
337     afs_CFileClose(tfile);
338     return 0;
339
340 } /*afs_InitVolumeInfo*/
341
342 /*
343  * afs_InitCacheInfo
344  *
345  * Description:
346  *      Set up the given file as the AFS cache info file.
347  *
348  * Parameters:
349  *      afile : Name of the file assumed to be the cache info file
350  *              for the Cache Manager; it will be used as such.
351  * Side Effects:  This sets afs_fragsize, which is used in the cache usage 
352  *                calculations such as in afs_adjustsize()
353  *
354  * Environment:
355  *      This function is called only during initialization.  The given
356  *      file should NOT be truncated to 0 lenght; its contents descrebe
357  *      what data is really in the cache.
358  *
359  *      WARNING: data will be written to this file over time by AFS.
360  *
361  * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
362  * code.
363  *
364  */
365 afs_InitCacheInfo(afile)
366     register char *afile;
367
368 { /*afs_InitCacheInfo*/
369
370     register afs_int32 code;
371     struct osi_stat tstat;
372     register struct osi_file *tfile;
373     struct afs_fheader theader;
374     struct vnode *filevp;
375     int goodFile;
376
377     AFS_STATCNT(afs_InitCacheInfo);
378     if(cacheDiskType != AFS_FCACHE_TYPE_UFS)
379         osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
380 #ifdef AFS_LINUX22_ENV
381     code = osi_InitCacheInfo(afile);
382     if (code) return code;
383 #else
384     code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
385     if (code || !filevp) return ENOENT;
386     {
387 #if     defined(AFS_SUN56_ENV)
388       struct statvfs64 st;
389 #else
390 #if     defined(AFS_HPUX102_ENV)
391       struct k_statvfs st;
392 #else
393 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
394       struct statvfs st;
395 #else 
396 #if defined(AFS_DUX40_ENV)
397       struct nstatfs st;
398 #else
399       struct statfs st;
400 #endif /* DUX40 */
401 #endif /* SUN5 SGI */
402 #endif /* HP 10.20 */
403 #endif /* SUN56 */
404
405 #if     defined(AFS_SGI_ENV)
406 #ifdef AFS_SGI65_ENV
407       VFS_STATVFS(filevp->v_vfsp, &st, (struct vnode *)0, code);
408       if (!code) 
409 #else
410       if (!VFS_STATFS(filevp->v_vfsp, &st, (struct vnode *)0))
411 #endif /* AFS_SGI65_ENV */
412 #else /* AFS_SGI_ENV */
413 #if     defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
414       if (!VFS_STATVFS(filevp->v_vfsp, &st)) 
415 #else
416 #ifdef  AFS_OSF_ENV
417       
418       VFS_STATFS(filevp->v_vfsp, code);
419       /* struct copy */
420       st = filevp->v_vfsp->m_stat;
421       if (code == 0)
422 #else   /* AFS_OSF_ENV */
423 #ifdef AFS_AIX41_ENV
424       if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
425 #else
426 #ifdef AFS_LINUX20_ENV
427           {
428               KERNEL_SPACE_DECL;
429               TO_USER_SPACE();
430
431               VFS_STATFS(filevp->v_vfsp, &st);
432               TO_KERNEL_SPACE();
433           }
434 #else
435 #ifdef AFS_DARWIN_ENV
436         if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
437 #else 
438         if (!VFS_STATFS(filevp->v_vfsp, &st))  
439 #endif /* AFS_DARWIN_ENV */
440 #endif /* AFS_LINUX20_ENV */
441 #endif /* AIX41 */
442 #endif /* OSF */
443 #endif /* SUN5 HP10 */
444 #endif /* SGI */
445 #if     defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
446         afs_fsfragsize = st.f_frsize - 1; 
447 #else
448         afs_fsfragsize = st.f_bsize - 1; 
449 #endif
450     }
451 #ifdef AFS_LINUX20_ENV
452     cacheInode = filevp->i_ino;
453     afs_cacheSBp = filevp->i_sb;
454 #else
455 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
456     afs_InitDualFSCacheOps(filevp);
457 #endif
458     cacheInode = afs_vnodeToInumber(filevp);
459     cacheDev.dev = afs_vnodeToDev(filevp);
460     afs_cacheVfsp = filevp->v_vfsp;
461 #endif /* AFS_LINUX20_ENV */
462     AFS_RELE((struct vnode *)filevp);
463 #endif /* AFS_LINUX22_ENV */
464     tfile = osi_UFSOpen(cacheInode);
465     afs_osi_Stat(tfile, &tstat);
466     cacheInfoModTime = tstat.mtime;
467     code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
468     goodFile = 0;
469     if (code == sizeof(theader)) {
470         /* read the header correctly */
471         if (theader.magic == AFS_FHMAGIC &&
472             theader.firstCSize == AFS_FIRSTCSIZE &&
473             theader.otherCSize == AFS_OTHERCSIZE &&
474             theader.version == AFS_CI_VERSION
475         )
476             goodFile = 1;
477     }
478     if (!goodFile) {
479         /* write out a good file label */
480         theader.magic = AFS_FHMAGIC;
481         theader.firstCSize = AFS_FIRSTCSIZE;
482         theader.otherCSize = AFS_OTHERCSIZE;
483         theader.version = AFS_CI_VERSION;
484         afs_osi_Write(tfile, 0, &theader, sizeof(theader));
485         /*
486          * Truncate the rest of the file, since it may be arbitrarily
487          * wrong
488          */
489         osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
490     }
491     /* Leave the file open now, since reopening the file makes public pool
492      * vnode systems (like OSF/Alpha) much harder to handle, That's because
493      * they can do a vnode recycle operation any time we open a file, which
494      * we'd do on any afs_GetDSlot call, etc.
495      */
496     afs_cacheInodep = (struct osi_file *)tfile;
497     return 0;
498
499 } /*afs_InitCacheInfo*/
500
501 int afs_resourceinit_flag = 0;
502 afs_ResourceInit(preallocs)
503   int preallocs;
504 {
505     register afs_int32 i;
506     static struct rx_securityClass *secobj;
507
508     AFS_STATCNT(afs_ResourceInit);
509     RWLOCK_INIT(&afs_xuser, "afs_xuser");
510     RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
511     RWLOCK_INIT(&afs_xcell, "afs_xcell");
512     RWLOCK_INIT(&afs_xserver, "afs_xserver");
513     RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
514     LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
515 #ifndef AFS_AIX32_ENV
516     LOCK_INIT(&osi_fsplock, "osi_fsplock");
517 #endif
518     LOCK_INIT(&osi_flplock, "osi_flplock");
519     RWLOCK_INIT(&afs_xconn, "afs_xconn");
520
521     afs_InitCBQueue(1);  /* initialize callback queues */
522
523     if (afs_resourceinit_flag == 0) {
524         afs_resourceinit_flag = 1;
525         for (i=0;i<NFENTRIES;i++)
526             fvTable[i] = 0;
527         afs_sysname = afs_osi_Alloc(MAXSYSNAME);
528         strcpy(afs_sysname, SYS_NAME);
529         QInit(&CellLRU);        
530 #if     defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
531     {  extern afs_int32 afs_preallocs;
532
533        if ((preallocs > 256) && (preallocs < 3600))
534            afs_preallocs = preallocs;
535        osi_AllocMoreSSpace(afs_preallocs);
536        osi_AllocMoreMSpace(100); 
537     }
538 #endif
539     }
540     
541     secobj = rxnull_NewServerSecurityObject();
542     afs_server =
543         rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
544     afs_server =
545         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
546                       RXSTATS_ExecuteRequest);
547     rx_StartServer(0);
548     afs_osi_Wakeup(&afs_server);        /* wakeup anyone waiting for it */
549     return 0;
550
551 } /*afs_ResourceInit*/
552
553 #if defined(AFS_AIX_ENV)
554
555 /*
556  * AIX dynamic sizeof(struct proc)
557  *
558  * AIX keeps its proc structures in an array.  The size of struct proc
559  * varies from release to release of the OS.  In order to maintain
560  * binary compatibility with releases later than what we build on, we
561  * need to determine the size of struct proc at run time.
562  *
563  * We need this in order to walk the proc[] array to do PAG garbage
564  * collection.
565  *
566  * We also need this in order to support 'klog -setpag', since the
567  * kernel code needs to locate the proc structure for the parent process
568  * of the current process.
569  *
570  * To compute sizeof(struct proc), we need the addresses of two proc
571  * structures and their corresponding pids.  Given the pids, we can use
572  * the PROCMASK() macro to compute their corresponding indices in the
573  * proc[] array.  By dividing the distance between the pointers by the
574  * number of proc structures, we can compute the size of a single proc
575  * structure.
576  *
577  * We know the base address of the proc table from v.vb_proc:
578  *
579  * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
580  * (we don't use this, but I note it here for completeness)
581  *
582  * <sys/var.h> declares struct var and external variable v;
583  *
584  * v.v_proc             NPROC
585  * v.vb_proc            &proc[0]
586  * v.ve_proc            &proc[x] (current highwater mark for
587  *                                proc[] array usage)
588  *
589  * The first proc pointer is v.vb_proc, which is the proc structure for
590  * process 0.  Process 0's pointer to its first child is the other proc
591  * pointer.  If process 0 has no children, we simply give up and do not
592  * support features that require knowing the size of struct proc.
593  */
594
595 static void
596 afs_procsize_init(void)
597 {
598     struct proc *p0;    /* pointer to process 0 */
599     struct proc *pN;    /* pointer to process 0's first child */
600     int pN_index;
601     ptrdiff_t pN_offset;
602     int procsize;
603
604     p0 = (struct proc *)v.vb_proc;
605     if (!p0) {
606         afs_gcpags = AFS_GCPAGS_EPROC0;
607         return;
608     }
609
610     pN = p0->p_child;
611     if (!pN) {
612         afs_gcpags = AFS_GCPAGS_EPROCN;
613         return;
614     }
615
616     if (pN->p_pid == p0->p_pid) {
617         afs_gcpags = AFS_GCPAGS_EEQPID;
618         return;
619     }
620
621     pN_index = PROCMASK(pN->p_pid);
622     pN_offset = ((char *)pN - (char *)p0);
623     procsize = pN_offset / pN_index;
624
625     /*
626      * check that the computation was exact
627      */
628
629     if (pN_index * procsize != pN_offset) {
630         afs_gcpags = AFS_GCPAGS_EINEXACT;
631         return;
632     }
633
634     /*
635      * check that the proc table size is a multiple of procsize.
636      */
637
638     if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
639         afs_gcpags = AFS_GCPAGS_EPROCEND;
640         return;
641     }
642
643     /* okay, use it */
644
645     afs_gcpags_procsize = procsize;
646 }
647
648 #endif
649
650 /*
651  * shutdown_cache
652  *
653  * Description:
654  *      Clean up and shut down the AFS cache.
655  *
656  * Parameters:
657  *      None.
658  *
659  * Environment:
660  *      Nothing interesting.
661  */
662 void
663 shutdown_cache()
664
665 { /*shutdown_cache*/
666     register struct afs_cbr *tsp, *nsp;
667     extern int afs_cold_shutdown;
668     extern int pagCounter;
669     int i;
670
671   AFS_STATCNT(shutdown_cache);
672   afs_WriteThroughDSlots();
673   if (afs_cold_shutdown) {
674     afs_cacheinit_flag = 0;
675     shutdown_dcache();
676     shutdown_vcache();
677
678     afs_cacheStats = 0;
679     afs_cacheFiles = afs_cacheBlocks = 0;
680     pag_epoch = maxIHint = nihints = usedihint = 0;
681     pagCounter = 0;
682     cacheInode = volumeInode = (ino_t)0;
683
684
685     cacheInfoModTime = 0;
686
687     afs_fsfragsize = 1023;
688     bzero((char *)&afs_stats_cmperf, sizeof(afs_stats_cmperf));
689     bzero((char *)&cacheDev, sizeof(struct osi_dev));
690     osi_dnlc_shutdown();
691   }
692 } /*shutdown_cache*/
693
694
695 void shutdown_vnodeops()
696 {
697     extern int afs_cold_shutdown;
698 #ifndef AFS_LINUX20_ENV
699     extern int afs_rd_stash_i;
700 #endif
701 #ifndef AFS_SUN5_ENV
702     extern int lastWarnTime;
703 #endif
704 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
705     struct buf *afs_bread_freebp = 0;
706 #endif
707     
708
709     AFS_STATCNT(shutdown_vnodeops);
710     if (afs_cold_shutdown) {
711 #ifndef AFS_SUN5_ENV    /* XXX */
712       lastWarnTime = 0;
713 #endif
714 #ifndef AFS_LINUX20_ENV
715       afs_rd_stash_i = 0;      
716 #endif
717 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
718       afs_bread_freebp = 0;
719 #endif
720       shutdown_mariner();
721   }
722 }
723
724
725 void shutdown_AFS()
726
727 {
728     int i;
729     register struct srvAddr *sa;
730     extern int afs_cold_shutdown;
731
732     AFS_STATCNT(shutdown_AFS);
733     if (afs_cold_shutdown) {
734       afs_resourceinit_flag = 0; 
735       /* 
736        * Free Cells table allocations 
737        */
738       { 
739         struct cell *tc;
740         register struct afs_q *cq, *tq;
741         for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
742             tc = QTOC(cq); tq = QNext(cq);
743             if (tc->cellName)
744                 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
745             afs_osi_Free(tc, sizeof(struct cell));
746         }
747       }
748       /* 
749        * Free Volumes table allocations 
750        */
751       { 
752         struct volume *tv;
753         for (i = 0; i < NVOLS; i++) {
754             for (tv = afs_volumes[i]; tv; tv = tv->next) {
755                 if (tv->name) {
756                     afs_osi_Free(tv->name, strlen(tv->name)+1);
757                     tv->name = 0;
758                 }
759             }
760             afs_volumes[i] = 0;
761         }
762       }
763
764       /* 
765        * Free FreeVolList allocations 
766        */
767       afs_osi_Free(Initialafs_freeVolList, afs_memvolumes * sizeof(struct volume));
768       afs_freeVolList = Initialafs_freeVolList = 0;
769
770       /* XXX HACK fort MEM systems XXX 
771        *
772        * For -memcache cache managers when we run out of free in memory volumes
773        * we simply malloc more; we won't be able to free those additional volumes.
774        */
775
776       
777
778       /* 
779        * Free Users table allocation 
780        */
781       { 
782         struct unixuser *tu, *ntu;
783         for (i=0; i < NUSERS; i++) {
784             for (tu=afs_users[i]; tu; tu = ntu) {
785                 ntu = tu->next;
786                 if (tu->stp)
787                     afs_osi_Free(tu->stp, tu->stLen);
788                 if (tu->exporter)
789                     EXP_RELE(tu->exporter);
790                 afs_osi_Free(tu, sizeof(struct unixuser));
791             }
792             afs_users[i] = 0;
793         }
794       }
795
796       /* 
797        * Free Servers table allocation 
798        */
799       { 
800         struct server *ts, *nts;
801         struct conn *tc, *ntc;
802         register struct afs_cbr *tcbrp, *tbrp;
803         struct afs_cbr **lcbrpp;
804
805         for (i=0; i < NSERVERS; i++) {
806             for (ts = afs_servers[i]; ts; ts = nts) {
807                 nts = ts->next;
808                 for (sa = ts->addr; sa; sa = sa->next_sa) {     
809                     if (sa->conns) {
810                         /*
811                          * Free all server's connection structs
812                          */
813                         tc = sa->conns;
814                         while (tc) {
815                             ntc = tc->next;
816                             AFS_GUNLOCK();
817                             rx_DestroyConnection(tc->id);
818                             AFS_GLOCK();
819                             afs_osi_Free(tc, sizeof(struct conn));
820                             tc = ntc;
821                         }
822                     }
823                 }
824                 for (tcbrp = ts->cbrs; tcbrp;  tcbrp = tbrp) {
825                     /*
826                      * Free all server's callback structs
827                      */
828                     tbrp = tcbrp->next;
829                     afs_FreeCBR(tcbrp);
830                 }
831                 afs_osi_Free(ts, sizeof(struct server));
832             }
833             afs_servers[i] = 0;
834         }
835       }
836       for (i=0; i<NFENTRIES; i++)
837         fvTable[i] = 0;
838       /* Reinitialize local globals to defaults */
839       afs_osi_Free(afs_sysname, MAXSYSNAME);
840       afs_sysname = 0;
841       afs_marinerHost = 0;
842       QInit(&CellLRU);      
843       afs_setTimeHost = (struct server *)0;
844       afs_volCounter = 1;
845       afs_waitForever = afs_waitForeverCount = 0;
846       afs_cellindex = 0;
847       afs_FVIndex = -1;
848       afs_server = (struct rx_service *)0;
849       RWLOCK_INIT(&afs_xconn, "afs_xconn");
850       bzero((char *)&afs_rootFid, sizeof(struct VenusFid));
851       RWLOCK_INIT(&afs_xuser, "afs_xuser");
852       RWLOCK_INIT(&afs_xvolume, "afs_xvolume"), RWLOCK_INIT(&afs_xcell, "afs_xcell");
853       RWLOCK_INIT(&afs_xserver, "afs_xserver"), LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
854     }
855     
856 } /*shutdown_AFS*/