Use f_bsize for ZFS afs_fsfragsize
[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
20 #include "afs/stds.h"
21 #include "afs/sysincludes.h"    /* Standard vendor system headers */
22 #include "afsincludes.h"        /* Afs-based standard headers */
23 #include "afs/afs_stats.h"      /* afs statistics */
24 #include "rx/rxstat.h"
25
26 #define FSINT_COMMON_XG
27 #include "afs/afscbint.h"
28
29 /* Exported variables */
30 struct osi_dev cacheDev;        /*Cache device */
31 afs_int32 cacheInfoModTime;     /*Last time cache info modified */
32 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
33 struct mount *afs_cacheVfsp = 0;
34 #elif defined(AFS_LINUX20_ENV)
35 struct super_block *afs_cacheSBp = 0;
36 #else
37 struct vfs *afs_cacheVfsp = 0;
38 #endif
39 afs_rwlock_t afs_puttofileLock; /* not used */
40 char *afs_sysname = 0;          /* So that superuser may change the
41                                  * local value of @sys */
42 char *afs_sysnamelist[MAXNUMSYSNAMES];  /* For support of a list of sysname */
43 int afs_sysnamecount = 0;
44 int afs_sysnamegen = 0;
45 struct volume *Initialafs_freeVolList;
46 int afs_memvolumes = 0;
47 #if defined(AFS_XBSD_ENV)
48 static struct vnode *volumeVnode;
49 #endif
50 #if defined(AFS_DISCON_ENV)
51 afs_rwlock_t afs_discon_lock;
52 extern afs_rwlock_t afs_disconDirtyLock;
53 #endif
54
55 /* This is the kernel side of the dynamic vcache setting */
56 int afsd_dynamic_vcaches = 0;   /* Enable dynamic-vcache support */
57
58 /*
59  * Initialization order is important.  Must first call afs_CacheInit,
60  * then cache file and volume file initialization routines.  Next, the
61  * individual cache entry initialization routines are called.
62  */
63
64
65 /*
66  * afs_CacheInit
67  *
68  * Description:
69  *
70  * Parameters:
71  *      astatSize : The number of stat cache (vnode) entries to
72  *                  allocate.
73  *      afiles    : The number of disk files to allocate to the cache
74  *      ablocks   : The max number of 1 Kbyte blocks that all of
75  *                  the files in the cache may occupy.
76  *      aDentries : Number of dcache entries to allocate.
77  *      aVolumes  : Number of volume cache entries to allocate.
78  *      achunk    : Power of 2 to make the chunks.
79  *      aflags    : Flags passed in.
80  *      inodes    : max inodes to pin down in inode[]
81  *      users     : what should size of per-user access cache be?
82  *
83  * Environment:
84  *      This routine should only be called at initialization time, since
85  *      it reclaims no resources and doesn't sufficiently synchronize
86  *      with other processes.
87  */
88
89 struct cm_initparams cm_initParams;
90 static int afs_cacheinit_flag = 0;
91 int
92 afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
93               afs_int32 aDentries, afs_int32 aVolumes, afs_int32 achunk,
94               afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers, afs_int32 dynamic_vcaches)
95 {
96     register afs_int32 i;
97     register struct volume *tv;
98
99     AFS_STATCNT(afs_CacheInit);
100     /*
101      * Jot down the epoch time, namely when this incarnation of the
102      * Cache Manager started.
103      */
104     afs_stats_cmperf.epoch = pag_epoch = osi_Time();
105 #ifdef SYS_NAME_ID
106     afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
107 #else
108     afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
109 #endif /* SYS_NAME_ID */
110
111 #ifdef AFS_MAXVCOUNT_ENV
112         afsd_dynamic_vcaches = dynamic_vcaches;
113     printf("%s dynamically allocated vcaches\n", ( afsd_dynamic_vcaches ? "enabling" : "disabling" ));
114 #else
115         afsd_dynamic_vcaches = 0;
116 #endif
117
118     printf("Starting AFS cache scan...");
119     if (afs_cacheinit_flag)
120         return 0;
121     afs_cacheinit_flag = 1;
122     cacheInfoModTime = 0;
123     maxIHint = ninodes;
124     nihints = 0;
125     usedihint = 0;
126
127     LOCK_INIT(&afs_ftf, "afs_ftf");
128     AFS_RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
129 #ifdef AFS_DISCON_ENV
130     AFS_RWLOCK_INIT(&afs_discon_lock, "afs_discon_lock");
131     AFS_RWLOCK_INIT(&afs_disconDirtyLock, "afs_disconDirtyLock");
132     QInit(&afs_disconDirty);
133     QInit(&afs_disconShadow);
134 #endif
135     osi_dnlc_init();
136
137     /* 
138      * create volume list structure 
139      */
140     if (aVolumes < 50)
141         aVolumes = 50;
142     else if (aVolumes > 32767)
143         aVolumes = 32767;
144
145     tv = (struct volume *)afs_osi_Alloc(aVolumes * sizeof(struct volume));
146     for (i = 0; i < aVolumes - 1; i++)
147         tv[i].next = &tv[i + 1];
148     tv[aVolumes - 1].next = NULL;
149     afs_freeVolList = Initialafs_freeVolList = tv;
150     afs_memvolumes = aVolumes;
151
152     afs_cacheFiles = afiles;
153     afs_cacheStats = astatSize;
154     afs_vcacheInit(astatSize);
155     afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
156 #ifdef AFS_64BIT_CLIENT
157 #ifdef AFS_VM_RDWR_ENV
158     afs_vmMappingEnd = AFS_CHUNKBASE(0x7fffffff);
159 #endif /* AFS_VM_RDWR_ENV */
160 #endif /* AFS_64BIT_CLIENT */
161
162 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
163     {
164         static void afs_procsize_init(void);
165
166         afs_procsize_init();
167     }
168 #endif
169
170     /* Save the initialization parameters for later pioctl queries. */
171     cm_initParams.cmi_version = CMI_VERSION;
172     cm_initParams.cmi_nChunkFiles = afiles;
173     cm_initParams.cmi_nStatCaches = astatSize;
174     cm_initParams.cmi_nDataCaches = aDentries;
175     cm_initParams.cmi_nVolumeCaches = aVolumes;
176     cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
177     cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
178     cm_initParams.cmi_cacheSize = afs_cacheBlocks;
179     cm_initParams.cmi_setTime = afs_setTime;
180     cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
181
182     return 0;
183
184 }                               /*afs_CacheInit */
185
186
187 /*
188   * afs_ComputeCacheParams
189   *
190   * Description:
191   *     Set some cache parameters.
192   *
193   * Parameters:
194   *     None.
195   */
196
197 void
198 afs_ComputeCacheParms(void)
199 {
200     register afs_int32 i;
201     afs_int32 afs_maxCacheDirty;
202
203     /*
204      * Don't allow more than 2/3 of the files in the cache to be dirty.
205      */
206     afs_maxCacheDirty = (2 * afs_cacheFiles) / 3;
207
208     /*
209      * Also, don't allow more than 2/3 of the total space get filled
210      * with dirty chunks.  Compute the total number of chunks required
211      * to fill the cache, make sure we don't set out limit above 2/3 of
212      * that. If the cache size is greater than 1G, avoid overflow at
213      * the expense of precision on the chunk size.
214      */
215     if (afs_cacheBlocks & 0xffe00000) {
216         i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
217     } else {
218         i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
219     }
220     i = (2 * i) / 3;
221     if (afs_maxCacheDirty > i)
222         afs_maxCacheDirty = i;
223     if (afs_maxCacheDirty < 1)
224         afs_maxCacheDirty = 1;
225     afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
226 }                               /*afs_ComputeCacheParms */
227
228
229 /*
230  * afs_LookupInodeByPath
231  *
232  * Look up inode given a file name.
233  * Optionally return the vnode too.
234  * If the vnode is not returned, we rele it.
235  */
236 int
237 afs_LookupInodeByPath(char *filename, afs_ufs_dcache_id_t *inode, struct vnode **fvpp)
238 {
239     afs_int32 code;
240
241 #if defined(AFS_LINUX22_ENV)
242     struct dentry *dp;
243     code = gop_lookupname(filename, AFS_UIOSYS, 0, &dp);
244     if (code)
245         return code;
246     osi_get_fh(dp, inode);
247     dput(dp);
248 #else
249     struct vnode *filevp;
250     code = gop_lookupname(filename, AFS_UIOSYS, 0, &filevp);
251     if (code)
252         return code;
253     *inode = afs_vnodeToInumber(filevp);
254     if (fvpp)
255         *fvpp = filevp;
256     else {
257         AFS_RELE(filevp);
258     }
259 #endif
260
261     return 0;
262 }
263
264 int
265 afs_InitCellInfo(char *afile)
266 {
267     afs_dcache_id_t inode;
268     int code = 0;
269     
270 #ifdef AFS_CACHE_VNODE_PATH
271     inode.ufs = AFS_CACHE_CELLS_INODE;
272 #else
273     code = afs_LookupInodeByPath(afile, &inode.ufs, NULL);
274 #endif
275     return afs_cellname_init(&inode, code);
276 }
277
278 /*
279  * afs_InitVolumeInfo
280  *
281  * Description:
282  *      Set up the volume info storage file.
283  *
284  * Parameters:
285  *      afile : the file to be declared to be the volume info storage
286  *              file for AFS.  It must be already truncated to 0 length.
287  *
288  * Environment:
289  *      This function is called only during initialization.
290  *
291  *      WARNING: Data will be written to this file over time by AFS.
292  */
293
294 int
295 afs_InitVolumeInfo(char *afile)
296 {
297     int code = 0;
298     struct osi_file *tfile;
299
300     AFS_STATCNT(afs_InitVolumeInfo);
301 #if defined(AFS_XBSD_ENV)
302     /*
303      * On Open/Free/NetBSD, we can get into big trouble if we don't hold the volume file
304      * vnode.  SetupVolume holds afs_xvolume lock exclusive.
305      * SetupVolume->GetVolSlot->UFSGetVolSlot->{GetVolCache or WriteVolCache}
306      * ->osi_UFSOpen->VFS_VGET()->ffs_vget->getnewvnode->vgone on some vnode.
307      * If it's AFS, then ->vclean->afs_nbsd_reclaim->FlushVCache->QueueVCB->
308      * GetVolume->FindVolume-> waits on afs_xvolume lock !
309      *
310      * In general, anything that's called with afs_xvolume locked must not
311      * end up calling getnewvnode().  The only cases I've found so far
312      * are things which try to get the volumeInode, and since we keep
313      * it in the cache...
314      */
315     code = afs_LookupInodeByPath(afile, &volumeInode.ufs, &volumeVnode);
316 #elif defined(AFS_CACHE_VNODE_PATH)
317     volumeInode.ufs = AFS_CACHE_VOLUME_INODE;
318 #else
319     code = afs_LookupInodeByPath(afile, &volumeInode.ufs, NULL);
320 #endif
321     if (code)
322         return code;
323     tfile = afs_CFileOpen(&volumeInode);
324     afs_CFileTruncate(tfile, 0);
325     afs_CFileClose(tfile);
326     return 0;
327 }
328
329 /*
330  * afs_InitCacheInfo
331  *
332  * Description:
333  *      Set up the given file as the AFS cache info file.
334  *
335  * Parameters:
336  *      afile : Name of the file assumed to be the cache info file
337  *              for the Cache Manager; it will be used as such.
338  * Side Effects:  This sets afs_fragsize, which is used in the cache usage 
339  *                calculations such as in afs_adjustsize()
340  *
341  * Environment:
342  *      This function is called only during initialization.  The given
343  *      file should NOT be truncated to 0 lenght; its contents descrebe
344  *      what data is really in the cache.
345  *
346  *      WARNING: data will be written to this file over time by AFS.
347  *
348  * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
349  * code.
350  *
351  */
352 int
353 afs_InitCacheInfo(register char *afile)
354 {
355     register afs_int32 code;
356     struct osi_stat tstat;
357     register struct osi_file *tfile;
358     struct afs_fheader theader;
359 #ifndef AFS_LINUX22_ENV
360     struct vnode *filevp;
361 #endif
362     int goodFile;
363
364     AFS_STATCNT(afs_InitCacheInfo);
365     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
366         osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
367 #ifdef AFS_LINUX22_ENV
368     code = osi_InitCacheInfo(afile);
369     if (code)
370         return code;
371 #else
372     code = gop_lookupname(afile, AFS_UIOSYS, 0, &filevp);
373     if (code || !filevp)
374         return ENOENT;
375     {
376 #if     defined(AFS_SUN56_ENV)
377         struct statvfs64 st;
378 #elif   defined(AFS_HPUX102_ENV)
379         struct k_statvfs st;
380 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
381         struct statvfs st;
382 #elif defined(AFS_DUX40_ENV)
383         struct nstatfs st;
384 #elif defined(AFS_DARWIN80_ENV)
385         struct vfsstatfs st;
386 #else
387         struct statfs st;
388 #endif /* SUN56 */
389
390 #if     defined(AFS_SGI_ENV)
391 #ifdef AFS_SGI65_ENV
392         VFS_STATVFS(filevp->v_vfsp, &st, NULL, code);
393         if (!code)
394 #else
395         if (!VFS_STATFS(filevp->v_vfsp, &st, NULL))
396 #endif /* AFS_SGI65_ENV */
397 #elif   defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
398         if (!VFS_STATVFS(filevp->v_vfsp, &st))
399 #elif defined(AFS_OSF_ENV)
400
401         VFS_STATFS(filevp->v_vfsp, code);
402         /* struct copy */
403         st = filevp->v_vfsp->m_stat;
404         if (code == 0)
405 #elif defined(AFS_AIX41_ENV)
406         if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
407 #elif defined(AFS_LINUX20_ENV)
408         {
409             KERNEL_SPACE_DECL;
410             TO_USER_SPACE();
411
412             VFS_STATFS(filevp->v_vfsp, &st);
413             TO_KERNEL_SPACE();
414         }
415 #elif defined(AFS_DARWIN80_ENV)
416         afs_cacheVfsp = vnode_mount(filevp);
417         if (afs_cacheVfsp && ((st = *(vfs_statfs(afs_cacheVfsp))),1))
418 #elif defined(AFS_DARWIN_ENV)
419         if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
420 #elif defined(AFS_FBSD50_ENV)
421         if (!VFS_STATFS(filevp->v_mount, &st, curthread))
422 #elif defined(AFS_XBSD_ENV)
423         if (!VFS_STATFS(filevp->v_mount, &st, curproc))
424 #else
425         if (!VFS_STATFS(filevp->v_vfsp, &st))
426 #endif /* SGI... */
427 #if     defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
428             if (strcmp("zfs", st.f_basetype) == 0) {
429                 /*
430                  * Files in ZFS can take up to around the next
431                  * recordsize boundary after being truncated. recordsize
432                  * is reported in statvfs by f_bsize, so use that
433                  * instead.
434                  */
435                 afs_fsfragsize = st.f_bsize - 1;
436             } else {
437                 afs_fsfragsize = st.f_frsize - 1;
438             }
439 #else
440             afs_fsfragsize = st.f_bsize - 1;
441 #endif
442     }
443 #if defined(AFS_LINUX20_ENV)
444     cacheInode.ufs = filevp->i_ino;
445     afs_cacheSBp = filevp->i_sb;
446 #elif defined(AFS_XBSD_ENV)
447     cacheInode.ufs = VTOI(filevp)->i_number;
448     cacheDev.mp = filevp->v_mount;
449     cacheDev.held_vnode = filevp;
450     vref(filevp);               /* Make sure mount point stays busy. XXX */
451 #if !defined(AFS_OBSD_ENV)
452     afs_cacheVfsp = filevp->v_vfsp;
453 #endif
454 #else
455 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
456     afs_InitDualFSCacheOps(filevp);
457 #endif
458 #ifndef AFS_CACHE_VNODE_PATH
459 #ifndef AFS_DARWIN80_ENV
460     afs_cacheVfsp = filevp->v_vfsp;
461 #endif
462     cacheInode.ufs = afs_vnodeToInumber(filevp);
463 #else
464     cacheInode.ufs = AFS_CACHE_ITEMS_INODE;
465 #endif
466     cacheDev.dev = afs_vnodeToDev(filevp);
467 #endif /* AFS_LINUX20_ENV */
468     AFS_RELE(filevp);
469 #endif /* AFS_LINUX22_ENV */
470     if (afs_fsfragsize < AFS_MIN_FRAGSIZE) {
471         afs_fsfragsize = AFS_MIN_FRAGSIZE;
472     }
473     tfile = osi_UFSOpen(&cacheInode);
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     AFS_PROC *p0;               /* pointer to process 0 */
602     AFS_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 = (AFS_PROC *)v.vb_proc;
611     if (!p0) {
612         afs_gcpags = AFS_GCPAGS_EPROC0;
613         return;
614     }
615 #ifdef AFS_AIX51_ENV
616     pN = NULL;
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         afs_reset_inode(&cacheInode);
699         afs_reset_inode(&volumeInode);
700         cacheInfoModTime = 0;
701
702         afs_fsfragsize = 1023;
703         memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
704         memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
705         osi_dnlc_shutdown();
706     }
707 }                               /*shutdown_cache */
708
709
710 void
711 shutdown_vnodeops(void)
712 {
713 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
714     struct buf *afs_bread_freebp = 0;
715 #endif
716
717
718     AFS_STATCNT(shutdown_vnodeops);
719     if (afs_cold_shutdown) {
720 #ifndef AFS_SUN5_ENV            /* XXX */
721         lastWarnTime = 0;
722 #endif
723 #ifndef AFS_LINUX20_ENV
724         afs_rd_stash_i = 0;
725 #endif
726 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
727         afs_bread_freebp = 0;
728 #endif
729         shutdown_mariner();
730     }
731 }
732
733
734 void
735 shutdown_AFS(void)
736 {
737     int i;
738     register struct srvAddr *sa;
739
740     AFS_STATCNT(shutdown_AFS);
741     if (afs_cold_shutdown) {
742         afs_resourceinit_flag = 0;
743         /* 
744          * Free Volumes table allocations 
745          */
746         {
747             struct volume *tv;
748             for (i = 0; i < NVOLS; i++) {
749                 for (tv = afs_volumes[i]; tv; tv = tv->next) {
750                     if (tv->name) {
751                         afs_osi_Free(tv->name, strlen(tv->name) + 1);
752                         tv->name = 0;
753                     }
754                 }
755                 afs_volumes[i] = 0;
756             }
757         }
758
759         /* 
760          * Free FreeVolList allocations 
761          */
762         afs_osi_Free(Initialafs_freeVolList,
763                      afs_memvolumes * sizeof(struct volume));
764         afs_freeVolList = Initialafs_freeVolList = 0;
765
766         /* XXX HACK fort MEM systems XXX 
767          *
768          * For -memcache cache managers when we run out of free in memory volumes
769          * we simply malloc more; we won't be able to free those additional volumes.
770          */
771
772
773
774         /* 
775          * Free Users table allocation 
776          */
777         {
778             struct unixuser *tu, *ntu;
779             for (i = 0; i < NUSERS; i++) {
780                 for (tu = afs_users[i]; tu; tu = ntu) {
781                     ntu = tu->next;
782                     if (tu->stp)
783                         afs_osi_Free(tu->stp, tu->stLen);
784                     if (tu->exporter)
785                         EXP_RELE(tu->exporter);
786                     afs_osi_Free(tu, sizeof(struct unixuser));
787                 }
788                 afs_users[i] = 0;
789             }
790         }
791
792         /* 
793          * Free Servers table allocation 
794          */
795         {
796             struct server *ts, *nts;
797             struct afs_conn *tc, *ntc;
798             register struct afs_cbr *tcbrp, *tbrp;
799
800             for (i = 0; i < NSERVERS; i++) {
801                 for (ts = afs_servers[i]; ts; ts = nts) {
802                     nts = ts->next;
803                     for (sa = ts->addr; sa; sa = sa->next_sa) {
804                         if (sa->conns) {
805                             /*
806                              * Free all server's connection structs
807                              */
808                             tc = sa->conns;
809                             while (tc) {
810                                 ntc = tc->next;
811                                 AFS_GUNLOCK();
812                                 rx_DestroyConnection(tc->id);
813                                 AFS_GLOCK();
814                                 afs_osi_Free(tc, sizeof(struct afs_conn));
815                                 tc = ntc;
816                             }
817                         }
818                     }
819                     for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
820                         /*
821                          * Free all server's callback structs
822                          */
823                         tbrp = tcbrp->next;
824                         afs_FreeCBR(tcbrp);
825                     }
826                     afs_osi_Free(ts, sizeof(struct server));
827                 }
828                 afs_servers[i] = 0;
829             }
830         }
831         for (i = 0; i < NFENTRIES; i++)
832             fvTable[i] = 0;
833         /* Reinitialize local globals to defaults */
834         for (i = 0; i < MAXNUMSYSNAMES; i++)
835             afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
836         afs_sysname = 0;
837         afs_sysnamecount = 0;
838         afs_marinerHost = 0;
839         afs_setTimeHost = NULL;
840         afs_volCounter = 1;
841         afs_waitForever = afs_waitForeverCount = 0;
842         afs_FVIndex = -1;
843         afs_server = (struct rx_service *)0;
844         AFS_RWLOCK_INIT(&afs_xconn, "afs_xconn");
845         memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
846         AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
847         AFS_RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
848         AFS_RWLOCK_INIT(&afs_xserver, "afs_xserver");
849         LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
850
851         shutdown_cell();
852         shutdown_server();
853     }
854 }