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;
50 extern afs_rwlock_t afs_xconn;
51 extern afs_rwlock_t afs_xinterface;
54 extern struct rx_service *afs_server;
58 extern afs_int32 afs_mariner;
59 extern afs_int32 afs_marinerHost;
62 extern ino_t volumeInode;
65 extern afs_uint32 pag_epoch;
68 extern afs_rwlock_t afs_xdcache;
69 extern int cacheDiskType;
70 extern afs_int32 afs_fsfragsize;
71 extern ino_t cacheInode;
72 extern struct osi_file *afs_cacheInodep;
73 extern afs_int32 afs_freeDCList; /*Free list for disk cache entries*/
77 extern afs_rwlock_t afs_xvcache;
78 extern afs_rwlock_t afs_xvcb;
80 /* VNOPS/afs_vnop_read.c */
81 extern afs_int32 maxIHint;
82 extern afs_int32 nihints; /* # of above actually in-use */
83 extern afs_int32 usedihint;
86 extern afs_int32 afs_setTime;
88 /* Imported functions. */
89 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
90 extern int RXAFSCB_ExecuteRequest();
91 extern int RXSTATS_ExecuteRequest();
95 extern afs_lock_t afs_ftf;
97 /* Exported variables */
98 struct osi_dev cacheDev; /*Cache device*/
99 afs_int32 cacheInfoModTime; /*Last time cache info modified*/
100 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
101 struct mount *afs_cacheVfsp=0;
102 #elif defined(AFS_LINUX20_ENV)
103 struct super_block *afs_cacheSBp = 0;
105 struct vfs *afs_cacheVfsp=0;
107 afs_rwlock_t afs_puttofileLock; /* not used */
108 char *afs_sysname = 0; /* So that superuser may change the
109 * local value of @sys */
110 struct volume *Initialafs_freeVolList;
111 int afs_memvolumes = 0;
113 /* Local variables */
117 * Initialization order is important. Must first call afs_CacheInit,
118 * then cache file and volume file initialization routines. Next, the
119 * individual cache entry initialization routines are called.
129 * astatSize : The number of stat cache (vnode) entries to
131 * afiles : The number of disk files to allocate to the cache
132 * ablocks : The max number of 1 Kbyte blocks that all of
133 * the files in the cache may occupy.
134 * aDentries : Number of dcache entries to allocate.
135 * aVolumes : Number of volume cache entries to allocate.
136 * achunk : Power of 2 to make the chunks.
137 * aflags : Flags passed in.
138 * inodes : max inodes to pin down in inode[]
139 * users : what should size of per-user access cache be?
142 * This routine should only be called at initialization time, since
143 * it reclaims no resources and doesn't sufficiently synchronize
144 * with other processes.
147 struct cm_initparams cm_initParams;
148 static int afs_cacheinit_flag = 0;
150 afs_CacheInit(astatSize, afiles, ablocks, aDentries, aVolumes, achunk, aflags,
153 afs_int32 astatSize, ablocks;
154 afs_int32 achunk, aflags, ninodes, nusers;
157 extern int afs_memvolumes;
158 register afs_int32 i, preallocs;
159 register struct volume *tv;
162 AFS_STATCNT(afs_CacheInit);
164 * Jot down the epoch time, namely when this incarnation of the
165 * Cache Manager started.
167 afs_stats_cmperf.epoch = pag_epoch = osi_Time();
169 afs_stats_cmperf.sysName_ID = SYS_NAME_ID;
171 afs_stats_cmperf.sysName_ID = SYS_NAME_ID_UNDEFINED;
172 #endif /* SYS_NAME_ID */
174 printf("Starting AFS cache scan...");
175 if (afs_cacheinit_flag)
177 afs_cacheinit_flag = 1;
178 cacheInfoModTime = 0;
183 LOCK_INIT(&afs_ftf, "afs_ftf");
184 RWLOCK_INIT(&afs_xaxs, "afs_xaxs");
188 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
190 * We want to also reserve space for the gnode struct which is associated
191 * with each vnode (vcache) one; we want to use the pinned pool for them
192 * since they're referenced at interrupt level.
194 if (afs_stats_cmperf.SmallBlocksAlloced + astatSize < 3600)
195 preallocs = astatSize;
197 preallocs = 3600 - afs_stats_cmperf.SmallBlocksAlloced;
198 if (preallocs <= 0) preallocs = 10;
200 osi_AllocMoreSSpace(preallocs);
203 * create volume list structure
205 if ( aVolumes < 50 ) aVolumes = 50;
206 if (aVolumes > 3000) aVolumes = 3000;
208 tv = (struct volume *) afs_osi_Alloc(aVolumes * sizeof(struct volume));
209 for (i=0;i<aVolumes-1;i++)
210 tv[i].next = &tv[i+1];
211 tv[aVolumes-1].next = (struct volume *) 0;
212 afs_freeVolList = Initialafs_freeVolList = tv;
213 afs_memvolumes = aVolumes;
215 afs_cacheFiles = afiles;
216 afs_cacheStats = astatSize;
217 afs_vcacheInit(astatSize);
218 afs_dcacheInit(afiles, ablocks, aDentries, achunk, aflags);
220 #if defined(AFS_AIX_ENV)
222 static void afs_procsize_init(void);
228 /* Save the initialization parameters for later pioctl queries. */
229 cm_initParams.cmi_version = CMI_VERSION;
230 cm_initParams.cmi_nChunkFiles = afiles;
231 cm_initParams.cmi_nStatCaches = astatSize;
232 cm_initParams.cmi_nDataCaches = aDentries;
233 cm_initParams.cmi_nVolumeCaches = aVolumes;
234 cm_initParams.cmi_firstChunkSize = AFS_FIRSTCSIZE;
235 cm_initParams.cmi_otherChunkSize = AFS_OTHERCSIZE;
236 cm_initParams.cmi_cacheSize = ablocks;
237 cm_initParams.cmi_setTime = afs_setTime;
238 cm_initParams.cmi_memCache = (aflags & AFSCALL_INIT_MEMCACHE) ? 1 : 0;
246 * afs_ComputeCacheParams
249 * Set some cache parameters.
256 afs_ComputeCacheParms()
258 { /*afs_ComputeCacheParms*/
260 register afs_int32 i;
261 afs_int32 afs_maxCacheDirty;
264 * Don't allow more than 2/3 of the files in the cache to be dirty.
266 afs_maxCacheDirty = (2*afs_cacheFiles) / 3;
269 * Also, don't allow more than 2/3 of the total space get filled
270 * with dirty chunks. Compute the total number of chunks required
271 * to fill the cache, make sure we don't set out limit above 2/3 of
272 * that. If the cache size is greater than 1G, avoid overflow at
273 * the expense of precision on the chunk size.
275 if (afs_cacheBlocks & 0xffe00000) {
276 i = afs_cacheBlocks / (AFS_FIRSTCSIZE >> 10);
279 i = (afs_cacheBlocks << 10) / AFS_FIRSTCSIZE;
282 if (afs_maxCacheDirty > i)
283 afs_maxCacheDirty = i;
284 if (afs_maxCacheDirty < 1)
285 afs_maxCacheDirty = 1;
286 afs_stats_cmperf.cacheMaxDirtyChunks = afs_maxCacheDirty;
287 } /*afs_ComputeCacheParms*/
294 * Set up the volume info storage file.
297 * afile : the file to be declared to be the volume info storage
298 * file for AFS. It must be already truncated to 0 length.
301 * This function is called only during initialization.
303 * WARNING: Data will be written to this file over time by AFS.
306 afs_InitVolumeInfo(afile)
307 register char *afile;
309 { /*afs_InitVolumeInfo*/
312 struct osi_file *tfile;
313 struct vnode *filevp;
316 AFS_STATCNT(afs_InitVolumeInfo);
317 #ifdef AFS_LINUX22_ENV
320 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &dp);
321 if (code) return ENOENT;
322 fce.inode = volumeInode = dp->d_inode->i_ino;
326 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
327 if (code) return ENOENT;
328 fce.inode = volumeInode = afs_vnodeToInumber(filevp);
332 AFS_RELE((struct vnode *)filevp);
334 #endif /* AFS_LINUX22_ENV */
335 tfile = afs_CFileOpen(fce.inode);
336 afs_CFileTruncate(tfile, 0);
337 afs_CFileClose(tfile);
340 } /*afs_InitVolumeInfo*/
346 * Set up the given file as the AFS cache info file.
349 * afile : Name of the file assumed to be the cache info file
350 * for the Cache Manager; it will be used as such.
351 * Side Effects: This sets afs_fragsize, which is used in the cache usage
352 * calculations such as in afs_adjustsize()
355 * This function is called only during initialization. The given
356 * file should NOT be truncated to 0 lenght; its contents descrebe
357 * what data is really in the cache.
359 * WARNING: data will be written to this file over time by AFS.
361 * NOTE: Starting to use separate osi_InitCacheInfo() routines to clean up
365 afs_InitCacheInfo(afile)
366 register char *afile;
368 { /*afs_InitCacheInfo*/
370 register afs_int32 code;
371 struct osi_stat tstat;
372 register struct osi_file *tfile;
373 struct afs_fheader theader;
374 struct vnode *filevp;
377 AFS_STATCNT(afs_InitCacheInfo);
378 if(cacheDiskType != AFS_FCACHE_TYPE_UFS)
379 osi_Panic("afs_InitCacheInfo --- called for non-ufs cache!");
380 #ifdef AFS_LINUX22_ENV
381 code = osi_InitCacheInfo(afile);
382 if (code) return code;
384 code = gop_lookupname(afile, AFS_UIOSYS, 0, (struct vnode **) 0, &filevp);
385 if (code || !filevp) return ENOENT;
387 #if defined(AFS_SUN56_ENV)
390 #if defined(AFS_HPUX102_ENV)
393 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) ||defined(AFS_HPUX100_ENV)
396 #if defined(AFS_DUX40_ENV)
401 #endif /* SUN5 SGI */
402 #endif /* HP 10.20 */
405 #if defined(AFS_SGI_ENV)
407 VFS_STATVFS(filevp->v_vfsp, &st, (struct vnode *)0, code);
410 if (!VFS_STATFS(filevp->v_vfsp, &st, (struct vnode *)0))
411 #endif /* AFS_SGI65_ENV */
412 #else /* AFS_SGI_ENV */
413 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
414 if (!VFS_STATVFS(filevp->v_vfsp, &st))
418 VFS_STATFS(filevp->v_vfsp, code);
420 st = filevp->v_vfsp->m_stat;
422 #else /* AFS_OSF_ENV */
424 if (!VFS_STATFS(filevp->v_vfsp, &st, &afs_osi_cred))
426 #ifdef AFS_LINUX20_ENV
431 VFS_STATFS(filevp->v_vfsp, &st);
435 if (!VFS_STATFS(filevp->v_vfsp, &st))
436 #endif /* AFS_LINUX20_ENV */
439 #endif /* SUN5 HP10 */
441 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX100_ENV)
442 afs_fsfragsize = st.f_frsize - 1;
444 afs_fsfragsize = st.f_bsize - 1;
447 #ifdef AFS_LINUX20_ENV
448 cacheInode = filevp->i_ino;
449 afs_cacheSBp = filevp->i_sb;
451 cacheInode = afs_vnodeToInumber(filevp);
452 cacheDev.dev = afs_vnodeToDev(filevp);
453 #if defined(AFS_SGI62_ENV) || defined(AFS_HAVE_VXFS)
454 afs_InitDualFSCacheOps(filevp);
456 afs_cacheVfsp = filevp->v_vfsp;
457 #endif /* AFS_LINUX20_ENV */
458 AFS_RELE((struct vnode *)filevp);
459 #endif /* AFS_LINUX22_ENV */
460 tfile = osi_UFSOpen(cacheInode);
461 afs_osi_Stat(tfile, &tstat);
462 cacheInfoModTime = tstat.mtime;
463 code = afs_osi_Read(tfile, -1, &theader, sizeof(theader));
465 if (code == sizeof(theader)) {
466 /* read the header correctly */
467 if (theader.magic == AFS_FHMAGIC &&
468 theader.firstCSize == AFS_FIRSTCSIZE &&
469 theader.otherCSize == AFS_OTHERCSIZE &&
470 theader.version == AFS_CI_VERSION
475 /* write out a good file label */
476 theader.magic = AFS_FHMAGIC;
477 theader.firstCSize = AFS_FIRSTCSIZE;
478 theader.otherCSize = AFS_OTHERCSIZE;
479 theader.version = AFS_CI_VERSION;
480 afs_osi_Write(tfile, 0, &theader, sizeof(theader));
482 * Truncate the rest of the file, since it may be arbitrarily
485 osi_UFSTruncate(tfile, sizeof(struct afs_fheader));
487 /* Leave the file open now, since reopening the file makes public pool
488 * vnode systems (like OSF/Alpha) much harder to handle, That's because
489 * they can do a vnode recycle operation any time we open a file, which
490 * we'd do on any afs_GetDSlot call, etc.
492 afs_cacheInodep = (struct osi_file *)tfile;
495 } /*afs_InitCacheInfo*/
497 int afs_resourceinit_flag = 0;
498 afs_ResourceInit(preallocs)
501 register afs_int32 i;
502 static struct rx_securityClass *secobj;
504 AFS_STATCNT(afs_ResourceInit);
505 RWLOCK_INIT(&afs_xuser, "afs_xuser");
506 RWLOCK_INIT(&afs_xvolume, "afs_xvolume");
507 RWLOCK_INIT(&afs_xcell, "afs_xcell");
508 RWLOCK_INIT(&afs_xserver, "afs_xserver");
509 RWLOCK_INIT(&afs_xinterface, "afs_xinterface");
510 LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");
511 #ifndef AFS_AIX32_ENV
512 LOCK_INIT(&osi_fsplock, "osi_fsplock");
514 LOCK_INIT(&osi_flplock, "osi_flplock");
515 RWLOCK_INIT(&afs_xconn, "afs_xconn");
517 afs_InitCBQueue(1); /* initialize callback queues */
519 if (afs_resourceinit_flag == 0) {
520 afs_resourceinit_flag = 1;
521 for (i=0;i<NFENTRIES;i++)
523 afs_sysname = afs_osi_Alloc(MAXSYSNAME);
524 strcpy(afs_sysname, SYS_NAME);
526 #if defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)
527 { extern afs_int32 afs_preallocs;
529 if ((preallocs > 256) && (preallocs < 3600))
530 afs_preallocs = preallocs;
531 osi_AllocMoreSSpace(afs_preallocs);
532 osi_AllocMoreMSpace(100);
537 secobj = rxnull_NewServerSecurityObject();
539 rx_NewService(0, 1, "afs", &secobj, 1, RXAFSCB_ExecuteRequest);
541 rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &secobj, 1,
542 RXSTATS_ExecuteRequest);
544 afs_osi_Wakeup(&afs_server); /* wakeup anyone waiting for it */
547 } /*afs_ResourceInit*/
549 #if defined(AFS_AIX_ENV)
552 * AIX dynamic sizeof(struct proc)
554 * AIX keeps its proc structures in an array. The size of struct proc
555 * varies from release to release of the OS. In order to maintain
556 * binary compatibility with releases later than what we build on, we
557 * need to determine the size of struct proc at run time.
559 * We need this in order to walk the proc[] array to do PAG garbage
562 * We also need this in order to support 'klog -setpag', since the
563 * kernel code needs to locate the proc structure for the parent process
564 * of the current process.
566 * To compute sizeof(struct proc), we need the addresses of two proc
567 * structures and their corresponding pids. Given the pids, we can use
568 * the PROCMASK() macro to compute their corresponding indices in the
569 * proc[] array. By dividing the distance between the pointers by the
570 * number of proc structures, we can compute the size of a single proc
573 * We know the base address of the proc table from v.vb_proc:
575 * <sys/sysconfig.h> declares sysconfig() and SYS_GETPARMS;
576 * (we don't use this, but I note it here for completeness)
578 * <sys/var.h> declares struct var and external variable v;
582 * v.ve_proc &proc[x] (current highwater mark for
583 * proc[] array usage)
585 * The first proc pointer is v.vb_proc, which is the proc structure for
586 * process 0. Process 0's pointer to its first child is the other proc
587 * pointer. If process 0 has no children, we simply give up and do not
588 * support features that require knowing the size of struct proc.
592 afs_procsize_init(void)
594 struct proc *p0; /* pointer to process 0 */
595 struct proc *pN; /* pointer to process 0's first child */
600 p0 = (struct proc *)v.vb_proc;
602 afs_gcpags = AFS_GCPAGS_EPROC0;
608 afs_gcpags = AFS_GCPAGS_EPROCN;
612 if (pN->p_pid == p0->p_pid) {
613 afs_gcpags = AFS_GCPAGS_EEQPID;
617 pN_index = PROCMASK(pN->p_pid);
618 pN_offset = ((char *)pN - (char *)p0);
619 procsize = pN_offset / pN_index;
622 * check that the computation was exact
625 if (pN_index * procsize != pN_offset) {
626 afs_gcpags = AFS_GCPAGS_EINEXACT;
631 * check that the proc table size is a multiple of procsize.
634 if ((((char *)v.ve_proc - (char *)v.vb_proc) % procsize) != 0) {
635 afs_gcpags = AFS_GCPAGS_EPROCEND;
641 afs_gcpags_procsize = procsize;
650 * Clean up and shut down the AFS cache.
656 * Nothing interesting.
662 register struct afs_cbr *tsp, *nsp;
663 extern int afs_cold_shutdown;
664 extern int pagCounter;
667 AFS_STATCNT(shutdown_cache);
668 afs_WriteThroughDSlots();
669 if (afs_cold_shutdown) {
670 afs_cacheinit_flag = 0;
675 afs_cacheFiles = afs_cacheBlocks = 0;
676 pag_epoch = maxIHint = nihints = usedihint = 0;
678 cacheInode = volumeInode = (ino_t)0;
681 cacheInfoModTime = 0;
683 afs_fsfragsize = 1023;
684 bzero((char *)&afs_stats_cmperf, sizeof(afs_stats_cmperf));
685 bzero((char *)&cacheDev, sizeof(struct osi_dev));
691 void shutdown_vnodeops()
693 extern int afs_cold_shutdown;
694 #ifndef AFS_LINUX20_ENV
695 extern int afs_rd_stash_i;
698 extern int lastWarnTime;
700 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
701 struct buf *afs_bread_freebp = 0;
705 AFS_STATCNT(shutdown_vnodeops);
706 if (afs_cold_shutdown) {
707 #ifndef AFS_SUN5_ENV /* XXX */
710 #ifndef AFS_LINUX20_ENV
713 #if !defined(AFS_SGI_ENV) && !defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
714 afs_bread_freebp = 0;
725 register struct srvAddr *sa;
726 extern int afs_cold_shutdown;
728 AFS_STATCNT(shutdown_AFS);
729 if (afs_cold_shutdown) {
730 afs_resourceinit_flag = 0;
732 * Free Cells table allocations
736 register struct afs_q *cq, *tq;
737 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
738 tc = QTOC(cq); tq = QNext(cq);
740 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
741 afs_osi_Free(tc, sizeof(struct cell));
745 * Free Volumes table allocations
749 for (i = 0; i < NVOLS; i++) {
750 for (tv = afs_volumes[i]; tv; tv = tv->next) {
752 afs_osi_Free(tv->name, strlen(tv->name)+1);
761 * Free FreeVolList allocations
763 afs_osi_Free(Initialafs_freeVolList, afs_memvolumes * sizeof(struct volume));
764 afs_freeVolList = Initialafs_freeVolList = 0;
766 /* XXX HACK fort MEM systems XXX
768 * For -memcache cache managers when we run out of free in memory volumes
769 * we simply malloc more; we won't be able to free those additional volumes.
775 * Free Users table allocation
778 struct unixuser *tu, *ntu;
779 for (i=0; i < NUSERS; i++) {
780 for (tu=afs_users[i]; tu; tu = ntu) {
783 afs_osi_Free(tu->stp, tu->stLen);
785 EXP_RELE(tu->exporter);
786 afs_osi_Free(tu, sizeof(struct unixuser));
793 * Free Servers table allocation
796 struct server *ts, *nts;
797 struct conn *tc, *ntc;
798 register struct afs_cbr *tcbrp, *tbrp;
799 struct afs_cbr **lcbrpp;
801 for (i=0; i < NSERVERS; i++) {
802 for (ts = afs_servers[i]; ts; ts = nts) {
804 for (sa = ts->addr; sa; sa = sa->next_sa) {
807 * Free all server's connection structs
813 rx_DestroyConnection(tc->id);
815 afs_osi_Free(tc, sizeof(struct conn));
820 for (tcbrp = ts->cbrs; tcbrp; tcbrp = tbrp) {
822 * Free all server's callback structs
827 afs_osi_Free(ts, sizeof(struct server));
832 for (i=0; i<NFENTRIES; i++)
834 /* Reinitialize local globals to defaults */
835 afs_osi_Free(afs_sysname, MAXSYSNAME);
839 afs_setTimeHost = (struct server *)0;
841 afs_waitForever = afs_waitForeverCount = 0;
844 afs_server = (struct rx_service *)0;
845 RWLOCK_INIT(&afs_xconn, "afs_xconn");
846 bzero((char *)&afs_rootFid, sizeof(struct VenusFid));
847 RWLOCK_INIT(&afs_xuser, "afs_xuser");
848 RWLOCK_INIT(&afs_xvolume, "afs_xvolume"), RWLOCK_INIT(&afs_xcell, "afs_xcell");
849 RWLOCK_INIT(&afs_xserver, "afs_xserver"), LOCK_INIT(&afs_puttofileLock, "afs_puttofileLock");