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