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
9 * Portions Copyright (c) 2005-2008 Sine Nomine Associates
15 Institution: The Information Technology Center, Carnegie-Mellon University
18 #include <afsconfig.h>
19 #include <afs/param.h>
20 #define MAXINT (~(1<<((sizeof(int)*8)-1)))
28 #ifdef AFS_PTHREAD_ENV
30 #else /* AFS_PTHREAD_ENV */
31 #include <afs/assert.h>
32 #endif /* AFS_PTHREAD_ENV */
35 #include "rx/rx_queue.h"
36 #include <afs/afsint.h>
38 #include <afs/errors.h>
41 #include <afs/afssyscalls.h>
45 #include "volume_inline.h"
46 #include "vnode_inline.h"
47 #include "partition.h"
49 #if defined(AFS_SGI_ENV)
50 #include "sys/types.h"
62 #include <sys/fcntl.h>
65 #endif /* AFS_NT40_ENV */
68 /*@printflike@*/ extern void Log(const char *format, ...);
70 /*@printflike@*/ extern void Abort(const char *format, ...);
73 struct VnodeClassInfo VnodeClassInfo[nVNODECLASSES];
75 private void StickOnLruChain_r(register Vnode * vnp,
76 register struct VnodeClassInfo *vcp);
83 #define BAD_IGET -1000
85 /* There are two separate vnode queue types defined here:
86 * Each hash conflict chain -- is singly linked, with a single head
87 * pointer. New entries are added at the beginning. Old
88 * entries are removed by linear search, which generally
89 * only occurs after a disk read).
90 * LRU chain -- is doubly linked, single head pointer.
91 * Entries are added at the head, reclaimed from the tail,
92 * or removed from anywhere in the queue.
96 /* Vnode hash table. Find hash chain by taking lower bits of
97 * (volume_hash_offset + vnode).
98 * This distributes the root inodes of the volumes over the
99 * hash table entries and also distributes the vnodes of
100 * volumes reasonably fairly. The volume_hash_offset field
101 * for each volume is established as the volume comes on line
102 * by using the VOLUME_HASH_OFFSET macro. This distributes the
103 * volumes fairly among the cache entries, both when servicing
104 * a small number of volumes and when servicing a large number.
107 /* logging stuff for finding bugs */
108 #define THELOGSIZE 5120
109 static afs_int32 theLog[THELOGSIZE];
110 static afs_int32 vnLogPtr = 0;
112 VNLog(afs_int32 aop, afs_int32 anparms, afs_int32 av1, afs_int32 av2,
113 afs_int32 av3, afs_int32 av4)
115 register afs_int32 temp;
118 /* copy data to array */
124 anparms = 4; /* do bounds checking */
126 temp = (aop << 16) | anparms;
127 theLog[vnLogPtr++] = temp;
128 if (vnLogPtr >= THELOGSIZE)
130 for (temp = 0; temp < anparms; temp++) {
131 theLog[vnLogPtr++] = data[temp];
132 if (vnLogPtr >= THELOGSIZE)
137 /* VolumeHashOffset -- returns a new value to be stored in the
138 * volumeHashOffset of a Volume structure. Called when a
139 * volume is initialized. Sets the volumeHashOffset so that
140 * vnode cache entries are distributed reasonably between
141 * volumes (the root vnodes of the volumes will hash to
142 * different values, and spacing is maintained between volumes
143 * when there are not many volumes represented), and spread
144 * equally amongst vnodes within a single volume.
147 VolumeHashOffset_r(void)
149 static int nextVolumeHashOffset = 0;
150 /* hashindex Must be power of two in size */
152 # define hashMask ((1<<hashShift)-1)
153 static byte hashindex[1 << hashShift] =
154 { 0, 128, 64, 192, 32, 160, 96, 224 };
156 offset = hashindex[nextVolumeHashOffset & hashMask]
157 + (nextVolumeHashOffset >> hashShift);
158 nextVolumeHashOffset++;
162 /* Change hashindex (above) if you change this constant */
163 #define VNODE_HASH_TABLE_SIZE 256
164 private Vnode *VnodeHashTable[VNODE_HASH_TABLE_SIZE];
165 #define VNODE_HASH(volumeptr,vnodenumber)\
166 ((volumeptr->vnodeHashOffset + vnodenumber)&(VNODE_HASH_TABLE_SIZE-1))
170 * add a vnode to the volume's vnode list.
172 * @param[in] vp volume object pointer
173 * @param[in] vnp vnode object pointer
175 * @note for DAFS, it may seem like we should be acquiring a lightweight ref
176 * on vp, but this would actually break things. Right now, this is ok
177 * because we destroy all vnode cache contents during during volume
182 * @internal volume package internal use only
185 AddToVVnList(Volume * vp, Vnode * vnp)
187 if (queue_IsOnQueue(vnp))
191 Vn_cacheCheck(vnp) = vp->cacheCheck;
192 queue_Append(&vp->vnode_list, vnp);
193 Vn_stateFlags(vnp) |= VN_ON_VVN;
197 * delete a vnode from the volume's vnode list.
201 * @internal volume package internal use only
204 DeleteFromVVnList(register Vnode * vnp)
206 Vn_volume(vnp) = NULL;
208 if (!queue_IsOnQueue(vnp))
212 Vn_stateFlags(vnp) &= ~(VN_ON_VVN);
216 * add a vnode to the end of the lru.
218 * @param[in] vcp vnode class info object pointer
219 * @param[in] vnp vnode object pointer
221 * @internal vnode package internal use only
224 AddToVnLRU(struct VnodeClassInfo * vcp, Vnode * vnp)
226 if (Vn_stateFlags(vnp) & VN_ON_LRU) {
230 /* Add it to the circular LRU list */
231 if (vcp->lruHead == NULL)
232 Abort("VPutVnode: vcp->lruHead==NULL");
234 vnp->lruNext = vcp->lruHead;
235 vnp->lruPrev = vcp->lruHead->lruPrev;
236 vcp->lruHead->lruPrev = vnp;
237 vnp->lruPrev->lruNext = vnp;
241 /* If the vnode was just deleted, put it at the end of the chain so it
242 * will be reused immediately */
244 vcp->lruHead = vnp->lruNext;
246 Vn_stateFlags(vnp) |= VN_ON_LRU;
250 * delete a vnode from the lru.
252 * @param[in] vcp vnode class info object pointer
253 * @param[in] vnp vnode object pointer
255 * @internal vnode package internal use only
258 DeleteFromVnLRU(struct VnodeClassInfo * vcp, Vnode * vnp)
260 if (!(Vn_stateFlags(vnp) & VN_ON_LRU)) {
264 if (vnp == vcp->lruHead)
265 vcp->lruHead = vcp->lruHead->lruNext;
267 if ((vnp == vcp->lruHead) ||
268 (vcp->lruHead == NULL))
269 Abort("DeleteFromVnLRU: lru chain addled!\n");
271 vnp->lruPrev->lruNext = vnp->lruNext;
272 vnp->lruNext->lruPrev = vnp->lruPrev;
274 Vn_stateFlags(vnp) &= ~(VN_ON_LRU);
278 * add a vnode to the vnode hash table.
280 * @param[in] vnp vnode object pointer
284 * @post vnode on hash
286 * @internal vnode package internal use only
289 AddToVnHash(Vnode * vnp)
291 unsigned int newHash;
293 if (!(Vn_stateFlags(vnp) & VN_ON_HASH)) {
294 newHash = VNODE_HASH(Vn_volume(vnp), Vn_id(vnp));
295 vnp->hashNext = VnodeHashTable[newHash];
296 VnodeHashTable[newHash] = vnp;
297 vnp->hashIndex = newHash;
299 Vn_stateFlags(vnp) |= VN_ON_HASH;
304 * delete a vnode from the vnode hash table.
311 * @post vnode removed from hash
313 * @internal vnode package internal use only
316 DeleteFromVnHash(Vnode * vnp)
320 if (Vn_stateFlags(vnp) & VN_ON_HASH) {
321 tvnp = VnodeHashTable[vnp->hashIndex];
323 VnodeHashTable[vnp->hashIndex] = vnp->hashNext;
325 while (tvnp && tvnp->hashNext != vnp)
326 tvnp = tvnp->hashNext;
328 tvnp->hashNext = vnp->hashNext;
331 vnp->hashNext = NULL;
333 Vn_stateFlags(vnp) &= ~(VN_ON_HASH);
339 * invalidate a vnode cache entry.
341 * @param[in] avnode vnode object pointer
345 * @post vnode metadata invalidated.
346 * vnode removed from hash table.
347 * DAFS: vnode state set to VN_STATE_INVALID.
349 * @internal vnode package internal use only
352 VInvalidateVnode_r(register struct Vnode *avnode)
354 avnode->changed_newTime = 0; /* don't let it get flushed out again */
355 avnode->changed_oldTime = 0;
356 avnode->delete = 0; /* it isn't deleted, really */
357 avnode->cacheCheck = 0; /* invalid: prevents future vnode searches from working */
358 DeleteFromVnHash(avnode);
359 #ifdef AFS_DEMAND_ATTACH_FS
360 VnChangeState_r(avnode, VN_STATE_INVALID);
366 * initialize vnode cache for a given vnode class.
368 * @param[in] class vnode class
369 * @param[in] nVnodes size of cache
371 * @post vnode cache allocated and initialized
373 * @internal volume package internal use only
375 * @note generally called by VInitVolumePackage_r
377 * @see VInitVolumePackage_r
380 VInitVnodes(VnodeClass class, int nVnodes)
383 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
385 vcp->allocs = vcp->gets = vcp->reads = vcp->writes = 0;
386 vcp->cacheSize = nVnodes;
389 assert(CHECKSIZE_SMALLVNODE);
391 vcp->residentSize = SIZEOF_SMALLVNODE;
392 vcp->diskSize = SIZEOF_SMALLDISKVNODE;
393 vcp->magic = SMALLVNODEMAGIC;
397 vcp->residentSize = SIZEOF_LARGEVNODE;
398 vcp->diskSize = SIZEOF_LARGEDISKVNODE;
399 vcp->magic = LARGEVNODEMAGIC;
403 int s = vcp->diskSize - 1;
413 va = (byte *) calloc(nVnodes, vcp->residentSize);
416 Vnode *vnp = (Vnode *) va;
417 Vn_refcount(vnp) = 0; /* no context switches */
418 Vn_stateFlags(vnp) |= VN_ON_LRU;
419 #ifdef AFS_DEMAND_ATTACH_FS
420 assert(pthread_cond_init(&Vn_stateCV(vnp), NULL) == 0);
421 Vn_state(vnp) = VN_STATE_INVALID;
423 #else /* !AFS_DEMAND_ATTACH_FS */
424 Lock_Init(&vnp->lock);
425 #endif /* !AFS_DEMAND_ATTACH_FS */
426 vnp->changed_oldTime = 0;
427 vnp->changed_newTime = 0;
428 Vn_volume(vnp) = NULL;
429 Vn_cacheCheck(vnp) = 0;
430 vnp->delete = Vn_id(vnp) = 0;
431 #ifdef AFS_PTHREAD_ENV
432 vnp->writer = (pthread_t) 0;
433 #else /* AFS_PTHREAD_ENV */
434 vnp->writer = (PROCESS) 0;
435 #endif /* AFS_PTHREAD_ENV */
439 if (vcp->lruHead == NULL)
440 vcp->lruHead = vnp->lruNext = vnp->lruPrev = vnp;
442 vnp->lruNext = vcp->lruHead;
443 vnp->lruPrev = vcp->lruHead->lruPrev;
444 vcp->lruHead->lruPrev = vnp;
445 vnp->lruPrev->lruNext = vnp;
448 va += vcp->residentSize;
455 * allocate an unused vnode from the lru chain.
457 * @param[in] vcp vnode class info object pointer
459 * @pre VOL_LOCK is held
461 * @post vnode object is removed from lru, and vnode hash table.
462 * vnode is disassociated from volume object.
463 * state is set to VN_STATE_INVALID.
464 * inode handle is released.
466 * @note we traverse backwards along the lru circlist. It shouldn't
467 * be necessary to specify that nUsers == 0 since if it is in the list,
468 * nUsers should be 0. Things shouldn't be in lruq unless no one is
471 * @warning DAFS: VOL_LOCK is dropped while doing inode handle release
473 * @return vnode object pointer
476 VGetFreeVnode_r(struct VnodeClassInfo * vcp)
480 vnp = vcp->lruHead->lruPrev;
481 #ifdef AFS_DEMAND_ATTACH_FS
482 if (Vn_refcount(vnp) != 0 || VnIsExclusiveState(Vn_state(vnp)) ||
483 Vn_readers(vnp) != 0)
484 Abort("VGetFreeVnode_r: in-use vnode in lruq");
486 if (Vn_refcount(vnp) != 0 || CheckLock(&vnp->lock))
487 Abort("VGetFreeVnode_r: locked vnode in lruq");
489 VNLog(1, 2, Vn_id(vnp), (afs_int32) vnp, 0, 0);
492 * it's going to be overwritten soon enough.
493 * remove from LRU, delete hash entry, and
494 * disassociate from old parent volume before
495 * we have a chance to drop the vol glock
497 DeleteFromVnLRU(vcp, vnp);
498 DeleteFromVnHash(vnp);
499 if (Vn_volume(vnp)) {
500 DeleteFromVVnList(vnp);
503 /* drop the file descriptor */
505 #ifdef AFS_DEMAND_ATTACH_FS
506 VnChangeState_r(vnp, VN_STATE_RELEASING);
509 IH_RELEASE(vnp->handle);
510 #ifdef AFS_DEMAND_ATTACH_FS
515 #ifdef AFS_DEMAND_ATTACH_FS
516 VnChangeState_r(vnp, VN_STATE_INVALID);
524 * lookup a vnode in the vnode cache hash table.
526 * @param[in] vp pointer to volume object
527 * @param[in] vnodeId vnode id
531 * @post matching vnode object or NULL is returned
533 * @return vnode object pointer
534 * @retval NULL no matching vnode object was found in the cache
536 * @internal vnode package internal use only
538 * @note this symbol is exported strictly for fssync debug protocol use
541 VLookupVnode(Volume * vp, VnodeId vnodeId)
544 unsigned int newHash;
546 newHash = VNODE_HASH(vp, vnodeId);
547 for (vnp = VnodeHashTable[newHash];
549 ((Vn_id(vnp) != vnodeId) ||
550 (Vn_volume(vnp) != vp) ||
551 (vp->cacheCheck != Vn_cacheCheck(vnp))));
552 vnp = vnp->hashNext);
559 VAllocVnode(Error * ec, Volume * vp, VnodeType type)
563 retVal = VAllocVnode_r(ec, vp, type);
569 * allocate a new vnode.
571 * @param[out] ec error code return
572 * @param[in] vp volume object pointer
573 * @param[in] type desired vnode type
575 * @return vnode object pointer
577 * @pre VOL_LOCK held;
578 * heavyweight ref held on vp
580 * @post vnode allocated and returned
583 VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
588 register struct VnodeClassInfo *vcp;
591 #ifdef AFS_DEMAND_ATTACH_FS
592 VolState vol_state_save;
597 #ifdef AFS_DEMAND_ATTACH_FS
599 * once a volume has entered an error state, don't permit
600 * further operations to proceed
601 * -- tkeiser 11/21/2007
603 VWaitExclusiveState_r(vp);
604 if (VIsErrorState(V_attachState(vp))) {
605 /* XXX is VSALVAGING acceptable here? */
611 if (programType == fileServer && !V_inUse(vp)) {
612 if (vp->specialStatus) {
613 *ec = vp->specialStatus;
619 class = vnodeTypeToClass(type);
620 vcp = &VnodeClassInfo[class];
622 if (!VolumeWriteable(vp)) {
623 *ec = (bit32) VREADONLY;
627 unique = vp->nextVnodeUnique++;
629 unique = vp->nextVnodeUnique++;
631 if (vp->nextVnodeUnique > V_uniquifier(vp)) {
632 VUpdateVolume_r(ec, vp, 0);
637 if (programType == fileServer) {
638 VAddToVolumeUpdateList_r(ec, vp);
643 /* Find a slot in the bit map */
644 bitNumber = VAllocBitmapEntry_r(ec, vp, &vp->vnodeIndex[class],
645 VOL_ALLOC_BITMAP_WAIT);
648 vnodeNumber = bitNumberToVnodeNumber(bitNumber, class);
652 * at this point we should be assured that V_attachState(vp) is non-exclusive
656 VNLog(2, 1, vnodeNumber, 0, 0, 0);
657 /* Prepare to move it to the new hash chain */
658 vnp = VLookupVnode(vp, vnodeNumber);
660 /* slot already exists. May even not be in lruq (consider store file locking a file being deleted)
661 * so we may have to wait for it below */
662 VNLog(3, 2, vnodeNumber, (afs_int32) vnp, 0, 0);
664 VnCreateReservation_r(vnp);
665 if (Vn_refcount(vnp) == 1) {
666 /* we're the only user */
667 /* This won't block */
668 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
670 /* other users present; follow locking hierarchy */
671 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, MIGHT_DEADLOCK);
673 #ifdef AFS_DEMAND_ATTACH_FS
676 * vnode was cached, wait for any existing exclusive ops to finish.
677 * once we have reacquired the lock, re-verify volume state.
679 * note: any vnode error state is related to the old vnode; disregard.
681 VnWaitQuiescent_r(vnp);
682 if (VIsErrorState(V_attachState(vp))) {
683 VnUnlock(vnp, WRITE_LOCK);
684 VnCancelReservation_r(vnp);
691 * verify state of the world hasn't changed
693 * (technically, this should never happen because cachecheck
694 * is only updated during a volume attach, which should not
695 * happen when refs are held)
697 if (Vn_volume(vnp)->cacheCheck != Vn_cacheCheck(vnp)) {
698 VnUnlock(vnp, WRITE_LOCK);
699 VnCancelReservation_r(vnp);
705 /* no such vnode in the cache */
707 vnp = VGetFreeVnode_r(vcp);
709 /* Initialize the header fields so noone allocates another
710 * vnode with the same number */
711 Vn_id(vnp) = vnodeNumber;
712 VnCreateReservation_r(vnp);
713 AddToVVnList(vp, vnp);
714 #ifdef AFS_DEMAND_ATTACH_FS
718 /* This will never block (guaranteed by check in VGetFreeVnode_r() */
719 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
721 #ifdef AFS_DEMAND_ATTACH_FS
722 VnChangeState_r(vnp, VN_STATE_ALLOC);
725 /* Sanity check: is this vnode really not in use? */
728 IHandle_t *ihP = vp->vnodeIndex[class].handle;
730 off_t off = vnodeIndexOffset(vcp, vnodeNumber);
732 /* XXX we have a potential race here if two threads
733 * allocate new vnodes at the same time, and they
734 * both decide it's time to extend the index
737 #ifdef AFS_DEMAND_ATTACH_FS
739 * this race has been eliminated for the DAFS case
740 * using exclusive state VOL_STATE_VNODE_ALLOC
742 * if this becomes a bottleneck, there are ways to
743 * improve parallelism for this code path
744 * -- tkeiser 11/28/2007
746 VCreateReservation_r(vp);
747 VWaitExclusiveState_r(vp);
748 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_ALLOC);
754 Log("VAllocVnode: can't open index file!\n");
755 goto error_encountered;
757 if ((size = FDH_SIZE(fdP)) < 0) {
758 Log("VAllocVnode: can't stat index file!\n");
759 goto error_encountered;
761 if (FDH_SEEK(fdP, off, SEEK_SET) < 0) {
762 Log("VAllocVnode: can't seek on index file!\n");
763 goto error_encountered;
765 if (off + vcp->diskSize <= size) {
766 if (FDH_READ(fdP, &vnp->disk, vcp->diskSize) != vcp->diskSize) {
767 Log("VAllocVnode: can't read index file!\n");
768 goto error_encountered;
770 if (vnp->disk.type != vNull) {
771 Log("VAllocVnode: addled bitmap or index!\n");
772 goto error_encountered;
775 /* growing file - grow in a reasonable increment */
776 char *buf = (char *)malloc(16 * 1024);
778 Abort("VAllocVnode: malloc failed\n");
779 memset(buf, 0, 16 * 1024);
780 (void)FDH_WRITE(fdP, buf, 16 * 1024);
786 #ifdef AFS_DEMAND_ATTACH_FS
787 VChangeState_r(vp, vol_state_save);
788 VCancelReservation_r(vp);
794 #ifdef AFS_DEMAND_ATTACH_FS
796 * close the file handle
798 * invalidate the vnode
799 * free up the bitmap entry (although salvager should take care of it)
801 * drop vnode lock and refs
806 VFreeBitMapEntry_r(ec, &vp->vnodeIndex[class], bitNumber);
807 VInvalidateVnode_r(vnp);
808 VnUnlock(vnp, WRITE_LOCK);
809 VnCancelReservation_r(vnp);
810 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
811 VCancelReservation_r(vp);
819 VNLog(4, 2, vnodeNumber, (afs_int32) vnp, 0, 0);
820 #ifndef AFS_DEMAND_ATTACH_FS
825 VNLog(5, 1, (afs_int32) vnp, 0, 0, 0);
826 memset(&vnp->disk, 0, sizeof(vnp->disk));
827 vnp->changed_newTime = 0; /* set this bit when vnode is updated */
828 vnp->changed_oldTime = 0; /* set this on CopyOnWrite. */
830 vnp->disk.vnodeMagic = vcp->magic;
831 vnp->disk.type = type;
832 vnp->disk.uniquifier = unique;
835 vp->header->diskstuff.filecount++;
836 #ifdef AFS_DEMAND_ATTACH_FS
837 VnChangeState_r(vnp, VN_STATE_EXCLUSIVE);
843 * load a vnode from disk.
845 * @param[out] ec client error code return
846 * @param[in] vp volume object pointer
847 * @param[in] vnp vnode object pointer
848 * @param[in] vcp vnode class info object pointer
849 * @param[in] class vnode class enumeration
851 * @pre vnode is registered in appropriate data structures;
852 * caller holds a ref on vnode; VOL_LOCK is held
854 * @post vnode data is loaded from disk.
855 * vnode state is set to VN_STATE_ONLINE.
856 * on failure, vnode is invalidated.
858 * @internal vnode package internal use only
861 VnLoad(Error * ec, Volume * vp, Vnode * vnp,
862 struct VnodeClassInfo * vcp, VnodeClass class)
864 /* vnode not cached */
867 IHandle_t *ihP = vp->vnodeIndex[class].handle;
873 #ifdef AFS_DEMAND_ATTACH_FS
874 VnChangeState_r(vnp, VN_STATE_LOAD);
877 /* This will never block */
878 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
883 Log("VnLoad: can't open index dev=%u, i=%s\n", vp->device,
884 PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
886 goto error_encountered_nolock;
887 } else if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, Vn_id(vnp)), SEEK_SET)
889 Log("VnLoad: can't seek on index file vn=%u\n", Vn_id(vnp));
891 goto error_encountered_nolock;
892 } else if ((n = FDH_READ(fdP, (char *)&vnp->disk, vcp->diskSize))
894 /* Don't take volume off line if the inumber is out of range
895 * or the inode table is full. */
897 Log("VnLoad: bad inumber %s\n",
898 PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
901 } else if (n == -1 && errno == EIO) {
902 /* disk error; salvage */
903 Log("VnLoad: Couldn't read vnode %u, volume %u (%s); volume needs salvage\n", Vn_id(vnp), V_id(vp), V_name(vp));
905 /* vnode is not allocated */
907 Log("VnLoad: Couldn't read vnode %u, volume %u (%s); read %d bytes, errno %d\n",
908 Vn_id(vnp), V_id(vp), V_name(vp), n, errno);
912 goto error_encountered_nolock;
917 /* Quick check to see that the data is reasonable */
918 if (vnp->disk.vnodeMagic != vcp->magic || vnp->disk.type == vNull) {
919 if (vnp->disk.type == vNull) {
923 struct vnodeIndex *index = &vp->vnodeIndex[class];
924 unsigned int bitNumber = vnodeIdToBitNumber(Vn_id(vnp));
925 unsigned int offset = bitNumber >> 3;
927 /* Test to see if vnode number is valid. */
928 if ((offset >= index->bitmapSize)
929 || ((*(index->bitmap + offset) & (1 << (bitNumber & 0x7)))
931 Log("VnLoad: Request for unallocated vnode %u, volume %u (%s) denied.\n", Vn_id(vnp), V_id(vp), V_name(vp));
935 Log("VnLoad: Bad magic number, vnode %u, volume %u (%s); volume needs salvage\n", Vn_id(vnp), V_id(vp), V_name(vp));
938 goto error_encountered;
941 IH_INIT(vnp->handle, V_device(vp), V_parentId(vp), VN_GET_INO(vnp));
942 VnUnlock(vnp, WRITE_LOCK);
943 #ifdef AFS_DEMAND_ATTACH_FS
944 VnChangeState_r(vnp, VN_STATE_ONLINE);
949 error_encountered_nolock:
951 FDH_REALLYCLOSE(fdP);
957 #ifdef AFS_DEMAND_ATTACH_FS
958 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0);
960 VForceOffline_r(vp, 0);
967 VInvalidateVnode_r(vnp);
968 VnUnlock(vnp, WRITE_LOCK);
972 * store a vnode to disk.
974 * @param[out] ec error code output
975 * @param[in] vp volume object pointer
976 * @param[in] vnp vnode object pointer
977 * @param[in] vcp vnode class info object pointer
978 * @param[in] class vnode class enumeration
980 * @pre VOL_LOCK held.
981 * caller holds refs to volume and vnode.
982 * DAFS: caller is responsible for performing state sanity checks.
984 * @post vnode state is stored to disk.
986 * @internal vnode package internal use only
989 VnStore(Error * ec, Volume * vp, Vnode * vnp,
990 struct VnodeClassInfo * vcp, VnodeClass class)
993 IHandle_t *ihP = vp->vnodeIndex[class].handle;
995 #ifdef AFS_DEMAND_ATTACH_FS
996 VnState vn_state_save;
1001 #ifdef AFS_DEMAND_ATTACH_FS
1002 vn_state_save = VnChangeState_r(vnp, VN_STATE_STORE);
1005 offset = vnodeIndexOffset(vcp, Vn_id(vnp));
1009 Log("VnStore: can't open index file!\n");
1010 goto error_encountered;
1012 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
1013 Log("VnStore: can't seek on index file! fdp=0x%x offset=%d, errno=%d\n",
1014 fdP, offset, errno);
1015 goto error_encountered;
1018 code = FDH_WRITE(fdP, &vnp->disk, vcp->diskSize);
1019 if (code != vcp->diskSize) {
1020 /* Don't force volume offline if the inumber is out of
1021 * range or the inode table is full.
1023 FDH_REALLYCLOSE(fdP);
1024 if (code == BAD_IGET) {
1025 Log("VnStore: bad inumber %s\n",
1027 vp->vnodeIndex[class].handle->ih_ino));
1030 #ifdef AFS_DEMAND_ATTACH_FS
1031 VnChangeState_r(vnp, VN_STATE_ERROR);
1034 Log("VnStore: Couldn't write vnode %u, volume %u (%s) (error %d)\n", Vn_id(vnp), V_id(Vn_volume(vnp)), V_name(Vn_volume(vnp)), code);
1035 #ifdef AFS_DEMAND_ATTACH_FS
1036 goto error_encountered;
1039 VForceOffline_r(vp, 0);
1049 #ifdef AFS_DEMAND_ATTACH_FS
1050 VnChangeState_r(vnp, vn_state_save);
1055 #ifdef AFS_DEMAND_ATTACH_FS
1056 /* XXX instead of dumping core, let's try to request a salvage
1057 * and just fail the putvnode */
1061 VnChangeState_r(vnp, VN_STATE_ERROR);
1062 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1069 * get a handle to a vnode object.
1071 * @param[out] ec error code
1072 * @param[in] vp volume object
1073 * @param[in] vnodeNumber vnode id
1074 * @param[in] locktype type of lock to acquire
1076 * @return vnode object pointer
1081 VGetVnode(Error * ec, Volume * vp, VnodeId vnodeNumber, int locktype)
1082 { /* READ_LOCK or WRITE_LOCK, as defined in lock.h */
1085 retVal = VGetVnode_r(ec, vp, vnodeNumber, locktype);
1091 * get a handle to a vnode object.
1093 * @param[out] ec error code
1094 * @param[in] vp volume object
1095 * @param[in] vnodeNumber vnode id
1096 * @param[in] locktype type of lock to acquire
1098 * @return vnode object pointer
1100 * @internal vnode package internal use only
1102 * @pre VOL_LOCK held.
1103 * heavyweight ref held on volume object.
1106 VGetVnode_r(Error * ec, Volume * vp, VnodeId vnodeNumber, int locktype)
1107 { /* READ_LOCK or WRITE_LOCK, as defined in lock.h */
1108 register Vnode *vnp;
1111 struct VnodeClassInfo *vcp;
1112 Volume * oldvp = NULL;
1116 if (vnodeNumber == 0) {
1121 VNLog(100, 1, vnodeNumber, 0, 0, 0);
1123 #ifdef AFS_DEMAND_ATTACH_FS
1125 * once a volume has entered an error state, don't permit
1126 * further operations to proceed
1127 * -- tkeiser 11/21/2007
1129 VWaitExclusiveState_r(vp);
1130 if (VIsErrorState(V_attachState(vp))) {
1131 /* XXX is VSALVAGING acceptable here? */
1137 if (programType == fileServer && !V_inUse(vp)) {
1138 *ec = (vp->specialStatus ? vp->specialStatus : VOFFLINE);
1140 /* If the volume is VBUSY (being cloned or dumped) and this is
1141 * a READ operation, then don't fail.
1143 if ((*ec != VBUSY) || (locktype != READ_LOCK)) {
1148 class = vnodeIdToClass(vnodeNumber);
1149 vcp = &VnodeClassInfo[class];
1150 if (locktype == WRITE_LOCK && !VolumeWriteable(vp)) {
1151 *ec = (bit32) VREADONLY;
1155 if (locktype == WRITE_LOCK && programType == fileServer) {
1156 VAddToVolumeUpdateList_r(ec, vp);
1164 /* See whether the vnode is in the cache. */
1165 vnp = VLookupVnode(vp, vnodeNumber);
1167 /* vnode is in cache */
1169 VNLog(101, 2, vnodeNumber, (afs_int32) vnp, 0, 0);
1170 VnCreateReservation_r(vnp);
1172 #ifdef AFS_DEMAND_ATTACH_FS
1174 * this is the one DAFS case where we may run into contention.
1175 * here's the basic control flow:
1177 * if locktype is READ_LOCK:
1178 * wait until vnode is not exclusive
1179 * set to VN_STATE_READ
1180 * increment read count
1183 * wait until vnode is quiescent
1184 * set to VN_STATE_EXCLUSIVE
1187 if (locktype == READ_LOCK) {
1188 VnWaitExclusiveState_r(vnp);
1190 VnWaitQuiescent_r(vnp);
1193 if (VnIsErrorState(Vn_state(vnp))) {
1194 VnCancelReservation_r(vnp);
1198 #endif /* AFS_DEMAND_ATTACH_FS */
1200 /* vnode not cached */
1202 /* Not in cache; tentatively grab most distantly used one from the LRU
1205 vnp = VGetFreeVnode_r(vcp);
1208 vnp->changed_newTime = vnp->changed_oldTime = 0;
1210 Vn_id(vnp) = vnodeNumber;
1211 VnCreateReservation_r(vnp);
1212 AddToVVnList(vp, vnp);
1213 #ifdef AFS_DEMAND_ATTACH_FS
1218 * XXX for non-DAFS, there is a serious
1219 * race condition here:
1221 * two threads can race to load a vnode. the net
1222 * result is two struct Vnodes can be allocated
1223 * and hashed, which point to the same underlying
1224 * disk data store. conflicting vnode locks can
1225 * thus be held concurrently.
1227 * for non-DAFS to be safe, VOL_LOCK really shouldn't
1228 * be dropped in VnLoad. Of course, this would likely
1229 * lead to an unacceptable slow-down.
1232 VnLoad(ec, vp, vnp, vcp, class);
1234 VnCancelReservation_r(vnp);
1237 #ifndef AFS_DEMAND_ATTACH_FS
1242 * there is no possibility for contention. we "own" this vnode.
1248 * it is imperative that nothing drop vol lock between here
1249 * and the VnBeginRead/VnChangeState stanza below
1252 VnLock(vnp, locktype, VOL_LOCK_HELD, MIGHT_DEADLOCK);
1254 /* Check that the vnode hasn't been removed while we were obtaining
1256 VNLog(102, 2, vnodeNumber, (afs_int32) vnp, 0, 0);
1257 if ((vnp->disk.type == vNull) || (Vn_cacheCheck(vnp) == 0)) {
1258 VnUnlock(vnp, locktype);
1259 VnCancelReservation_r(vnp);
1261 /* vnode is labelled correctly by now, so we don't have to invalidate it */
1265 #ifdef AFS_DEMAND_ATTACH_FS
1266 if (locktype == READ_LOCK) {
1269 VnChangeState_r(vnp, VN_STATE_EXCLUSIVE);
1273 if (programType == fileServer)
1274 VBumpVolumeUsage_r(Vn_volume(vnp)); /* Hack; don't know where it should be
1275 * called from. Maybe VGetVolume */
1280 int TrustVnodeCacheEntry = 1;
1281 /* This variable is bogus--when it's set to 0, the hash chains fill
1282 up with multiple versions of the same vnode. Should fix this!! */
1284 VPutVnode(Error * ec, register Vnode * vnp)
1287 VPutVnode_r(ec, vnp);
1292 * put back a handle to a vnode object.
1294 * @param[out] ec client error code
1295 * @param[in] vnp vnode object pointer
1297 * @pre VOL_LOCK held.
1298 * ref held on vnode.
1300 * @post ref dropped on vnode.
1301 * if vnode was modified or deleted, it is written out to disk
1302 * (assuming a write lock was held).
1304 * @internal volume package internal use only
1307 VPutVnode_r(Error * ec, register Vnode * vnp)
1311 struct VnodeClassInfo *vcp;
1315 assert(Vn_refcount(vnp) != 0);
1316 class = vnodeIdToClass(Vn_id(vnp));
1317 vcp = &VnodeClassInfo[class];
1318 assert(vnp->disk.vnodeMagic == vcp->magic);
1319 VNLog(200, 2, Vn_id(vnp), (afs_int32) vnp, 0, 0);
1321 #ifdef AFS_DEMAND_ATTACH_FS
1322 writeLocked = (Vn_state(vnp) == VN_STATE_EXCLUSIVE);
1324 writeLocked = WriteLocked(&vnp->lock);
1329 #ifdef AFS_PTHREAD_ENV
1330 pthread_t thisProcess = pthread_self();
1331 #else /* AFS_PTHREAD_ENV */
1332 PROCESS thisProcess;
1333 LWP_CurrentProcess(&thisProcess);
1334 #endif /* AFS_PTHREAD_ENV */
1335 VNLog(201, 2, (afs_int32) vnp,
1336 ((vnp->changed_newTime) << 1) | ((vnp->
1337 changed_oldTime) << 1) | vnp->
1339 if (thisProcess != vnp->writer)
1340 Abort("VPutVnode: Vnode at 0x%x locked by another process!\n",
1344 if (vnp->changed_oldTime || vnp->changed_newTime || vnp->delete) {
1345 Volume *vp = Vn_volume(vnp);
1346 afs_uint32 now = FT_ApproxTime();
1347 assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
1350 /* No longer any directory entries for this vnode. Free the Vnode */
1351 memset(&vnp->disk, 0, sizeof(vnp->disk));
1352 /* delete flag turned off further down */
1353 VNLog(202, 2, Vn_id(vnp), (afs_int32) vnp, 0, 0);
1354 } else if (vnp->changed_newTime) {
1355 vnp->disk.serverModifyTime = now;
1357 if (vnp->changed_newTime)
1359 V_updateDate(vp) = vp->updateTime = now;
1360 if(V_volUpCounter(vp)<MAXINT)
1361 V_volUpCounter(vp)++;
1364 /* The vnode has been changed. Write it out to disk */
1366 #ifdef AFS_DEMAND_ATTACH_FS
1367 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1369 assert(V_needsSalvaged(vp));
1373 VnStore(ec, vp, vnp, vcp, class);
1375 /* If the vnode is to be deleted, and we wrote the vnode out,
1376 * free its bitmap entry. Do after the vnode is written so we
1377 * don't allocate from bitmap before the vnode is written
1378 * (doing so could cause a "addled bitmap" message).
1380 if (vnp->delete && !*ec) {
1381 if (Vn_volume(vnp)->header->diskstuff.filecount-- < 1)
1382 Vn_volume(vnp)->header->diskstuff.filecount = 0;
1383 VFreeBitMapEntry_r(ec, &vp->vnodeIndex[class],
1384 vnodeIdToBitNumber(Vn_id(vnp)));
1388 vnp->changed_newTime = vnp->changed_oldTime = 0;
1390 #ifdef AFS_DEMAND_ATTACH_FS
1391 VnChangeState_r(vnp, VN_STATE_ONLINE);
1393 } else { /* Not write locked */
1394 if (vnp->changed_newTime || vnp->changed_oldTime || vnp->delete)
1396 ("VPutVnode: Change or delete flag for vnode 0x%x is set but vnode is not write locked!\n",
1398 #ifdef AFS_DEMAND_ATTACH_FS
1403 /* Do not look at disk portion of vnode after this point; it may
1404 * have been deleted above */
1406 VnUnlock(vnp, ((writeLocked) ? WRITE_LOCK : READ_LOCK));
1407 VnCancelReservation_r(vnp);
1411 * Make an attempt to convert a vnode lock from write to read.
1412 * Do nothing if the vnode isn't write locked or the vnode has
1416 VVnodeWriteToRead(Error * ec, register Vnode * vnp)
1420 retVal = VVnodeWriteToRead_r(ec, vnp);
1426 * convert vnode handle from mutually exclusive to shared access.
1428 * @param[out] ec client error code
1429 * @param[in] vnp vnode object pointer
1431 * @return unspecified use (see out argument 'ec' for error code return)
1433 * @pre VOL_LOCK held.
1434 * ref held on vnode.
1435 * write lock held on vnode.
1437 * @post read lock held on vnode.
1438 * if vnode was modified, it has been written to disk.
1440 * @internal volume package internal use only
1443 VVnodeWriteToRead_r(Error * ec, register Vnode * vnp)
1447 struct VnodeClassInfo *vcp;
1449 #ifdef AFS_PTHREAD_ENV
1450 pthread_t thisProcess;
1451 #else /* AFS_PTHREAD_ENV */
1452 PROCESS thisProcess;
1453 #endif /* AFS_PTHREAD_ENV */
1456 assert(Vn_refcount(vnp) != 0);
1457 class = vnodeIdToClass(Vn_id(vnp));
1458 vcp = &VnodeClassInfo[class];
1459 assert(vnp->disk.vnodeMagic == vcp->magic);
1460 VNLog(300, 2, Vn_id(vnp), (afs_int32) vnp, 0, 0);
1462 #ifdef AFS_DEMAND_ATTACH_FS
1463 writeLocked = (Vn_state(vnp) == VN_STATE_EXCLUSIVE);
1465 writeLocked = WriteLocked(&vnp->lock);
1472 VNLog(301, 2, (afs_int32) vnp,
1473 ((vnp->changed_newTime) << 1) | ((vnp->
1474 changed_oldTime) << 1) | vnp->
1478 #ifdef AFS_PTHREAD_ENV
1479 thisProcess = pthread_self();
1480 #else /* AFS_PTHREAD_ENV */
1481 LWP_CurrentProcess(&thisProcess);
1482 #endif /* AFS_PTHREAD_ENV */
1483 if (thisProcess != vnp->writer)
1484 Abort("VPutVnode: Vnode at 0x%x locked by another process!\n",
1490 if (vnp->changed_oldTime || vnp->changed_newTime) {
1491 Volume *vp = Vn_volume(vnp);
1492 afs_uint32 now = FT_ApproxTime();
1493 assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
1494 if (vnp->changed_newTime)
1495 vnp->disk.serverModifyTime = now;
1496 if (vnp->changed_newTime)
1497 V_updateDate(vp) = vp->updateTime = now;
1499 /* The inode has been changed. Write it out to disk */
1501 #ifdef AFS_DEMAND_ATTACH_FS
1502 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1504 assert(V_needsSalvaged(vp));
1508 VnStore(ec, vp, vnp, vcp, class);
1512 vnp->changed_newTime = vnp->changed_oldTime = 0;
1516 #ifdef AFS_DEMAND_ATTACH_FS
1517 VnChangeState_r(vnp, VN_STATE_ONLINE);
1520 ConvertWriteToReadLock(&vnp->lock);
1525 /* VCloseVnodeFiles - called when a volume is going off line. All open
1526 * files for vnodes in that volume are closed. This might be excessive,
1527 * since we may only be taking one volume of a volume group offline.
1530 VCloseVnodeFiles_r(Volume * vp)
1534 #ifdef AFS_DEMAND_ATTACH_FS
1535 VolState vol_state_save;
1537 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_CLOSE);
1539 #endif /* AFS_DEMAND_ATTACH_FS */
1541 for (queue_Scan(&vp->vnode_list, vnp, nvnp, Vnode)) {
1542 IH_REALLYCLOSE(vnp->handle);
1543 DeleteFromVVnList(vnp);
1546 #ifdef AFS_DEMAND_ATTACH_FS
1548 VChangeState_r(vp, vol_state_save);
1549 #endif /* AFS_DEMAND_ATTACH_FS */
1553 * shut down all vnode cache state for a given volume.
1555 * @param[in] vp volume object pointer
1557 * @pre VOL_LOCK is held
1559 * @post all file descriptors closed.
1560 * all inode handles released.
1561 * all vnode cache objects disassociated from volume.
1563 * @note for DAFS, these operations are performed outside the vol glock under
1564 * volume exclusive state VOL_STATE_VNODE_RELEASE. Please further note
1565 * that it would be a bug to acquire and release a volume reservation
1566 * during this exclusive operation. This is due to the fact that we are
1567 * generally called during the refcount 1->0 transition.
1569 * @internal this routine is internal to the volume package
1572 VReleaseVnodeFiles_r(Volume * vp)
1576 #ifdef AFS_DEMAND_ATTACH_FS
1577 VolState vol_state_save;
1579 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_RELEASE);
1581 #endif /* AFS_DEMAND_ATTACH_FS */
1583 for (queue_Scan(&vp->vnode_list, vnp, nvnp, Vnode)) {
1584 IH_RELEASE(vnp->handle);
1585 DeleteFromVVnList(vnp);
1588 #ifdef AFS_DEMAND_ATTACH_FS
1590 VChangeState_r(vp, vol_state_save);
1591 #endif /* AFS_DEMAND_ATTACH_FS */