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