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)))
27 #ifdef AFS_PTHREAD_ENV
29 #else /* AFS_PTHREAD_ENV */
30 #include <afs/assert.h>
31 #endif /* AFS_PTHREAD_ENV */
34 #include "rx/rx_queue.h"
35 #include <afs/afsint.h>
37 #include <afs/errors.h>
40 #include <afs/afssyscalls.h>
44 #include "volume_inline.h"
45 #include "vnode_inline.h"
46 #include "partition.h"
48 #if defined(AFS_SGI_ENV)
49 #include "sys/types.h"
61 #include <sys/fcntl.h>
64 #endif /* AFS_NT40_ENV */
71 /*@printflike@*/ extern void Log(const char *format, ...);
73 /*@printflike@*/ extern void Abort(const char *format, ...) AFS_NORETURN;
76 struct VnodeClassInfo VnodeClassInfo[nVNODECLASSES];
78 void VNLog(afs_int32 aop, afs_int32 anparms, ... );
85 #define BAD_IGET -1000
87 /* There are two separate vnode queue types defined here:
88 * Each hash conflict chain -- is singly linked, with a single head
89 * pointer. New entries are added at the beginning. Old
90 * entries are removed by linear search, which generally
91 * only occurs after a disk read).
92 * LRU chain -- is doubly linked, single head pointer.
93 * Entries are added at the head, reclaimed from the tail,
94 * or removed from anywhere in the queue.
98 /* Vnode hash table. Find hash chain by taking lower bits of
99 * (volume_hash_offset + vnode).
100 * This distributes the root inodes of the volumes over the
101 * hash table entries and also distributes the vnodes of
102 * volumes reasonably fairly. The volume_hash_offset field
103 * for each volume is established as the volume comes on line
104 * by using the VOLUME_HASH_OFFSET macro. This distributes the
105 * volumes fairly among the cache entries, both when servicing
106 * a small number of volumes and when servicing a large number.
109 /* logging stuff for finding bugs */
110 #define THELOGSIZE 5120
111 static afs_int32 theLog[THELOGSIZE];
112 static afs_int32 vnLogPtr = 0;
114 VNLog(afs_int32 aop, afs_int32 anparms, ... )
116 register afs_int32 temp;
119 va_start(ap, anparms);
122 anparms = 4; /* do bounds checking */
124 temp = (aop << 16) | anparms;
125 theLog[vnLogPtr++] = temp;
126 if (vnLogPtr >= THELOGSIZE)
128 for (temp = 0; temp < anparms; temp++) {
129 theLog[vnLogPtr++] = va_arg(ap, afs_int32);
130 if (vnLogPtr >= THELOGSIZE)
136 /* VolumeHashOffset -- returns a new value to be stored in the
137 * volumeHashOffset of a Volume structure. Called when a
138 * volume is initialized. Sets the volumeHashOffset so that
139 * vnode cache entries are distributed reasonably between
140 * volumes (the root vnodes of the volumes will hash to
141 * different values, and spacing is maintained between volumes
142 * when there are not many volumes represented), and spread
143 * equally amongst vnodes within a single volume.
146 VolumeHashOffset_r(void)
148 static int nextVolumeHashOffset = 0;
149 /* hashindex Must be power of two in size */
151 # define hashMask ((1<<hashShift)-1)
152 static byte hashindex[1 << hashShift] =
153 { 0, 128, 64, 192, 32, 160, 96, 224 };
155 offset = hashindex[nextVolumeHashOffset & hashMask]
156 + (nextVolumeHashOffset >> hashShift);
157 nextVolumeHashOffset++;
161 /* Change hashindex (above) if you change this constant */
162 #define VNODE_HASH_TABLE_SIZE 256
163 private Vnode *VnodeHashTable[VNODE_HASH_TABLE_SIZE];
164 #define VNODE_HASH(volumeptr,vnodenumber)\
165 ((volumeptr->vnodeHashOffset + vnodenumber)&(VNODE_HASH_TABLE_SIZE-1))
169 * add a vnode to the volume's vnode list.
171 * @param[in] vp volume object pointer
172 * @param[in] vnp vnode object pointer
174 * @note for DAFS, it may seem like we should be acquiring a lightweight ref
175 * on vp, but this would actually break things. Right now, this is ok
176 * because we destroy all vnode cache contents during during volume
181 * @internal volume package internal use only
184 AddToVVnList(Volume * vp, Vnode * vnp)
186 if (queue_IsOnQueue(vnp))
190 Vn_cacheCheck(vnp) = vp->cacheCheck;
191 queue_Append(&vp->vnode_list, vnp);
192 Vn_stateFlags(vnp) |= VN_ON_VVN;
196 * delete a vnode from the volume's vnode list.
200 * @internal volume package internal use only
203 DeleteFromVVnList(register Vnode * vnp)
205 Vn_volume(vnp) = NULL;
207 if (!queue_IsOnQueue(vnp))
211 Vn_stateFlags(vnp) &= ~(VN_ON_VVN);
215 * add a vnode to the end of the lru.
217 * @param[in] vcp vnode class info object pointer
218 * @param[in] vnp vnode object pointer
220 * @internal vnode package internal use only
223 AddToVnLRU(struct VnodeClassInfo * vcp, Vnode * vnp)
225 if (Vn_stateFlags(vnp) & VN_ON_LRU) {
229 /* Add it to the circular LRU list */
230 if (vcp->lruHead == NULL)
231 Abort("VPutVnode: vcp->lruHead==NULL");
233 vnp->lruNext = vcp->lruHead;
234 vnp->lruPrev = vcp->lruHead->lruPrev;
235 vcp->lruHead->lruPrev = vnp;
236 vnp->lruPrev->lruNext = vnp;
240 /* If the vnode was just deleted, put it at the end of the chain so it
241 * will be reused immediately */
243 vcp->lruHead = vnp->lruNext;
245 Vn_stateFlags(vnp) |= VN_ON_LRU;
249 * delete a vnode from the lru.
251 * @param[in] vcp vnode class info object pointer
252 * @param[in] vnp vnode object pointer
254 * @internal vnode package internal use only
257 DeleteFromVnLRU(struct VnodeClassInfo * vcp, Vnode * vnp)
259 if (!(Vn_stateFlags(vnp) & VN_ON_LRU)) {
263 if (vnp == vcp->lruHead)
264 vcp->lruHead = vcp->lruHead->lruNext;
266 if ((vnp == vcp->lruHead) ||
267 (vcp->lruHead == NULL))
268 Abort("DeleteFromVnLRU: lru chain addled!\n");
270 vnp->lruPrev->lruNext = vnp->lruNext;
271 vnp->lruNext->lruPrev = vnp->lruPrev;
273 Vn_stateFlags(vnp) &= ~(VN_ON_LRU);
277 * add a vnode to the vnode hash table.
279 * @param[in] vnp vnode object pointer
283 * @post vnode on hash
285 * @internal vnode package internal use only
288 AddToVnHash(Vnode * vnp)
290 unsigned int newHash;
292 if (!(Vn_stateFlags(vnp) & VN_ON_HASH)) {
293 newHash = VNODE_HASH(Vn_volume(vnp), Vn_id(vnp));
294 vnp->hashNext = VnodeHashTable[newHash];
295 VnodeHashTable[newHash] = vnp;
296 vnp->hashIndex = newHash;
298 Vn_stateFlags(vnp) |= VN_ON_HASH;
303 * delete a vnode from the vnode hash table.
310 * @post vnode removed from hash
312 * @internal vnode package internal use only
315 DeleteFromVnHash(Vnode * vnp)
319 if (Vn_stateFlags(vnp) & VN_ON_HASH) {
320 tvnp = VnodeHashTable[vnp->hashIndex];
322 VnodeHashTable[vnp->hashIndex] = vnp->hashNext;
324 while (tvnp && tvnp->hashNext != vnp)
325 tvnp = tvnp->hashNext;
327 tvnp->hashNext = vnp->hashNext;
330 vnp->hashNext = NULL;
332 Vn_stateFlags(vnp) &= ~(VN_ON_HASH);
338 * invalidate a vnode cache entry.
340 * @param[in] avnode vnode object pointer
344 * @post vnode metadata invalidated.
345 * vnode removed from hash table.
346 * DAFS: vnode state set to VN_STATE_INVALID.
348 * @internal vnode package internal use only
351 VInvalidateVnode_r(register struct Vnode *avnode)
353 avnode->changed_newTime = 0; /* don't let it get flushed out again */
354 avnode->changed_oldTime = 0;
355 avnode->delete = 0; /* it isn't deleted, really */
356 avnode->cacheCheck = 0; /* invalid: prevents future vnode searches from working */
357 DeleteFromVnHash(avnode);
358 #ifdef AFS_DEMAND_ATTACH_FS
359 VnChangeState_r(avnode, VN_STATE_INVALID);
365 * initialize vnode cache for a given vnode class.
367 * @param[in] class vnode class
368 * @param[in] nVnodes size of cache
370 * @post vnode cache allocated and initialized
372 * @internal volume package internal use only
374 * @note generally called by VInitVolumePackage_r
376 * @see VInitVolumePackage_r
379 VInitVnodes(VnodeClass class, int nVnodes)
382 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
384 vcp->allocs = vcp->gets = vcp->reads = vcp->writes = 0;
385 vcp->cacheSize = nVnodes;
388 assert(CHECKSIZE_SMALLVNODE);
390 vcp->residentSize = SIZEOF_SMALLVNODE;
391 vcp->diskSize = SIZEOF_SMALLDISKVNODE;
392 vcp->magic = SMALLVNODEMAGIC;
396 vcp->residentSize = SIZEOF_LARGEVNODE;
397 vcp->diskSize = SIZEOF_LARGEDISKVNODE;
398 vcp->magic = LARGEVNODEMAGIC;
402 int s = vcp->diskSize - 1;
412 va = (byte *) calloc(nVnodes, vcp->residentSize);
415 Vnode *vnp = (Vnode *) va;
416 Vn_refcount(vnp) = 0; /* no context switches */
417 Vn_stateFlags(vnp) |= VN_ON_LRU;
418 #ifdef AFS_DEMAND_ATTACH_FS
419 assert(pthread_cond_init(&Vn_stateCV(vnp), NULL) == 0);
420 Vn_state(vnp) = VN_STATE_INVALID;
422 #else /* !AFS_DEMAND_ATTACH_FS */
423 Lock_Init(&vnp->lock);
424 #endif /* !AFS_DEMAND_ATTACH_FS */
425 vnp->changed_oldTime = 0;
426 vnp->changed_newTime = 0;
427 Vn_volume(vnp) = NULL;
428 Vn_cacheCheck(vnp) = 0;
429 vnp->delete = Vn_id(vnp) = 0;
430 #ifdef AFS_PTHREAD_ENV
431 vnp->writer = (pthread_t) 0;
432 #else /* AFS_PTHREAD_ENV */
433 vnp->writer = (PROCESS) 0;
434 #endif /* AFS_PTHREAD_ENV */
438 if (vcp->lruHead == NULL)
439 vcp->lruHead = vnp->lruNext = vnp->lruPrev = vnp;
441 vnp->lruNext = vcp->lruHead;
442 vnp->lruPrev = vcp->lruHead->lruPrev;
443 vcp->lruHead->lruPrev = vnp;
444 vnp->lruPrev->lruNext = vnp;
447 va += vcp->residentSize;
454 * allocate an unused vnode from the lru chain.
456 * @param[in] vcp vnode class info object pointer
458 * @pre VOL_LOCK is held
460 * @post vnode object is removed from lru, and vnode hash table.
461 * vnode is disassociated from volume object.
462 * state is set to VN_STATE_INVALID.
463 * inode handle is released.
465 * @note we traverse backwards along the lru circlist. It shouldn't
466 * be necessary to specify that nUsers == 0 since if it is in the list,
467 * nUsers should be 0. Things shouldn't be in lruq unless no one is
470 * @warning DAFS: VOL_LOCK is dropped while doing inode handle release
472 * @return vnode object pointer
475 VGetFreeVnode_r(struct VnodeClassInfo * vcp)
479 vnp = vcp->lruHead->lruPrev;
480 #ifdef AFS_DEMAND_ATTACH_FS
481 if (Vn_refcount(vnp) != 0 || VnIsExclusiveState(Vn_state(vnp)) ||
482 Vn_readers(vnp) != 0)
483 Abort("VGetFreeVnode_r: in-use vnode in lruq");
485 if (Vn_refcount(vnp) != 0 || CheckLock(&vnp->lock))
486 Abort("VGetFreeVnode_r: locked vnode in lruq");
488 VNLog(1, 2, Vn_id(vnp), (intptr_t)vnp, 0, 0);
491 * it's going to be overwritten soon enough.
492 * remove from LRU, delete hash entry, and
493 * disassociate from old parent volume before
494 * we have a chance to drop the vol glock
496 DeleteFromVnLRU(vcp, vnp);
497 DeleteFromVnHash(vnp);
498 if (Vn_volume(vnp)) {
499 DeleteFromVVnList(vnp);
502 /* drop the file descriptor */
504 #ifdef AFS_DEMAND_ATTACH_FS
505 VnChangeState_r(vnp, VN_STATE_RELEASING);
508 /* release is, potentially, a highly latent operation due to a couple
510 * - ihandle package lock contention
511 * - closing file descriptor(s) associated with ih
513 * Hance, we perform outside of the volume package lock in order to
514 * reduce the probability of contention.
516 IH_RELEASE(vnp->handle);
517 #ifdef AFS_DEMAND_ATTACH_FS
522 #ifdef AFS_DEMAND_ATTACH_FS
523 VnChangeState_r(vnp, VN_STATE_INVALID);
531 * lookup a vnode in the vnode cache hash table.
533 * @param[in] vp pointer to volume object
534 * @param[in] vnodeId vnode id
538 * @post matching vnode object or NULL is returned
540 * @return vnode object pointer
541 * @retval NULL no matching vnode object was found in the cache
543 * @internal vnode package internal use only
545 * @note this symbol is exported strictly for fssync debug protocol use
548 VLookupVnode(Volume * vp, VnodeId vnodeId)
551 unsigned int newHash;
553 newHash = VNODE_HASH(vp, vnodeId);
554 for (vnp = VnodeHashTable[newHash];
556 ((Vn_id(vnp) != vnodeId) ||
557 (Vn_volume(vnp) != vp) ||
558 (vp->cacheCheck != Vn_cacheCheck(vnp))));
559 vnp = vnp->hashNext);
566 VAllocVnode(Error * ec, Volume * vp, VnodeType type)
570 retVal = VAllocVnode_r(ec, vp, type);
576 * allocate a new vnode.
578 * @param[out] ec error code return
579 * @param[in] vp volume object pointer
580 * @param[in] type desired vnode type
582 * @return vnode object pointer
584 * @pre VOL_LOCK held;
585 * heavyweight ref held on vp
587 * @post vnode allocated and returned
590 VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
595 register struct VnodeClassInfo *vcp;
598 #ifdef AFS_DEMAND_ATTACH_FS
599 VolState vol_state_save;
604 #ifdef AFS_DEMAND_ATTACH_FS
606 * once a volume has entered an error state, don't permit
607 * further operations to proceed
608 * -- tkeiser 11/21/2007
610 VWaitExclusiveState_r(vp);
611 if (VIsErrorState(V_attachState(vp))) {
612 /* XXX is VSALVAGING acceptable here? */
618 if (programType == fileServer && !V_inUse(vp)) {
619 if (vp->specialStatus) {
620 *ec = vp->specialStatus;
626 class = vnodeTypeToClass(type);
627 vcp = &VnodeClassInfo[class];
629 if (!VolumeWriteable(vp)) {
630 *ec = (bit32) VREADONLY;
634 unique = vp->nextVnodeUnique++;
636 unique = vp->nextVnodeUnique++;
638 if (vp->nextVnodeUnique > V_uniquifier(vp)) {
639 VUpdateVolume_r(ec, vp, 0);
644 if (programType == fileServer) {
645 VAddToVolumeUpdateList_r(ec, vp);
650 /* Find a slot in the bit map */
651 bitNumber = VAllocBitmapEntry_r(ec, vp, &vp->vnodeIndex[class],
652 VOL_ALLOC_BITMAP_WAIT);
655 vnodeNumber = bitNumberToVnodeNumber(bitNumber, class);
659 * at this point we should be assured that V_attachState(vp) is non-exclusive
663 VNLog(2, 1, vnodeNumber, 0, 0, 0);
664 /* Prepare to move it to the new hash chain */
665 vnp = VLookupVnode(vp, vnodeNumber);
667 /* slot already exists. May even not be in lruq (consider store file locking a file being deleted)
668 * so we may have to wait for it below */
669 VNLog(3, 2, vnodeNumber, (intptr_t)vnp, 0, 0);
671 VnCreateReservation_r(vnp);
672 if (Vn_refcount(vnp) == 1) {
673 /* we're the only user */
674 /* This won't block */
675 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
677 /* other users present; follow locking hierarchy */
678 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, MIGHT_DEADLOCK);
680 #ifdef AFS_DEMAND_ATTACH_FS
683 * vnode was cached, wait for any existing exclusive ops to finish.
684 * once we have reacquired the lock, re-verify volume state.
686 * note: any vnode error state is related to the old vnode; disregard.
688 VnWaitQuiescent_r(vnp);
689 if (VIsErrorState(V_attachState(vp))) {
690 VnUnlock(vnp, WRITE_LOCK);
691 VnCancelReservation_r(vnp);
698 * verify state of the world hasn't changed
700 * (technically, this should never happen because cachecheck
701 * is only updated during a volume attach, which should not
702 * happen when refs are held)
704 if (Vn_volume(vnp)->cacheCheck != Vn_cacheCheck(vnp)) {
705 VnUnlock(vnp, WRITE_LOCK);
706 VnCancelReservation_r(vnp);
712 /* no such vnode in the cache */
714 vnp = VGetFreeVnode_r(vcp);
716 /* Initialize the header fields so noone allocates another
717 * vnode with the same number */
718 Vn_id(vnp) = vnodeNumber;
719 VnCreateReservation_r(vnp);
720 AddToVVnList(vp, vnp);
721 #ifdef AFS_DEMAND_ATTACH_FS
725 /* This will never block (guaranteed by check in VGetFreeVnode_r() */
726 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
728 #ifdef AFS_DEMAND_ATTACH_FS
729 VnChangeState_r(vnp, VN_STATE_ALLOC);
732 /* Sanity check: is this vnode really not in use? */
735 IHandle_t *ihP = vp->vnodeIndex[class].handle;
737 afs_foff_t off = vnodeIndexOffset(vcp, vnodeNumber);
740 /* XXX we have a potential race here if two threads
741 * allocate new vnodes at the same time, and they
742 * both decide it's time to extend the index
745 #ifdef AFS_DEMAND_ATTACH_FS
747 * this race has been eliminated for the DAFS case
748 * using exclusive state VOL_STATE_VNODE_ALLOC
750 * if this becomes a bottleneck, there are ways to
751 * improve parallelism for this code path
752 * -- tkeiser 11/28/2007
754 VCreateReservation_r(vp);
755 VWaitExclusiveState_r(vp);
756 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_ALLOC);
762 Log("VAllocVnode: can't open index file!\n");
764 goto error_encountered;
766 if ((size = FDH_SIZE(fdP)) < 0) {
767 Log("VAllocVnode: can't stat index file!\n");
769 goto error_encountered;
771 if (FDH_SEEK(fdP, off, SEEK_SET) < 0) {
772 Log("VAllocVnode: can't seek on index file!\n");
774 goto error_encountered;
776 if (off + vcp->diskSize <= size) {
777 if (FDH_READ(fdP, &vnp->disk, vcp->diskSize) != vcp->diskSize) {
778 Log("VAllocVnode: can't read index file!\n");
780 goto error_encountered;
782 if (vnp->disk.type != vNull) {
783 Log("VAllocVnode: addled bitmap or index!\n");
785 goto error_encountered;
788 /* growing file - grow in a reasonable increment */
789 char *buf = (char *)malloc(16 * 1024);
792 goto error_encountered;
794 memset(buf, 0, 16 * 1024);
795 if ((FDH_WRITE(fdP, buf, 16 * 1024)) != 16 * 1024) {
796 Log("VAllocVnode: can't grow vnode index: out of memory\n");
799 goto error_encountered;
806 #ifdef AFS_DEMAND_ATTACH_FS
807 VChangeState_r(vp, vol_state_save);
808 VCancelReservation_r(vp);
815 * close the file handle
817 * invalidate the vnode
818 * free up the bitmap entry (although salvager should take care of it)
820 * drop vnode lock and refs
825 VFreeBitMapEntry_r(&tmp, &vp->vnodeIndex[class], bitNumber);
826 VInvalidateVnode_r(vnp);
827 VnUnlock(vnp, WRITE_LOCK);
828 VnCancelReservation_r(vnp);
829 #ifdef AFS_DEMAND_ATTACH_FS
830 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
831 VCancelReservation_r(vp);
833 VForceOffline_r(vp, 0);
838 VNLog(4, 2, vnodeNumber, (intptr_t)vnp, 0, 0);
839 #ifndef AFS_DEMAND_ATTACH_FS
844 VNLog(5, 1, (intptr_t)vnp, 0, 0, 0);
845 memset(&vnp->disk, 0, sizeof(vnp->disk));
846 vnp->changed_newTime = 0; /* set this bit when vnode is updated */
847 vnp->changed_oldTime = 0; /* set this on CopyOnWrite. */
849 vnp->disk.vnodeMagic = vcp->magic;
850 vnp->disk.type = type;
851 vnp->disk.uniquifier = unique;
854 vp->header->diskstuff.filecount++;
855 #ifdef AFS_DEMAND_ATTACH_FS
856 VnChangeState_r(vnp, VN_STATE_EXCLUSIVE);
862 * load a vnode from disk.
864 * @param[out] ec client error code return
865 * @param[in] vp volume object pointer
866 * @param[in] vnp vnode object pointer
867 * @param[in] vcp vnode class info object pointer
868 * @param[in] class vnode class enumeration
870 * @pre vnode is registered in appropriate data structures;
871 * caller holds a ref on vnode; VOL_LOCK is held
873 * @post vnode data is loaded from disk.
874 * vnode state is set to VN_STATE_ONLINE.
875 * on failure, vnode is invalidated.
877 * @internal vnode package internal use only
880 VnLoad(Error * ec, Volume * vp, Vnode * vnp,
881 struct VnodeClassInfo * vcp, VnodeClass class)
883 /* vnode not cached */
887 IHandle_t *ihP = vp->vnodeIndex[class].handle;
893 #ifdef AFS_DEMAND_ATTACH_FS
894 VnChangeState_r(vnp, VN_STATE_LOAD);
897 /* This will never block */
898 VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
903 Log("VnLoad: can't open index dev=%u, i=%s\n", vp->device,
904 PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
906 goto error_encountered_nolock;
907 } else if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, Vn_id(vnp)), SEEK_SET)
909 Log("VnLoad: can't seek on index file vn=%u\n", Vn_id(vnp));
911 goto error_encountered_nolock;
912 } else if ((nBytes = FDH_READ(fdP, (char *)&vnp->disk, vcp->diskSize))
914 /* Don't take volume off line if the inumber is out of range
915 * or the inode table is full. */
916 if (nBytes == BAD_IGET) {
917 Log("VnLoad: bad inumber %s\n",
918 PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
921 } else if (nBytes == -1 && errno == EIO) {
922 /* disk error; salvage */
923 Log("VnLoad: Couldn't read vnode %u, volume %u (%s); volume needs salvage\n", Vn_id(vnp), V_id(vp), V_name(vp));
925 /* vnode is not allocated */
927 Log("VnLoad: Couldn't read vnode %u, volume %u (%s); read %d bytes, errno %d\n",
928 Vn_id(vnp), V_id(vp), V_name(vp), (int)nBytes, errno);
932 goto error_encountered_nolock;
937 /* Quick check to see that the data is reasonable */
938 if (vnp->disk.vnodeMagic != vcp->magic || vnp->disk.type == vNull) {
939 if (vnp->disk.type == vNull) {
943 struct vnodeIndex *index = &vp->vnodeIndex[class];
944 unsigned int bitNumber = vnodeIdToBitNumber(Vn_id(vnp));
945 unsigned int offset = bitNumber >> 3;
947 /* Test to see if vnode number is valid. */
948 if ((offset >= index->bitmapSize)
949 || ((*(index->bitmap + offset) & (1 << (bitNumber & 0x7)))
951 Log("VnLoad: Request for unallocated vnode %u, volume %u (%s) denied.\n", Vn_id(vnp), V_id(vp), V_name(vp));
955 Log("VnLoad: Bad magic number, vnode %u, volume %u (%s); volume needs salvage\n", Vn_id(vnp), V_id(vp), V_name(vp));
958 goto error_encountered;
961 IH_INIT(vnp->handle, V_device(vp), V_parentId(vp), VN_GET_INO(vnp));
962 VnUnlock(vnp, WRITE_LOCK);
963 #ifdef AFS_DEMAND_ATTACH_FS
964 VnChangeState_r(vnp, VN_STATE_ONLINE);
969 error_encountered_nolock:
971 FDH_REALLYCLOSE(fdP);
977 #ifdef AFS_DEMAND_ATTACH_FS
978 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0);
980 VForceOffline_r(vp, 0);
987 VInvalidateVnode_r(vnp);
988 VnUnlock(vnp, WRITE_LOCK);
992 * store a vnode to disk.
994 * @param[out] ec error code output
995 * @param[in] vp volume object pointer
996 * @param[in] vnp vnode object pointer
997 * @param[in] vcp vnode class info object pointer
998 * @param[in] class vnode class enumeration
1000 * @pre VOL_LOCK held.
1001 * caller holds refs to volume and vnode.
1002 * DAFS: caller is responsible for performing state sanity checks.
1004 * @post vnode state is stored to disk.
1006 * @internal vnode package internal use only
1009 VnStore(Error * ec, Volume * vp, Vnode * vnp,
1010 struct VnodeClassInfo * vcp, VnodeClass class)
1014 IHandle_t *ihP = vp->vnodeIndex[class].handle;
1016 #ifdef AFS_DEMAND_ATTACH_FS
1017 VnState vn_state_save;
1022 #ifdef AFS_DEMAND_ATTACH_FS
1023 vn_state_save = VnChangeState_r(vnp, VN_STATE_STORE);
1026 offset = vnodeIndexOffset(vcp, Vn_id(vnp));
1030 Log("VnStore: can't open index file!\n");
1031 goto error_encountered;
1033 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
1034 Log("VnStore: can't seek on index file! fdp=0x%x offset=%d, errno=%d\n",
1035 fdP, offset, errno);
1036 goto error_encountered;
1039 nBytes = FDH_WRITE(fdP, &vnp->disk, vcp->diskSize);
1040 if (nBytes != vcp->diskSize) {
1041 /* Don't force volume offline if the inumber is out of
1042 * range or the inode table is full.
1044 FDH_REALLYCLOSE(fdP);
1045 if (nBytes == BAD_IGET) {
1046 Log("VnStore: bad inumber %s\n",
1048 vp->vnodeIndex[class].handle->ih_ino));
1051 #ifdef AFS_DEMAND_ATTACH_FS
1052 VnChangeState_r(vnp, VN_STATE_ERROR);
1055 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)), (int)nBytes);
1056 #ifdef AFS_DEMAND_ATTACH_FS
1057 goto error_encountered;
1060 VForceOffline_r(vp, 0);
1070 #ifdef AFS_DEMAND_ATTACH_FS
1071 VnChangeState_r(vnp, vn_state_save);
1076 #ifdef AFS_DEMAND_ATTACH_FS
1077 /* XXX instead of dumping core, let's try to request a salvage
1078 * and just fail the putvnode */
1082 VnChangeState_r(vnp, VN_STATE_ERROR);
1083 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1090 * get a handle to a vnode object.
1092 * @param[out] ec error code
1093 * @param[in] vp volume object
1094 * @param[in] vnodeNumber vnode id
1095 * @param[in] locktype type of lock to acquire
1097 * @return vnode object pointer
1102 VGetVnode(Error * ec, Volume * vp, VnodeId vnodeNumber, int locktype)
1103 { /* READ_LOCK or WRITE_LOCK, as defined in lock.h */
1106 retVal = VGetVnode_r(ec, vp, vnodeNumber, locktype);
1112 * get a handle to a vnode object.
1114 * @param[out] ec error code
1115 * @param[in] vp volume object
1116 * @param[in] vnodeNumber vnode id
1117 * @param[in] locktype type of lock to acquire
1119 * @return vnode object pointer
1121 * @internal vnode package internal use only
1123 * @pre VOL_LOCK held.
1124 * heavyweight ref held on volume object.
1127 VGetVnode_r(Error * ec, Volume * vp, VnodeId vnodeNumber, int locktype)
1128 { /* READ_LOCK or WRITE_LOCK, as defined in lock.h */
1129 register Vnode *vnp;
1131 struct VnodeClassInfo *vcp;
1135 if (vnodeNumber == 0) {
1140 VNLog(100, 1, vnodeNumber, 0, 0, 0);
1142 #ifdef AFS_DEMAND_ATTACH_FS
1144 * once a volume has entered an error state, don't permit
1145 * further operations to proceed
1146 * -- tkeiser 11/21/2007
1148 VWaitExclusiveState_r(vp);
1149 if (VIsErrorState(V_attachState(vp))) {
1150 /* XXX is VSALVAGING acceptable here? */
1156 if (programType == fileServer && !V_inUse(vp)) {
1157 *ec = (vp->specialStatus ? vp->specialStatus : VOFFLINE);
1159 /* If the volume is VBUSY (being cloned or dumped) and this is
1160 * a READ operation, then don't fail.
1162 if ((*ec != VBUSY) || (locktype != READ_LOCK)) {
1167 class = vnodeIdToClass(vnodeNumber);
1168 vcp = &VnodeClassInfo[class];
1169 if (locktype == WRITE_LOCK && !VolumeWriteable(vp)) {
1170 *ec = (bit32) VREADONLY;
1174 if (locktype == WRITE_LOCK && programType == fileServer) {
1175 VAddToVolumeUpdateList_r(ec, vp);
1183 /* See whether the vnode is in the cache. */
1184 vnp = VLookupVnode(vp, vnodeNumber);
1186 /* vnode is in cache */
1188 VNLog(101, 2, vnodeNumber, (intptr_t)vnp, 0, 0);
1189 VnCreateReservation_r(vnp);
1191 #ifdef AFS_DEMAND_ATTACH_FS
1193 * this is the one DAFS case where we may run into contention.
1194 * here's the basic control flow:
1196 * if locktype is READ_LOCK:
1197 * wait until vnode is not exclusive
1198 * set to VN_STATE_READ
1199 * increment read count
1202 * wait until vnode is quiescent
1203 * set to VN_STATE_EXCLUSIVE
1206 if (locktype == READ_LOCK) {
1207 VnWaitExclusiveState_r(vnp);
1209 VnWaitQuiescent_r(vnp);
1212 if (VnIsErrorState(Vn_state(vnp))) {
1213 VnCancelReservation_r(vnp);
1217 #endif /* AFS_DEMAND_ATTACH_FS */
1219 /* vnode not cached */
1221 /* Not in cache; tentatively grab most distantly used one from the LRU
1224 vnp = VGetFreeVnode_r(vcp);
1227 vnp->changed_newTime = vnp->changed_oldTime = 0;
1229 Vn_id(vnp) = vnodeNumber;
1230 VnCreateReservation_r(vnp);
1231 AddToVVnList(vp, vnp);
1232 #ifdef AFS_DEMAND_ATTACH_FS
1237 * XXX for non-DAFS, there is a serious
1238 * race condition here:
1240 * two threads can race to load a vnode. the net
1241 * result is two struct Vnodes can be allocated
1242 * and hashed, which point to the same underlying
1243 * disk data store. conflicting vnode locks can
1244 * thus be held concurrently.
1246 * for non-DAFS to be safe, VOL_LOCK really shouldn't
1247 * be dropped in VnLoad. Of course, this would likely
1248 * lead to an unacceptable slow-down.
1251 VnLoad(ec, vp, vnp, vcp, class);
1253 VnCancelReservation_r(vnp);
1256 #ifndef AFS_DEMAND_ATTACH_FS
1261 * there is no possibility for contention. we "own" this vnode.
1267 * it is imperative that nothing drop vol lock between here
1268 * and the VnBeginRead/VnChangeState stanza below
1271 VnLock(vnp, locktype, VOL_LOCK_HELD, MIGHT_DEADLOCK);
1273 /* Check that the vnode hasn't been removed while we were obtaining
1275 VNLog(102, 2, vnodeNumber, (intptr_t) vnp, 0, 0);
1276 if ((vnp->disk.type == vNull) || (Vn_cacheCheck(vnp) == 0)) {
1277 VnUnlock(vnp, locktype);
1278 VnCancelReservation_r(vnp);
1280 /* vnode is labelled correctly by now, so we don't have to invalidate it */
1284 #ifdef AFS_DEMAND_ATTACH_FS
1285 if (locktype == READ_LOCK) {
1288 VnChangeState_r(vnp, VN_STATE_EXCLUSIVE);
1292 if (programType == fileServer)
1293 VBumpVolumeUsage_r(Vn_volume(vnp)); /* Hack; don't know where it should be
1294 * called from. Maybe VGetVolume */
1299 int TrustVnodeCacheEntry = 1;
1300 /* This variable is bogus--when it's set to 0, the hash chains fill
1301 up with multiple versions of the same vnode. Should fix this!! */
1303 VPutVnode(Error * ec, register Vnode * vnp)
1306 VPutVnode_r(ec, vnp);
1311 * put back a handle to a vnode object.
1313 * @param[out] ec client error code
1314 * @param[in] vnp vnode object pointer
1316 * @pre VOL_LOCK held.
1317 * ref held on vnode.
1319 * @post ref dropped on vnode.
1320 * if vnode was modified or deleted, it is written out to disk
1321 * (assuming a write lock was held).
1323 * @internal volume package internal use only
1326 VPutVnode_r(Error * ec, register Vnode * vnp)
1330 struct VnodeClassInfo *vcp;
1333 assert(Vn_refcount(vnp) != 0);
1334 class = vnodeIdToClass(Vn_id(vnp));
1335 vcp = &VnodeClassInfo[class];
1336 assert(vnp->disk.vnodeMagic == vcp->magic);
1337 VNLog(200, 2, Vn_id(vnp), (intptr_t) vnp, 0, 0);
1339 #ifdef AFS_DEMAND_ATTACH_FS
1340 writeLocked = (Vn_state(vnp) == VN_STATE_EXCLUSIVE);
1342 writeLocked = WriteLocked(&vnp->lock);
1347 #ifdef AFS_PTHREAD_ENV
1348 pthread_t thisProcess = pthread_self();
1349 #else /* AFS_PTHREAD_ENV */
1350 PROCESS thisProcess;
1351 LWP_CurrentProcess(&thisProcess);
1352 #endif /* AFS_PTHREAD_ENV */
1353 VNLog(201, 2, (intptr_t) vnp,
1354 ((vnp->changed_newTime) << 1) | ((vnp->
1355 changed_oldTime) << 1) | vnp->
1357 if (thisProcess != vnp->writer)
1358 Abort("VPutVnode: Vnode at 0x%x locked by another process!\n",
1362 if (vnp->changed_oldTime || vnp->changed_newTime || vnp->delete) {
1363 Volume *vp = Vn_volume(vnp);
1364 afs_uint32 now = FT_ApproxTime();
1365 assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
1368 /* No longer any directory entries for this vnode. Free the Vnode */
1369 memset(&vnp->disk, 0, sizeof(vnp->disk));
1370 /* delete flag turned off further down */
1371 VNLog(202, 2, Vn_id(vnp), (intptr_t) vnp, 0, 0);
1372 } else if (vnp->changed_newTime) {
1373 vnp->disk.serverModifyTime = now;
1375 if (vnp->changed_newTime)
1377 V_updateDate(vp) = vp->updateTime = now;
1378 if(V_volUpCounter(vp)<MAXINT)
1379 V_volUpCounter(vp)++;
1382 /* The vnode has been changed. Write it out to disk */
1384 #ifdef AFS_DEMAND_ATTACH_FS
1385 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1387 assert(V_needsSalvaged(vp));
1391 VnStore(ec, vp, vnp, vcp, class);
1393 /* If the vnode is to be deleted, and we wrote the vnode out,
1394 * free its bitmap entry. Do after the vnode is written so we
1395 * don't allocate from bitmap before the vnode is written
1396 * (doing so could cause a "addled bitmap" message).
1398 if (vnp->delete && !*ec) {
1399 if (Vn_volume(vnp)->header->diskstuff.filecount-- < 1)
1400 Vn_volume(vnp)->header->diskstuff.filecount = 0;
1401 VFreeBitMapEntry_r(ec, &vp->vnodeIndex[class],
1402 vnodeIdToBitNumber(Vn_id(vnp)));
1406 vnp->changed_newTime = vnp->changed_oldTime = 0;
1408 #ifdef AFS_DEMAND_ATTACH_FS
1409 VnChangeState_r(vnp, VN_STATE_ONLINE);
1411 } else { /* Not write locked */
1412 if (vnp->changed_newTime || vnp->changed_oldTime || vnp->delete)
1414 ("VPutVnode: Change or delete flag for vnode 0x%x is set but vnode is not write locked!\n",
1416 #ifdef AFS_DEMAND_ATTACH_FS
1421 /* Do not look at disk portion of vnode after this point; it may
1422 * have been deleted above */
1424 VnUnlock(vnp, ((writeLocked) ? WRITE_LOCK : READ_LOCK));
1425 VnCancelReservation_r(vnp);
1429 * Make an attempt to convert a vnode lock from write to read.
1430 * Do nothing if the vnode isn't write locked or the vnode has
1434 VVnodeWriteToRead(Error * ec, register Vnode * vnp)
1438 retVal = VVnodeWriteToRead_r(ec, vnp);
1444 * convert vnode handle from mutually exclusive to shared access.
1446 * @param[out] ec client error code
1447 * @param[in] vnp vnode object pointer
1449 * @return unspecified use (see out argument 'ec' for error code return)
1451 * @pre VOL_LOCK held.
1452 * ref held on vnode.
1453 * write lock held on vnode.
1455 * @post read lock held on vnode.
1456 * if vnode was modified, it has been written to disk.
1458 * @internal volume package internal use only
1461 VVnodeWriteToRead_r(Error * ec, register Vnode * vnp)
1465 struct VnodeClassInfo *vcp;
1466 #ifdef AFS_PTHREAD_ENV
1467 pthread_t thisProcess;
1468 #else /* AFS_PTHREAD_ENV */
1469 PROCESS thisProcess;
1470 #endif /* AFS_PTHREAD_ENV */
1473 assert(Vn_refcount(vnp) != 0);
1474 class = vnodeIdToClass(Vn_id(vnp));
1475 vcp = &VnodeClassInfo[class];
1476 assert(vnp->disk.vnodeMagic == vcp->magic);
1477 VNLog(300, 2, Vn_id(vnp), (intptr_t) vnp, 0, 0);
1479 #ifdef AFS_DEMAND_ATTACH_FS
1480 writeLocked = (Vn_state(vnp) == VN_STATE_EXCLUSIVE);
1482 writeLocked = WriteLocked(&vnp->lock);
1489 VNLog(301, 2, (intptr_t) vnp,
1490 ((vnp->changed_newTime) << 1) | ((vnp->
1491 changed_oldTime) << 1) | vnp->
1495 #ifdef AFS_PTHREAD_ENV
1496 thisProcess = pthread_self();
1497 #else /* AFS_PTHREAD_ENV */
1498 LWP_CurrentProcess(&thisProcess);
1499 #endif /* AFS_PTHREAD_ENV */
1500 if (thisProcess != vnp->writer)
1501 Abort("VPutVnode: Vnode at 0x%x locked by another process!\n",
1507 if (vnp->changed_oldTime || vnp->changed_newTime) {
1508 Volume *vp = Vn_volume(vnp);
1509 afs_uint32 now = FT_ApproxTime();
1510 assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
1511 if (vnp->changed_newTime)
1512 vnp->disk.serverModifyTime = now;
1513 if (vnp->changed_newTime)
1514 V_updateDate(vp) = vp->updateTime = now;
1516 /* The inode has been changed. Write it out to disk */
1518 #ifdef AFS_DEMAND_ATTACH_FS
1519 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
1521 assert(V_needsSalvaged(vp));
1525 VnStore(ec, vp, vnp, vcp, class);
1528 vnp->changed_newTime = vnp->changed_oldTime = 0;
1532 #ifdef AFS_DEMAND_ATTACH_FS
1533 VnChangeState_r(vnp, VN_STATE_ONLINE);
1536 ConvertWriteToReadLock(&vnp->lock);
1542 * initial size of ihandle pointer vector.
1544 * @see VInvalidateVnodesByVolume_r
1546 #define IH_VEC_BASE_SIZE 256
1549 * increment amount for growing ihandle pointer vector.
1551 * @see VInvalidateVnodesByVolume_r
1553 #define IH_VEC_INCREMENT 256
1556 * Compile list of ihandles to be released/reallyclosed at a later time.
1558 * @param[in] vp volume object pointer
1559 * @param[out] vec_out vector of ihandle pointers to be released/reallyclosed
1560 * @param[out] vec_len_out number of valid elements in ihandle vector
1562 * @pre - VOL_LOCK is held
1563 * - volume is in appropriate exclusive state (e.g. VOL_STATE_VNODE_CLOSE,
1564 * VOL_STATE_VNODE_RELEASE)
1566 * @post - all vnodes on VVn list are invalidated
1567 * - ih_vec is populated with all valid ihandles
1569 * @return operation status
1571 * @retval ENOMEM out of memory
1573 * @todo we should handle out of memory conditions more gracefully.
1575 * @internal vnode package internal use only
1578 VInvalidateVnodesByVolume_r(Volume * vp,
1579 IHandle_t *** vec_out,
1580 size_t * vec_len_out)
1584 size_t i = 0, vec_len;
1585 IHandle_t **ih_vec, **ih_vec_new;
1587 #ifdef AFS_DEMAND_ATTACH_FS
1589 #endif /* AFS_DEMAND_ATTACH_FS */
1591 vec_len = IH_VEC_BASE_SIZE;
1592 ih_vec = malloc(sizeof(IHandle_t *) * vec_len);
1593 #ifdef AFS_DEMAND_ATTACH_FS
1600 * Traverse the volume's vnode list. Pull all the ihandles out into a
1601 * thread-private array for later asynchronous processing.
1603 #ifdef AFS_DEMAND_ATTACH_FS
1606 for (queue_Scan(&vp->vnode_list, vnp, nvnp, Vnode)) {
1607 if (vnp->handle != NULL) {
1609 #ifdef AFS_DEMAND_ATTACH_FS
1612 vec_len += IH_VEC_INCREMENT;
1613 ih_vec_new = realloc(ih_vec, sizeof(IHandle_t *) * vec_len);
1614 #ifdef AFS_DEMAND_ATTACH_FS
1617 if (ih_vec_new == NULL) {
1621 ih_vec = ih_vec_new;
1622 #ifdef AFS_DEMAND_ATTACH_FS
1624 * Theoretically, the volume's VVn list should not change
1625 * because the volume is in an exclusive state. For the
1626 * sake of safety, we will restart the traversal from the
1627 * the beginning (which is not expensive because we're
1628 * deleting the items from the list as we go).
1630 goto restart_traversal;
1633 ih_vec[i++] = vnp->handle;
1636 DeleteFromVVnList(vnp);
1637 VInvalidateVnode_r(vnp);
1647 /* VCloseVnodeFiles - called when a volume is going off line. All open
1648 * files for vnodes in that volume are closed. This might be excessive,
1649 * since we may only be taking one volume of a volume group offline.
1652 VCloseVnodeFiles_r(Volume * vp)
1654 #ifdef AFS_DEMAND_ATTACH_FS
1655 VolState vol_state_save;
1657 IHandle_t ** ih_vec;
1660 #ifdef AFS_DEMAND_ATTACH_FS
1661 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_CLOSE);
1662 #endif /* AFS_DEMAND_ATTACH_FS */
1664 /* XXX need better error handling here */
1665 assert(VInvalidateVnodesByVolume_r(vp,
1671 * now we drop VOL_LOCK while we perform some potentially very
1672 * expensive operations in the background
1674 #ifdef AFS_DEMAND_ATTACH_FS
1678 for (i = 0; i < vec_len; i++) {
1679 IH_REALLYCLOSE(ih_vec[i]);
1684 #ifdef AFS_DEMAND_ATTACH_FS
1686 VChangeState_r(vp, vol_state_save);
1687 #endif /* AFS_DEMAND_ATTACH_FS */
1692 * shut down all vnode cache state for a given volume.
1694 * @param[in] vp volume object pointer
1696 * @pre VOL_LOCK is held
1698 * @post all file descriptors closed.
1699 * all inode handles released.
1700 * all vnode cache objects disassociated from volume.
1702 * @note for DAFS, these operations are performed outside the vol glock under
1703 * volume exclusive state VOL_STATE_VNODE_RELEASE. Please further note
1704 * that it would be a bug to acquire and release a volume reservation
1705 * during this exclusive operation. This is due to the fact that we are
1706 * generally called during the refcount 1->0 transition.
1708 * @todo we should handle failures in VInvalidateVnodesByVolume_r more
1711 * @see VInvalidateVnodesByVolume_r
1713 * @internal this routine is internal to the volume package
1716 VReleaseVnodeFiles_r(Volume * vp)
1718 #ifdef AFS_DEMAND_ATTACH_FS
1719 VolState vol_state_save;
1721 IHandle_t ** ih_vec;
1724 #ifdef AFS_DEMAND_ATTACH_FS
1725 vol_state_save = VChangeState_r(vp, VOL_STATE_VNODE_RELEASE);
1726 #endif /* AFS_DEMAND_ATTACH_FS */
1728 /* XXX need better error handling here */
1729 assert(VInvalidateVnodesByVolume_r(vp,
1735 * now we drop VOL_LOCK while we perform some potentially very
1736 * expensive operations in the background
1738 #ifdef AFS_DEMAND_ATTACH_FS
1742 for (i = 0; i < vec_len; i++) {
1743 IH_RELEASE(ih_vec[i]);
1748 #ifdef AFS_DEMAND_ATTACH_FS
1750 VChangeState_r(vp, vol_state_save);
1751 #endif /* AFS_DEMAND_ATTACH_FS */