dynamic-vcache-allocation-20090319
[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  * 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 static int
235 LookupInodeByPath(char *filename, ino_t * inode, struct vnode **fvpp)
236 {
237     afs_int32 code;
238
239 #ifdef AFS_LINUX22_ENV
240     struct dentry *dp;
241     code = gop_lookupname(filename, AFS_UIOSYS, 0, &dp);
242     if (code)
243         return code;
244     *inode = dp->d_inode->i_ino;
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 /* AFS_LINUX22_ENV */
258
259     return 0;
260 }
261
262 int
263 afs_InitCellInfo(char *afile)
264 {
265     ino_t inode = 0;
266     int code;
267 #if defined(LINUX_USE_FH)
268     struct fid fh;
269     int fh_type;
270     int max_len = sizeof(struct fid);
271     struct dentry *dp;
272 #endif
273
274 #ifdef AFS_CACHE_VNODE_PATH
275     return afs_cellname_init(AFS_CACHE_CELLS_INODE, code);
276 #elif defined(LINUX_USE_FH)
277     code = gop_lookupname(afile, AFS_UIOSYS, 0, &dp);
278     fh_type = osi_get_fh(dp, &fh, &max_len);
279     return afs_cellname_init(&fh, fh_type, code);
280 #else
281     code = LookupInodeByPath(afile, &inode, NULL);
282     return afs_cellname_init(inode, code);
283 #endif
284 }
285
286 /*
287  * afs_InitVolumeInfo
288  *
289  * Description:
290  *      Set up the volume info storage file.
291  *
292  * Parameters:
293  *      afile : the file to be declared to be the volume info storage
294  *              file for AFS.  It must be already truncated to 0 length.
295  *
296  * Environment:
297  *      This function is called only during initialization.
298  *
299  *      WARNING: Data will be written to this file over time by AFS.
300  */
301
302 int
303 afs_InitVolumeInfo(char *afile)
304 {
305     int code;
306     struct osi_file *tfile;
307 #if defined(LINUX_USE_FH)
308     int max_len = sizeof(struct fid);
309     struct dentry *dp;
310 #endif
311
312     AFS_STATCNT(afs_InitVolumeInfo);
313 #if defined(AFS_XBSD_ENV)
314     /*
315      * On Open/Free/NetBSD, we can get into big trouble if we don't hold the volume file
316      * vnode.  SetupVolume holds afs_xvolume lock exclusive.
317      * SetupVolume->GetVolSlot->UFSGetVolSlot->{GetVolCache or WriteVolCache}
318      * ->osi_UFSOpen->VFS_VGET()->ffs_vget->getnewvnode->vgone on some vnode.
319      * If it's AFS, then ->vclean->afs_nbsd_reclaim->FlushVCache->QueueVCB->
320      * GetVolume->FindVolume-> waits on afs_xvolume lock !
321      *
322      * In general, anything that's called with afs_xvolume locked must not
323      * end up calling getnewvnode().  The only cases I've found so far
324      * are things which try to get the volumeInode, and since we keep
325      * it in the cache...
326      */
327     code = LookupInodeByPath(afile, &volumeInode, &volumeVnode);
328 #elif defined(AFS_CACHE_VNODE_PATH)
329     volumeInode = AFS_CACHE_VOLUME_INODE;
330 #elif defined(LINUX_USE_FH)
331     code = gop_lookupname(afile, AFS_UIOSYS, 0, &dp);
332     volumeinfo_fh_type = osi_get_fh(dp, &volumeinfo_fh, &max_len);
333 #else
334     code = LookupInodeByPath(afile, &volumeInode, NULL);
335 #endif
336     if (code)
337         return code;
338 #if defined(LINUX_USE_FH)
339     tfile = osi_UFSOpen_fh(&volumeinfo_fh, volumeinfo_fh_type);
340 #else
341     tfile = afs_CFileOpen(volumeInode);
342 #endif
343     afs_CFileTruncate(tfile, 0);
344     afs_CFileClose(tfile);
345     return 0;
346 }
347
348 /*
349  * afs_InitCacheInfo
350  *
351  * Description:
352  *      Set up the given file as the AFS cache info file.
353  *
354  * Parameters:
355  *      afile : Name of the file assumed to be the cache info file
356  *              for the Cache Manager; it will be used as such.
357  * Side Effects:  This sets afs_fragsize, which is used in the cache usage 
358  *                calculations such as in afs_adjustsize()
359  *
360  * Environment:
361  *      This function is called only during initialization.  The given
362  *      file should NOT be truncated to 0 lenght; its contents descrebe
363  *      what data is really in the cache.
364  *
365  *      WARNING: data will be written to this file over time by AFS.
366  *
367  * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
368  * code.
369  *
370  */
371 int
372 afs_InitCacheInfo(register char *afile)
373 {
374     register afs_int32 code;
375     struct osi_stat tstat;
376     register struct osi_file *tfile;
377     struct afs_fheader theader;
378 #ifndef AFS_LINUX22_ENV
379     struct vnode *filevp;
380 #endif
381     int goodFile;
382
383     AFS_STATCNT(afs_InitCacheInfo);
384     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
385         osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
386 #ifdef AFS_LINUX22_ENV
387     code = osi_InitCacheInfo(afile);
388     if (code)
389         return code;
390 #else
391     code = gop_lookupname(afile, AFS_UIOSYS, 0, &filevp);
392     if (code || !filevp)
393         return ENOENT;
394     {
395 #if     defined(AFS_SUN56_ENV)
396         struct statvfs64 st;
397 #elif   defined(AFS_HPUX102_ENV)
398         struct k_statvfs st;
399 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
400         struct statvfs st;
401 #elif defined(AFS_DUX40_ENV)
402         struct nstatfs st;
403 #elif defined(AFS_DARWIN80_ENV)
404         struct vfsstatfs st;
405 #else
406         struct statfs st;
407 #endif /* SUN56 */
408
409 #if     defined(AFS_SGI_ENV)
410 #ifdef AFS_SGI65_ENV
411         VFS_STATVFS(filevp->v_vfsp, &st, NULL, code);
412         if (!code)
413 #else
414         if (!VFS_STATFS(filevp->v_vfsp, &st, NULL))
415 #endif /* AFS_SGI65_ENV */
416 #elif   defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
417         if (!VFS_STATVFS(filevp->v_vfsp, &st))
418 #elif defined(AFS_OSF_ENV)
419
420         VFS_STATFS(filevp->v_vfsp, code);
421         /* struct copy */
422         st = filevp->v_vfsp->m_stat;
423         if (code == 0)
424 #elif defined(AFS_AIX41_ENV)
425         if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
426 #elif defined(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 #elif defined(AFS_DARWIN80_ENV)
435         afs_cacheVfsp = vnode_mount(filevp);
436         if (afs_cacheVfsp && ((st = *(vfs_statfs(afs_cacheVfsp))),1))
437 #elif defined(AFS_DARWIN_ENV)
438         if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
439 #elif defined(AFS_FBSD50_ENV)
440         if (!VFS_STATFS(filevp->v_mount, &st, curthread))
441 #elif defined(AFS_XBSD_ENV)
442         if (!VFS_STATFS(filevp->v_mount, &st, curproc))
443 #else
444         if (!VFS_STATFS(filevp->v_vfsp, &st))
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 #if defined(AFS_LINUX20_ENV)
453     cacheInode = filevp->i_ino;
454     afs_cacheSBp = filevp->i_sb;
455 #elif defined(AFS_XBSD_ENV)
456     cacheInode = VTOI(filevp)->i_number;
457     cacheDev.mp = filevp->v_mount;
458     cacheDev.held_vnode = filevp;
459     vref(filevp);               /* Make sure mount point stays busy. XXX */
460 #if !defined(AFS_OBSD_ENV)
461     afs_cacheVfsp = filevp->v_vfsp;
462 #endif
463 #else
464 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
465     afs_InitDualFSCacheOps(filevp);
466 #endif
467 #ifndef AFS_CACHE_VNODE_PATH
468 #ifndef AFS_DARWIN80_ENV
469     afs_cacheVfsp = filevp->v_vfsp;
470 #endif
471     cacheInode = afs_vnodeToInumber(filevp);
472 #else
473     cacheInode = AFS_CACHE_ITEMS_INODE;
474 #endif
475     cacheDev.dev = afs_vnodeToDev(filevp);
476 #endif /* AFS_LINUX20_ENV */
477     AFS_RELE(filevp);
478 #endif /* AFS_LINUX22_ENV */
479 #if defined(LINUX_USE_FH)
480     tfile = osi_UFSOpen_fh(&cacheitems_fh, cacheitems_fh_type);
481 #else
482     tfile = osi_UFSOpen(cacheInode);
483 #endif
484     afs_osi_Stat(tfile, &tstat);
485     cacheInfoModTime = tstat.mtime;
486     code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
487     goodFile = 0;
488     if (code == sizeof(theader)) {
489         /* read the header correctly */
490         if (theader.magic == AFS_FHMAGIC
491             && theader.firstCSize == AFS_FIRSTCSIZE
492             && theader.otherCSize == AFS_OTHERCSIZE
493             && theader.version == AFS_CI_VERSION)
494             goodFile = 1;
495     }
496     if (!goodFile) {
497         /* write out a good file label */
498         theader.magic = AFS_FHMAGIC;
499         theader.firstCSize = AFS_FIRSTCSIZE;
500         theader.otherCSize = AFS_OTHERCSIZE;
501         theader.version = AFS_CI_VERSION;
502         afs_osi_Write(tfile, 0, &theader, sizeof(theader));
503         /*
504          * Truncate the rest of the file, since it may be arbitrarily
505          * wrong
506          */
507         osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
508     }
509     /* Leave the file open now, since reopening the file makes public pool
510      * vnode systems (like OSF/Alpha) much harder to handle, That's because
511      * they can do a vnode recycle operation any time we open a file, which
512      * we'd do on any afs_GetDSlot call, etc.
513      */
514     afs_cacheInodep = (struct osi_file *)tfile;
515     return 0;
516 }
517
518 int afs_resourceinit_flag = 0;
519 int
520 afs_ResourceInit(int preallocs)
521 {
522     register afs_int32 i;
523     static struct rx_securityClass *secobj;
524
525     AFS_STATCNT(afs_ResourceInit);
526     AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
527     AFS_RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
528     AFS_RWLOCK_INIT(&afs_xserver, "afs_xserver");
529     AFS_RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
530     AFS_RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
531     AFS_RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
532     LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
533 #ifndef AFS_FBSD_ENV
534     LOCK_INIT(&osi_fsplock, "osi_fsplock");
535     LOCK_INIT(&osi_flplock, "osi_flplock");
536 #endif
537     AFS_RWLOCK_INIT(&afs_xconn, "afs_xconn");
538
539     afs_CellInit();
540     afs_InitCBQueue(1);         /* initialize callback queues */
541
542     if (afs_resourceinit_flag == 0) {
543         afs_resourceinit_flag = 1;
544         for (i = 0; i < NFENTRIES; i++)
545             fvTable[i] = 0;
546         for (i = 0; i < MAXNUMSYSNAMES; i++)
547             afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
548         afs_sysname = afs_sysnamelist[0];
549         strcpy(afs_sysname, SYS_NAME);
550         afs_sysnamecount = 1;
551         afs_sysnamegen++;
552     }
553
554     secobj = rxnull_NewServerSecurityObject();
555     afs_server =
556         rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
557     afs_server =
558         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
559                       RXSTATS_ExecuteRequest);
560     rx_StartServer(0);
561     afs_osi_Wakeup(&afs_server);        /* wakeup anyone waiting for it */
562     return 0;
563
564 }                               /*afs_ResourceInit */
565
566 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
567
568 /*
569  * AIX dynamic sizeof(struct proc)
570  *
571  * AIX keeps its proc structures in an array.  The size of struct proc
572  * varies from release to release of the OS.  In order to maintain
573  * binary compatibility with releases later than what we build on, we
574  * need to determine the size of struct proc at run time.
575  *
576  * We need this in order to walk the proc[] array to do PAG garbage
577  * collection.
578  *
579  * We also need this in order to support 'klog -setpag', since the
580  * kernel code needs to locate the proc structure for the parent process
581  * of the current process.
582  *
583  * To compute sizeof(struct proc), we need the addresses of two proc
584  * structures and their corresponding pids.  Given the pids, we can use
585  * the PROCMASK() macro to compute their corresponding indices in the
586  * proc[] array.  By dividing the distance between the pointers by the
587  * number of proc structures, we can compute the size of a single proc
588  * structure.
589  *
590  * We know the base address of the proc table from v.vb_proc:
591  *
592  * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
593  * (we don't use this, but I note it here for completeness)
594  *
595  * <sys/var.h> declares struct var and external variable v;
596  *
597  * v.v_proc             NPROC
598  * v.vb_proc            &proc[0]
599  * v.ve_proc            &proc[x] (current highwater mark for
600  *                                proc[] array usage)
601  *
602  * The first proc pointer is v.vb_proc, which is the proc structure for
603  * process 0.  Process 0's pointer to its first child is the other proc
604  * pointer.  If process 0 has no children, we simply give up and do not
605  * support features that require knowing the size of struct proc.
606  */
607
608 static void
609 afs_procsize_init(void)
610 {
611     struct proc *p0;            /* pointer to process 0 */
612     struct proc *pN;            /* pointer to process 0's first child */
613 #ifdef AFS_AIX51_ENV
614     struct pvproc *pV;
615 #endif
616     int pN_index;
617     ptrdiff_t pN_offset;
618     int procsize;
619
620     p0 = (struct proc *)v.vb_proc;
621     if (!p0) {
622         afs_gcpags = AFS_GCPAGS_EPROC0;
623         return;
624     }
625 #ifdef AFS_AIX51_ENV
626     pN = (struct proc *)0;
627     pV = p0->p_pvprocp;
628     if (pV) {
629         pV = pV->pv_child;
630         if (pV)
631             pN = pV->pv_procp;
632     }
633 #else
634     pN = p0->p_child;
635 #endif
636     if (!pN) {
637         afs_gcpags = AFS_GCPAGS_EPROCN;
638         return;
639     }
640
641     if (pN->p_pid == p0->p_pid) {
642         afs_gcpags = AFS_GCPAGS_EEQPID;
643         return;
644     }
645
646     pN_index = PROCMASK(pN->p_pid);
647     pN_offset = ((char *)pN - (char *)p0);
648     procsize = pN_offset / pN_index;
649
650     /*
651      * check that the computation was exact
652      */
653
654     if (pN_index * procsize != pN_offset) {
655         afs_gcpags = AFS_GCPAGS_EINEXACT;
656         return;
657     }
658
659     /*
660      * check that the proc table size is a multiple of procsize.
661      */
662
663     if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
664         afs_gcpags = AFS_GCPAGS_EPROCEND;
665         return;
666     }
667
668     /* okay, use it */
669
670     afs_gcpags_procsize = procsize;
671 }
672 #endif
673
674 /*
675  * shutdown_cache
676  *
677  * Description:
678  *      Clean up and shut down the AFS cache.
679  *
680  * Parameters:
681  *      None.
682  *
683  * Environment:
684  *      Nothing interesting.
685  */
686 void
687 shutdown_cache(void)
688 {
689     AFS_STATCNT(shutdown_cache);
690     afs_WriteThroughDSlots();
691     if (afs_cold_shutdown) {
692         afs_cacheinit_flag = 0;
693         shutdown_dcache();
694         shutdown_vcache();
695
696         afs_cacheStats = 0;
697         afs_cacheFiles = afs_cacheBlocks = 0;
698         pag_epoch = maxIHint = nihints = usedihint = 0;
699         pagCounter = 0;
700 #if defined(AFS_XBSD_ENV)
701         vrele(volumeVnode);     /* let it go, finally. */
702         volumeVnode = NULL;
703         if (cacheDev.held_vnode) {
704             vrele(cacheDev.held_vnode);
705             cacheDev.held_vnode = NULL;
706         }
707 #endif
708 #if !defined(LINUX_USE_FH)
709         cacheInode = volumeInode = (ino_t) 0;
710 #endif
711         cacheInfoModTime = 0;
712
713         afs_fsfragsize = 1023;
714         memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
715         memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
716         osi_dnlc_shutdown();
717     }
718 }                               /*shutdown_cache */
719
720
721 void
722 shutdown_vnodeops(void)
723 {
724 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
725     struct buf *afs_bread_freebp = 0;
726 #endif
727
728
729     AFS_STATCNT(shutdown_vnodeops);
730     if (afs_cold_shutdown) {
731 #ifndef AFS_SUN5_ENV            /* XXX */
732         lastWarnTime = 0;
733 #endif
734 #ifndef AFS_LINUX20_ENV
735         afs_rd_stash_i = 0;
736 #endif
737 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
738         afs_bread_freebp = 0;
739 #endif
740         shutdown_mariner();
741     }
742 }
743
744
745 void
746 shutdown_AFS(void)
747 {
748     int i;
749     register struct srvAddr *sa;
750
751     AFS_STATCNT(shutdown_AFS);
752     if (afs_cold_shutdown) {
753         afs_resourceinit_flag = 0;
754         /* 
755          * Free Volumes table allocations 
756          */
757         {
758             struct volume *tv;
759             for (i = 0; i < NVOLS; i++) {
760                 for (tv = afs_volumes[i]; tv; tv = tv->next) {
761                     if (tv->name) {
762                         afs_osi_Free(tv->name, strlen(tv->name) + 1);
763                         tv->name = 0;
764                     }
765                 }
766                 afs_volumes[i] = 0;
767             }
768         }
769
770         /* 
771          * Free FreeVolList allocations 
772          */
773         afs_osi_Free(Initialafs_freeVolList,
774                      afs_memvolumes * sizeof(struct volume));
775         afs_freeVolList = Initialafs_freeVolList = 0;
776
777         /* XXX HACK fort MEM systems XXX 
778          *
779          * For -memcache cache managers when we run out of free in memory volumes
780          * we simply malloc more; we won't be able to free those additional volumes.
781          */
782
783
784
785         /* 
786          * Free Users table allocation 
787          */
788         {
789             struct unixuser *tu, *ntu;
790             for (i = 0; i < NUSERS; i++) {
791                 for (tu = afs_users[i]; tu; tu = ntu) {
792                     ntu = tu->next;
793                     if (tu->stp)
794                         afs_osi_Free(tu->stp, tu->stLen);
795                     if (tu->exporter)
796                         EXP_RELE(tu->exporter);
797                     afs_osi_Free(tu, sizeof(struct unixuser));
798                 }
799                 afs_users[i] = 0;
800             }
801         }
802
803         /* 
804          * Free Servers table allocation 
805          */
806         {
807             struct server *ts, *nts;
808             struct afs_conn *tc, *ntc;
809             register struct afs_cbr *tcbrp, *tbrp;
810
811             for (i = 0; i < NSERVERS; i++) {
812                 for (ts = afs_servers[i]; ts; ts = nts) {
813                     nts = ts->next;
814                     for (sa = ts->addr; sa; sa = sa->next_sa) {
815                         if (sa->conns) {
816                             /*
817                              * Free all server's connection structs
818                              */
819                             tc = sa->conns;
820                             while (tc) {
821                                 ntc = tc->next;
822                                 AFS_GUNLOCK();
823                                 rx_DestroyConnection(tc->id);
824                                 AFS_GLOCK();
825                                 afs_osi_Free(tc, sizeof(struct afs_conn));
826                                 tc = ntc;
827                             }
828                         }
829                     }
830                     for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
831                         /*
832                          * Free all server's callback structs
833                          */
834                         tbrp = tcbrp->next;
835                         afs_FreeCBR(tcbrp);
836                     }
837                     afs_osi_Free(ts, sizeof(struct server));
838                 }
839                 afs_servers[i] = 0;
840             }
841         }
842         for (i = 0; i < NFENTRIES; i++)
843             fvTable[i] = 0;
844         /* Reinitialize local globals to defaults */
845         for (i = 0; i < MAXNUMSYSNAMES; i++)
846             afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
847         afs_sysname = 0;
848         afs_sysnamecount = 0;
849         afs_marinerHost = 0;
850         afs_setTimeHost = NULL;
851         afs_volCounter = 1;
852         afs_waitForever = afs_waitForeverCount = 0;
853         afs_FVIndex = -1;
854         afs_server = (struct rx_service *)0;
855         AFS_RWLOCK_INIT(&afs_xconn, "afs_xconn");
856         memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
857         AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
858         AFS_RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
859         AFS_RWLOCK_INIT(&afs_xserver, "afs_xserver");
860         LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
861
862         shutdown_cell();
863         shutdown_server();
864     }
865 }