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