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