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_DEC_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 struct volume *Initialafs_freeVolList;
43 int afs_memvolumes = 0;
44 #if defined(AFS_XBSD_ENV)
45 static struct vnode *volumeVnode;
49 * Initialization order is important. Must first call afs_CacheInit,
50 * then cache file and volume file initialization routines. Next, the
51 * individual cache entry initialization routines are called.
61 * astatSize : The number of stat cache (vnode) entries to
63 * afiles : The number of disk files to allocate to the cache
64 * ablocks : The max number of 1 Kbyte blocks that all of
65 * the files in the cache may occupy.
66 * aDentries : Number of dcache entries to allocate.
67 * aVolumes : Number of volume cache entries to allocate.
68 * achunk : Power of 2 to make the chunks.
69 * aflags : Flags passed in.
70 * inodes : max inodes to pin down in inode[]
71 * users : what should size of per-user access cache be?
74 * This routine should only be called at initialization time, since
75 * it reclaims no resources and doesn't sufficiently synchronize
76 * with other processes.
79 struct cm_initparams cm_initParams;
80 static int afs_cacheinit_flag = 0;
82 afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32 ablocks,
83 afs_int32 aDentries, afs_int32 aVolumes, afs_int32 achunk,
84 afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers)
87 register struct volume *tv;
89 AFS_STATCNT(afs_CacheInit);
91 * Jot down the epoch time, namely when this incarnation of the
92 * Cache Manager started.
94 afs_stats_cmperf.epoch = pag_epoch = osi_Time();
96 afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
98 afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
99 #endif /* SYS_NAME_ID */
101 printf("Starting AFS cache scan...");
102 if (afs_cacheinit_flag)
104 afs_cacheinit_flag = 1;
105 cacheInfoModTime = 0;
110 LOCK_INIT(&afs_ftf, "afs_ftf");
111 RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
115 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
120 * We want to also reserve space for the gnode struct which is associated
121 * with each vnode (vcache) one; we want to use the pinned pool for them
122 * since they're referenced at interrupt level.
124 if (afs_stats_cmperf.SmallBlocksAlloced + astatSize < 3600)
125 preallocs = astatSize;
127 preallocs = 3600 - afs_stats_cmperf.SmallBlocksAlloced;
131 osi_AllocMoreSSpace(preallocs);
135 * create volume list structure
139 else if (aVolumes > 3000)
142 tv = (struct volume *)afs_osi_Alloc(aVolumes * sizeof(struct volume));
143 for (i = 0; i < aVolumes - 1; i++)
144 tv[i].next = &tv[i + 1];
145 tv[aVolumes - 1].next = NULL;
146 afs_freeVolList = Initialafs_freeVolList = tv;
147 afs_memvolumes = aVolumes;
149 afs_cacheFiles = afiles;
150 afs_cacheStats = astatSize;
151 afs_vcacheInit(astatSize);
152 afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
153 #ifdef AFS_64BIT_CLIENT
154 #ifdef AFS_VM_RDWR_ENV
155 afs_vmMappingEnd = AFS_CHUNKBASE(0x7fffffff);
156 #endif /* AFS_VM_RDWR_ENV */
157 #endif /* AFS_64BIT_CLIENT */
159 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
161 static void afs_procsize_init(void);
167 /* Save the initialization parameters for later pioctl queries. */
168 cm_initParams.cmi_version = CMI_VERSION;
169 cm_initParams.cmi_nChunkFiles = afiles;
170 cm_initParams.cmi_nStatCaches = astatSize;
171 cm_initParams.cmi_nDataCaches = aDentries;
172 cm_initParams.cmi_nVolumeCaches = aVolumes;
173 cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
174 cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
175 cm_initParams.cmi_cacheSize = afs_cacheBlocks;
176 cm_initParams.cmi_setTime = afs_setTime;
177 cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
185 * afs_ComputeCacheParams
188 * Set some cache parameters.
195 afs_ComputeCacheParms(void)
197 register afs_int32 i;
198 afs_int32 afs_maxCacheDirty;
201 * Don't allow more than 2/3 of the files in the cache to be dirty.
203 afs_maxCacheDirty = (2 * afs_cacheFiles) / 3;
206 * Also, don't allow more than 2/3 of the total space get filled
207 * with dirty chunks. Compute the total number of chunks required
208 * to fill the cache, make sure we don't set out limit above 2/3 of
209 * that. If the cache size is greater than 1G, avoid overflow at
210 * the expense of precision on the chunk size.
212 if (afs_cacheBlocks & 0xffe00000) {
213 i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
215 i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
218 if (afs_maxCacheDirty > i)
219 afs_maxCacheDirty = i;
220 if (afs_maxCacheDirty < 1)
221 afs_maxCacheDirty = 1;
222 afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
223 } /*afs_ComputeCacheParms */
229 * Look up inode given a file name.
230 * Optionally return the vnode too.
231 * If the vnode is not returned, we rele it.
234 LookupInodeByPath(char *filename, ino_t * inode, struct vnode **fvpp)
238 #ifdef AFS_LINUX22_ENV
240 code = gop_lookupname(filename, AFS_UIOSYS, 0, NULL, &dp);
243 *inode = dp->d_inode->i_ino;
246 struct vnode *filevp;
247 code = gop_lookupname(filename, AFS_UIOSYS, 0, NULL, &filevp);
250 *inode = afs_vnodeToInumber(filevp);
254 #if defined(AFS_DEC_ENV)
260 #endif /* AFS_LINUX22_ENV */
266 afs_InitCellInfo(char *afile)
271 code = LookupInodeByPath(afile, &inode, NULL);
272 return afs_cellname_init(inode, code);
279 * Set up the volume info storage file.
282 * afile : the file to be declared to be the volume info storage
283 * file for AFS. It must be already truncated to 0 length.
286 * This function is called only during initialization.
288 * WARNING: Data will be written to this file over time by AFS.
292 afs_InitVolumeInfo(char *afile)
295 struct osi_file *tfile;
297 AFS_STATCNT(afs_InitVolumeInfo);
298 #if defined(AFS_XBSD_ENV)
300 * On Open/Free/NetBSD, we can get into big trouble if we don't hold the volume file
301 * vnode. SetupVolume holds afs_xvolume lock exclusive.
302 * SetupVolume->GetVolSlot->UFSGetVolSlot->{GetVolCache or WriteVolCache}
303 * ->osi_UFSOpen->VFS_VGET()->ffs_vget->getnewvnode->vgone on some vnode.
304 * If it's AFS, then ->vclean->afs_nbsd_reclaim->FlushVCache->QueueVCB->
305 * GetVolume->FindVolume-> waits on afs_xvolume lock !
307 * In general, anything that's called with afs_xvolume locked must not
308 * end up calling getnewvnode(). The only cases I've found so far
309 * are things which try to get the volumeInode, and since we keep
312 code = LookupInodeByPath(afile, &volumeInode, &volumeVnode);
314 code = LookupInodeByPath(afile, &volumeInode, NULL);
318 tfile = afs_CFileOpen(volumeInode);
319 afs_CFileTruncate(tfile, 0);
320 afs_CFileClose(tfile);
328 * Set up the given file as the AFS cache info file.
331 * afile : Name of the file assumed to be the cache info file
332 * for the Cache Manager; it will be used as such.
333 * Side Effects: This sets afs_fragsize, which is used in the cache usage
334 * calculations such as in afs_adjustsize()
337 * This function is called only during initialization. The given
338 * file should NOT be truncated to 0 lenght; its contents descrebe
339 * what data is really in the cache.
341 * WARNING: data will be written to this file over time by AFS.
343 * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
348 afs_InitCacheInfo(register char *afile)
350 register afs_int32 code;
351 struct osi_stat tstat;
352 register struct osi_file *tfile;
353 struct afs_fheader theader;
354 struct vnode *filevp;
357 AFS_STATCNT(afs_InitCacheInfo);
358 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
359 osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
360 #ifdef AFS_LINUX22_ENV
361 code = osi_InitCacheInfo(afile);
365 code = gop_lookupname(afile, AFS_UIOSYS, 0, NULL, &filevp);
369 #if defined(AFS_SUN56_ENV)
371 #elif defined(AFS_HPUX102_ENV)
373 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
375 #elif defined(AFS_DUX40_ENV)
381 #if defined(AFS_SGI_ENV)
383 VFS_STATVFS(filevp->v_vfsp, &st, NULL, code);
386 if (!VFS_STATFS(filevp->v_vfsp, &st, NULL))
387 #endif /* AFS_SGI65_ENV */
388 #elif defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
389 if (!VFS_STATVFS(filevp->v_vfsp, &st))
390 #elif defined(AFS_OSF_ENV)
392 VFS_STATFS(filevp->v_vfsp, code);
394 st = filevp->v_vfsp->m_stat;
396 #elif defined(AFS_AIX41_ENV)
397 if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
398 #elif defined(AFS_LINUX20_ENV)
403 VFS_STATFS(filevp->v_vfsp, &st);
406 #elif defined(AFS_DARWIN_ENV)
407 if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
408 #elif defined(AFS_FBSD50_ENV)
409 if (!VFS_STATFS(filevp->v_mount, &st, curthread))
410 #elif defined(AFS_XBSD_ENV)
411 if (!VFS_STATFS(filevp->v_mount, &st, curproc))
413 if (!VFS_STATFS(filevp->v_vfsp, &st))
415 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
416 afs_fsfragsize = st.f_frsize - 1;
418 afs_fsfragsize = st.f_bsize - 1;
421 #if defined(AFS_LINUX20_ENV)
422 cacheInode = filevp->i_ino;
423 afs_cacheSBp = filevp->i_sb;
424 #elif defined(AFS_XBSD_ENV)
425 cacheInode = VTOI(filevp)->i_number;
426 cacheDev.mp = filevp->v_mount;
427 cacheDev.held_vnode = filevp;
428 vref(filevp); /* Make sure mount point stays busy. XXX */
429 #if !defined(AFS_OBSD_ENV)
430 afs_cacheVfsp = filevp->v_vfsp;
433 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
434 afs_InitDualFSCacheOps(filevp);
436 cacheInode = afs_vnodeToInumber(filevp);
437 cacheDev.dev = afs_vnodeToDev(filevp);
438 afs_cacheVfsp = filevp->v_vfsp;
439 #endif /* AFS_LINUX20_ENV */
441 #endif /* AFS_LINUX22_ENV */
442 tfile = osi_UFSOpen(cacheInode);
443 afs_osi_Stat(tfile, &tstat);
444 cacheInfoModTime = tstat.mtime;
445 code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
447 if (code == sizeof(theader)) {
448 /* read the header correctly */
449 if (theader.magic == AFS_FHMAGIC
450 && theader.firstCSize == AFS_FIRSTCSIZE
451 && theader.otherCSize == AFS_OTHERCSIZE
452 && theader.version == AFS_CI_VERSION)
456 /* write out a good file label */
457 theader.magic = AFS_FHMAGIC;
458 theader.firstCSize = AFS_FIRSTCSIZE;
459 theader.otherCSize = AFS_OTHERCSIZE;
460 theader.version = AFS_CI_VERSION;
461 afs_osi_Write(tfile, 0, &theader, sizeof(theader));
463 * Truncate the rest of the file, since it may be arbitrarily
466 osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
468 /* Leave the file open now, since reopening the file makes public pool
469 * vnode systems (like OSF/Alpha) much harder to handle, That's because
470 * they can do a vnode recycle operation any time we open a file, which
471 * we'd do on any afs_GetDSlot call, etc.
473 afs_cacheInodep = (struct osi_file *)tfile;
477 int afs_resourceinit_flag = 0;
479 afs_ResourceInit(int preallocs)
481 register afs_int32 i;
482 static struct rx_securityClass *secobj;
484 AFS_STATCNT(afs_ResourceInit);
485 RWLOCK_INIT(&afs_xuser, "afs_xuser");
486 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
487 RWLOCK_INIT(&afs_xserver, "afs_xserver");
488 RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
489 RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
490 RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
491 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
493 #ifndef AFS_AIX32_ENV
494 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;
512 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
515 if ((preallocs > 256) && (preallocs < 3600))
516 afs_preallocs = preallocs;
517 osi_AllocMoreSSpace(afs_preallocs);
518 osi_AllocMoreMSpace(100);
523 secobj = rxnull_NewServerSecurityObject();
525 rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
527 rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
528 RXSTATS_ExecuteRequest);
530 afs_osi_Wakeup(&afs_server); /* wakeup anyone waiting for it */
533 } /*afs_ResourceInit */
535 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
538 * AIX dynamic sizeof(struct proc)
540 * AIX keeps its proc structures in an array. The size of struct proc
541 * varies from release to release of the OS. In order to maintain
542 * binary compatibility with releases later than what we build on, we
543 * need to determine the size of struct proc at run time.
545 * We need this in order to walk the proc[] array to do PAG garbage
548 * We also need this in order to support 'klog -setpag', since the
549 * kernel code needs to locate the proc structure for the parent process
550 * of the current process.
552 * To compute sizeof(struct proc), we need the addresses of two proc
553 * structures and their corresponding pids. Given the pids, we can use
554 * the PROCMASK() macro to compute their corresponding indices in the
555 * proc[] array. By dividing the distance between the pointers by the
556 * number of proc structures, we can compute the size of a single proc
559 * We know the base address of the proc table from v.vb_proc:
561 * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
562 * (we don't use this, but I note it here for completeness)
564 * <sys/var.h> declares struct var and external variable v;
568 * v.ve_proc &proc[x] (current highwater mark for
569 * proc[] array usage)
571 * The first proc pointer is v.vb_proc, which is the proc structure for
572 * process 0. Process 0's pointer to its first child is the other proc
573 * pointer. If process 0 has no children, we simply give up and do not
574 * support features that require knowing the size of struct proc.
578 afs_procsize_init(void)
580 struct proc *p0; /* pointer to process 0 */
581 struct proc *pN; /* pointer to process 0's first child */
589 p0 = (struct proc *)v.vb_proc;
591 afs_gcpags = AFS_GCPAGS_EPROC0;
595 pN = (struct proc *)0;
606 afs_gcpags = AFS_GCPAGS_EPROCN;
610 if (pN->p_pid == p0->p_pid) {
611 afs_gcpags = AFS_GCPAGS_EEQPID;
615 pN_index = PROCMASK(pN->p_pid);
616 pN_offset = ((char *)pN - (char *)p0);
617 procsize = pN_offset / pN_index;
620 * check that the computation was exact
623 if (pN_index * procsize != pN_offset) {
624 afs_gcpags = AFS_GCPAGS_EINEXACT;
629 * check that the proc table size is a multiple of procsize.
632 if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
633 afs_gcpags = AFS_GCPAGS_EPROCEND;
639 afs_gcpags_procsize = procsize;
647 * Clean up and shut down the AFS cache.
653 * Nothing interesting.
658 AFS_STATCNT(shutdown_cache);
659 afs_WriteThroughDSlots();
660 if (afs_cold_shutdown) {
661 afs_cacheinit_flag = 0;
666 afs_cacheFiles = afs_cacheBlocks = 0;
667 pag_epoch = maxIHint = nihints = usedihint = 0;
669 #if defined(AFS_XBSD_ENV)
670 vrele(volumeVnode); /* let it go, finally. */
672 if (cacheDev.held_vnode) {
673 vrele(cacheDev.held_vnode);
674 cacheDev.held_vnode = NULL;
677 cacheInode = volumeInode = (ino_t) 0;
679 cacheInfoModTime = 0;
681 afs_fsfragsize = 1023;
682 memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
683 memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
686 } /*shutdown_cache */
690 shutdown_vnodeops(void)
692 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
693 struct buf *afs_bread_freebp = 0;
697 AFS_STATCNT(shutdown_vnodeops);
698 if (afs_cold_shutdown) {
699 #ifndef AFS_SUN5_ENV /* XXX */
702 #ifndef AFS_LINUX20_ENV
705 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
706 afs_bread_freebp = 0;
717 register struct srvAddr *sa;
719 AFS_STATCNT(shutdown_AFS);
720 if (afs_cold_shutdown) {
721 afs_resourceinit_flag = 0;
723 * Free Volumes table allocations
727 for (i = 0; i < NVOLS; i++) {
728 for (tv = afs_volumes[i]; tv; tv = tv->next) {
730 afs_osi_Free(tv->name, strlen(tv->name) + 1);
739 * Free FreeVolList allocations
741 afs_osi_Free(Initialafs_freeVolList,
742 afs_memvolumes * sizeof(struct volume));
743 afs_freeVolList = Initialafs_freeVolList = 0;
745 /* XXX HACK fort MEM systems XXX
747 * For -memcache cache managers when we run out of free in memory volumes
748 * we simply malloc more; we won't be able to free those additional volumes.
754 * Free Users table allocation
757 struct unixuser *tu, *ntu;
758 for (i = 0; i < NUSERS; i++) {
759 for (tu = afs_users[i]; tu; tu = ntu) {
762 afs_osi_Free(tu->stp, tu->stLen);
764 EXP_RELE(tu->exporter);
765 afs_osi_Free(tu, sizeof(struct unixuser));
772 * Free Servers table allocation
775 struct server *ts, *nts;
776 struct conn *tc, *ntc;
777 register struct afs_cbr *tcbrp, *tbrp;
779 for (i = 0; i < NSERVERS; i++) {
780 for (ts = afs_servers[i]; ts; ts = nts) {
782 for (sa = ts->addr; sa; sa = sa->next_sa) {
785 * Free all server's connection structs
791 rx_DestroyConnection(tc->id);
793 afs_osi_Free(tc, sizeof(struct conn));
798 for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
800 * Free all server's callback structs
805 afs_osi_Free(ts, sizeof(struct server));
810 for (i = 0; i < NFENTRIES; i++)
812 /* Reinitialize local globals to defaults */
813 for (i = 0; i < MAXNUMSYSNAMES; i++)
814 afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
816 afs_sysnamecount = 0;
818 afs_setTimeHost = NULL;
820 afs_waitForever = afs_waitForeverCount = 0;
822 afs_server = (struct rx_service *)0;
823 RWLOCK_INIT(&afs_xconn, "afs_xconn");
824 memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
825 RWLOCK_INIT(&afs_xuser, "afs_xuser");
826 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
827 RWLOCK_INIT(&afs_xserver, "afs_xserver");
828 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");