2 * Copyright 2000, International Business Machines Corporation and others.
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
11 * afs_init.c - initialize AFS client.
16 #include <afsconfig.h>
17 #include "afs/param.h"
23 #include "afs/sysincludes.h" /* Standard vendor system headers */
24 #include "afsincludes.h" /* Afs-based standard headers */
25 #include "afs/afs_stats.h" /* afs statistics */
27 /* Exported variables */
28 struct osi_dev cacheDev; /*Cache device */
29 afs_int32 cacheInfoModTime; /*Last time cache info modified */
30 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
31 struct mount *afs_cacheVfsp = 0;
32 #elif defined(AFS_LINUX20_ENV)
33 struct super_block *afs_cacheSBp = 0;
35 struct vfs *afs_cacheVfsp = 0;
37 afs_rwlock_t afs_puttofileLock; /* not used */
38 char *afs_sysname = 0; /* So that superuser may change the
39 * local value of @sys */
40 char *afs_sysnamelist[MAXNUMSYSNAMES]; /* For support of a list of sysname */
41 int afs_sysnamecount = 0;
42 int afs_sysnamegen = 0;
43 struct volume *Initialafs_freeVolList;
44 int afs_memvolumes = 0;
45 #if defined(AFS_XBSD_ENV)
46 static struct vnode *volumeVnode;
48 #if defined(AFS_DISCON_ENV)
49 afs_rwlock_t afs_discon_lock;
53 * Initialization order is important. Must first call afs_CacheInit,
54 * then cache file and volume file initialization routines. Next, the
55 * individual cache entry initialization routines are called.
65 * astatSize : The number of stat cache (vnode) entries to
67 * afiles : The number of disk files to allocate to the cache
68 * ablocks : The max number of 1 Kbyte blocks that all of
69 * the files in the cache may occupy.
70 * aDentries : Number of dcache entries to allocate.
71 * aVolumes : Number of volume cache entries to allocate.
72 * achunk : Power of 2 to make the chunks.
73 * aflags : Flags passed in.
74 * inodes : max inodes to pin down in inode[]
75 * users : what should size of per-user access cache be?
78 * This routine should only be called at initialization time, since
79 * it reclaims no resources and doesn't sufficiently synchronize
80 * with other processes.
83 struct cm_initparams cm_initParams;
84 static int afs_cacheinit_flag = 0;
86 afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
87 afs_int32 aDentries, afs_int32 aVolumes, afs_int32 achunk,
88 afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers)
91 register struct volume *tv;
93 AFS_STATCNT(afs_CacheInit);
95 * Jot down the epoch time, namely when this incarnation of the
96 * Cache Manager started.
98 afs_stats_cmperf.epoch = pag_epoch = osi_Time();
100 afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
102 afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
103 #endif /* SYS_NAME_ID */
105 printf("Starting AFS cache scan...");
106 if (afs_cacheinit_flag)
108 afs_cacheinit_flag = 1;
109 cacheInfoModTime = 0;
114 LOCK_INIT(&afs_ftf, "afs_ftf");
115 RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
116 #ifdef AFS_DISCON_ENV
117 RWLOCK_INIT(&afs_discon_lock, "afs_discon_lock");
122 * create volume list structure
126 else if (aVolumes > 3000)
129 tv = (struct volume *)afs_osi_Alloc(aVolumes * sizeof(struct volume));
130 for (i = 0; i < aVolumes - 1; i++)
131 tv[i].next = &tv[i + 1];
132 tv[aVolumes - 1].next = NULL;
133 afs_freeVolList = Initialafs_freeVolList = tv;
134 afs_memvolumes = aVolumes;
136 afs_cacheFiles = afiles;
137 afs_cacheStats = astatSize;
138 afs_vcacheInit(astatSize);
139 afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
140 #ifdef AFS_64BIT_CLIENT
141 #ifdef AFS_VM_RDWR_ENV
142 afs_vmMappingEnd = AFS_CHUNKBASE(0x7fffffff);
143 #endif /* AFS_VM_RDWR_ENV */
144 #endif /* AFS_64BIT_CLIENT */
146 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
148 static void afs_procsize_init(void);
154 /* Save the initialization parameters for later pioctl queries. */
155 cm_initParams.cmi_version = CMI_VERSION;
156 cm_initParams.cmi_nChunkFiles = afiles;
157 cm_initParams.cmi_nStatCaches = astatSize;
158 cm_initParams.cmi_nDataCaches = aDentries;
159 cm_initParams.cmi_nVolumeCaches = aVolumes;
160 cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
161 cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
162 cm_initParams.cmi_cacheSize = afs_cacheBlocks;
163 cm_initParams.cmi_setTime = afs_setTime;
164 cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
172 * afs_ComputeCacheParams
175 * Set some cache parameters.
182 afs_ComputeCacheParms(void)
184 register afs_int32 i;
185 afs_int32 afs_maxCacheDirty;
188 * Don't allow more than 2/3 of the files in the cache to be dirty.
190 afs_maxCacheDirty = (2 * afs_cacheFiles) / 3;
193 * Also, don't allow more than 2/3 of the total space get filled
194 * with dirty chunks. Compute the total number of chunks required
195 * to fill the cache, make sure we don't set out limit above 2/3 of
196 * that. If the cache size is greater than 1G, avoid overflow at
197 * the expense of precision on the chunk size.
199 if (afs_cacheBlocks & 0xffe00000) {
200 i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
202 i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
205 if (afs_maxCacheDirty > i)
206 afs_maxCacheDirty = i;
207 if (afs_maxCacheDirty < 1)
208 afs_maxCacheDirty = 1;
209 afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
210 } /*afs_ComputeCacheParms */
216 * Look up inode given a file name.
217 * Optionally return the vnode too.
218 * If the vnode is not returned, we rele it.
221 LookupInodeByPath(char *filename, ino_t * inode, struct vnode **fvpp)
225 #ifdef AFS_LINUX22_ENV
227 code = gop_lookupname(filename, AFS_UIOSYS, 0, &dp);
230 *inode = dp->d_inode->i_ino;
233 struct vnode *filevp;
234 code = gop_lookupname(filename, AFS_UIOSYS, 0, &filevp);
237 *inode = afs_vnodeToInumber(filevp);
243 #endif /* AFS_LINUX22_ENV */
249 afs_InitCellInfo(char *afile)
254 #ifdef AFS_CACHE_VNODE_PATH
255 return afs_cellname_init(AFS_CACHE_CELLS_INODE, code);
257 code = LookupInodeByPath(afile, &inode, NULL);
258 return afs_cellname_init(inode, code);
266 * Set up the volume info storage file.
269 * afile : the file to be declared to be the volume info storage
270 * file for AFS. It must be already truncated to 0 length.
273 * This function is called only during initialization.
275 * WARNING: Data will be written to this file over time by AFS.
279 afs_InitVolumeInfo(char *afile)
282 struct osi_file *tfile;
284 AFS_STATCNT(afs_InitVolumeInfo);
285 #if defined(AFS_XBSD_ENV)
287 * On Open/Free/NetBSD, we can get into big trouble if we don't hold the volume file
288 * vnode. SetupVolume holds afs_xvolume lock exclusive.
289 * SetupVolume->GetVolSlot->UFSGetVolSlot->{GetVolCache or WriteVolCache}
290 * ->osi_UFSOpen->VFS_VGET()->ffs_vget->getnewvnode->vgone on some vnode.
291 * If it's AFS, then ->vclean->afs_nbsd_reclaim->FlushVCache->QueueVCB->
292 * GetVolume->FindVolume-> waits on afs_xvolume lock !
294 * In general, anything that's called with afs_xvolume locked must not
295 * end up calling getnewvnode(). The only cases I've found so far
296 * are things which try to get the volumeInode, and since we keep
299 code = LookupInodeByPath(afile, &volumeInode, &volumeVnode);
300 #elif defined(AFS_CACHE_VNODE_PATH)
301 volumeInode = AFS_CACHE_VOLUME_INODE;
303 code = LookupInodeByPath(afile, &volumeInode, NULL);
307 tfile = afs_CFileOpen(volumeInode);
308 afs_CFileTruncate(tfile, 0);
309 afs_CFileClose(tfile);
317 * Set up the given file as the AFS cache info file.
320 * afile : Name of the file assumed to be the cache info file
321 * for the Cache Manager; it will be used as such.
322 * Side Effects: This sets afs_fragsize, which is used in the cache usage
323 * calculations such as in afs_adjustsize()
326 * This function is called only during initialization. The given
327 * file should NOT be truncated to 0 lenght; its contents descrebe
328 * what data is really in the cache.
330 * WARNING: data will be written to this file over time by AFS.
332 * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
337 afs_InitCacheInfo(register char *afile)
339 register afs_int32 code;
340 struct osi_stat tstat;
341 register struct osi_file *tfile;
342 struct afs_fheader theader;
343 #ifndef AFS_LINUX22_ENV
344 struct vnode *filevp;
348 AFS_STATCNT(afs_InitCacheInfo);
349 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
350 osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
351 #ifdef AFS_LINUX22_ENV
352 code = osi_InitCacheInfo(afile);
356 code = gop_lookupname(afile, AFS_UIOSYS, 0, &filevp);
360 #if defined(AFS_SUN56_ENV)
362 #elif defined(AFS_HPUX102_ENV)
364 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
366 #elif defined(AFS_DUX40_ENV)
368 #elif defined(AFS_DARWIN80_ENV)
374 #if defined(AFS_SGI_ENV)
376 VFS_STATVFS(filevp->v_vfsp, &st, NULL, code);
379 if (!VFS_STATFS(filevp->v_vfsp, &st, NULL))
380 #endif /* AFS_SGI65_ENV */
381 #elif defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
382 if (!VFS_STATVFS(filevp->v_vfsp, &st))
383 #elif defined(AFS_OSF_ENV)
385 VFS_STATFS(filevp->v_vfsp, code);
387 st = filevp->v_vfsp->m_stat;
389 #elif defined(AFS_AIX41_ENV)
390 if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
391 #elif defined(AFS_LINUX20_ENV)
396 VFS_STATFS(filevp->v_vfsp, &st);
399 #elif defined(AFS_DARWIN80_ENV)
400 afs_cacheVfsp = vnode_mount(filevp);
401 if (afs_cacheVfsp && ((st = *(vfs_statfs(afs_cacheVfsp))),1))
402 #elif defined(AFS_DARWIN_ENV)
403 if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
404 #elif defined(AFS_FBSD50_ENV)
405 if (!VFS_STATFS(filevp->v_mount, &st, curthread))
406 #elif defined(AFS_XBSD_ENV)
407 if (!VFS_STATFS(filevp->v_mount, &st, curproc))
409 if (!VFS_STATFS(filevp->v_vfsp, &st))
411 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
412 afs_fsfragsize = st.f_frsize - 1;
414 afs_fsfragsize = st.f_bsize - 1;
417 #if defined(AFS_LINUX20_ENV)
418 cacheInode = filevp->i_ino;
419 afs_cacheSBp = filevp->i_sb;
420 #elif defined(AFS_XBSD_ENV)
421 cacheInode = VTOI(filevp)->i_number;
422 cacheDev.mp = filevp->v_mount;
423 cacheDev.held_vnode = filevp;
424 vref(filevp); /* Make sure mount point stays busy. XXX */
425 #if !defined(AFS_OBSD_ENV)
426 afs_cacheVfsp = filevp->v_vfsp;
429 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
430 afs_InitDualFSCacheOps(filevp);
432 #ifndef AFS_CACHE_VNODE_PATH
433 #ifndef AFS_DARWIN80_ENV
434 afs_cacheVfsp = filevp->v_vfsp;
436 cacheInode = afs_vnodeToInumber(filevp);
438 cacheInode = AFS_CACHE_ITEMS_INODE;
440 cacheDev.dev = afs_vnodeToDev(filevp);
441 #endif /* AFS_LINUX20_ENV */
443 #endif /* AFS_LINUX22_ENV */
444 tfile = osi_UFSOpen(cacheInode);
445 afs_osi_Stat(tfile, &tstat);
446 cacheInfoModTime = tstat.mtime;
447 code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
449 if (code == sizeof(theader)) {
450 /* read the header correctly */
451 if (theader.magic == AFS_FHMAGIC
452 && theader.firstCSize == AFS_FIRSTCSIZE
453 && theader.otherCSize == AFS_OTHERCSIZE
454 && theader.version == AFS_CI_VERSION)
458 /* write out a good file label */
459 theader.magic = AFS_FHMAGIC;
460 theader.firstCSize = AFS_FIRSTCSIZE;
461 theader.otherCSize = AFS_OTHERCSIZE;
462 theader.version = AFS_CI_VERSION;
463 afs_osi_Write(tfile, 0, &theader, sizeof(theader));
465 * Truncate the rest of the file, since it may be arbitrarily
468 osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
470 /* Leave the file open now, since reopening the file makes public pool
471 * vnode systems (like OSF/Alpha) much harder to handle, That's because
472 * they can do a vnode recycle operation any time we open a file, which
473 * we'd do on any afs_GetDSlot call, etc.
475 afs_cacheInodep = (struct osi_file *)tfile;
479 int afs_resourceinit_flag = 0;
481 afs_ResourceInit(int preallocs)
483 register afs_int32 i;
484 static struct rx_securityClass *secobj;
486 AFS_STATCNT(afs_ResourceInit);
487 RWLOCK_INIT(&afs_xuser, "afs_xuser");
488 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
489 RWLOCK_INIT(&afs_xserver, "afs_xserver");
490 RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
491 RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
492 RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
493 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
495 LOCK_INIT(&osi_fsplock, "osi_fsplock");
496 LOCK_INIT(&osi_flplock, "osi_flplock");
498 RWLOCK_INIT(&afs_xconn, "afs_xconn");
501 afs_InitCBQueue(1); /* initialize callback queues */
503 if (afs_resourceinit_flag == 0) {
504 afs_resourceinit_flag = 1;
505 for (i = 0; i < NFENTRIES; i++)
507 for (i = 0; i < MAXNUMSYSNAMES; i++)
508 afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
509 afs_sysname = afs_sysnamelist[0];
510 strcpy(afs_sysname, SYS_NAME);
511 afs_sysnamecount = 1;
515 secobj = rxnull_NewServerSecurityObject();
517 rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
519 rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
520 RXSTATS_ExecuteRequest);
522 afs_osi_Wakeup(&afs_server); /* wakeup anyone waiting for it */
525 } /*afs_ResourceInit */
527 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
530 * AIX dynamic sizeof(struct proc)
532 * AIX keeps its proc structures in an array. The size of struct proc
533 * varies from release to release of the OS. In order to maintain
534 * binary compatibility with releases later than what we build on, we
535 * need to determine the size of struct proc at run time.
537 * We need this in order to walk the proc[] array to do PAG garbage
540 * We also need this in order to support 'klog -setpag', since the
541 * kernel code needs to locate the proc structure for the parent process
542 * of the current process.
544 * To compute sizeof(struct proc), we need the addresses of two proc
545 * structures and their corresponding pids. Given the pids, we can use
546 * the PROCMASK() macro to compute their corresponding indices in the
547 * proc[] array. By dividing the distance between the pointers by the
548 * number of proc structures, we can compute the size of a single proc
551 * We know the base address of the proc table from v.vb_proc:
553 * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
554 * (we don't use this, but I note it here for completeness)
556 * <sys/var.h> declares struct var and external variable v;
560 * v.ve_proc &proc[x] (current highwater mark for
561 * proc[] array usage)
563 * The first proc pointer is v.vb_proc, which is the proc structure for
564 * process 0. Process 0's pointer to its first child is the other proc
565 * pointer. If process 0 has no children, we simply give up and do not
566 * support features that require knowing the size of struct proc.
570 afs_procsize_init(void)
572 struct proc *p0; /* pointer to process 0 */
573 struct proc *pN; /* pointer to process 0's first child */
581 p0 = (struct proc *)v.vb_proc;
583 afs_gcpags = AFS_GCPAGS_EPROC0;
587 pN = (struct proc *)0;
598 afs_gcpags = AFS_GCPAGS_EPROCN;
602 if (pN->p_pid == p0->p_pid) {
603 afs_gcpags = AFS_GCPAGS_EEQPID;
607 pN_index = PROCMASK(pN->p_pid);
608 pN_offset = ((char *)pN - (char *)p0);
609 procsize = pN_offset / pN_index;
612 * check that the computation was exact
615 if (pN_index * procsize != pN_offset) {
616 afs_gcpags = AFS_GCPAGS_EINEXACT;
621 * check that the proc table size is a multiple of procsize.
624 if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
625 afs_gcpags = AFS_GCPAGS_EPROCEND;
631 afs_gcpags_procsize = procsize;
639 * Clean up and shut down the AFS cache.
645 * Nothing interesting.
650 AFS_STATCNT(shutdown_cache);
651 afs_WriteThroughDSlots();
652 if (afs_cold_shutdown) {
653 afs_cacheinit_flag = 0;
658 afs_cacheFiles = afs_cacheBlocks = 0;
659 pag_epoch = maxIHint = nihints = usedihint = 0;
661 #if defined(AFS_XBSD_ENV)
662 vrele(volumeVnode); /* let it go, finally. */
664 if (cacheDev.held_vnode) {
665 vrele(cacheDev.held_vnode);
666 cacheDev.held_vnode = NULL;
669 cacheInode = volumeInode = (ino_t) 0;
671 cacheInfoModTime = 0;
673 afs_fsfragsize = 1023;
674 memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
675 memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
678 } /*shutdown_cache */
682 shutdown_vnodeops(void)
684 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
685 struct buf *afs_bread_freebp = 0;
689 AFS_STATCNT(shutdown_vnodeops);
690 if (afs_cold_shutdown) {
691 #ifndef AFS_SUN5_ENV /* XXX */
694 #ifndef AFS_LINUX20_ENV
697 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV)
698 afs_bread_freebp = 0;
709 register struct srvAddr *sa;
711 AFS_STATCNT(shutdown_AFS);
712 if (afs_cold_shutdown) {
713 afs_resourceinit_flag = 0;
715 * Free Volumes table allocations
719 for (i = 0; i < NVOLS; i++) {
720 for (tv = afs_volumes[i]; tv; tv = tv->next) {
722 afs_osi_Free(tv->name, strlen(tv->name) + 1);
731 * Free FreeVolList allocations
733 afs_osi_Free(Initialafs_freeVolList,
734 afs_memvolumes * sizeof(struct volume));
735 afs_freeVolList = Initialafs_freeVolList = 0;
737 /* XXX HACK fort MEM systems XXX
739 * For -memcache cache managers when we run out of free in memory volumes
740 * we simply malloc more; we won't be able to free those additional volumes.
746 * Free Users table allocation
749 struct unixuser *tu, *ntu;
750 for (i = 0; i < NUSERS; i++) {
751 for (tu = afs_users[i]; tu; tu = ntu) {
754 afs_osi_Free(tu->stp, tu->stLen);
756 EXP_RELE(tu->exporter);
757 afs_osi_Free(tu, sizeof(struct unixuser));
764 * Free Servers table allocation
767 struct server *ts, *nts;
768 struct conn *tc, *ntc;
769 register struct afs_cbr *tcbrp, *tbrp;
771 for (i = 0; i < NSERVERS; i++) {
772 for (ts = afs_servers[i]; ts; ts = nts) {
774 for (sa = ts->addr; sa; sa = sa->next_sa) {
777 * Free all server's connection structs
783 rx_DestroyConnection(tc->id);
785 afs_osi_Free(tc, sizeof(struct conn));
790 for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
792 * Free all server's callback structs
797 afs_osi_Free(ts, sizeof(struct server));
802 for (i = 0; i < NFENTRIES; i++)
804 /* Reinitialize local globals to defaults */
805 for (i = 0; i < MAXNUMSYSNAMES; i++)
806 afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
808 afs_sysnamecount = 0;
810 afs_setTimeHost = NULL;
812 afs_waitForever = afs_waitForeverCount = 0;
814 afs_server = (struct rx_service *)0;
815 RWLOCK_INIT(&afs_xconn, "afs_xconn");
816 memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
817 RWLOCK_INIT(&afs_xuser, "afs_xuser");
818 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
819 RWLOCK_INIT(&afs_xserver, "afs_xserver");
820 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");