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