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 "../afs/param.h" /* Should be always first */
17 #include "../afs/stds.h"
18 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include "../afs/afsincludes.h" /* Afs-based standard headers */
20 #include "../afs/afs_stats.h" /* afs statistics */
23 /* Imported variables */
24 extern afs_int32 afs_waitForever;
25 extern short afs_waitForeverCount;
26 extern afs_int32 afs_FVIndex;
27 extern struct server *afs_setTimeHost;
28 extern struct server *afs_servers[NSERVERS];
29 extern struct unixuser *afs_users[NUSERS];
30 extern struct volume *afs_freeVolList;
31 extern struct volume *afs_volumes[NVOLS];
32 extern afs_int32 afs_volCounter;
34 extern afs_rwlock_t afs_xaxs;
35 extern afs_rwlock_t afs_xvolume;
36 extern afs_rwlock_t afs_xuser;
37 extern afs_rwlock_t afs_xserver;
39 extern afs_lock_t osi_fsplock;
41 extern afs_lock_t osi_flplock;
42 extern afs_int32 fvTable[NFENTRIES];
45 extern afs_rwlock_t afs_xcell;
46 extern struct afs_q CellLRU;
47 extern afs_int32 afs_cellindex;
48 extern afs_int32 afs_nextCellNum;
51 extern afs_rwlock_t afs_xconn;
52 extern afs_rwlock_t afs_xinterface;
55 extern struct rx_service *afs_server;
59 extern afs_int32 afs_mariner;
60 extern afs_int32 afs_marinerHost;
63 extern ino_t volumeInode;
66 extern afs_uint32 pag_epoch;
69 extern afs_rwlock_t afs_xdcache;
70 extern int cacheDiskType;
71 extern afs_int32 afs_fsfragsize;
72 extern ino_t cacheInode;
73 extern struct osi_file *afs_cacheInodep;
74 extern afs_int32 afs_freeDCList; /*Free list for disk cache entries*/
78 extern afs_rwlock_t afs_xvcache;
79 extern afs_rwlock_t afs_xvcb;
81 /* VNOPS/afs_vnop_read.c */
82 extern afs_int32 maxIHint;
83 extern afs_int32 nihints; /* # of above actually in-use */
84 extern afs_int32 usedihint;
87 extern afs_int32 afs_setTime;
89 /* Imported functions. */
90 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
91 extern int RXAFSCB_ExecuteRequest();
92 extern int RXSTATS_ExecuteRequest();
96 extern afs_lock_t afs_ftf;
98 /* Exported variables */
99 struct osi_dev cacheDev; /*Cache device*/
100 afs_int32 cacheInfoModTime; /*Last time cache info modified*/
101 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV) || defined(AFS_DARWIN_ENV)
102 struct mount *afs_cacheVfsp=0;
103 #elif defined(AFS_LINUX20_ENV)
104 struct super_block *afs_cacheSBp = 0;
106 struct vfs *afs_cacheVfsp=0;
108 afs_rwlock_t afs_puttofileLock; /* not used */
109 char *afs_sysname = 0; /* So that superuser may change the
110 * local value of @sys */
111 struct volume *Initialafs_freeVolList;
112 int afs_memvolumes = 0;
114 /* Local variables */
118 * Initialization order is important. Must first call afs_CacheInit,
119 * then cache file and volume file initialization routines. Next, the
120 * individual cache entry initialization routines are called.
130 * astatSize : The number of stat cache (vnode) entries to
132 * afiles : The number of disk files to allocate to the cache
133 * ablocks : The max number of 1 Kbyte blocks that all of
134 * the files in the cache may occupy.
135 * aDentries : Number of dcache entries to allocate.
136 * aVolumes : Number of volume cache entries to allocate.
137 * achunk : Power of 2 to make the chunks.
138 * aflags : Flags passed in.
139 * inodes : max inodes to pin down in inode[]
140 * users : what should size of per-user access cache be?
143 * This routine should only be called at initialization time, since
144 * it reclaims no resources and doesn't sufficiently synchronize
145 * with other processes.
148 struct cm_initparams cm_initParams;
149 static int afs_cacheinit_flag = 0;
151 afs_CacheInit(astatSize, afiles, ablocks, aDentries, aVolumes, achunk, aflags,
154 afs_int32 astatSize, ablocks;
155 afs_int32 achunk, aflags, ninodes, nusers;
158 extern int afs_memvolumes;
159 register afs_int32 i, preallocs;
160 register struct volume *tv;
163 AFS_STATCNT(afs_CacheInit);
165 * Jot down the epoch time, namely when this incarnation of the
166 * Cache Manager started.
168 afs_stats_cmperf.epoch = pag_epoch = osi_Time();
170 afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
172 afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
173 #endif /* SYS_NAME_ID */
175 printf("Starting AFS cache scan...");
176 if (afs_cacheinit_flag)
178 afs_cacheinit_flag = 1;
179 cacheInfoModTime = 0;
184 LOCK_INIT(&afs_ftf, "afs_ftf");
185 RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
189 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
191 * We want to also reserve space for the gnode struct which is associated
192 * with each vnode (vcache) one; we want to use the pinned pool for them
193 * since they're referenced at interrupt level.
195 if (afs_stats_cmperf.SmallBlocksAlloced + astatSize < 3600)
196 preallocs = astatSize;
198 preallocs = 3600 - afs_stats_cmperf.SmallBlocksAlloced;
199 if (preallocs <= 0) preallocs = 10;
201 osi_AllocMoreSSpace(preallocs);
204 * create volume list structure
206 if ( aVolumes < 50 ) aVolumes = 50;
207 if (aVolumes > 3000) aVolumes = 3000;
209 tv = (struct volume *) afs_osi_Alloc(aVolumes * sizeof(struct volume));
210 for (i=0;i<aVolumes-1;i++)
211 tv[i].next = &tv[i+1];
212 tv[aVolumes-1].next = (struct volume *) 0;
213 afs_freeVolList = Initialafs_freeVolList = tv;
214 afs_memvolumes = aVolumes;
216 afs_cacheFiles = afiles;
217 afs_cacheStats = astatSize;
218 afs_vcacheInit(astatSize);
219 afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
221 #if defined(AFS_AIX_ENV)
223 static void afs_procsize_init(void);
229 /* Save the initialization parameters for later pioctl queries. */
230 cm_initParams.cmi_version = CMI_VERSION;
231 cm_initParams.cmi_nChunkFiles = afiles;
232 cm_initParams.cmi_nStatCaches = astatSize;
233 cm_initParams.cmi_nDataCaches = aDentries;
234 cm_initParams.cmi_nVolumeCaches = aVolumes;
235 cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
236 cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
237 cm_initParams.cmi_cacheSize = ablocks;
238 cm_initParams.cmi_setTime = afs_setTime;
239 cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
247 * afs_ComputeCacheParams
250 * Set some cache parameters.
257 afs_ComputeCacheParms()
259 { /*afs_ComputeCacheParms*/
261 register afs_int32 i;
262 afs_int32 afs_maxCacheDirty;
265 * Don't allow more than 2/3 of the files in the cache to be dirty.
267 afs_maxCacheDirty = (2*afs_cacheFiles) / 3;
270 * Also, don't allow more than 2/3 of the total space get filled
271 * with dirty chunks. Compute the total number of chunks required
272 * to fill the cache, make sure we don't set out limit above 2/3 of
273 * that. If the cache size is greater than 1G, avoid overflow at
274 * the expense of precision on the chunk size.
276 if (afs_cacheBlocks & 0xffe00000) {
277 i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
280 i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
283 if (afs_maxCacheDirty > i)
284 afs_maxCacheDirty = i;
285 if (afs_maxCacheDirty < 1)
286 afs_maxCacheDirty = 1;
287 afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
288 } /*afs_ComputeCacheParms*/
295 * Set up the volume info storage file.
298 * afile : the file to be declared to be the volume info storage
299 * file for AFS. It must be already truncated to 0 length.
302 * This function is called only during initialization.
304 * WARNING: Data will be written to this file over time by AFS.
307 afs_InitVolumeInfo(afile)
308 register char *afile;
310 { /*afs_InitVolumeInfo*/
313 struct osi_file *tfile;
314 struct vnode *filevp;
317 AFS_STATCNT(afs_InitVolumeInfo);
318 #ifdef AFS_LINUX22_ENV
321 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &dp);
322 if (code) return ENOENT;
323 fce.inode = volumeInode = dp->d_inode->i_ino;
327 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
328 if (code) return ENOENT;
329 fce.inode = volumeInode = afs_vnodeToInumber(filevp);
333 AFS_RELE((struct vnode *)filevp);
335 #endif /* AFS_LINUX22_ENV */
336 tfile = afs_CFileOpen(fce.inode);
337 afs_CFileTruncate(tfile, 0);
338 afs_CFileClose(tfile);
341 } /*afs_InitVolumeInfo*/
347 * Set up the given file as the AFS cache info file.
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()
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.
360 * WARNING: data will be written to this file over time by AFS.
362 * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
366 afs_InitCacheInfo(afile)
367 register char *afile;
369 { /*afs_InitCacheInfo*/
371 register afs_int32 code;
372 struct osi_stat tstat;
373 register struct osi_file *tfile;
374 struct afs_fheader theader;
375 struct vnode *filevp;
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) return code;
385 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
386 if (code || !filevp) return ENOENT;
388 #if defined(AFS_SUN56_ENV)
391 #if defined(AFS_HPUX102_ENV)
394 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
397 #if defined(AFS_DUX40_ENV)
402 #endif /* SUN5 SGI */
403 #endif /* HP 10.20 */
406 #if defined(AFS_SGI_ENV)
408 VFS_STATVFS(filevp->v_vfsp, &st, (struct vnode *)0, code);
411 if (!VFS_STATFS(filevp->v_vfsp, &st, (struct vnode *)0))
412 #endif /* AFS_SGI65_ENV */
413 #else /* AFS_SGI_ENV */
414 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
415 if (!VFS_STATVFS(filevp->v_vfsp, &st))
419 VFS_STATFS(filevp->v_vfsp, code);
421 st = filevp->v_vfsp->m_stat;
423 #else /* AFS_OSF_ENV */
425 if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
427 #ifdef AFS_LINUX20_ENV
432 VFS_STATFS(filevp->v_vfsp, &st);
436 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
437 if (!VFS_STATFS(filevp->v_mount, &st, current_proc()))
439 if (!VFS_STATFS(filevp->v_vfsp, &st))
440 #endif /* AFS_DARWIN_ENV || AFS_FBSD_ENV */
441 #endif /* AFS_LINUX20_ENV */
444 #endif /* SUN5 HP10 */
446 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
447 afs_fsfragsize = st.f_frsize - 1;
449 afs_fsfragsize = st.f_bsize - 1;
452 #ifdef AFS_LINUX20_ENV
453 cacheInode = filevp->i_ino;
454 afs_cacheSBp = filevp->i_sb;
456 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS) || defined(AFS_DARWIN_ENV)
457 afs_InitDualFSCacheOps(filevp);
459 cacheInode = afs_vnodeToInumber(filevp);
460 cacheDev.dev = afs_vnodeToDev(filevp);
461 afs_cacheVfsp = filevp->v_vfsp;
462 #endif /* AFS_LINUX20_ENV */
463 AFS_RELE((struct vnode *)filevp);
464 #endif /* AFS_LINUX22_ENV */
465 tfile = osi_UFSOpen(cacheInode);
466 afs_osi_Stat(tfile, &tstat);
467 cacheInfoModTime = tstat.mtime;
468 code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
470 if (code == sizeof(theader)) {
471 /* read the header correctly */
472 if (theader.magic == AFS_FHMAGIC &&
473 theader.firstCSize == AFS_FIRSTCSIZE &&
474 theader.otherCSize == AFS_OTHERCSIZE &&
475 theader.version == AFS_CI_VERSION
480 /* write out a good file label */
481 theader.magic = AFS_FHMAGIC;
482 theader.firstCSize = AFS_FIRSTCSIZE;
483 theader.otherCSize = AFS_OTHERCSIZE;
484 theader.version = AFS_CI_VERSION;
485 afs_osi_Write(tfile, 0, &theader, sizeof(theader));
487 * Truncate the rest of the file, since it may be arbitrarily
490 osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
492 /* Leave the file open now, since reopening the file makes public pool
493 * vnode systems (like OSF/Alpha) much harder to handle, That's because
494 * they can do a vnode recycle operation any time we open a file, which
495 * we'd do on any afs_GetDSlot call, etc.
497 afs_cacheInodep = (struct osi_file *)tfile;
500 } /*afs_InitCacheInfo*/
502 int afs_resourceinit_flag = 0;
503 afs_ResourceInit(preallocs)
506 register afs_int32 i;
507 static struct rx_securityClass *secobj;
509 AFS_STATCNT(afs_ResourceInit);
510 RWLOCK_INIT(&afs_xuser, "afs_xuser");
511 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
512 RWLOCK_INIT(&afs_xcell, "afs_xcell");
513 RWLOCK_INIT(&afs_xserver, "afs_xserver");
514 RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
515 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
516 #ifndef AFS_AIX32_ENV
517 LOCK_INIT(&osi_fsplock, "osi_fsplock");
519 LOCK_INIT(&osi_flplock, "osi_flplock");
520 RWLOCK_INIT(&afs_xconn, "afs_xconn");
522 afs_InitCBQueue(1); /* initialize callback queues */
524 if (afs_resourceinit_flag == 0) {
525 afs_resourceinit_flag = 1;
526 for (i=0;i<NFENTRIES;i++)
528 afs_sysname = afs_osi_Alloc(MAXSYSNAME);
529 strcpy(afs_sysname, SYS_NAME);
531 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
532 { extern afs_int32 afs_preallocs;
534 if ((preallocs > 256) && (preallocs < 3600))
535 afs_preallocs = preallocs;
536 osi_AllocMoreSSpace(afs_preallocs);
537 osi_AllocMoreMSpace(100);
542 secobj = rxnull_NewServerSecurityObject();
544 rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
546 rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
547 RXSTATS_ExecuteRequest);
549 afs_osi_Wakeup(&afs_server); /* wakeup anyone waiting for it */
552 } /*afs_ResourceInit*/
554 #if defined(AFS_AIX_ENV)
557 * AIX dynamic sizeof(struct proc)
559 * AIX keeps its proc structures in an array. The size of struct proc
560 * varies from release to release of the OS. In order to maintain
561 * binary compatibility with releases later than what we build on, we
562 * need to determine the size of struct proc at run time.
564 * We need this in order to walk the proc[] array to do PAG garbage
567 * We also need this in order to support 'klog -setpag', since the
568 * kernel code needs to locate the proc structure for the parent process
569 * of the current process.
571 * To compute sizeof(struct proc), we need the addresses of two proc
572 * structures and their corresponding pids. Given the pids, we can use
573 * the PROCMASK() macro to compute their corresponding indices in the
574 * proc[] array. By dividing the distance between the pointers by the
575 * number of proc structures, we can compute the size of a single proc
578 * We know the base address of the proc table from v.vb_proc:
580 * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
581 * (we don't use this, but I note it here for completeness)
583 * <sys/var.h> declares struct var and external variable v;
587 * v.ve_proc &proc[x] (current highwater mark for
588 * proc[] array usage)
590 * The first proc pointer is v.vb_proc, which is the proc structure for
591 * process 0. Process 0's pointer to its first child is the other proc
592 * pointer. If process 0 has no children, we simply give up and do not
593 * support features that require knowing the size of struct proc.
597 afs_procsize_init(void)
599 struct proc *p0; /* pointer to process 0 */
600 struct proc *pN; /* pointer to process 0's first child */
605 p0 = (struct proc *)v.vb_proc;
607 afs_gcpags = AFS_GCPAGS_EPROC0;
613 afs_gcpags = AFS_GCPAGS_EPROCN;
617 if (pN->p_pid == p0->p_pid) {
618 afs_gcpags = AFS_GCPAGS_EEQPID;
622 pN_index = PROCMASK(pN->p_pid);
623 pN_offset = ((char *)pN - (char *)p0);
624 procsize = pN_offset / pN_index;
627 * check that the computation was exact
630 if (pN_index * procsize != pN_offset) {
631 afs_gcpags = AFS_GCPAGS_EINEXACT;
636 * check that the proc table size is a multiple of procsize.
639 if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
640 afs_gcpags = AFS_GCPAGS_EPROCEND;
646 afs_gcpags_procsize = procsize;
655 * Clean up and shut down the AFS cache.
661 * Nothing interesting.
667 register struct afs_cbr *tsp, *nsp;
668 extern int afs_cold_shutdown;
669 extern int pagCounter;
672 AFS_STATCNT(shutdown_cache);
673 afs_WriteThroughDSlots();
674 if (afs_cold_shutdown) {
675 afs_cacheinit_flag = 0;
680 afs_cacheFiles = afs_cacheBlocks = 0;
681 pag_epoch = maxIHint = nihints = usedihint = 0;
683 cacheInode = volumeInode = (ino_t)0;
686 cacheInfoModTime = 0;
688 afs_fsfragsize = 1023;
689 bzero((char *)&afs_stats_cmperf, sizeof(afs_stats_cmperf));
690 bzero((char *)&cacheDev, sizeof(struct osi_dev));
696 void shutdown_vnodeops()
698 extern int afs_cold_shutdown;
699 #ifndef AFS_LINUX20_ENV
700 extern int afs_rd_stash_i;
703 extern int lastWarnTime;
705 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
706 struct buf *afs_bread_freebp = 0;
710 AFS_STATCNT(shutdown_vnodeops);
711 if (afs_cold_shutdown) {
712 #ifndef AFS_SUN5_ENV /* XXX */
715 #ifndef AFS_LINUX20_ENV
718 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
719 afs_bread_freebp = 0;
730 register struct srvAddr *sa;
731 extern int afs_cold_shutdown;
733 AFS_STATCNT(shutdown_AFS);
734 if (afs_cold_shutdown) {
735 afs_resourceinit_flag = 0;
737 * Free Cells table allocations
741 register struct afs_q *cq, *tq;
742 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
743 tc = QTOC(cq); tq = QNext(cq);
745 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
746 afs_osi_Free(tc, sizeof(struct cell));
750 * Free Volumes table allocations
754 for (i = 0; i < NVOLS; i++) {
755 for (tv = afs_volumes[i]; tv; tv = tv->next) {
757 afs_osi_Free(tv->name, strlen(tv->name)+1);
766 * Free FreeVolList allocations
768 afs_osi_Free(Initialafs_freeVolList, afs_memvolumes * sizeof(struct volume));
769 afs_freeVolList = Initialafs_freeVolList = 0;
771 /* XXX HACK fort MEM systems XXX
773 * For -memcache cache managers when we run out of free in memory volumes
774 * we simply malloc more; we won't be able to free those additional volumes.
780 * Free Users table allocation
783 struct unixuser *tu, *ntu;
784 for (i=0; i < NUSERS; i++) {
785 for (tu=afs_users[i]; tu; tu = ntu) {
788 afs_osi_Free(tu->stp, tu->stLen);
790 EXP_RELE(tu->exporter);
791 afs_osi_Free(tu, sizeof(struct unixuser));
798 * Free Servers table allocation
801 struct server *ts, *nts;
802 struct conn *tc, *ntc;
803 register struct afs_cbr *tcbrp, *tbrp;
804 struct afs_cbr **lcbrpp;
806 for (i=0; i < NSERVERS; i++) {
807 for (ts = afs_servers[i]; ts; ts = nts) {
809 for (sa = ts->addr; sa; sa = sa->next_sa) {
812 * Free all server's connection structs
818 rx_DestroyConnection(tc->id);
820 afs_osi_Free(tc, sizeof(struct conn));
825 for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
827 * Free all server's callback structs
832 afs_osi_Free(ts, sizeof(struct server));
837 for (i=0; i<NFENTRIES; i++)
839 /* Reinitialize local globals to defaults */
840 afs_osi_Free(afs_sysname, MAXSYSNAME);
844 afs_setTimeHost = (struct server *)0;
846 afs_waitForever = afs_waitForeverCount = 0;
848 afs_nextCellNum = 0x100;
850 afs_server = (struct rx_service *)0;
851 RWLOCK_INIT(&afs_xconn, "afs_xconn");
852 bzero((char *)&afs_rootFid, sizeof(struct VenusFid));
853 RWLOCK_INIT(&afs_xuser, "afs_xuser");
854 RWLOCK_INIT(&afs_xvolume, "afs_xvolume"), RWLOCK_INIT(&afs_xcell, "afs_xcell");
855 RWLOCK_INIT(&afs_xserver, "afs_xserver"), LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");