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