simplification-of-code-in-cache-manager-to-make-dux-advfs-support-simpler-20010315
[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)
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         if (!VFS_STATFS(filevp->v_vfsp, &st))  
436 #endif /* AFS_LINUX20_ENV */
437 #endif /* AIX41 */
438 #endif /* OSF */
439 #endif /* SUN5 HP10 */
440 #endif /* SGI */
441 #if     defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
442         afs_fsfragsize = st.f_frsize - 1; 
443 #else
444         afs_fsfragsize = st.f_bsize - 1; 
445 #endif
446     }
447 #ifdef AFS_LINUX20_ENV
448     cacheInode = filevp->i_ino;
449     afs_cacheSBp = filevp->i_sb;
450 #else
451     cacheInode = afs_vnodeToInumber(filevp);
452     cacheDev.dev = afs_vnodeToDev(filevp);
453 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS)
454     afs_InitDualFSCacheOps(filevp);
455 #endif
456     afs_cacheVfsp = filevp->v_vfsp;
457 #endif /* AFS_LINUX20_ENV */
458     AFS_RELE((struct vnode *)filevp);
459 #endif /* AFS_LINUX22_ENV */
460     tfile = osi_UFSOpen(cacheInode);
461     afs_osi_Stat(tfile, &tstat);
462     cacheInfoModTime = tstat.mtime;
463     code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
464     goodFile = 0;
465     if (code == sizeof(theader)) {
466         /* read the header correctly */
467         if (theader.magic == AFS_FHMAGIC &&
468             theader.firstCSize == AFS_FIRSTCSIZE &&
469             theader.otherCSize == AFS_OTHERCSIZE &&
470             theader.version == AFS_CI_VERSION
471         )
472             goodFile = 1;
473     }
474     if (!goodFile) {
475         /* write out a good file label */
476         theader.magic = AFS_FHMAGIC;
477         theader.firstCSize = AFS_FIRSTCSIZE;
478         theader.otherCSize = AFS_OTHERCSIZE;
479         theader.version = AFS_CI_VERSION;
480         afs_osi_Write(tfile, 0, &theader, sizeof(theader));
481         /*
482          * Truncate the rest of the file, since it may be arbitrarily
483          * wrong
484          */
485         osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
486     }
487     /* Leave the file open now, since reopening the file makes public pool
488      * vnode systems (like OSF/Alpha) much harder to handle, That's because
489      * they can do a vnode recycle operation any time we open a file, which
490      * we'd do on any afs_GetDSlot call, etc.
491      */
492     afs_cacheInodep = (struct osi_file *)tfile;
493     return 0;
494
495 } /*afs_InitCacheInfo*/
496
497 int afs_resourceinit_flag = 0;
498 afs_ResourceInit(preallocs)
499   int preallocs;
500 {
501     register afs_int32 i;
502     static struct rx_securityClass *secobj;
503
504     AFS_STATCNT(afs_ResourceInit);
505     RWLOCK_INIT(&afs_xuser, "afs_xuser");
506     RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
507     RWLOCK_INIT(&afs_xcell, "afs_xcell");
508     RWLOCK_INIT(&afs_xserver, "afs_xserver");
509     RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
510     LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
511 #ifndef AFS_AIX32_ENV
512     LOCK_INIT(&osi_fsplock, "osi_fsplock");
513 #endif
514     LOCK_INIT(&osi_flplock, "osi_flplock");
515     RWLOCK_INIT(&afs_xconn, "afs_xconn");
516
517     afs_InitCBQueue(1);  /* initialize callback queues */
518
519     if (afs_resourceinit_flag == 0) {
520         afs_resourceinit_flag = 1;
521         for (i=0;i<NFENTRIES;i++)
522             fvTable[i] = 0;
523         afs_sysname = afs_osi_Alloc(MAXSYSNAME);
524         strcpy(afs_sysname, SYS_NAME);
525         QInit(&CellLRU);        
526 #if     defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
527     {  extern afs_int32 afs_preallocs;
528
529        if ((preallocs > 256) && (preallocs < 3600))
530            afs_preallocs = preallocs;
531        osi_AllocMoreSSpace(afs_preallocs);
532        osi_AllocMoreMSpace(100); 
533     }
534 #endif
535     }
536     
537     secobj = rxnull_NewServerSecurityObject();
538     afs_server =
539         rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
540     afs_server =
541         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
542                       RXSTATS_ExecuteRequest);
543     rx_StartServer(0);
544     afs_osi_Wakeup(&afs_server);        /* wakeup anyone waiting for it */
545     return 0;
546
547 } /*afs_ResourceInit*/
548
549 #if defined(AFS_AIX_ENV)
550
551 /*
552  * AIX dynamic sizeof(struct proc)
553  *
554  * AIX keeps its proc structures in an array.  The size of struct proc
555  * varies from release to release of the OS.  In order to maintain
556  * binary compatibility with releases later than what we build on, we
557  * need to determine the size of struct proc at run time.
558  *
559  * We need this in order to walk the proc[] array to do PAG garbage
560  * collection.
561  *
562  * We also need this in order to support 'klog -setpag', since the
563  * kernel code needs to locate the proc structure for the parent process
564  * of the current process.
565  *
566  * To compute sizeof(struct proc), we need the addresses of two proc
567  * structures and their corresponding pids.  Given the pids, we can use
568  * the PROCMASK() macro to compute their corresponding indices in the
569  * proc[] array.  By dividing the distance between the pointers by the
570  * number of proc structures, we can compute the size of a single proc
571  * structure.
572  *
573  * We know the base address of the proc table from v.vb_proc:
574  *
575  * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
576  * (we don't use this, but I note it here for completeness)
577  *
578  * <sys/var.h> declares struct var and external variable v;
579  *
580  * v.v_proc             NPROC
581  * v.vb_proc            &proc[0]
582  * v.ve_proc            &proc[x] (current highwater mark for
583  *                                proc[] array usage)
584  *
585  * The first proc pointer is v.vb_proc, which is the proc structure for
586  * process 0.  Process 0's pointer to its first child is the other proc
587  * pointer.  If process 0 has no children, we simply give up and do not
588  * support features that require knowing the size of struct proc.
589  */
590
591 static void
592 afs_procsize_init(void)
593 {
594     struct proc *p0;    /* pointer to process 0 */
595     struct proc *pN;    /* pointer to process 0's first child */
596     int pN_index;
597     ptrdiff_t pN_offset;
598     int procsize;
599
600     p0 = (struct proc *)v.vb_proc;
601     if (!p0) {
602         afs_gcpags = AFS_GCPAGS_EPROC0;
603         return;
604     }
605
606     pN = p0->p_child;
607     if (!pN) {
608         afs_gcpags = AFS_GCPAGS_EPROCN;
609         return;
610     }
611
612     if (pN->p_pid == p0->p_pid) {
613         afs_gcpags = AFS_GCPAGS_EEQPID;
614         return;
615     }
616
617     pN_index = PROCMASK(pN->p_pid);
618     pN_offset = ((char *)pN - (char *)p0);
619     procsize = pN_offset / pN_index;
620
621     /*
622      * check that the computation was exact
623      */
624
625     if (pN_index * procsize != pN_offset) {
626         afs_gcpags = AFS_GCPAGS_EINEXACT;
627         return;
628     }
629
630     /*
631      * check that the proc table size is a multiple of procsize.
632      */
633
634     if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
635         afs_gcpags = AFS_GCPAGS_EPROCEND;
636         return;
637     }
638
639     /* okay, use it */
640
641     afs_gcpags_procsize = procsize;
642 }
643
644 #endif
645
646 /*
647  * shutdown_cache
648  *
649  * Description:
650  *      Clean up and shut down the AFS cache.
651  *
652  * Parameters:
653  *      None.
654  *
655  * Environment:
656  *      Nothing interesting.
657  */
658 void
659 shutdown_cache()
660
661 { /*shutdown_cache*/
662     register struct afs_cbr *tsp, *nsp;
663     extern int afs_cold_shutdown;
664     extern int pagCounter;
665     int i;
666
667   AFS_STATCNT(shutdown_cache);
668   afs_WriteThroughDSlots();
669   if (afs_cold_shutdown) {
670     afs_cacheinit_flag = 0;
671     shutdown_dcache();
672     shutdown_vcache();
673
674     afs_cacheStats = 0;
675     afs_cacheFiles = afs_cacheBlocks = 0;
676     pag_epoch = maxIHint = nihints = usedihint = 0;
677     pagCounter = 0;
678     cacheInode = volumeInode = (ino_t)0;
679
680
681     cacheInfoModTime = 0;
682
683     afs_fsfragsize = 1023;
684     bzero((char *)&afs_stats_cmperf, sizeof(afs_stats_cmperf));
685     bzero((char *)&cacheDev, sizeof(struct osi_dev));
686     osi_dnlc_shutdown();
687   }
688 } /*shutdown_cache*/
689
690
691 void shutdown_vnodeops()
692 {
693     extern int afs_cold_shutdown;
694 #ifndef AFS_LINUX20_ENV
695     extern int afs_rd_stash_i;
696 #endif
697 #ifndef AFS_SUN5_ENV
698     extern int lastWarnTime;
699 #endif
700 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
701     struct buf *afs_bread_freebp = 0;
702 #endif
703     
704
705     AFS_STATCNT(shutdown_vnodeops);
706     if (afs_cold_shutdown) {
707 #ifndef AFS_SUN5_ENV    /* XXX */
708       lastWarnTime = 0;
709 #endif
710 #ifndef AFS_LINUX20_ENV
711       afs_rd_stash_i = 0;      
712 #endif
713 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
714       afs_bread_freebp = 0;
715 #endif
716       shutdown_mariner();
717   }
718 }
719
720
721 void shutdown_AFS()
722
723 {
724     int i;
725     register struct srvAddr *sa;
726     extern int afs_cold_shutdown;
727
728     AFS_STATCNT(shutdown_AFS);
729     if (afs_cold_shutdown) {
730       afs_resourceinit_flag = 0; 
731       /* 
732        * Free Cells table allocations 
733        */
734       { 
735         struct cell *tc;
736         register struct afs_q *cq, *tq;
737         for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
738             tc = QTOC(cq); tq = QNext(cq);
739             if (tc->cellName)
740                 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
741             afs_osi_Free(tc, sizeof(struct cell));
742         }
743       }
744       /* 
745        * Free Volumes table allocations 
746        */
747       { 
748         struct volume *tv;
749         for (i = 0; i < NVOLS; i++) {
750             for (tv = afs_volumes[i]; tv; tv = tv->next) {
751                 if (tv->name) {
752                     afs_osi_Free(tv->name, strlen(tv->name)+1);
753                     tv->name = 0;
754                 }
755             }
756             afs_volumes[i] = 0;
757         }
758       }
759
760       /* 
761        * Free FreeVolList allocations 
762        */
763       afs_osi_Free(Initialafs_freeVolList, afs_memvolumes * sizeof(struct volume));
764       afs_freeVolList = Initialafs_freeVolList = 0;
765
766       /* XXX HACK fort MEM systems XXX 
767        *
768        * For -memcache cache managers when we run out of free in memory volumes
769        * we simply malloc more; we won't be able to free those additional volumes.
770        */
771
772       
773
774       /* 
775        * Free Users table allocation 
776        */
777       { 
778         struct unixuser *tu, *ntu;
779         for (i=0; i < NUSERS; i++) {
780             for (tu=afs_users[i]; tu; tu = ntu) {
781                 ntu = tu->next;
782                 if (tu->stp)
783                     afs_osi_Free(tu->stp, tu->stLen);
784                 if (tu->exporter)
785                     EXP_RELE(tu->exporter);
786                 afs_osi_Free(tu, sizeof(struct unixuser));
787             }
788             afs_users[i] = 0;
789         }
790       }
791
792       /* 
793        * Free Servers table allocation 
794        */
795       { 
796         struct server *ts, *nts;
797         struct conn *tc, *ntc;
798         register struct afs_cbr *tcbrp, *tbrp;
799         struct afs_cbr **lcbrpp;
800
801         for (i=0; i < NSERVERS; i++) {
802             for (ts = afs_servers[i]; ts; ts = nts) {
803                 nts = ts->next;
804                 for (sa = ts->addr; sa; sa = sa->next_sa) {     
805                     if (sa->conns) {
806                         /*
807                          * Free all server's connection structs
808                          */
809                         tc = sa->conns;
810                         while (tc) {
811                             ntc = tc->next;
812                             AFS_GUNLOCK();
813                             rx_DestroyConnection(tc->id);
814                             AFS_GLOCK();
815                             afs_osi_Free(tc, sizeof(struct conn));
816                             tc = ntc;
817                         }
818                     }
819                 }
820                 for (tcbrp = ts->cbrs; tcbrp;  tcbrp = tbrp) {
821                     /*
822                      * Free all server's callback structs
823                      */
824                     tbrp = tcbrp->next;
825                     afs_FreeCBR(tcbrp);
826                 }
827                 afs_osi_Free(ts, sizeof(struct server));
828             }
829             afs_servers[i] = 0;
830         }
831       }
832       for (i=0; i<NFENTRIES; i++)
833         fvTable[i] = 0;
834       /* Reinitialize local globals to defaults */
835       afs_osi_Free(afs_sysname, MAXSYSNAME);
836       afs_sysname = 0;
837       afs_marinerHost = 0;
838       QInit(&CellLRU);      
839       afs_setTimeHost = (struct server *)0;
840       afs_volCounter = 1;
841       afs_waitForever = afs_waitForeverCount = 0;
842       afs_cellindex = 0;
843       afs_FVIndex = -1;
844       afs_server = (struct rx_service *)0;
845       RWLOCK_INIT(&afs_xconn, "afs_xconn");
846       bzero((char *)&afs_rootFid, sizeof(struct VenusFid));
847       RWLOCK_INIT(&afs_xuser, "afs_xuser");
848       RWLOCK_INIT(&afs_xvolume, "afs_xvolume"), RWLOCK_INIT(&afs_xcell, "afs_xcell");
849       RWLOCK_INIT(&afs_xserver, "afs_xserver"), LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
850     }
851     
852 } /*shutdown_AFS*/