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