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"
21 #include "../afs/stds.h"
22 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include "../afs/afsincludes.h" /* Afs-based standard headers */
24 #include "../afs/afs_stats.h" /* afs statistics */
26 /* Exported variables */
27 struct osi_dev cacheDev; /*Cache device*/
28 afs_int32 cacheInfoModTime; /*Last time cache info modified*/
29 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
30 struct mount *afs_cacheVfsp=0;
31 #elif defined(AFS_LINUX20_ENV)
32 struct super_block *afs_cacheSBp = 0;
34 struct vfs *afs_cacheVfsp=0;
36 afs_rwlock_t afs_puttofileLock; /* not used */
37 char *afs_sysname = 0; /* So that superuser may change the
38 * local value of @sys */
39 char *afs_sysnamelist[MAXNUMSYSNAMES]; /* For support of a list of sysname */
40 int afs_sysnamecount = 0;
41 struct volume *Initialafs_freeVolList;
42 int afs_memvolumes = 0;
45 * Initialization order is important. Must first call afs_CacheInit,
46 * then cache file and volume file initialization routines. Next, the
47 * individual cache entry initialization routines are called.
57 * astatSize : The number of stat cache (vnode) entries to
59 * afiles : The number of disk files to allocate to the cache
60 * ablocks : The max number of 1 Kbyte blocks that all of
61 * the files in the cache may occupy.
62 * aDentries : Number of dcache entries to allocate.
63 * aVolumes : Number of volume cache entries to allocate.
64 * achunk : Power of 2 to make the chunks.
65 * aflags : Flags passed in.
66 * inodes : max inodes to pin down in inode[]
67 * users : what should size of per-user access cache be?
70 * This routine should only be called at initialization time, since
71 * it reclaims no resources and doesn't sufficiently synchronize
72 * with other processes.
75 struct cm_initparams cm_initParams;
76 static int afs_cacheinit_flag = 0;
77 int afs_CacheInit(afs_int32 astatSize, afs_int32 afiles, afs_int32
78 ablocks, afs_int32 aDentries, afs_int32 aVolumes, afs_int32 achunk,
79 afs_int32 aflags, afs_int32 ninodes, afs_int32 nusers)
82 register struct volume *tv;
84 AFS_STATCNT(afs_CacheInit);
86 * Jot down the epoch time, namely when this incarnation of the
87 * Cache Manager started.
89 afs_stats_cmperf.epoch = pag_epoch = osi_Time();
91 afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
93 afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
94 #endif /* SYS_NAME_ID */
96 printf("Starting AFS cache scan...");
97 if (afs_cacheinit_flag)
99 afs_cacheinit_flag = 1;
100 cacheInfoModTime = 0;
105 LOCK_INIT(&afs_ftf, "afs_ftf");
106 RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
110 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
115 * We want to also reserve space for the gnode struct which is associated
116 * with each vnode (vcache) one; we want to use the pinned pool for them
117 * since they're referenced at interrupt level.
119 if (afs_stats_cmperf.SmallBlocksAlloced + astatSize < 3600)
120 preallocs = astatSize;
122 preallocs = 3600 - afs_stats_cmperf.SmallBlocksAlloced;
123 if (preallocs <= 0) preallocs = 10;
125 osi_AllocMoreSSpace(preallocs);
129 * create volume list structure
131 if (aVolumes < 50) aVolumes = 50;
132 else if (aVolumes > 3000) aVolumes = 3000;
134 tv = (struct volume *) afs_osi_Alloc(aVolumes * sizeof(struct volume));
135 for (i=0;i<aVolumes-1;i++)
136 tv[i].next = &tv[i+1];
137 tv[aVolumes-1].next = NULL;
138 afs_freeVolList = Initialafs_freeVolList = tv;
139 afs_memvolumes = aVolumes;
141 afs_cacheFiles = afiles;
142 afs_cacheStats = astatSize;
143 afs_vcacheInit(astatSize);
144 afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
145 #ifdef AFS_64BIT_CLIENT
146 #ifdef AFS_VM_RDWR_ENV
147 afs_vmMappingEnd = AFS_CHUNKBASE(0x7fffffff);
148 #endif /* AFS_VM_RDWR_ENV */
149 #endif /* AFS_64BIT_CLIENT */
151 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
153 static void afs_procsize_init(void);
159 /* Save the initialization parameters for later pioctl queries. */
160 cm_initParams.cmi_version = CMI_VERSION;
161 cm_initParams.cmi_nChunkFiles = afiles;
162 cm_initParams.cmi_nStatCaches = astatSize;
163 cm_initParams.cmi_nDataCaches = aDentries;
164 cm_initParams.cmi_nVolumeCaches = aVolumes;
165 cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
166 cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
167 cm_initParams.cmi_cacheSize = afs_cacheBlocks;
168 cm_initParams.cmi_setTime = afs_setTime;
169 cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
177 * afs_ComputeCacheParams
180 * Set some cache parameters.
186 void afs_ComputeCacheParms(void)
188 register afs_int32 i;
189 afs_int32 afs_maxCacheDirty;
192 * Don't allow more than 2/3 of the files in the cache to be dirty.
194 afs_maxCacheDirty = (2*afs_cacheFiles) / 3;
197 * Also, don't allow more than 2/3 of the total space get filled
198 * with dirty chunks. Compute the total number of chunks required
199 * to fill the cache, make sure we don't set out limit above 2/3 of
200 * that. If the cache size is greater than 1G, avoid overflow at
201 * the expense of precision on the chunk size.
203 if (afs_cacheBlocks & 0xffe00000) {
204 i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
207 i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
210 if (afs_maxCacheDirty > i)
211 afs_maxCacheDirty = i;
212 if (afs_maxCacheDirty < 1)
213 afs_maxCacheDirty = 1;
214 afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
215 } /*afs_ComputeCacheParms*/
222 * Set up the volume info storage file.
225 * afile : the file to be declared to be the volume info storage
226 * file for AFS. It must be already truncated to 0 length.
229 * This function is called only during initialization.
231 * WARNING: Data will be written to this file over time by AFS.
234 static int LookupInodeByPath(char *filename, ino_t *inode)
238 #ifdef AFS_LINUX22_ENV
240 code = gop_lookupname(filename, AFS_UIOSYS, 0, NULL, &dp);
241 if (code) return code;
242 *inode = dp->d_inode->i_ino;
245 struct vnode *filevp;
246 code = gop_lookupname(filename, AFS_UIOSYS, 0, NULL, &filevp);
247 if (code) return code;
248 *inode = afs_vnodeToInumber(filevp);
252 AFS_RELE((struct vnode *)filevp);
254 #endif /* AFS_LINUX22_ENV */
259 int afs_InitCellInfo(char *afile)
264 code = LookupInodeByPath(afile, &inode);
265 return afs_cellname_init(inode, code);
268 int afs_InitVolumeInfo(char *afile)
271 struct osi_file *tfile;
273 AFS_STATCNT(afs_InitVolumeInfo);
274 code = LookupInodeByPath(afile, &volumeInode);
275 if (code) return code;
276 tfile = afs_CFileOpen(volumeInode);
277 afs_CFileTruncate(tfile, 0);
278 afs_CFileClose(tfile);
286 * Set up the given file as the AFS cache info file.
289 * afile : Name of the file assumed to be the cache info file
290 * for the Cache Manager; it will be used as such.
291 * Side Effects: This sets afs_fragsize, which is used in the cache usage
292 * calculations such as in afs_adjustsize()
295 * This function is called only during initialization. The given
296 * file should NOT be truncated to 0 lenght; its contents descrebe
297 * what data is really in the cache.
299 * WARNING: data will be written to this file over time by AFS.
301 * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
305 int afs_InitCacheInfo(register char *afile)
307 register afs_int32 code;
308 struct osi_stat tstat;
309 register struct osi_file *tfile;
310 struct afs_fheader theader;
311 struct vnode *filevp;
314 AFS_STATCNT(afs_InitCacheInfo);
315 if(cacheDiskType != AFS_FCACHE_TYPE_UFS)
316 osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
317 #ifdef AFS_LINUX22_ENV
318 code = osi_InitCacheInfo(afile);
319 if (code) return code;
321 code = gop_lookupname(afile, AFS_UIOSYS, 0, NULL, &filevp);
322 if (code || !filevp) return ENOENT;
324 #if defined(AFS_SUN56_ENV)
327 #if defined(AFS_HPUX102_ENV)
330 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
333 #if defined(AFS_DUX40_ENV)
338 #endif /* SUN5 SGI */
339 #endif /* HP 10.20 */
342 #if defined(AFS_SGI_ENV)
344 VFS_STATVFS(filevp->v_vfsp, &st, NULL, code);
347 if (!VFS_STATFS(filevp->v_vfsp, &st, NULL))
348 #endif /* AFS_SGI65_ENV */
349 #else /* AFS_SGI_ENV */
350 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
351 if (!VFS_STATVFS(filevp->v_vfsp, &st))
355 VFS_STATFS(filevp->v_vfsp, code);
357 st = filevp->v_vfsp->m_stat;
359 #else /* AFS_OSF_ENV */
361 if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
363 #ifdef AFS_LINUX20_ENV
368 VFS_STATFS(filevp->v_vfsp, &st);
372 #if defined(AFS_DARWIN_ENV)
373 if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
375 #if defined(AFS_XBSD_ENV)
376 if (!VFS_STATFS(filevp->v_mount, &st, curproc))
378 if (!VFS_STATFS(filevp->v_vfsp, &st))
379 #endif /* AFS_XBSD_ENV */
380 #endif /* AFS_DARWIN_ENV */
381 #endif /* AFS_LINUX20_ENV */
384 #endif /* SUN5 HP10 */
386 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
387 afs_fsfragsize = st.f_frsize - 1;
389 afs_fsfragsize = st.f_bsize - 1;
392 #ifdef AFS_LINUX20_ENV
393 cacheInode = filevp->i_ino;
394 afs_cacheSBp = filevp->i_sb;
397 cacheInode = VTOI(filevp)->i_number;
398 cacheDev.mp = filevp->v_mount;
399 cacheDev.held_vnode = filevp;
400 AFS_HOLD(filevp); /* Make sure mount point stays busy. XXX */
402 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
403 afs_InitDualFSCacheOps(filevp);
405 cacheInode = afs_vnodeToInumber(filevp);
406 cacheDev.dev = afs_vnodeToDev(filevp);
407 afs_cacheVfsp = filevp->v_vfsp;
408 #endif /* AFS_OBSD_ENV */
409 #endif /* AFS_LINUX20_ENV */
410 AFS_RELE((struct vnode *)filevp);
411 #endif /* AFS_LINUX22_ENV */
412 tfile = osi_UFSOpen(cacheInode);
413 afs_osi_Stat(tfile, &tstat);
414 cacheInfoModTime = tstat.mtime;
415 code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
417 if (code == sizeof(theader)) {
418 /* read the header correctly */
419 if (theader.magic == AFS_FHMAGIC &&
420 theader.firstCSize == AFS_FIRSTCSIZE &&
421 theader.otherCSize == AFS_OTHERCSIZE &&
422 theader.version == AFS_CI_VERSION
427 /* write out a good file label */
428 theader.magic = AFS_FHMAGIC;
429 theader.firstCSize = AFS_FIRSTCSIZE;
430 theader.otherCSize = AFS_OTHERCSIZE;
431 theader.version = AFS_CI_VERSION;
432 afs_osi_Write(tfile, 0, &theader, sizeof(theader));
434 * Truncate the rest of the file, since it may be arbitrarily
437 osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
439 /* Leave the file open now, since reopening the file makes public pool
440 * vnode systems (like OSF/Alpha) much harder to handle, That's because
441 * they can do a vnode recycle operation any time we open a file, which
442 * we'd do on any afs_GetDSlot call, etc.
444 afs_cacheInodep = (struct osi_file *)tfile;
448 int afs_resourceinit_flag = 0;
449 int afs_ResourceInit(int preallocs)
451 register afs_int32 i;
452 static struct rx_securityClass *secobj;
454 AFS_STATCNT(afs_ResourceInit);
455 RWLOCK_INIT(&afs_xuser, "afs_xuser");
456 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
457 RWLOCK_INIT(&afs_xserver, "afs_xserver");
458 RWLOCK_INIT(&afs_xsrvAddr, "afs_xsrvAddr");
459 RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
460 RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
461 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
462 #ifndef AFS_AIX32_ENV
463 LOCK_INIT(&osi_fsplock, "osi_fsplock");
465 LOCK_INIT(&osi_flplock, "osi_flplock");
466 RWLOCK_INIT(&afs_xconn, "afs_xconn");
469 afs_InitCBQueue(1); /* initialize callback queues */
471 if (afs_resourceinit_flag == 0) {
472 afs_resourceinit_flag = 1;
473 for (i=0;i<NFENTRIES;i++)
475 for(i=0;i<MAXNUMSYSNAMES;i++)
476 afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
477 afs_sysname = afs_sysnamelist[0];
478 strcpy(afs_sysname, SYS_NAME);
479 afs_sysnamecount = 1;
480 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
483 if ((preallocs > 256) && (preallocs < 3600))
484 afs_preallocs = preallocs;
485 osi_AllocMoreSSpace(afs_preallocs);
486 osi_AllocMoreMSpace(100);
491 secobj = rxnull_NewServerSecurityObject();
493 rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
495 rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
496 RXSTATS_ExecuteRequest);
498 afs_osi_Wakeup(&afs_server); /* wakeup anyone waiting for it */
501 } /*afs_ResourceInit*/
503 #if defined(AFS_AIX_ENV) && !defined(AFS_AIX51_ENV)
506 * AIX dynamic sizeof(struct proc)
508 * AIX keeps its proc structures in an array. The size of struct proc
509 * varies from release to release of the OS. In order to maintain
510 * binary compatibility with releases later than what we build on, we
511 * need to determine the size of struct proc at run time.
513 * We need this in order to walk the proc[] array to do PAG garbage
516 * We also need this in order to support 'klog -setpag', since the
517 * kernel code needs to locate the proc structure for the parent process
518 * of the current process.
520 * To compute sizeof(struct proc), we need the addresses of two proc
521 * structures and their corresponding pids. Given the pids, we can use
522 * the PROCMASK() macro to compute their corresponding indices in the
523 * proc[] array. By dividing the distance between the pointers by the
524 * number of proc structures, we can compute the size of a single proc
527 * We know the base address of the proc table from v.vb_proc:
529 * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
530 * (we don't use this, but I note it here for completeness)
532 * <sys/var.h> declares struct var and external variable v;
536 * v.ve_proc &proc[x] (current highwater mark for
537 * proc[] array usage)
539 * The first proc pointer is v.vb_proc, which is the proc structure for
540 * process 0. Process 0's pointer to its first child is the other proc
541 * pointer. If process 0 has no children, we simply give up and do not
542 * support features that require knowing the size of struct proc.
545 static void afs_procsize_init(void)
547 struct proc *p0; /* pointer to process 0 */
548 struct proc *pN; /* pointer to process 0's first child */
556 p0 = (struct proc *)v.vb_proc;
558 afs_gcpags = AFS_GCPAGS_EPROC0;
563 pN = (struct proc *)0;
574 afs_gcpags = AFS_GCPAGS_EPROCN;
578 if (pN->p_pid == p0->p_pid) {
579 afs_gcpags = AFS_GCPAGS_EEQPID;
583 pN_index = PROCMASK(pN->p_pid);
584 pN_offset = ((char *)pN - (char *)p0);
585 procsize = pN_offset / pN_index;
588 * check that the computation was exact
591 if (pN_index * procsize != pN_offset) {
592 afs_gcpags = AFS_GCPAGS_EINEXACT;
597 * check that the proc table size is a multiple of procsize.
600 if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
601 afs_gcpags = AFS_GCPAGS_EPROCEND;
607 afs_gcpags_procsize = procsize;
615 * Clean up and shut down the AFS cache.
621 * Nothing interesting.
623 void shutdown_cache(void)
625 AFS_STATCNT(shutdown_cache);
626 afs_WriteThroughDSlots();
627 if (afs_cold_shutdown) {
628 afs_cacheinit_flag = 0;
633 afs_cacheFiles = afs_cacheBlocks = 0;
634 pag_epoch = maxIHint = nihints = usedihint = 0;
636 cacheInode = volumeInode = (ino_t)0;
639 cacheInfoModTime = 0;
641 afs_fsfragsize = 1023;
642 memset((char *)&afs_stats_cmperf, 0, sizeof(afs_stats_cmperf));
643 memset((char *)&cacheDev, 0, sizeof(struct osi_dev));
649 void shutdown_vnodeops(void)
651 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
652 struct buf *afs_bread_freebp = 0;
656 AFS_STATCNT(shutdown_vnodeops);
657 if (afs_cold_shutdown) {
658 #ifndef AFS_SUN5_ENV /* XXX */
661 #ifndef AFS_LINUX20_ENV
664 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
665 afs_bread_freebp = 0;
672 void shutdown_AFS(void)
675 register struct srvAddr *sa;
677 AFS_STATCNT(shutdown_AFS);
678 if (afs_cold_shutdown) {
679 afs_resourceinit_flag = 0;
681 * Free Volumes table allocations
685 for (i = 0; i < NVOLS; i++) {
686 for (tv = afs_volumes[i]; tv; tv = tv->next) {
688 afs_osi_Free(tv->name, strlen(tv->name)+1);
697 * Free FreeVolList allocations
699 afs_osi_Free(Initialafs_freeVolList, afs_memvolumes * sizeof(struct volume));
700 afs_freeVolList = Initialafs_freeVolList = 0;
702 /* XXX HACK fort MEM systems XXX
704 * For -memcache cache managers when we run out of free in memory volumes
705 * we simply malloc more; we won't be able to free those additional volumes.
711 * Free Users table allocation
714 struct unixuser *tu, *ntu;
715 for (i=0; i < NUSERS; i++) {
716 for (tu=afs_users[i]; tu; tu = ntu) {
719 afs_osi_Free(tu->stp, tu->stLen);
721 EXP_RELE(tu->exporter);
722 afs_osi_Free(tu, sizeof(struct unixuser));
729 * Free Servers table allocation
732 struct server *ts, *nts;
733 struct conn *tc, *ntc;
734 register struct afs_cbr *tcbrp, *tbrp;
736 for (i=0; i < NSERVERS; i++) {
737 for (ts = afs_servers[i]; ts; ts = nts) {
739 for (sa = ts->addr; sa; sa = sa->next_sa) {
742 * Free all server's connection structs
748 rx_DestroyConnection(tc->id);
750 afs_osi_Free(tc, sizeof(struct conn));
755 for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
757 * Free all server's callback structs
762 afs_osi_Free(ts, sizeof(struct server));
767 for (i=0; i<NFENTRIES; i++)
769 /* Reinitialize local globals to defaults */
770 for(i=0; i<MAXNUMSYSNAMES; i++)
771 afs_osi_Free(afs_sysnamelist[i], MAXSYSNAME);
773 afs_sysnamecount = 0;
775 afs_setTimeHost = NULL;
777 afs_waitForever = afs_waitForeverCount = 0;
779 afs_server = (struct rx_service *)0;
780 RWLOCK_INIT(&afs_xconn, "afs_xconn");
781 memset((char *)&afs_rootFid, 0, sizeof(struct VenusFid));
782 RWLOCK_INIT(&afs_xuser, "afs_xuser");
783 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
784 RWLOCK_INIT(&afs_xserver, "afs_xserver");
785 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");