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