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