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