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
10 /* 1/1/89: NB: this stuff is all going to be replaced. Don't take it too seriously */
15 Institution: The Information Technology Center, Carnegie-Mellon University
19 #include <afsconfig.h>
20 #include <afs/param.h>
26 #include <afs/afsint.h>
29 #include <sys/param.h>
30 #if !defined(AFS_SGI_ENV)
33 #else /* AFS_OSF_ENV */
34 #ifdef AFS_VFSINCL_ENV
37 #include <sys/fs/ufs_fs.h>
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
46 #else /* AFS_VFSINCL_ENV */
47 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
50 #endif /* AFS_VFSINCL_ENV */
51 #endif /* AFS_OSF_ENV */
52 #endif /* AFS_SGI_ENV */
53 #endif /* AFS_NT40_ENV */
71 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
73 #include <sys/mnttab.h>
74 #include <sys/mntent.h>
80 #if defined(AFS_SGI_ENV)
83 #ifdef AFS_SGI_EFS_IOPS_ENV
84 #define ROOTINO EFS_ROOTINO
85 #include <sys/fs/efs.h>
86 #include "sgiefs/efs.h" /* until 5.1 release */
91 #ifndef AFS_LINUX20_ENV
92 #include <fstab.h> /* Need to find in libc 5, present in libc 6 */
95 #endif /* AFS_SGI_ENV */
97 #endif /* AFS_HPUX_ENV */
101 #include <netinet/in.h>
102 #include <sys/wait.h>
105 #include <sys/time.h>
106 #endif /* ITIMER_REAL */
107 #endif /* AFS_NT40_ENV */
108 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
115 #include <afs/errors.h>
118 #include <afs/afssyscalls.h>
120 #include <afs/afsutil.h>
126 #include "partition.h"
127 #ifdef AFS_PTHREAD_ENV
129 #else /* AFS_PTHREAD_ENV */
130 #include "afs/assert.h"
131 #endif /* AFS_PTHREAD_ENV */
139 #define afs_stat stat64
140 #define afs_fstat fstat64
141 #define afs_open open64
142 #else /* !O_LARGEFILE */
143 #define afs_stat stat
144 #define afs_fstat fstat
145 #define afs_open open
146 #endif /* !O_LARGEFILE */
148 #ifdef AFS_PTHREAD_ENV
149 pthread_mutex_t vol_glock_mutex;
150 pthread_mutex_t vol_attach_mutex;
151 pthread_mutex_t vol_fsync_mutex;
152 pthread_cond_t vol_put_volume_cond;
153 pthread_cond_t vol_sleep_cond;
154 #endif /* AFS_PTHREAD_ENV */
157 extern void *calloc(), *realloc();
160 /*@printflike@*/ extern void Log(const char *format, ...);
162 /* Forward declarations */
163 static Volume *attach2(Error * ec, char *path,
164 register struct VolumeHeader *header,
165 struct DiskPartition *partp, int isbusy);
166 static void FreeVolume(Volume * vp);
167 static void VScanUpdateList(void);
168 static void InitLRU(int howMany);
169 static int GetVolumeHeader(register Volume * vp);
170 static void ReleaseVolumeHeader(register struct volHeader *hd);
171 static void FreeVolumeHeader(register Volume * vp);
172 static void AddVolumeToHashTable(register Volume * vp, int hashid);
173 static void DeleteVolumeFromHashTable(register Volume * vp);
174 static int VHold(Volume * vp);
175 static int VHold_r(Volume * vp);
176 static void GetBitmap(Error * ec, Volume * vp, VnodeClass class);
177 static void GetVolumePath(Error * ec, VolId volumeId, char **partitionp,
179 static void VReleaseVolumeHandles_r(Volume * vp);
180 static void VCloseVolumeHandles_r(Volume * vp);
182 int LogLevel; /* Vice loglevel--not defined as extern so that it will be
183 * defined when not linked with vice, XXXX */
184 ProgramType programType; /* The type of program using the package */
187 #define VOLUME_BITMAP_GROWSIZE 16 /* bytes, => 128vnodes */
188 /* Must be a multiple of 4 (1 word) !! */
189 #define VOLUME_HASH_TABLE_SIZE 128 /* Must be a power of 2!! */
190 #define VOLUME_HASH(volumeId) (volumeId&(VOLUME_HASH_TABLE_SIZE-1))
191 private Volume *VolumeHashTable[VOLUME_HASH_TABLE_SIZE];
194 /* This macro is used where an ffs() call does not exist. Was in util/ffs.c */
198 afs_int32 ffs_tmp = x;
202 for (ffs_i = 1;; ffs_i++) {
209 #endif /* !AFS_HAVE_FFS */
211 struct Lock vol_listLock; /* Lock obtained when listing volumes: prevents a volume from being missed if the volume is attached during a list volumes */
213 extern struct Lock FSYNC_handler_lock;
215 static int TimeZoneCorrection; /* Number of seconds west of GMT */
217 /* Common message used when the volume goes off line */
218 char *VSalvageMessage =
219 "Files in this volume are currently unavailable; call operations";
221 int VInit; /* 0 - uninitialized,
222 * 1 - initialized but not all volumes have been attached,
223 * 2 - initialized and all volumes have been attached,
224 * 3 - initialized, all volumes have been attached, and
225 * VConnectFS() has completed. */
228 bit32 VolumeCacheCheck; /* Incremented everytime a volume goes on line--
229 * used to stamp volume headers and in-core
230 * vnodes. When the volume goes on-line the
231 * vnode will be invalidated */
233 int VolumeCacheSize = 200, VolumeGets = 0, VolumeReplacements = 0, Vlooks = 0;
237 VInitVolumePackage(ProgramType pt, int nLargeVnodes, int nSmallVnodes,
238 int connect, int volcache)
240 int errors = 0; /* Number of errors while finding vice partitions. */
246 #ifdef AFS_PTHREAD_ENV
247 assert(pthread_mutex_init(&vol_glock_mutex, NULL) == 0);
248 assert(pthread_mutex_init(&vol_attach_mutex, NULL) == 0);
249 assert(pthread_cond_init(&vol_put_volume_cond, NULL) == 0);
250 assert(pthread_cond_init(&vol_sleep_cond, NULL) == 0);
251 #else /* AFS_PTHREAD_ENV */
253 #endif /* AFS_PTHREAD_ENV */
254 Lock_Init(&vol_listLock);
255 Lock_Init(&FSYNC_handler_lock);
256 srandom(time(0)); /* For VGetVolumeInfo */
257 gettimeofday(&tv, &tz);
258 TimeZoneCorrection = tz.tz_minuteswest * 60;
260 /* Ok, we have done enough initialization that fileserver can
261 * start accepting calls, even though the volumes may not be
262 * available just yet.
266 if (programType == fileServer) {
267 /* File server or "stand" */
271 if (volcache > VolumeCacheSize)
272 VolumeCacheSize = volcache;
273 InitLRU(VolumeCacheSize);
275 VInitVnodes(vLarge, nLargeVnodes);
276 VInitVnodes(vSmall, nSmallVnodes);
279 errors = VAttachPartitions();
283 if (programType == fileServer) {
286 struct DiskPartition *diskP;
289 /* Attach all the volumes in this partition */
290 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
291 int nAttached = 0, nUnattached = 0;
292 Log("Partition %s: attaching volumes\n", diskP->name);
293 dirp = opendir(VPartitionPath(diskP));
295 while ((dp = readdir(dirp))) {
297 p = strrchr(dp->d_name, '.');
298 if (p != NULL && strcmp(p, VHDREXT) == 0) {
301 vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
303 (*(vp ? &nAttached : &nUnattached))++;
304 if (error == VOFFLINE)
305 Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
306 else if (LogLevel >= 5) {
307 Log("Partition %s: attached volume %d (%s)\n",
308 diskP->name, VolumeNumber(dp->d_name),
316 Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, nAttached, nUnattached);
321 VInit = 2; /* Initialized, and all volumes have been attached */
322 if (programType == volumeUtility && connect) {
324 Log("Unable to connect to file server; aborted\n");
331 /* This must be called by any volume utility which needs to run while the
332 file server is also running. This is separated from VInitVolumePackage so
333 that a utility can fork--and each of the children can independently
334 initialize communication with the file server */
339 VOL_LOCK retVal = VConnectFS_r();
340 VOL_UNLOCK return retVal;
347 assert(VInit == 2 && programType == volumeUtility);
348 rc = FSYNC_clientInit();
355 VDisconnectFS_r(void)
357 assert(programType == volumeUtility);
365 VOL_LOCK VDisconnectFS_r();
372 register Volume *vp, *np;
373 register afs_int32 code;
375 Log("VShutdown: shutting down on-line volumes...\n");
376 for (i = 0; i < VOLUME_HASH_TABLE_SIZE; i++) {
377 /* try to hold first volume in the hash table */
378 for (vp = VolumeHashTable[i]; vp; vp = vp->hashNext) {
382 /* otherwise we go around again, trying another volume */
386 Log("VShutdown: Attempting to take volume %u offline.\n",
388 /* first compute np before releasing vp, in case vp disappears
389 * after releasing. Hold it, so it doesn't disapear. If we
390 * can't hold it, try the next one in the chain. Invariant
391 * at the top of this loop is that vp is held (has extra ref count).
393 for (np = vp->hashNext; np; np = np->hashNext) {
398 /* next, take the volume offline (drops reference count) */
399 VOffline_r(vp, "File server was shut down");
400 vp = np; /* next guy to try */
403 Log("VShutdown: complete.\n");
409 VOL_LOCK VShutdown_r();
414 ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
417 struct versionStamp *vsn;
432 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
434 FDH_REALLYCLOSE(fdP);
437 vsn = (struct versionStamp *)to;
438 if (FDH_READ(fdP, to, size) != size || vsn->magic != magic) {
440 FDH_REALLYCLOSE(fdP);
445 /* Check is conditional, in case caller wants to inspect version himself */
446 if (version && vsn->version != version) {
451 /* VolumeHeaderToDisk
452 * Allows for storing 64 bit inode numbers in on-disk volume header
456 VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h)
459 memset((char *)dh, 0, sizeof(VolumeDiskHeader_t));
460 dh->stamp = h->stamp;
462 dh->parent = h->parent;
464 #ifdef AFS_64BIT_IOPS_ENV
465 dh->volumeInfo_lo = (afs_int32) h->volumeInfo & 0xffffffff;
466 dh->volumeInfo_hi = (afs_int32) (h->volumeInfo >> 32) & 0xffffffff;
467 dh->smallVnodeIndex_lo = (afs_int32) h->smallVnodeIndex & 0xffffffff;
468 dh->smallVnodeIndex_hi =
469 (afs_int32) (h->smallVnodeIndex >> 32) & 0xffffffff;
470 dh->largeVnodeIndex_lo = (afs_int32) h->largeVnodeIndex & 0xffffffff;
471 dh->largeVnodeIndex_hi =
472 (afs_int32) (h->largeVnodeIndex >> 32) & 0xffffffff;
473 dh->linkTable_lo = (afs_int32) h->linkTable & 0xffffffff;
474 dh->linkTable_hi = (afs_int32) (h->linkTable >> 32) & 0xffffffff;
476 dh->volumeInfo_lo = h->volumeInfo;
477 dh->smallVnodeIndex_lo = h->smallVnodeIndex;
478 dh->largeVnodeIndex_lo = h->largeVnodeIndex;
479 dh->linkTable_lo = h->linkTable;
483 /* DiskToVolumeHeader
484 * Reads volume header file from disk, convering 64 bit inodes
485 * if required. Makes the assumption that AFS has *always*
486 * zero'd the volume header file so that high parts of inode
487 * numbers are 0 in older (SGI EFS) volume header files.
490 DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh)
492 memset((char *)h, 0, sizeof(VolumeHeader_t));
493 h->stamp = dh->stamp;
495 h->parent = dh->parent;
497 #ifdef AFS_64BIT_IOPS_ENV
499 (Inode) dh->volumeInfo_lo | ((Inode) dh->volumeInfo_hi << 32);
502 (Inode) dh->smallVnodeIndex_lo | ((Inode) dh->
503 smallVnodeIndex_hi << 32);
506 (Inode) dh->largeVnodeIndex_lo | ((Inode) dh->
507 largeVnodeIndex_hi << 32);
509 (Inode) dh->linkTable_lo | ((Inode) dh->linkTable_hi << 32);
511 h->volumeInfo = dh->volumeInfo_lo;
512 h->smallVnodeIndex = dh->smallVnodeIndex_lo;
513 h->largeVnodeIndex = dh->largeVnodeIndex_lo;
514 h->linkTable = dh->linkTable_lo;
520 WriteVolumeHeader_r(ec, vp)
524 IHandle_t *h = V_diskDataHandle(vp);
534 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
536 FDH_REALLYCLOSE(fdP);
539 if (FDH_WRITE(fdP, (char *)&V_disk(vp), sizeof(V_disk(vp)))
540 != sizeof(V_disk(vp))) {
542 FDH_REALLYCLOSE(fdP);
548 /* Attach an existing volume, given its pathname, and return a
549 pointer to the volume header information. The volume also
550 normally goes online at this time. An offline volume
551 must be reattached to make it go online */
553 VAttachVolumeByName(Error * ec, char *partition, char *name, int mode)
556 VATTACH_LOCK VOL_LOCK retVal =
557 VAttachVolumeByName_r(ec, partition, name, mode);
558 VOL_UNLOCK VATTACH_UNLOCK return retVal;
562 VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
566 struct afs_stat status;
567 struct VolumeDiskHeader diskHeader;
568 struct VolumeHeader iheader;
569 struct DiskPartition *partp;
573 if (programType == volumeUtility) {
575 VLockPartition_r(partition);
577 if (programType == fileServer) {
578 vp = VGetVolume_r(ec, VolumeNumber(name));
582 if (vp->specialStatus == VBUSY)
584 VDetachVolume_r(ec, vp);
586 Log("VAttachVolume: Error detaching volume (%s)\n", name);
591 if (!(partp = VGetPartition_r(partition, 0))) {
593 Log("VAttachVolume: Error getting partition (%s)\n", partition);
598 strcpy(path, VPartitionPath(partp));
601 VOL_UNLOCK if ((fd = afs_open(path, O_RDONLY)) == -1
602 || afs_fstat(fd, &status) == -1) {
603 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
606 VOL_LOCK *ec = VNOVOL;
609 n = read(fd, &diskHeader, sizeof(diskHeader));
611 VOL_LOCK if (n != sizeof(diskHeader)
612 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
613 Log("VAttachVolume: Error reading volume header %s\n", path);
617 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
618 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
623 DiskToVolumeHeader(&iheader, &diskHeader);
624 if (programType == volumeUtility && mode != V_SECRETLY) {
625 if (FSYNC_askfs(iheader.id, partition, FSYNC_NEEDVOLUME, mode)
627 Log("VAttachVolume: attach of volume %u apparently denied by file server\n", iheader.id);
628 *ec = VNOVOL; /* XXXX */
633 vp = attach2(ec, path, &iheader, partp, isbusy);
634 if (programType == volumeUtility && vp) {
635 /* duplicate computation in fssync.c about whether the server
636 * takes the volume offline or not. If the volume isn't
637 * offline, we must not return it when we detach the volume,
638 * or the server will abort */
639 if (mode == V_READONLY
640 || (!VolumeWriteable(vp) && (mode == V_CLONE || mode == V_DUMP)))
641 vp->needsPutBack = 0;
643 vp->needsPutBack = 1;
645 /* OK, there's a problem here, but one that I don't know how to
646 * fix right now, and that I don't think should arise often.
647 * Basically, we should only put back this volume to the server if
648 * it was given to us by the server, but since we don't have a vp,
649 * we can't run the VolumeWriteable function to find out as we do
650 * above when computing vp->needsPutBack. So we send it back, but
651 * there's a path in VAttachVolume on the server which may abort
652 * if this volume doesn't have a header. Should be pretty rare
653 * for all of that to happen, but if it does, probably the right
654 * fix is for the server to allow the return of readonly volumes
655 * that it doesn't think are really checked out. */
656 if (programType == volumeUtility && vp == NULL && mode != V_SECRETLY) {
657 FSYNC_askfs(iheader.id, partition, FSYNC_ON, 0);
658 } else if (programType == fileServer && vp) {
659 V_needsCallback(vp) = 0;
661 if (VInit >= 2 && V_BreakVolumeCallbacks) {
662 Log("VAttachVolume: Volume %u was changed externally; breaking callbacks\n", V_id(vp));
663 (*V_BreakVolumeCallbacks) (V_id(vp));
666 VUpdateVolume_r(ec, vp);
668 Log("VAttachVolume: Error updating volume\n");
673 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
674 /* This is a hack: by temporarily settint the incore
675 * dontSalvage flag ON, the volume will be put back on the
676 * Update list (with dontSalvage OFF again). It will then
677 * come back in N minutes with DONT_SALVAGE eventually
678 * set. This is the way that volumes that have never had
679 * it set get it set; or that volumes that have been
680 * offline without DONT SALVAGE having been set also
681 * eventually get it set */
682 V_dontSalvage(vp) = DONT_SALVAGE;
683 VAddToVolumeUpdateList_r(ec, vp);
685 Log("VAttachVolume: Error adding volume to update list\n");
692 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
696 if (programType == volumeUtility) {
697 VUnlockPartition_r(partition);
706 attach2(Error * ec, char *path, register struct VolumeHeader * header,
707 struct DiskPartition * partp, int isbusy)
711 VOL_UNLOCK vp = (Volume *) calloc(1, sizeof(Volume));
713 vp->specialStatus = (byte) (isbusy ? VBUSY : 0);
714 vp->device = partp->device;
715 vp->partition = partp;
716 IH_INIT(vp->vnodeIndex[vLarge].handle, partp->device, header->parent,
717 header->largeVnodeIndex);
718 IH_INIT(vp->vnodeIndex[vSmall].handle, partp->device, header->parent,
719 header->smallVnodeIndex);
720 IH_INIT(vp->diskDataHandle, partp->device, header->parent,
722 IH_INIT(vp->linkHandle, partp->device, header->parent, header->linkTable);
723 vp->cacheCheck = ++VolumeCacheCheck;
724 /* just in case this ever rolls over */
726 vp->cacheCheck = ++VolumeCacheCheck;
727 vp->shuttingDown = 0;
728 vp->goingOffline = 0;
730 VOL_LOCK GetVolumeHeader(vp);
731 VOL_UNLOCK(void) ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
732 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
735 Log("VAttachVolume: Error reading diskDataHandle vol header %s; error=%u\n", path, *ec);
738 struct IndexFileHeader iHead;
740 #if OPENAFS_VOL_STATS
742 * We just read in the diskstuff part of the header. If the detailed
743 * volume stats area has not yet been initialized, we should bzero the
744 * area and mark it as initialized.
746 if (!(V_stat_initialized(vp))) {
747 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
748 V_stat_initialized(vp) = 1;
750 #endif /* OPENAFS_VOL_STATS */
751 VOL_UNLOCK(void) ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
752 (char *)&iHead, sizeof(iHead),
753 SMALLINDEXMAGIC, SMALLINDEXVERSION);
755 Log("VAttachVolume: Error reading smallVnode vol header %s; error=%u\n", path, *ec);
759 struct IndexFileHeader iHead;
760 VOL_UNLOCK(void) ReadHeader(ec, vp->vnodeIndex[vLarge].handle,
761 (char *)&iHead, sizeof(iHead),
762 LARGEINDEXMAGIC, LARGEINDEXVERSION);
764 Log("VAttachVolume: Error reading largeVnode vol header %s; error=%u\n", path, *ec);
769 struct versionStamp stamp;
770 VOL_UNLOCK(void) ReadHeader(ec, V_linkHandle(vp), (char *)&stamp,
771 sizeof(stamp), LINKTABLEMAGIC,
774 Log("VAttachVolume: Error reading namei vol header %s; error=%u\n", path, *ec);
779 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
783 if (V_needsSalvaged(vp)) {
784 if (vp->specialStatus)
785 vp->specialStatus = 0;
786 Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
790 if (programType == fileServer) {
792 if (V_inUse(vp) && VolumeWriteable(vp)) {
793 if (!V_needsSalvaged(vp)) {
794 V_needsSalvaged(vp) = 1;
795 VUpdateVolume_r(ec, vp);
798 Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
802 #endif /* FAST_RESTART */
803 if (V_destroyMe(vp) == DESTROY_ME) {
805 Log("VAttachVolume: volume %s is junk; it should be destroyed at next salvage\n", path);
811 AddVolumeToHashTable(vp, V_id(vp));
812 vp->nextVnodeUnique = V_uniquifier(vp);
813 vp->vnodeIndex[vSmall].bitmap = vp->vnodeIndex[vLarge].bitmap = NULL;
815 if (programType == fileServer && VolumeWriteable(vp)) {
817 for (i = 0; i < nVNODECLASSES; i++) {
818 VOL_UNLOCK GetBitmap(ec, vp, i);
821 Log("VAttachVolume: error getting bitmap for volume (%s)\n",
827 #endif /* BITMAP_LATER */
829 if (programType == fileServer) {
830 if (vp->specialStatus)
831 vp->specialStatus = 0;
832 if (V_blessed(vp) && V_inService(vp) && !V_needsSalvaged(vp)) {
834 V_offlineMessage(vp)[0] = '\0';
841 /* Attach an existing volume.
842 The volume also normally goes online at this time.
843 An offline volume must be reattached to make it go online.
847 VAttachVolume(Error * ec, VolumeId volumeId, int mode)
850 VATTACH_LOCK VOL_LOCK retVal = VAttachVolume_r(ec, volumeId, mode);
851 VOL_UNLOCK VATTACH_UNLOCK return retVal;
855 VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
858 GetVolumePath(ec, volumeId, &part, &name);
862 vp = VGetVolume_r(&error, volumeId);
864 assert(V_inUse(vp) == 0);
865 VDetachVolume_r(ec, vp);
869 return VAttachVolumeByName_r(ec, part, name, mode);
872 /* Increment a reference count to a volume, sans context swaps. Requires
873 * possibly reading the volume header in from the disk, since there's
874 * an invariant in the volume package that nUsers>0 ==> vp->header is valid.
876 * N.B. This call can fail if we can't read in the header!! In this case
877 * we still guarantee we won't context swap, but the ref count won't be
878 * incremented (otherwise we'd violate the invariant).
881 VHold_r(register Volume * vp)
885 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
886 VolumeReplacements++;
887 ReadHeader(&error, V_diskDataHandle(vp), (char *)&V_disk(vp),
888 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
897 VHold(register Volume * vp)
900 VOL_LOCK retVal = VHold_r(vp);
901 VOL_UNLOCK return retVal;
905 VTakeOffline_r(register Volume * vp)
907 assert(vp->nUsers > 0);
908 assert(programType == fileServer);
909 vp->goingOffline = 1;
910 V_needsSalvaged(vp) = 1;
914 VTakeOffline(register Volume * vp)
916 VOL_LOCK VTakeOffline_r(vp);
920 VPutVolume_r(register Volume * vp)
922 assert(--vp->nUsers >= 0);
923 if (vp->nUsers == 0) {
924 ReleaseVolumeHeader(vp->header);
925 if (vp->goingOffline) {
927 assert(programType == fileServer);
928 vp->goingOffline = 0;
930 VUpdateVolume_r(&error, vp);
931 VCloseVolumeHandles_r(vp);
933 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
935 if (V_offlineMessage(vp)[0])
936 Log(" (%s)", V_offlineMessage(vp));
939 #ifdef AFS_PTHREAD_ENV
940 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
941 #else /* AFS_PTHREAD_ENV */
942 LWP_NoYieldSignal(VPutVolume);
943 #endif /* AFS_PTHREAD_ENV */
945 if (vp->shuttingDown) {
946 VReleaseVolumeHandles_r(vp);
948 if (programType == fileServer)
949 #ifdef AFS_PTHREAD_ENV
950 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
951 #else /* AFS_PTHREAD_ENV */
952 LWP_NoYieldSignal(VPutVolume);
953 #endif /* AFS_PTHREAD_ENV */
959 VPutVolume(register Volume * vp)
961 VOL_LOCK VPutVolume_r(vp);
964 /* Get a pointer to an attached volume. The pointer is returned regardless
965 of whether or not the volume is in service or on/off line. An error
966 code, however, is returned with an indication of the volume's status */
968 VGetVolume(Error * ec, VolId volumeId)
971 VOL_LOCK retVal = VGetVolume_r(ec, volumeId);
972 VOL_UNLOCK return retVal;
976 VGetVolume_r(Error * ec, VolId volumeId)
979 unsigned short V0 = 0, V1 = 0, V2 = 0, V3 = 0, V4 = 0, V5 = 0, V6 =
980 0, V7 = 0, V8 = 0, V9 = 0;
981 unsigned short V10 = 0, V11 = 0, V12 = 0, V13 = 0, V14 = 0, V15 = 0;
986 for (vp = VolumeHashTable[VOLUME_HASH(volumeId)];
987 vp && vp->hashid != volumeId; vp = vp->hashNext)
994 /* Until we have reached an initialization level of 2
995 * we don't know whether this volume exists or not.
996 * We can't sleep and retry later because before a volume
997 * is attached, the caller tries to get it first. Just
998 * return VOFFLINE and the caller can choose whether to
999 * retry the command or not. */
1010 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
1012 VolumeReplacements++;
1013 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
1014 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
1018 /* Only log the error if it was a totally unexpected error. Simply
1019 * a missing inode is likely to be caused by the volume being deleted */
1020 if (errno != ENXIO || LogLevel)
1021 Log("Volume %u: couldn't reread volume header\n",
1029 if (vp->shuttingDown) {
1035 if (programType == fileServer) {
1037 if (vp->goingOffline) {
1039 #ifdef AFS_PTHREAD_ENV
1040 pthread_cond_wait(&vol_put_volume_cond, &vol_glock_mutex);
1041 #else /* AFS_PTHREAD_ENV */
1042 LWP_WaitProcess(VPutVolume);
1043 #endif /* AFS_PTHREAD_ENV */
1046 if (vp->specialStatus) {
1048 *ec = vp->specialStatus;
1049 } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
1052 } else if (V_inUse(vp) == 0) {
1062 /* if no error, bump nUsers */
1071 /* For both VForceOffline and VOffline, we close all relevant handles.
1072 * For VOffline, if we re-attach the volume, the files may possible be
1073 * different than before.
1076 VReleaseVolumeHandles_r(Volume * vp)
1078 DFlushVolume(V_id(vp));
1079 VReleaseVnodeFiles_r(vp);
1081 /* Too time consuming and unnecessary for the volserver */
1082 if (programType != volumeUtility) {
1083 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
1084 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
1085 IH_CONDSYNC(vp->diskDataHandle);
1087 IH_CONDSYNC(vp->linkHandle);
1088 #endif /* AFS_NT40_ENV */
1091 IH_RELEASE(vp->vnodeIndex[vLarge].handle);
1092 IH_RELEASE(vp->vnodeIndex[vSmall].handle);
1093 IH_RELEASE(vp->diskDataHandle);
1094 IH_RELEASE(vp->linkHandle);
1097 /* Force the volume offline, set the salvage flag. No further references to
1098 * the volume through the volume package will be honored. */
1100 VForceOffline_r(Volume * vp)
1105 strcpy(V_offlineMessage(vp),
1106 "Forced offline due to internal error: volume needs to be salvaged");
1107 Log("Volume %u forced offline: it needs salvaging!\n", V_id(vp));
1109 vp->goingOffline = 0;
1110 V_needsSalvaged(vp) = 1;
1111 VUpdateVolume_r(&error, vp);
1112 #ifdef AFS_PTHREAD_ENV
1113 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
1114 #else /* AFS_PTHREAD_ENV */
1115 LWP_NoYieldSignal(VPutVolume);
1116 #endif /* AFS_PTHREAD_ENV */
1118 VReleaseVolumeHandles_r(vp);
1123 VForceOffline(Volume * vp)
1125 VOL_LOCK VForceOffline_r(vp);
1128 /* The opposite of VAttachVolume. The volume header is written to disk, with
1129 the inUse bit turned off. A copy of the header is maintained in memory,
1130 however (which is why this is VOffline, not VDetach).
1133 VOffline_r(Volume * vp, char *message)
1136 VolumeId vid = V_id(vp);
1137 assert(programType != volumeUtility);
1142 if (V_offlineMessage(vp)[0] == '\0')
1143 strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
1144 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
1145 vp->goingOffline = 1;
1147 vp = VGetVolume_r(&error, vid); /* Wait for it to go offline */
1148 if (vp) /* In case it was reattached... */
1153 VOffline(Volume * vp, char *message)
1155 VOL_LOCK VOffline_r(vp, message);
1158 /* For VDetachVolume, we close all cached file descriptors, but keep
1159 * the Inode handles in case we need to read from a busy volume.
1162 VCloseVolumeHandles_r(Volume * vp)
1164 DFlushVolume(V_id(vp));
1165 VCloseVnodeFiles_r(vp);
1167 /* Too time consuming and unnecessary for the volserver */
1168 if (programType != volumeUtility) {
1169 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
1170 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
1171 IH_CONDSYNC(vp->diskDataHandle);
1173 IH_CONDSYNC(vp->linkHandle);
1174 #endif /* AFS_NT40_ENV */
1177 IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
1178 IH_REALLYCLOSE(vp->vnodeIndex[vSmall].handle);
1179 IH_REALLYCLOSE(vp->diskDataHandle);
1180 IH_REALLYCLOSE(vp->linkHandle);
1183 /* This gets used for the most part by utility routines that don't want
1184 * to keep all the volume headers around. Generally, the file server won't
1185 * call this routine, because then the offline message in the volume header
1186 * (or other information) will still be available to clients. For NAMEI, also
1187 * close the file handles.
1190 VDetachVolume_r(Error * ec, Volume * vp)
1193 struct DiskPartition *tpartp;
1194 int notifyServer, useDone;
1196 *ec = 0; /* always "succeeds" */
1197 if (programType == volumeUtility) {
1198 notifyServer = vp->needsPutBack;
1199 useDone = (V_destroyMe(vp) == DESTROY_ME);
1201 tpartp = vp->partition;
1203 DeleteVolumeFromHashTable(vp);
1204 vp->shuttingDown = 1;
1206 /* Will be detached sometime in the future--this is OK since volume is offline */
1208 if (programType == volumeUtility && notifyServer) {
1210 * Note: The server is not notified in the case of a bogus volume
1211 * explicitly to make it possible to create a volume, do a partial
1212 * restore, then abort the operation without ever putting the volume
1213 * online. This is essential in the case of a volume move operation
1214 * between two partitions on the same server. In that case, there
1215 * would be two instances of the same volume, one of them bogus,
1216 * which the file server would attempt to put on line
1219 /* don't put online */
1220 FSYNC_askfs(volume, tpartp->name, FSYNC_DONE, 0);
1222 /* fs can use it again */
1223 FSYNC_askfs(volume, tpartp->name, FSYNC_ON, 0);
1224 /* Dettaching it so break all callbacks on it */
1225 if (V_BreakVolumeCallbacks) {
1226 Log("volume %u detached; breaking all call backs\n", volume);
1227 (*V_BreakVolumeCallbacks) (volume);
1234 VDetachVolume(Error * ec, Volume * vp)
1236 VOL_LOCK VDetachVolume_r(ec, vp);
1241 VAllocBitmapEntry_r(Error * ec, Volume * vp, register struct vnodeIndex
1244 register byte *bp, *ep;
1246 /* This test is probably redundant */
1247 if (!VolumeWriteable(vp)) {
1248 *ec = (bit32) VREADONLY;
1252 if ((programType == fileServer) && !index->bitmap) {
1255 if (vp->specialStatus == VBUSY) {
1256 if (vp->goingOffline) { /* vos dump waiting for the volume to
1257 * go offline. We probably come here
1258 * from AddNewReadableResidency */
1261 VOL_UNLOCK while (vp->specialStatus == VBUSY)
1262 #ifdef AFS_PTHREAD_ENV
1264 #else /* AFS_PTHREAD_ENV */
1266 #endif /* AFS_PTHREAD_ENV */
1269 if (!index->bitmap) {
1270 vp->specialStatus = VBUSY; /* Stop anyone else from using it. */
1271 for (i = 0; i < nVNODECLASSES; i++) {
1272 VOL_UNLOCK GetBitmap(ec, vp, i);
1274 vp->specialStatus = 0;
1275 vp->shuttingDown = 1; /* Let who has it free it. */
1280 vp->specialStatus = 0; /* Allow others to have access. */
1283 #endif /* BITMAP_LATER */
1284 bp = index->bitmap + index->bitmapOffset;
1285 ep = index->bitmap + index->bitmapSize;
1287 if ((*(bit32 *) bp) != (bit32) 0xffffffff) {
1289 index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
1292 o = ffs(~*bp) - 1; /* ffs is documented in BSTRING(3) */
1294 return (VnodeId) ((bp - index->bitmap) * 8 + o);
1296 bp += sizeof(bit32) /* i.e. 4 */ ;
1298 /* No bit map entry--must grow bitmap */
1300 realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
1303 bp += index->bitmapSize;
1304 memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
1305 index->bitmapOffset = index->bitmapSize;
1306 index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
1308 return index->bitmapOffset * 8;
1312 VAllocBitmapEntry(Error * ec, Volume * vp, register struct vnodeIndex * index)
1315 VOL_LOCK retVal = VAllocBitmapEntry_r(ec, vp, index);
1316 VOL_UNLOCK return retVal;
1320 VFreeBitMapEntry_r(Error * ec, register struct vnodeIndex *index,
1323 unsigned int offset;
1328 #endif /* BITMAP_LATER */
1329 offset = bitNumber >> 3;
1330 if (offset >= index->bitmapSize) {
1334 if (offset < index->bitmapOffset)
1335 index->bitmapOffset = offset & ~3; /* Truncate to nearest bit32 */
1336 *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
1340 VFreeBitMapEntry(Error * ec, register struct vnodeIndex *index,
1343 VOL_LOCK VFreeBitMapEntry_r(ec, index, bitNumber);
1347 VUpdateVolume_r(Error * ec, Volume * vp)
1350 if (programType == fileServer)
1352 (V_inUse(vp) ? V_nextVnodeUnique(vp) +
1353 200 : V_nextVnodeUnique(vp));
1354 /*printf("Writing volume header for '%s'\n", V_name(vp)); */
1355 WriteVolumeHeader_r(ec, vp);
1357 Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
1358 V_id(vp), V_name(vp));
1359 VForceOffline_r(vp);
1364 VUpdateVolume(Error * ec, Volume * vp)
1366 VOL_LOCK VUpdateVolume_r(ec, vp);
1370 VSyncVolume_r(Error * ec, Volume * vp)
1373 VUpdateVolume_r(ec, vp);
1376 fdP = IH_OPEN(V_diskDataHandle(vp));
1377 assert(fdP != NULL);
1378 code = FDH_SYNC(fdP);
1385 VSyncVolume(Error * ec, Volume * vp)
1387 VOL_LOCK VSyncVolume_r(ec, vp);
1391 FreeVolume(Volume * vp)
1396 for (i = 0; i < nVNODECLASSES; i++)
1397 if (vp->vnodeIndex[i].bitmap)
1398 free(vp->vnodeIndex[i].bitmap);
1399 FreeVolumeHeader(vp);
1400 DeleteVolumeFromHashTable(vp);
1405 GetBitmap(Error * ec, Volume * vp, VnodeClass class)
1407 StreamHandle_t *file;
1410 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1411 struct vnodeIndex *vip = &vp->vnodeIndex[class];
1412 struct VnodeDiskObject *vnode;
1413 unsigned int unique = 0;
1417 #endif /* BITMAP_LATER */
1421 fdP = IH_OPEN(vip->handle);
1422 assert(fdP != NULL);
1423 file = FDH_FDOPEN(fdP, "r");
1424 assert(file != NULL);
1425 vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
1426 assert(vnode != NULL);
1427 size = OS_SIZE(fdP->fd_fd);
1429 nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
1431 vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4; /* The 10 is a little extra so
1432 * a few files can be created in this volume,
1433 * the whole thing is rounded up to nearest 4
1434 * bytes, because the bit map allocator likes
1437 BitMap = (byte *) calloc(1, vip->bitmapSize);
1438 assert(BitMap != NULL);
1439 #else /* BITMAP_LATER */
1440 vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
1441 assert(vip->bitmap != NULL);
1442 vip->bitmapOffset = 0;
1443 #endif /* BITMAP_LATER */
1444 if (STREAM_SEEK(file, vcp->diskSize, 0) != -1) {
1446 for (bitNumber = 0; bitNumber < nVnodes + 100; bitNumber++) {
1447 if (STREAM_READ(vnode, vcp->diskSize, 1, file) != 1)
1449 if (vnode->type != vNull) {
1450 if (vnode->vnodeMagic != vcp->magic) {
1451 Log("GetBitmap: addled vnode index in volume %s; volume needs salvage\n", V_name(vp));
1456 *(BitMap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
1457 #else /* BITMAP_LATER */
1458 *(vip->bitmap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
1459 #endif /* BITMAP_LATER */
1460 if (unique <= vnode->uniquifier)
1461 unique = vnode->uniquifier + 1;
1463 #ifndef AFS_PTHREAD_ENV
1464 if ((bitNumber & 0x00ff) == 0x0ff) { /* every 256 iterations */
1467 #endif /* !AFS_PTHREAD_ENV */
1470 if (vp->nextVnodeUnique < unique) {
1471 Log("GetBitmap: bad volume uniquifier for volume %s; volume needs salvage\n", V_name(vp));
1474 /* Paranoia, partly justified--I think fclose after fdopen
1475 * doesn't seem to close fd. In any event, the documentation
1476 * doesn't specify, so it's safer to close it twice.
1482 /* There may have been a racing condition with some other thread, both
1483 * creating the bitmaps for this volume. If the other thread was faster
1484 * the pointer to bitmap should already be filled and we can free ours.
1486 if (vip->bitmap == NULL) {
1487 vip->bitmap = BitMap;
1488 vip->bitmapOffset = 0;
1490 free((byte *) BitMap);
1491 #endif /* BITMAP_LATER */
1495 GetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
1497 static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
1498 char path[VMAXPATHLEN];
1500 struct DiskPartition *dp;
1504 (void)afs_snprintf(&name[1], (sizeof name) - 1, VFORMAT, volumeId);
1505 for (dp = DiskPartitionList; dp; dp = dp->next) {
1506 struct afs_stat status;
1507 strcpy(path, VPartitionPath(dp));
1509 if (afs_stat(path, &status) == 0) {
1510 strcpy(partition, dp->name);
1517 *partitionp = *namep = NULL;
1519 *partitionp = partition;
1525 VolumeNumber(char *name)
1529 return atoi(name + 1);
1533 VolumeExternalName(VolumeId volumeId)
1535 static char name[VMAXPATHLEN];
1536 (void)afs_snprintf(name, sizeof name, VFORMAT, volumeId);
1540 #if OPENAFS_VOL_STATS
1541 #define OneDay (86400) /* 24 hours' worth of seconds */
1543 #define OneDay (24*60*60) /* 24 hours */
1544 #endif /* OPENAFS_VOL_STATS */
1546 #define Midnight(date) ((date-TimeZoneCorrection)/OneDay*OneDay+TimeZoneCorrection)
1548 /*------------------------------------------------------------------------
1549 * [export] VAdjustVolumeStatistics
1552 * If we've passed midnight, we need to update all the day use
1553 * statistics as well as zeroing the detailed volume statistics
1554 * (if we are implementing them).
1557 * vp : Pointer to the volume structure describing the lucky
1558 * volume being considered for update.
1564 * Nothing interesting.
1568 *------------------------------------------------------------------------*/
1571 VAdjustVolumeStatistics_r(register Volume * vp)
1573 unsigned int now = FT_ApproxTime();
1575 if (now - V_dayUseDate(vp) > OneDay) {
1578 ndays = (now - V_dayUseDate(vp)) / OneDay;
1579 for (i = 6; i > ndays - 1; i--)
1580 V_weekUse(vp)[i] = V_weekUse(vp)[i - ndays];
1581 for (i = 0; i < ndays - 1 && i < 7; i++)
1582 V_weekUse(vp)[i] = 0;
1584 V_weekUse(vp)[ndays - 1] = V_dayUse(vp);
1586 V_dayUseDate(vp) = Midnight(now);
1588 #if OPENAFS_VOL_STATS
1590 * All we need to do is bzero the entire VOL_STATS_BYTES of
1591 * the detailed volume statistics area.
1593 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
1594 #endif /* OPENAFS_VOL_STATS */
1597 /*It's been more than a day of collection */
1599 * Always return happily.
1602 } /*VAdjustVolumeStatistics */
1605 VAdjustVolumeStatistics(register Volume * vp)
1608 VOL_LOCK retVal = VAdjustVolumeStatistics_r(vp);
1609 VOL_UNLOCK return retVal;
1613 VBumpVolumeUsage_r(register Volume * vp)
1615 unsigned int now = FT_ApproxTime();
1616 if (now - V_dayUseDate(vp) > OneDay)
1617 VAdjustVolumeStatistics_r(vp);
1619 * Save the volume header image to disk after every 128 bumps to dayUse.
1621 if ((V_dayUse(vp)++ & 127) == 0) {
1623 VUpdateVolume_r(&error, vp);
1628 VBumpVolumeUsage(register Volume * vp)
1630 VOL_LOCK VBumpVolumeUsage_r(vp);
1634 VSetDiskUsage_r(void)
1636 static int FifteenMinuteCounter = 0;
1639 /* NOTE: Don't attempt to access the partitions list until the
1640 * initialization level indicates that all volumes are attached,
1641 * which implies that all partitions are initialized. */
1642 #ifdef AFS_PTHREAD_ENV
1644 #else /* AFS_PTHREAD_ENV */
1646 #endif /* AFS_PTHREAD_ENV */
1649 VResetDiskUsage_r();
1650 if (++FifteenMinuteCounter == 3) {
1651 FifteenMinuteCounter = 0;
1659 VOL_LOCK VSetDiskUsage_r();
1662 /* The number of minutes that a volume hasn't been updated before the
1663 * "Dont salvage" flag in the volume header will be turned on */
1664 #define SALVAGE_INTERVAL (10*60)
1666 static VolumeId *UpdateList; /* Pointer to array of Volume ID's */
1667 static int nUpdatedVolumes; /* Updated with entry in UpdateList, salvage after crash flag on */
1668 static int updateSize; /* number of entries possible */
1669 #define UPDATE_LIST_SIZE 100 /* size increment */
1672 VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
1675 vp->updateTime = FT_ApproxTime();
1676 if (V_dontSalvage(vp) == 0)
1678 V_dontSalvage(vp) = 0;
1679 VSyncVolume_r(ec, vp);
1683 updateSize = UPDATE_LIST_SIZE;
1684 UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
1686 if (nUpdatedVolumes == updateSize) {
1687 updateSize += UPDATE_LIST_SIZE;
1689 (VolumeId *) realloc(UpdateList,
1690 sizeof(VolumeId) * updateSize);
1693 assert(UpdateList != NULL);
1694 UpdateList[nUpdatedVolumes++] = V_id(vp);
1698 VScanUpdateList(void)
1700 register int i, gap;
1701 register Volume *vp;
1703 afs_uint32 now = FT_ApproxTime();
1704 /* Be careful with this code, since it works with interleaved calls to AddToVolumeUpdateList */
1705 for (i = gap = 0; i < nUpdatedVolumes; i++) {
1706 vp = VGetVolume_r(&error, UpdateList[i - gap] = UpdateList[i]);
1709 } else if (vp->nUsers == 1 && now - vp->updateTime > SALVAGE_INTERVAL) {
1710 V_dontSalvage(vp) = DONT_SALVAGE;
1711 VUpdateVolume_r(&error, vp); /* No need to fsync--not critical */
1716 #ifndef AFS_PTHREAD_ENV
1718 #endif /* !AFS_PTHREAD_ENV */
1720 nUpdatedVolumes -= gap;
1723 /***************************************************/
1724 /* Add on routines to manage a volume header cache */
1725 /***************************************************/
1727 static struct volHeader *volumeLRU;
1729 /* Allocate a bunch of headers; string them together */
1731 InitLRU(int howMany)
1733 register struct volHeader *hp;
1734 if (programType != fileServer)
1736 hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
1738 ReleaseVolumeHeader(hp++);
1741 /* Get a volume header from the LRU list; update the old one if necessary */
1742 /* Returns 1 if there was already a header, which is removed from the LRU list */
1744 GetVolumeHeader(register Volume * vp)
1747 register struct volHeader *hd;
1749 static int everLogged = 0;
1751 old = (vp->header != 0); /* old == volume already has a header */
1752 if (programType != fileServer) {
1754 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
1762 if (volumeLRU == hd)
1763 volumeLRU = hd->next;
1764 assert(hd->back == vp);
1767 /* not currently in use and least recently used */
1768 hd = volumeLRU->prev;
1770 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
1771 /* make it look like single elt LRU */
1772 hd->prev = hd->next = hd;
1774 Log("****Allocated more volume headers, probably leak****\n");
1779 if (hd->diskstuff.inUse) {
1780 WriteVolumeHeader_r(&error, hd->back);
1781 /* Ignore errors; catch them later */
1783 hd->back->header = 0;
1788 if (hd->next) { /* hd->next != 0 --> in LRU chain (we zero it later) */
1789 hd->prev->next = hd->next; /* pull hd out of LRU list */
1790 hd->next->prev = hd->prev; /* if hd only element, this is noop */
1792 hd->next = hd->prev = 0;
1793 /* if not in LRU chain, next test won't be true */
1794 if (hd == volumeLRU) /* last header item, turn into empty list */
1800 /* Put it at the top of the LRU chain */
1802 ReleaseVolumeHeader(register struct volHeader *hd)
1804 if (programType != fileServer)
1806 if (!hd || hd->next) /* no header, or header already released */
1809 hd->next = hd->prev = hd;
1811 hd->prev = volumeLRU->prev;
1812 hd->next = volumeLRU;
1813 hd->prev->next = hd->next->prev = hd;
1819 FreeVolumeHeader(register Volume * vp)
1821 register struct volHeader *hd = vp->header;
1824 if (programType == fileServer) {
1825 ReleaseVolumeHeader(hd);
1834 /***************************************************/
1835 /* Routines to add volume to hash chain, delete it */
1836 /***************************************************/
1839 AddVolumeToHashTable(register Volume * vp, int hashid)
1841 int hash = VOLUME_HASH(hashid);
1842 vp->hashid = hashid;
1843 vp->hashNext = VolumeHashTable[hash];
1844 VolumeHashTable[hash] = vp;
1845 vp->vnodeHashOffset = VolumeHashOffset_r();
1849 DeleteVolumeFromHashTable(register Volume * vp)
1851 int hash = VOLUME_HASH(vp->hashid);
1852 if (VolumeHashTable[hash] == vp)
1853 VolumeHashTable[hash] = vp->hashNext;
1855 Volume *tvp = VolumeHashTable[hash];
1858 while (tvp->hashNext && tvp->hashNext != vp)
1859 tvp = tvp->hashNext;
1860 if (tvp->hashNext == NULL)
1862 tvp->hashNext = vp->hashNext;
1868 VPrintCacheStats_r(void)
1870 register struct VnodeClassInfo *vcp;
1871 vcp = &VnodeClassInfo[vLarge];
1872 Log("Large vnode cache, %d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
1873 vcp = &VnodeClassInfo[vSmall];
1874 Log("Small vnode cache,%d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
1875 Log("Volume header cache, %d entries, %d gets, %d replacements\n",
1876 VolumeCacheSize, VolumeGets, VolumeReplacements);
1880 VPrintCacheStats(void)
1882 VOL_LOCK VPrintCacheStats_r();