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_mutex_t vol_trans_mutex;
153 pthread_cond_t vol_put_volume_cond;
154 pthread_cond_t vol_sleep_cond;
155 int vol_attach_threads = 1;
156 #endif /* AFS_PTHREAD_ENV */
159 extern void *calloc(), *realloc();
162 /*@printflike@*/ extern void Log(const char *format, ...);
164 /* Forward declarations */
165 static Volume *attach2(Error * ec, char *path,
166 register struct VolumeHeader *header,
167 struct DiskPartition *partp, int isbusy);
168 static void FreeVolume(Volume * vp);
169 static void VScanUpdateList(void);
170 static void InitLRU(int howMany);
171 static int GetVolumeHeader(register Volume * vp);
172 static void ReleaseVolumeHeader(register struct volHeader *hd);
173 static void FreeVolumeHeader(register Volume * vp);
174 static void AddVolumeToHashTable(register Volume * vp, int hashid);
175 static void DeleteVolumeFromHashTable(register Volume * vp);
176 static int VHold(Volume * vp);
177 static int VHold_r(Volume * vp);
178 static void GetBitmap(Error * ec, Volume * vp, VnodeClass class);
179 static void GetVolumePath(Error * ec, VolId volumeId, char **partitionp,
181 static void VReleaseVolumeHandles_r(Volume * vp);
182 static void VCloseVolumeHandles_r(Volume * vp);
184 int LogLevel; /* Vice loglevel--not defined as extern so that it will be
185 * defined when not linked with vice, XXXX */
186 ProgramType programType; /* The type of program using the package */
189 #define VOLUME_BITMAP_GROWSIZE 16 /* bytes, => 128vnodes */
190 /* Must be a multiple of 4 (1 word) !! */
191 #define VOLUME_HASH_TABLE_SIZE 128 /* Must be a power of 2!! */
192 #define VOLUME_HASH(volumeId) (volumeId&(VOLUME_HASH_TABLE_SIZE-1))
193 private Volume *VolumeHashTable[VOLUME_HASH_TABLE_SIZE];
196 /* This macro is used where an ffs() call does not exist. Was in util/ffs.c */
200 afs_int32 ffs_tmp = x;
204 for (ffs_i = 1;; ffs_i++) {
211 #endif /* !AFS_HAVE_FFS */
213 #ifdef AFS_PTHREAD_ENV
214 #include "rx/rx_queue.h"
215 typedef struct diskpartition_queue_t {
216 struct rx_queue queue;
217 struct DiskPartition * diskP;
218 } diskpartition_queue_t;
219 typedef struct vinitvolumepackage_thread_t {
220 struct rx_queue queue;
221 pthread_cond_t thread_done_cv;
222 int n_threads_complete;
223 } vinitvolumepackage_thread_t;
224 static void * VInitVolumePackageThread(void * args);
225 #endif /* AFS_PTHREAD_ENV */
227 struct Lock vol_listLock; /* Lock obtained when listing volumes: prevents a volume from being missed if the volume is attached during a list volumes */
229 extern struct Lock FSYNC_handler_lock;
231 static int TimeZoneCorrection; /* Number of seconds west of GMT */
233 /* Common message used when the volume goes off line */
234 char *VSalvageMessage =
235 "Files in this volume are currently unavailable; call operations";
237 int VInit; /* 0 - uninitialized,
238 * 1 - initialized but not all volumes have been attached,
239 * 2 - initialized and all volumes have been attached,
240 * 3 - initialized, all volumes have been attached, and
241 * VConnectFS() has completed. */
244 bit32 VolumeCacheCheck; /* Incremented everytime a volume goes on line--
245 * used to stamp volume headers and in-core
246 * vnodes. When the volume goes on-line the
247 * vnode will be invalidated
248 * access only with VOL_LOCK held */
250 int VolumeCacheSize = 200, VolumeGets = 0, VolumeReplacements = 0, Vlooks = 0;
254 VInitVolumePackage(ProgramType pt, int nLargeVnodes, int nSmallVnodes,
255 int connect, int volcache)
257 int errors = 0; /* Number of errors while finding vice partitions. */
263 #ifdef AFS_PTHREAD_ENV
264 assert(pthread_mutex_init(&vol_glock_mutex, NULL) == 0);
265 assert(pthread_mutex_init(&vol_attach_mutex, NULL) == 0);
266 assert(pthread_mutex_init(&vol_fsync_mutex, NULL) == 0);
267 assert(pthread_mutex_init(&vol_trans_mutex, NULL) == 0);
268 assert(pthread_cond_init(&vol_put_volume_cond, NULL) == 0);
269 assert(pthread_cond_init(&vol_sleep_cond, NULL) == 0);
270 #else /* AFS_PTHREAD_ENV */
272 #endif /* AFS_PTHREAD_ENV */
273 Lock_Init(&vol_listLock);
274 Lock_Init(&FSYNC_handler_lock);
275 srandom(time(0)); /* For VGetVolumeInfo */
276 gettimeofday(&tv, &tz);
277 TimeZoneCorrection = tz.tz_minuteswest * 60;
279 /* Ok, we have done enough initialization that fileserver can
280 * start accepting calls, even though the volumes may not be
281 * available just yet.
285 if (programType == fileServer) {
286 /* File server or "stand" */
290 if (volcache > VolumeCacheSize)
291 VolumeCacheSize = volcache;
292 InitLRU(VolumeCacheSize);
294 VInitVnodes(vLarge, nLargeVnodes);
295 VInitVnodes(vSmall, nSmallVnodes);
298 errors = VAttachPartitions();
302 if (programType == fileServer) {
303 struct DiskPartition *diskP;
304 #ifdef AFS_PTHREAD_ENV
305 struct vinitvolumepackage_thread_t params;
306 struct diskpartition_queue_t * dpq;
309 pthread_attr_t attrs;
311 assert(pthread_cond_init(¶ms.thread_done_cv,NULL) == 0);
313 params.n_threads_complete = 0;
315 /* create partition work queue */
316 for (len=0, diskP = DiskPartitionList; diskP; diskP = diskP->next, len++) {
317 dpq = (diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
320 queue_Prepend(¶ms,dpq);
323 assert(pthread_attr_init(&attrs) == 0);
324 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
326 len = MIN(len, vol_attach_threads);
329 for (i=0; i < len; i++) {
330 assert(pthread_create
331 (&tid, &attrs, &VInitVolumePackageThread,
335 while(params.n_threads_complete < len) {
336 pthread_cond_wait(¶ms.thread_done_cv,&vol_glock_mutex);
340 assert(pthread_cond_destroy(¶ms.thread_done_cv) == 0);
342 #else /* AFS_PTHREAD_ENV */
346 /* Attach all the volumes in this partition */
347 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
348 int nAttached = 0, nUnattached = 0;
349 Log("Partition %s: attaching volumes\n", diskP->name);
350 dirp = opendir(VPartitionPath(diskP));
352 while ((dp = readdir(dirp))) {
354 p = strrchr(dp->d_name, '.');
355 if (p != NULL && strcmp(p, VHDREXT) == 0) {
358 vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
360 (*(vp ? &nAttached : &nUnattached))++;
361 if (error == VOFFLINE)
362 Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
363 else if (LogLevel >= 5) {
364 Log("Partition %s: attached volume %d (%s)\n",
365 diskP->name, VolumeNumber(dp->d_name),
373 Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, nAttached, nUnattached);
376 #endif /* AFS_PTHREAD_ENV */
379 VInit = 2; /* Initialized, and all volumes have been attached */
380 if (programType == volumeUtility && connect) {
382 Log("Unable to connect to file server; aborted\n");
383 Lock_Destroy(&FSYNC_handler_lock);
390 #ifdef AFS_PTHREAD_ENV
392 VInitVolumePackageThread(void * args) {
393 int errors = 0; /* Number of errors while finding vice partitions. */
397 struct DiskPartition *diskP;
398 struct vinitvolumepackage_thread_t * params;
399 struct diskpartition_queue_t * dpq;
400 int nAttached = 0, nUnattached = 0;
402 params = (vinitvolumepackage_thread_t *) args;
405 /* Attach all the volumes in this partition */
406 while (queue_IsNotEmpty(params)) {
407 dpq = queue_First(params,diskpartition_queue_t);
413 Log("Partition %s: attaching volumes\n", diskP->name);
414 dirp = opendir(VPartitionPath(diskP));
416 while ((dp = readdir(dirp))) {
418 p = strrchr(dp->d_name, '.');
419 if (p != NULL && strcmp(p, VHDREXT) == 0) {
422 vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
424 (*(vp ? &nAttached : &nUnattached))++;
425 if (error == VOFFLINE)
426 Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
427 else if (LogLevel >= 5) {
428 Log("Partition %s: attached volume %d (%s)\n",
429 diskP->name, VolumeNumber(dp->d_name),
437 Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, nAttached, nUnattached);
442 params->n_threads_complete++;
443 pthread_cond_signal(¶ms->thread_done_cv);
447 #endif /* AFS_PTHREAD_ENV */
449 /* This must be called by any volume utility which needs to run while the
450 file server is also running. This is separated from VInitVolumePackage so
451 that a utility can fork--and each of the children can independently
452 initialize communication with the file server */
458 retVal = VConnectFS_r();
467 assert(VInit == 2 && programType == volumeUtility);
468 rc = FSYNC_clientInit();
475 VDisconnectFS_r(void)
477 assert(programType == volumeUtility);
494 register Volume *vp, *np;
495 register afs_int32 code;
497 Log("VShutdown: shutting down on-line volumes...\n");
498 for (i = 0; i < VOLUME_HASH_TABLE_SIZE; i++) {
499 /* try to hold first volume in the hash table */
500 for (vp = VolumeHashTable[i]; vp; vp = vp->hashNext) {
504 /* otherwise we go around again, trying another volume */
508 Log("VShutdown: Attempting to take volume %u offline.\n",
510 /* first compute np before releasing vp, in case vp disappears
511 * after releasing. Hold it, so it doesn't disapear. If we
512 * can't hold it, try the next one in the chain. Invariant
513 * at the top of this loop is that vp is held (has extra ref count).
515 for (np = vp->hashNext; np; np = np->hashNext) {
520 /* next, take the volume offline (drops reference count) */
521 VOffline_r(vp, "File server was shut down");
522 vp = np; /* next guy to try */
525 Log("VShutdown: complete.\n");
538 ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
541 struct versionStamp *vsn;
556 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
558 FDH_REALLYCLOSE(fdP);
561 vsn = (struct versionStamp *)to;
562 if (FDH_READ(fdP, to, size) != size || vsn->magic != magic) {
564 FDH_REALLYCLOSE(fdP);
569 /* Check is conditional, in case caller wants to inspect version himself */
570 if (version && vsn->version != version) {
575 /* VolumeHeaderToDisk
576 * Allows for storing 64 bit inode numbers in on-disk volume header
580 VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h)
583 memset((char *)dh, 0, sizeof(VolumeDiskHeader_t));
584 dh->stamp = h->stamp;
586 dh->parent = h->parent;
588 #ifdef AFS_64BIT_IOPS_ENV
589 dh->volumeInfo_lo = (afs_int32) h->volumeInfo & 0xffffffff;
590 dh->volumeInfo_hi = (afs_int32) (h->volumeInfo >> 32) & 0xffffffff;
591 dh->smallVnodeIndex_lo = (afs_int32) h->smallVnodeIndex & 0xffffffff;
592 dh->smallVnodeIndex_hi =
593 (afs_int32) (h->smallVnodeIndex >> 32) & 0xffffffff;
594 dh->largeVnodeIndex_lo = (afs_int32) h->largeVnodeIndex & 0xffffffff;
595 dh->largeVnodeIndex_hi =
596 (afs_int32) (h->largeVnodeIndex >> 32) & 0xffffffff;
597 dh->linkTable_lo = (afs_int32) h->linkTable & 0xffffffff;
598 dh->linkTable_hi = (afs_int32) (h->linkTable >> 32) & 0xffffffff;
600 dh->volumeInfo_lo = h->volumeInfo;
601 dh->smallVnodeIndex_lo = h->smallVnodeIndex;
602 dh->largeVnodeIndex_lo = h->largeVnodeIndex;
603 dh->linkTable_lo = h->linkTable;
607 /* DiskToVolumeHeader
608 * Reads volume header file from disk, convering 64 bit inodes
609 * if required. Makes the assumption that AFS has *always*
610 * zero'd the volume header file so that high parts of inode
611 * numbers are 0 in older (SGI EFS) volume header files.
614 DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh)
616 memset((char *)h, 0, sizeof(VolumeHeader_t));
617 h->stamp = dh->stamp;
619 h->parent = dh->parent;
621 #ifdef AFS_64BIT_IOPS_ENV
623 (Inode) dh->volumeInfo_lo | ((Inode) dh->volumeInfo_hi << 32);
626 (Inode) dh->smallVnodeIndex_lo | ((Inode) dh->
627 smallVnodeIndex_hi << 32);
630 (Inode) dh->largeVnodeIndex_lo | ((Inode) dh->
631 largeVnodeIndex_hi << 32);
633 (Inode) dh->linkTable_lo | ((Inode) dh->linkTable_hi << 32);
635 h->volumeInfo = dh->volumeInfo_lo;
636 h->smallVnodeIndex = dh->smallVnodeIndex_lo;
637 h->largeVnodeIndex = dh->largeVnodeIndex_lo;
638 h->linkTable = dh->linkTable_lo;
644 WriteVolumeHeader_r(ec, vp)
648 IHandle_t *h = V_diskDataHandle(vp);
658 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
660 FDH_REALLYCLOSE(fdP);
663 if (FDH_WRITE(fdP, (char *)&V_disk(vp), sizeof(V_disk(vp)))
664 != sizeof(V_disk(vp))) {
666 FDH_REALLYCLOSE(fdP);
672 /* Attach an existing volume, given its pathname, and return a
673 pointer to the volume header information. The volume also
674 normally goes online at this time. An offline volume
675 must be reattached to make it go online */
677 VAttachVolumeByName(Error * ec, char *partition, char *name, int mode)
682 retVal = VAttachVolumeByName_r(ec, partition, name, mode);
689 VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
693 struct afs_stat status;
694 struct VolumeDiskHeader diskHeader;
695 struct VolumeHeader iheader;
696 struct DiskPartition *partp;
700 if (programType == volumeUtility) {
702 VLockPartition_r(partition);
704 if (programType == fileServer) {
705 vp = VGetVolume_r(ec, VolumeNumber(name));
709 if (vp->specialStatus == VBUSY)
711 VDetachVolume_r(ec, vp);
713 Log("VAttachVolume: Error detaching volume (%s)\n", name);
718 if (!(partp = VGetPartition_r(partition, 0))) {
720 Log("VAttachVolume: Error getting partition (%s)\n", partition);
725 strcpy(path, VPartitionPath(partp));
729 if ((fd = afs_open(path, O_RDONLY)) == -1 || afs_fstat(fd, &status) == -1) {
730 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
737 n = read(fd, &diskHeader, sizeof(diskHeader));
740 if (n != sizeof(diskHeader)
741 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
742 Log("VAttachVolume: Error reading volume header %s\n", path);
746 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
747 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
752 DiskToVolumeHeader(&iheader, &diskHeader);
753 if (programType == volumeUtility && mode != V_SECRETLY && mode != V_PEEK) {
754 if (FSYNC_askfs(iheader.id, partition, FSYNC_NEEDVOLUME, mode)
756 Log("VAttachVolume: attach of volume %u apparently denied by file server\n", iheader.id);
757 *ec = VNOVOL; /* XXXX */
762 vp = attach2(ec, path, &iheader, partp, isbusy);
763 if (programType == volumeUtility && vp) {
764 /* duplicate computation in fssync.c about whether the server
765 * takes the volume offline or not. If the volume isn't
766 * offline, we must not return it when we detach the volume,
767 * or the server will abort */
768 if (mode == V_READONLY || mode == V_PEEK
769 || (!VolumeWriteable(vp) && (mode == V_CLONE || mode == V_DUMP)))
770 vp->needsPutBack = 0;
772 vp->needsPutBack = 1;
774 /* OK, there's a problem here, but one that I don't know how to
775 * fix right now, and that I don't think should arise often.
776 * Basically, we should only put back this volume to the server if
777 * it was given to us by the server, but since we don't have a vp,
778 * we can't run the VolumeWriteable function to find out as we do
779 * above when computing vp->needsPutBack. So we send it back, but
780 * there's a path in VAttachVolume on the server which may abort
781 * if this volume doesn't have a header. Should be pretty rare
782 * for all of that to happen, but if it does, probably the right
783 * fix is for the server to allow the return of readonly volumes
784 * that it doesn't think are really checked out. */
785 if (programType == volumeUtility && vp == NULL &&
786 mode != V_SECRETLY && mode != V_PEEK) {
787 FSYNC_askfs(iheader.id, partition, FSYNC_ON, 0);
788 } else if (programType == fileServer && vp) {
789 V_needsCallback(vp) = 0;
791 if (VInit >= 2 && V_BreakVolumeCallbacks) {
792 Log("VAttachVolume: Volume %u was changed externally; breaking callbacks\n", V_id(vp));
793 (*V_BreakVolumeCallbacks) (V_id(vp));
796 VUpdateVolume_r(ec, vp);
798 Log("VAttachVolume: Error updating volume\n");
803 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
804 /* This is a hack: by temporarily settint the incore
805 * dontSalvage flag ON, the volume will be put back on the
806 * Update list (with dontSalvage OFF again). It will then
807 * come back in N minutes with DONT_SALVAGE eventually
808 * set. This is the way that volumes that have never had
809 * it set get it set; or that volumes that have been
810 * offline without DONT SALVAGE having been set also
811 * eventually get it set */
812 V_dontSalvage(vp) = DONT_SALVAGE;
813 VAddToVolumeUpdateList_r(ec, vp);
815 Log("VAttachVolume: Error adding volume to update list\n");
822 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
826 if (programType == volumeUtility) {
827 VUnlockPartition_r(partition);
836 attach2(Error * ec, char *path, register struct VolumeHeader * header,
837 struct DiskPartition * partp, int isbusy)
843 vp = (Volume *) calloc(1, sizeof(Volume));
845 vp->specialStatus = (byte) (isbusy ? VBUSY : 0);
846 vp->device = partp->device;
847 vp->partition = partp;
848 IH_INIT(vp->vnodeIndex[vLarge].handle, partp->device, header->parent,
849 header->largeVnodeIndex);
850 IH_INIT(vp->vnodeIndex[vSmall].handle, partp->device, header->parent,
851 header->smallVnodeIndex);
852 IH_INIT(vp->diskDataHandle, partp->device, header->parent,
854 IH_INIT(vp->linkHandle, partp->device, header->parent, header->linkTable);
855 vp->shuttingDown = 0;
856 vp->goingOffline = 0;
860 vp->cacheCheck = ++VolumeCacheCheck;
861 /* just in case this ever rolls over */
863 vp->cacheCheck = ++VolumeCacheCheck;
867 (void)ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
868 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
872 Log("VAttachVolume: Error reading diskDataHandle vol header %s; error=%u\n", path, *ec);
875 struct IndexFileHeader iHead;
877 #if OPENAFS_VOL_STATS
879 * We just read in the diskstuff part of the header. If the detailed
880 * volume stats area has not yet been initialized, we should bzero the
881 * area and mark it as initialized.
883 if (!(V_stat_initialized(vp))) {
884 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
885 V_stat_initialized(vp) = 1;
887 #endif /* OPENAFS_VOL_STATS */
889 (void)ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
890 (char *)&iHead, sizeof(iHead),
891 SMALLINDEXMAGIC, SMALLINDEXVERSION);
894 Log("VAttachVolume: Error reading smallVnode vol header %s; error=%u\n", path, *ec);
898 struct IndexFileHeader iHead;
900 (void)ReadHeader(ec, vp->vnodeIndex[vLarge].handle,
901 (char *)&iHead, sizeof(iHead),
902 LARGEINDEXMAGIC, LARGEINDEXVERSION);
905 Log("VAttachVolume: Error reading largeVnode vol header %s; error=%u\n", path, *ec);
910 struct versionStamp stamp;
912 (void)ReadHeader(ec, V_linkHandle(vp), (char *)&stamp,
913 sizeof(stamp), LINKTABLEMAGIC, LINKTABLEVERSION);
916 Log("VAttachVolume: Error reading namei vol header %s; error=%u\n", path, *ec);
921 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
925 if (V_needsSalvaged(vp)) {
926 if (vp->specialStatus)
927 vp->specialStatus = 0;
928 Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
933 if (programType == fileServer) {
935 if (V_inUse(vp) && VolumeWriteable(vp)) {
936 if (!V_needsSalvaged(vp)) {
937 V_needsSalvaged(vp) = 1;
938 VUpdateVolume_r(ec, vp);
941 Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
945 #endif /* FAST_RESTART */
946 if (V_destroyMe(vp) == DESTROY_ME) {
948 Log("VAttachVolume: volume %s is junk; it should be destroyed at next salvage\n", path);
954 AddVolumeToHashTable(vp, V_id(vp));
955 vp->nextVnodeUnique = V_uniquifier(vp);
956 vp->vnodeIndex[vSmall].bitmap = vp->vnodeIndex[vLarge].bitmap = NULL;
958 if (programType == fileServer && VolumeWriteable(vp)) {
960 for (i = 0; i < nVNODECLASSES; i++) {
962 GetBitmap(ec, vp, i);
966 Log("VAttachVolume: error getting bitmap for volume (%s)\n",
972 #endif /* BITMAP_LATER */
974 if (programType == fileServer) {
975 if (vp->specialStatus)
976 vp->specialStatus = 0;
977 if (V_blessed(vp) && V_inService(vp) && !V_needsSalvaged(vp)) {
979 V_offlineMessage(vp)[0] = '\0';
986 /* Attach an existing volume.
987 The volume also normally goes online at this time.
988 An offline volume must be reattached to make it go online.
992 VAttachVolume(Error * ec, VolumeId volumeId, int mode)
997 retVal = VAttachVolume_r(ec, volumeId, mode);
1004 VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
1007 GetVolumePath(ec, volumeId, &part, &name);
1009 register Volume *vp;
1011 vp = VGetVolume_r(&error, volumeId);
1013 assert(V_inUse(vp) == 0);
1014 VDetachVolume_r(ec, vp);
1018 return VAttachVolumeByName_r(ec, part, name, mode);
1021 /* Increment a reference count to a volume, sans context swaps. Requires
1022 * possibly reading the volume header in from the disk, since there's
1023 * an invariant in the volume package that nUsers>0 ==> vp->header is valid.
1025 * N.B. This call can fail if we can't read in the header!! In this case
1026 * we still guarantee we won't context swap, but the ref count won't be
1027 * incremented (otherwise we'd violate the invariant).
1030 VHold_r(register Volume * vp)
1034 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
1035 VolumeReplacements++;
1036 ReadHeader(&error, V_diskDataHandle(vp), (char *)&V_disk(vp),
1037 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
1046 VHold(register Volume * vp)
1050 retVal = VHold_r(vp);
1056 VTakeOffline_r(register Volume * vp)
1058 assert(vp->nUsers > 0);
1059 assert(programType == fileServer);
1060 vp->goingOffline = 1;
1061 V_needsSalvaged(vp) = 1;
1065 VTakeOffline(register Volume * vp)
1073 VPutVolume_r(register Volume * vp)
1075 assert(--vp->nUsers >= 0);
1076 if (vp->nUsers == 0) {
1077 ReleaseVolumeHeader(vp->header);
1078 if (vp->goingOffline) {
1080 assert(programType == fileServer);
1081 vp->goingOffline = 0;
1083 VUpdateVolume_r(&error, vp);
1084 VCloseVolumeHandles_r(vp);
1086 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
1088 if (V_offlineMessage(vp)[0])
1089 Log(" (%s)", V_offlineMessage(vp));
1092 #ifdef AFS_PTHREAD_ENV
1093 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
1094 #else /* AFS_PTHREAD_ENV */
1095 LWP_NoYieldSignal(VPutVolume);
1096 #endif /* AFS_PTHREAD_ENV */
1098 if (vp->shuttingDown) {
1099 VReleaseVolumeHandles_r(vp);
1101 if (programType == fileServer)
1102 #ifdef AFS_PTHREAD_ENV
1103 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
1104 #else /* AFS_PTHREAD_ENV */
1105 LWP_NoYieldSignal(VPutVolume);
1106 #endif /* AFS_PTHREAD_ENV */
1112 VPutVolume(register Volume * vp)
1119 /* Get a pointer to an attached volume. The pointer is returned regardless
1120 of whether or not the volume is in service or on/off line. An error
1121 code, however, is returned with an indication of the volume's status */
1123 VGetVolume(Error * ec, VolId volumeId)
1127 retVal = VGetVolume_r(ec, volumeId);
1133 VGetVolume_r(Error * ec, VolId volumeId)
1136 unsigned short V0 = 0, V1 = 0, V2 = 0, V3 = 0, V4 = 0, V5 = 0, V6 =
1137 0, V7 = 0, V8 = 0, V9 = 0;
1138 unsigned short V10 = 0, V11 = 0, V12 = 0, V13 = 0, V14 = 0, V15 = 0;
1143 for (vp = VolumeHashTable[VOLUME_HASH(volumeId)];
1144 vp && vp->hashid != volumeId; vp = vp->hashNext)
1151 /* Until we have reached an initialization level of 2
1152 * we don't know whether this volume exists or not.
1153 * We can't sleep and retry later because before a volume
1154 * is attached, the caller tries to get it first. Just
1155 * return VOFFLINE and the caller can choose whether to
1156 * retry the command or not. */
1167 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
1169 VolumeReplacements++;
1170 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
1171 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
1175 /* Only log the error if it was a totally unexpected error. Simply
1176 * a missing inode is likely to be caused by the volume being deleted */
1177 if (errno != ENXIO || LogLevel)
1178 Log("Volume %u: couldn't reread volume header\n",
1186 if (vp->shuttingDown) {
1192 if (programType == fileServer) {
1194 if (vp->goingOffline) {
1196 #ifdef AFS_PTHREAD_ENV
1197 pthread_cond_wait(&vol_put_volume_cond, &vol_glock_mutex);
1198 #else /* AFS_PTHREAD_ENV */
1199 LWP_WaitProcess(VPutVolume);
1200 #endif /* AFS_PTHREAD_ENV */
1203 if (vp->specialStatus) {
1205 *ec = vp->specialStatus;
1206 } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
1209 } else if (V_inUse(vp) == 0) {
1219 /* if no error, bump nUsers */
1228 /* For both VForceOffline and VOffline, we close all relevant handles.
1229 * For VOffline, if we re-attach the volume, the files may possible be
1230 * different than before.
1233 VReleaseVolumeHandles_r(Volume * vp)
1235 DFlushVolume(V_id(vp));
1236 VReleaseVnodeFiles_r(vp);
1238 /* Too time consuming and unnecessary for the volserver */
1239 if (programType != volumeUtility) {
1240 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
1241 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
1242 IH_CONDSYNC(vp->diskDataHandle);
1244 IH_CONDSYNC(vp->linkHandle);
1245 #endif /* AFS_NT40_ENV */
1248 IH_RELEASE(vp->vnodeIndex[vLarge].handle);
1249 IH_RELEASE(vp->vnodeIndex[vSmall].handle);
1250 IH_RELEASE(vp->diskDataHandle);
1251 IH_RELEASE(vp->linkHandle);
1254 /* Force the volume offline, set the salvage flag. No further references to
1255 * the volume through the volume package will be honored. */
1257 VForceOffline_r(Volume * vp)
1262 strcpy(V_offlineMessage(vp),
1263 "Forced offline due to internal error: volume needs to be salvaged");
1264 Log("Volume %u forced offline: it needs salvaging!\n", V_id(vp));
1266 vp->goingOffline = 0;
1267 V_needsSalvaged(vp) = 1;
1268 VUpdateVolume_r(&error, vp);
1269 #ifdef AFS_PTHREAD_ENV
1270 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
1271 #else /* AFS_PTHREAD_ENV */
1272 LWP_NoYieldSignal(VPutVolume);
1273 #endif /* AFS_PTHREAD_ENV */
1275 VReleaseVolumeHandles_r(vp);
1280 VForceOffline(Volume * vp)
1283 VForceOffline_r(vp);
1287 /* The opposite of VAttachVolume. The volume header is written to disk, with
1288 the inUse bit turned off. A copy of the header is maintained in memory,
1289 however (which is why this is VOffline, not VDetach).
1292 VOffline_r(Volume * vp, char *message)
1295 VolumeId vid = V_id(vp);
1296 assert(programType != volumeUtility);
1301 if (V_offlineMessage(vp)[0] == '\0')
1302 strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
1303 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
1304 vp->goingOffline = 1;
1306 vp = VGetVolume_r(&error, vid); /* Wait for it to go offline */
1307 if (vp) /* In case it was reattached... */
1312 VOffline(Volume * vp, char *message)
1315 VOffline_r(vp, message);
1319 /* For VDetachVolume, we close all cached file descriptors, but keep
1320 * the Inode handles in case we need to read from a busy volume.
1323 VCloseVolumeHandles_r(Volume * vp)
1325 DFlushVolume(V_id(vp));
1326 VCloseVnodeFiles_r(vp);
1328 /* Too time consuming and unnecessary for the volserver */
1329 if (programType != volumeUtility) {
1330 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
1331 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
1332 IH_CONDSYNC(vp->diskDataHandle);
1334 IH_CONDSYNC(vp->linkHandle);
1335 #endif /* AFS_NT40_ENV */
1338 IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
1339 IH_REALLYCLOSE(vp->vnodeIndex[vSmall].handle);
1340 IH_REALLYCLOSE(vp->diskDataHandle);
1341 IH_REALLYCLOSE(vp->linkHandle);
1344 /* This gets used for the most part by utility routines that don't want
1345 * to keep all the volume headers around. Generally, the file server won't
1346 * call this routine, because then the offline message in the volume header
1347 * (or other information) will still be available to clients. For NAMEI, also
1348 * close the file handles.
1351 VDetachVolume_r(Error * ec, Volume * vp)
1354 struct DiskPartition *tpartp;
1355 int notifyServer, useDone;
1357 *ec = 0; /* always "succeeds" */
1358 if (programType == volumeUtility) {
1359 notifyServer = vp->needsPutBack;
1360 useDone = (V_destroyMe(vp) == DESTROY_ME);
1362 tpartp = vp->partition;
1364 DeleteVolumeFromHashTable(vp);
1365 vp->shuttingDown = 1;
1367 /* Will be detached sometime in the future--this is OK since volume is offline */
1369 if (programType == volumeUtility && notifyServer) {
1371 * Note: The server is not notified in the case of a bogus volume
1372 * explicitly to make it possible to create a volume, do a partial
1373 * restore, then abort the operation without ever putting the volume
1374 * online. This is essential in the case of a volume move operation
1375 * between two partitions on the same server. In that case, there
1376 * would be two instances of the same volume, one of them bogus,
1377 * which the file server would attempt to put on line
1380 /* don't put online */
1381 FSYNC_askfs(volume, tpartp->name, FSYNC_DONE, 0);
1383 /* fs can use it again */
1384 FSYNC_askfs(volume, tpartp->name, FSYNC_ON, 0);
1385 /* Dettaching it so break all callbacks on it */
1386 if (V_BreakVolumeCallbacks) {
1387 Log("volume %u detached; breaking all call backs\n", volume);
1388 (*V_BreakVolumeCallbacks) (volume);
1395 VDetachVolume(Error * ec, Volume * vp)
1398 VDetachVolume_r(ec, vp);
1404 VAllocBitmapEntry_r(Error * ec, Volume * vp, register struct vnodeIndex
1407 register byte *bp, *ep;
1409 /* This test is probably redundant */
1410 if (!VolumeWriteable(vp)) {
1411 *ec = (bit32) VREADONLY;
1415 if ((programType == fileServer) && !index->bitmap) {
1418 if (vp->specialStatus == VBUSY) {
1419 if (vp->goingOffline) { /* vos dump waiting for the volume to
1420 * go offline. We probably come here
1421 * from AddNewReadableResidency */
1425 while (vp->specialStatus == VBUSY)
1426 #ifdef AFS_PTHREAD_ENV
1428 #else /* AFS_PTHREAD_ENV */
1430 #endif /* AFS_PTHREAD_ENV */
1434 if (!index->bitmap) {
1435 vp->specialStatus = VBUSY; /* Stop anyone else from using it. */
1436 for (i = 0; i < nVNODECLASSES; i++) {
1438 GetBitmap(ec, vp, i);
1441 vp->specialStatus = 0;
1442 vp->shuttingDown = 1; /* Let who has it free it. */
1447 vp->specialStatus = 0; /* Allow others to have access. */
1450 #endif /* BITMAP_LATER */
1451 bp = index->bitmap + index->bitmapOffset;
1452 ep = index->bitmap + index->bitmapSize;
1454 if ((*(bit32 *) bp) != (bit32) 0xffffffff) {
1456 index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
1459 o = ffs(~*bp) - 1; /* ffs is documented in BSTRING(3) */
1461 return (VnodeId) ((bp - index->bitmap) * 8 + o);
1463 bp += sizeof(bit32) /* i.e. 4 */ ;
1465 /* No bit map entry--must grow bitmap */
1467 realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
1470 bp += index->bitmapSize;
1471 memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
1472 index->bitmapOffset = index->bitmapSize;
1473 index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
1475 return index->bitmapOffset * 8;
1479 VAllocBitmapEntry(Error * ec, Volume * vp, register struct vnodeIndex * index)
1483 retVal = VAllocBitmapEntry_r(ec, vp, index);
1489 VFreeBitMapEntry_r(Error * ec, register struct vnodeIndex *index,
1492 unsigned int offset;
1497 #endif /* BITMAP_LATER */
1498 offset = bitNumber >> 3;
1499 if (offset >= index->bitmapSize) {
1503 if (offset < index->bitmapOffset)
1504 index->bitmapOffset = offset & ~3; /* Truncate to nearest bit32 */
1505 *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
1509 VFreeBitMapEntry(Error * ec, register struct vnodeIndex *index,
1513 VFreeBitMapEntry_r(ec, index, bitNumber);
1518 VUpdateVolume_r(Error * ec, Volume * vp)
1521 if (programType == fileServer)
1523 (V_inUse(vp) ? V_nextVnodeUnique(vp) +
1524 200 : V_nextVnodeUnique(vp));
1525 /*printf("Writing volume header for '%s'\n", V_name(vp)); */
1526 WriteVolumeHeader_r(ec, vp);
1528 Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
1529 V_id(vp), V_name(vp));
1530 VForceOffline_r(vp);
1535 VUpdateVolume(Error * ec, Volume * vp)
1538 VUpdateVolume_r(ec, vp);
1543 VSyncVolume_r(Error * ec, Volume * vp)
1546 VUpdateVolume_r(ec, vp);
1549 fdP = IH_OPEN(V_diskDataHandle(vp));
1550 assert(fdP != NULL);
1551 code = FDH_SYNC(fdP);
1558 VSyncVolume(Error * ec, Volume * vp)
1561 VSyncVolume_r(ec, vp);
1566 FreeVolume(Volume * vp)
1571 for (i = 0; i < nVNODECLASSES; i++)
1572 if (vp->vnodeIndex[i].bitmap)
1573 free(vp->vnodeIndex[i].bitmap);
1574 FreeVolumeHeader(vp);
1575 DeleteVolumeFromHashTable(vp);
1580 GetBitmap(Error * ec, Volume * vp, VnodeClass class)
1582 StreamHandle_t *file;
1585 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1586 struct vnodeIndex *vip = &vp->vnodeIndex[class];
1587 struct VnodeDiskObject *vnode;
1588 unsigned int unique = 0;
1592 #endif /* BITMAP_LATER */
1596 fdP = IH_OPEN(vip->handle);
1597 assert(fdP != NULL);
1598 file = FDH_FDOPEN(fdP, "r");
1599 assert(file != NULL);
1600 vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
1601 assert(vnode != NULL);
1602 size = OS_SIZE(fdP->fd_fd);
1604 nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
1606 vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4; /* The 10 is a little extra so
1607 * a few files can be created in this volume,
1608 * the whole thing is rounded up to nearest 4
1609 * bytes, because the bit map allocator likes
1612 BitMap = (byte *) calloc(1, vip->bitmapSize);
1613 assert(BitMap != NULL);
1614 #else /* BITMAP_LATER */
1615 vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
1616 assert(vip->bitmap != NULL);
1617 vip->bitmapOffset = 0;
1618 #endif /* BITMAP_LATER */
1619 if (STREAM_SEEK(file, vcp->diskSize, 0) != -1) {
1621 for (bitNumber = 0; bitNumber < nVnodes + 100; bitNumber++) {
1622 if (STREAM_READ(vnode, vcp->diskSize, 1, file) != 1)
1624 if (vnode->type != vNull) {
1625 if (vnode->vnodeMagic != vcp->magic) {
1626 Log("GetBitmap: addled vnode index in volume %s; volume needs salvage\n", V_name(vp));
1631 *(BitMap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
1632 #else /* BITMAP_LATER */
1633 *(vip->bitmap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
1634 #endif /* BITMAP_LATER */
1635 if (unique <= vnode->uniquifier)
1636 unique = vnode->uniquifier + 1;
1638 #ifndef AFS_PTHREAD_ENV
1639 if ((bitNumber & 0x00ff) == 0x0ff) { /* every 256 iterations */
1642 #endif /* !AFS_PTHREAD_ENV */
1645 if (vp->nextVnodeUnique < unique) {
1646 Log("GetBitmap: bad volume uniquifier for volume %s; volume needs salvage\n", V_name(vp));
1649 /* Paranoia, partly justified--I think fclose after fdopen
1650 * doesn't seem to close fd. In any event, the documentation
1651 * doesn't specify, so it's safer to close it twice.
1657 /* There may have been a racing condition with some other thread, both
1658 * creating the bitmaps for this volume. If the other thread was faster
1659 * the pointer to bitmap should already be filled and we can free ours.
1661 if (vip->bitmap == NULL) {
1662 vip->bitmap = BitMap;
1663 vip->bitmapOffset = 0;
1665 free((byte *) BitMap);
1666 #endif /* BITMAP_LATER */
1670 GetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
1672 static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
1673 char path[VMAXPATHLEN];
1675 struct DiskPartition *dp;
1679 (void)afs_snprintf(&name[1], (sizeof name) - 1, VFORMAT, volumeId);
1680 for (dp = DiskPartitionList; dp; dp = dp->next) {
1681 struct afs_stat status;
1682 strcpy(path, VPartitionPath(dp));
1684 if (afs_stat(path, &status) == 0) {
1685 strcpy(partition, dp->name);
1692 *partitionp = *namep = NULL;
1694 *partitionp = partition;
1700 VolumeNumber(char *name)
1704 return atoi(name + 1);
1708 VolumeExternalName(VolumeId volumeId)
1710 static char name[VMAXPATHLEN];
1711 (void)afs_snprintf(name, sizeof name, VFORMAT, volumeId);
1715 #if OPENAFS_VOL_STATS
1716 #define OneDay (86400) /* 24 hours' worth of seconds */
1718 #define OneDay (24*60*60) /* 24 hours */
1719 #endif /* OPENAFS_VOL_STATS */
1721 #define Midnight(date) ((date-TimeZoneCorrection)/OneDay*OneDay+TimeZoneCorrection)
1723 /*------------------------------------------------------------------------
1724 * [export] VAdjustVolumeStatistics
1727 * If we've passed midnight, we need to update all the day use
1728 * statistics as well as zeroing the detailed volume statistics
1729 * (if we are implementing them).
1732 * vp : Pointer to the volume structure describing the lucky
1733 * volume being considered for update.
1739 * Nothing interesting.
1743 *------------------------------------------------------------------------*/
1746 VAdjustVolumeStatistics_r(register Volume * vp)
1748 unsigned int now = FT_ApproxTime();
1750 if (now - V_dayUseDate(vp) > OneDay) {
1753 ndays = (now - V_dayUseDate(vp)) / OneDay;
1754 for (i = 6; i > ndays - 1; i--)
1755 V_weekUse(vp)[i] = V_weekUse(vp)[i - ndays];
1756 for (i = 0; i < ndays - 1 && i < 7; i++)
1757 V_weekUse(vp)[i] = 0;
1759 V_weekUse(vp)[ndays - 1] = V_dayUse(vp);
1761 V_dayUseDate(vp) = Midnight(now);
1763 #if OPENAFS_VOL_STATS
1765 * All we need to do is bzero the entire VOL_STATS_BYTES of
1766 * the detailed volume statistics area.
1768 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
1769 #endif /* OPENAFS_VOL_STATS */
1772 /*It's been more than a day of collection */
1774 * Always return happily.
1777 } /*VAdjustVolumeStatistics */
1780 VAdjustVolumeStatistics(register Volume * vp)
1784 retVal = VAdjustVolumeStatistics_r(vp);
1790 VBumpVolumeUsage_r(register Volume * vp)
1792 unsigned int now = FT_ApproxTime();
1793 if (now - V_dayUseDate(vp) > OneDay)
1794 VAdjustVolumeStatistics_r(vp);
1796 * Save the volume header image to disk after every 128 bumps to dayUse.
1798 if ((V_dayUse(vp)++ & 127) == 0) {
1800 VUpdateVolume_r(&error, vp);
1805 VBumpVolumeUsage(register Volume * vp)
1808 VBumpVolumeUsage_r(vp);
1813 VSetDiskUsage_r(void)
1815 static int FifteenMinuteCounter = 0;
1818 /* NOTE: Don't attempt to access the partitions list until the
1819 * initialization level indicates that all volumes are attached,
1820 * which implies that all partitions are initialized. */
1821 #ifdef AFS_PTHREAD_ENV
1823 #else /* AFS_PTHREAD_ENV */
1825 #endif /* AFS_PTHREAD_ENV */
1828 VResetDiskUsage_r();
1829 if (++FifteenMinuteCounter == 3) {
1830 FifteenMinuteCounter = 0;
1843 /* The number of minutes that a volume hasn't been updated before the
1844 * "Dont salvage" flag in the volume header will be turned on */
1845 #define SALVAGE_INTERVAL (10*60)
1847 static VolumeId *UpdateList; /* Pointer to array of Volume ID's */
1848 static int nUpdatedVolumes; /* Updated with entry in UpdateList, salvage after crash flag on */
1849 static int updateSize; /* number of entries possible */
1850 #define UPDATE_LIST_SIZE 100 /* size increment */
1853 VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
1856 vp->updateTime = FT_ApproxTime();
1857 if (V_dontSalvage(vp) == 0)
1859 V_dontSalvage(vp) = 0;
1860 VSyncVolume_r(ec, vp);
1864 updateSize = UPDATE_LIST_SIZE;
1865 UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
1867 if (nUpdatedVolumes == updateSize) {
1868 updateSize += UPDATE_LIST_SIZE;
1870 (VolumeId *) realloc(UpdateList,
1871 sizeof(VolumeId) * updateSize);
1874 assert(UpdateList != NULL);
1875 UpdateList[nUpdatedVolumes++] = V_id(vp);
1879 VScanUpdateList(void)
1881 register int i, gap;
1882 register Volume *vp;
1884 afs_uint32 now = FT_ApproxTime();
1885 /* Be careful with this code, since it works with interleaved calls to AddToVolumeUpdateList */
1886 for (i = gap = 0; i < nUpdatedVolumes; i++) {
1887 vp = VGetVolume_r(&error, UpdateList[i - gap] = UpdateList[i]);
1890 } else if (vp->nUsers == 1 && now - vp->updateTime > SALVAGE_INTERVAL) {
1891 V_dontSalvage(vp) = DONT_SALVAGE;
1892 VUpdateVolume_r(&error, vp); /* No need to fsync--not critical */
1897 #ifndef AFS_PTHREAD_ENV
1899 #endif /* !AFS_PTHREAD_ENV */
1901 nUpdatedVolumes -= gap;
1904 /***************************************************/
1905 /* Add on routines to manage a volume header cache */
1906 /***************************************************/
1908 static struct volHeader *volumeLRU;
1910 /* Allocate a bunch of headers; string them together */
1912 InitLRU(int howMany)
1914 register struct volHeader *hp;
1915 if (programType != fileServer)
1917 hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
1919 ReleaseVolumeHeader(hp++);
1922 /* Get a volume header from the LRU list; update the old one if necessary */
1923 /* Returns 1 if there was already a header, which is removed from the LRU list */
1925 GetVolumeHeader(register Volume * vp)
1928 register struct volHeader *hd;
1930 static int everLogged = 0;
1932 old = (vp->header != NULL); /* old == volume already has a header */
1933 if (programType != fileServer) {
1935 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
1943 if (volumeLRU == hd)
1944 volumeLRU = hd->next;
1945 assert(hd->back == vp);
1948 /* not currently in use and least recently used */
1949 hd = volumeLRU->prev;
1951 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
1952 /* make it look like single elt LRU */
1953 hd->prev = hd->next = hd;
1955 Log("****Allocated more volume headers, probably leak****\n");
1960 if (hd->diskstuff.inUse) {
1961 WriteVolumeHeader_r(&error, hd->back);
1962 /* Ignore errors; catch them later */
1964 hd->back->header = 0;
1969 if (hd->next) { /* hd->next != 0 --> in LRU chain (we zero it later) */
1970 hd->prev->next = hd->next; /* pull hd out of LRU list */
1971 hd->next->prev = hd->prev; /* if hd only element, this is noop */
1973 hd->next = hd->prev = 0;
1974 /* if not in LRU chain, next test won't be true */
1975 if (hd == volumeLRU) /* last header item, turn into empty list */
1981 /* Put it at the top of the LRU chain */
1983 ReleaseVolumeHeader(register struct volHeader *hd)
1985 if (programType != fileServer)
1987 if (!hd || hd->next) /* no header, or header already released */
1990 hd->next = hd->prev = hd;
1992 hd->prev = volumeLRU->prev;
1993 hd->next = volumeLRU;
1994 hd->prev->next = hd->next->prev = hd;
2000 FreeVolumeHeader(register Volume * vp)
2002 register struct volHeader *hd = vp->header;
2005 if (programType == fileServer) {
2006 ReleaseVolumeHeader(hd);
2015 /***************************************************/
2016 /* Routines to add volume to hash chain, delete it */
2017 /***************************************************/
2020 AddVolumeToHashTable(register Volume * vp, int hashid)
2022 int hash = VOLUME_HASH(hashid);
2023 vp->hashid = hashid;
2024 vp->hashNext = VolumeHashTable[hash];
2025 VolumeHashTable[hash] = vp;
2026 vp->vnodeHashOffset = VolumeHashOffset_r();
2030 DeleteVolumeFromHashTable(register Volume * vp)
2032 int hash = VOLUME_HASH(vp->hashid);
2033 if (VolumeHashTable[hash] == vp)
2034 VolumeHashTable[hash] = vp->hashNext;
2036 Volume *tvp = VolumeHashTable[hash];
2039 while (tvp->hashNext && tvp->hashNext != vp)
2040 tvp = tvp->hashNext;
2041 if (tvp->hashNext == NULL)
2043 tvp->hashNext = vp->hashNext;
2049 VPrintCacheStats_r(void)
2051 register struct VnodeClassInfo *vcp;
2052 vcp = &VnodeClassInfo[vLarge];
2053 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);
2054 vcp = &VnodeClassInfo[vSmall];
2055 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);
2056 Log("Volume header cache, %d entries, %d gets, %d replacements\n",
2057 VolumeCacheSize, VolumeGets, VolumeReplacements);
2061 VPrintCacheStats(void)
2064 VPrintCacheStats_r();