DAFS: VnLock after VnWaitQuiescent in VAllocVnode
[openafs.git] / src / vol / vnode.c
index 45cfa85..532fe83 100644 (file)
@@ -17,6 +17,9 @@
  */
 #include <afsconfig.h>
 #include <afs/param.h>
+
+#include <roken.h>
+
 #define MAXINT     (~(1<<((sizeof(int)*8)-1)))
 
 
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
-#ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
+#include <afs/afs_assert.h>
 
 #include <rx/xdr.h>
 #include "rx/rx_queue.h"
@@ -381,7 +380,7 @@ VInitVnodes(VnodeClass class, int nVnodes)
     vcp->cacheSize = nVnodes;
     switch (class) {
     case vSmall:
-       assert(CHECKSIZE_SMALLVNODE);
+       osi_Assert(CHECKSIZE_SMALLVNODE);
        vcp->lruHead = NULL;
        vcp->residentSize = SIZEOF_SMALLVNODE;
        vcp->diskSize = SIZEOF_SMALLDISKVNODE;
@@ -406,13 +405,13 @@ VInitVnodes(VnodeClass class, int nVnodes)
        return 0;
 
     va = (byte *) calloc(nVnodes, vcp->residentSize);
-    assert(va != NULL);
+    osi_Assert(va != NULL);
     while (nVnodes--) {
        Vnode *vnp = (Vnode *) va;
        Vn_refcount(vnp) = 0;   /* no context switches */
        Vn_stateFlags(vnp) |= VN_ON_LRU;
 #ifdef AFS_DEMAND_ATTACH_FS
-       assert(pthread_cond_init(&Vn_stateCV(vnp), NULL) == 0);
+       CV_INIT(&Vn_stateCV(vnp), "vnode state", CV_DEFAULT, 0);
        Vn_state(vnp) = VN_STATE_INVALID;
        Vn_readers(vnp) = 0;
 #else /* !AFS_DEMAND_ATTACH_FS */
@@ -670,9 +669,6 @@ VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
            /* This won't block */
            VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, WILL_NOT_DEADLOCK);
        } else {
-           /* other users present; follow locking hierarchy */
-           VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, MIGHT_DEADLOCK);
-
 #ifdef AFS_DEMAND_ATTACH_FS
            /*
             * DAFS:
@@ -690,6 +686,9 @@ VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
            }
 #endif
 
+           /* other users present; follow locking hierarchy */
+           VnLock(vnp, WRITE_LOCK, VOL_LOCK_HELD, MIGHT_DEADLOCK);
+
            /*
             * verify state of the world hasn't changed
             *
@@ -764,13 +763,8 @@ VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
                *ec = EIO;
                goto error_encountered;
            }
-           if (FDH_SEEK(fdP, off, SEEK_SET) < 0) {
-               Log("VAllocVnode: can't seek on index file!\n");
-               *ec = EIO;
-               goto error_encountered;
-           }
            if (off + vcp->diskSize <= size) {
-               if (FDH_READ(fdP, &vnp->disk, vcp->diskSize) != vcp->diskSize) {
+             if (FDH_PREAD(fdP, &vnp->disk, vcp->diskSize, off) != vcp->diskSize) {
                    Log("VAllocVnode: can't read index file!\n");
                    *ec = EIO;
                    goto error_encountered;
@@ -789,7 +783,7 @@ VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
                    goto error_encountered;
                }
                memset(buf, 0, 16 * 1024);
-               if ((FDH_WRITE(fdP, buf, 16 * 1024)) != 16 * 1024) {
+               if ((FDH_PWRITE(fdP, buf, 16 * 1024, off)) != 16 * 1024) {
                    Log("VAllocVnode: can't grow vnode index: write failed\n");
                    *ec = EIO;
                    free(buf);
@@ -819,7 +813,7 @@ VAllocVnode_r(Error * ec, Volume * vp, VnodeType type)
            if (fdP)
                FDH_CLOSE(fdP);
            VOL_LOCK;
-           VFreeBitMapEntry_r(&tmp, &vp->vnodeIndex[class], bitNumber);
+           VFreeBitMapEntry_r(&tmp, vp, &vp->vnodeIndex[class], bitNumber, 0 /*flags*/);
            VInvalidateVnode_r(vnp);
            VnUnlock(vnp, WRITE_LOCK);
            VnCancelReservation_r(vnp);
@@ -883,6 +877,7 @@ VnLoad(Error * ec, Volume * vp, Vnode * vnp,
     ssize_t nBytes;
     IHandle_t *ihP = vp->vnodeIndex[class].handle;
     FdHandle_t *fdP;
+    afs_ino_str_t stmp;
 
     *ec = 0;
     vcp->reads++;
@@ -898,21 +893,16 @@ VnLoad(Error * ec, Volume * vp, Vnode * vnp,
     fdP = IH_OPEN(ihP);
     if (fdP == NULL) {
        Log("VnLoad: can't open index dev=%u, i=%s\n", vp->device,
-           PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
-       *ec = VIO;
-       goto error_encountered_nolock;
-    } else if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, Vn_id(vnp)), SEEK_SET)
-              < 0) {
-       Log("VnLoad: can't seek on index file vn=%u\n", Vn_id(vnp));
+           PrintInode(stmp, vp->vnodeIndex[class].handle->ih_ino));
        *ec = VIO;
        goto error_encountered_nolock;
-    } else if ((nBytes = FDH_READ(fdP, (char *)&vnp->disk, vcp->diskSize))
+    } else if ((nBytes = FDH_PREAD(fdP, (char *)&vnp->disk, vcp->diskSize, vnodeIndexOffset(vcp, Vn_id(vnp))))
               != vcp->diskSize) {
        /* Don't take volume off line if the inumber is out of range
         * or the inode table is full. */
        if (nBytes == BAD_IGET) {
            Log("VnLoad: bad inumber %s\n",
-               PrintInode(NULL, vp->vnodeIndex[class].handle->ih_ino));
+               PrintInode(stmp, vp->vnodeIndex[class].handle->ih_ino));
            *ec = VIO;
            dosalv = 0;
        } else if (nBytes == -1 && errno == EIO) {
@@ -941,6 +931,12 @@ VnLoad(Error * ec, Volume * vp, Vnode * vnp,
            unsigned int bitNumber = vnodeIdToBitNumber(Vn_id(vnp));
            unsigned int offset = bitNumber >> 3;
 
+#ifdef AFS_DEMAND_ATTACH_FS
+           /* Make sure the volume bitmap isn't getting updated while we are
+            * checking it */
+           VWaitExclusiveState_r(vp);
+#endif
+
            /* Test to see if vnode number is valid. */
            if ((offset >= index->bitmapSize)
                || ((*(index->bitmap + offset) & (1 << (bitNumber & 0x7)))
@@ -1010,6 +1006,7 @@ VnStore(Error * ec, Volume * vp, Vnode * vnp,
     afs_foff_t offset;
     IHandle_t *ihP = vp->vnodeIndex[class].handle;
     FdHandle_t *fdP;
+    afs_ino_str_t stmp;
 #ifdef AFS_DEMAND_ATTACH_FS
     VnState vn_state_save;
 #endif
@@ -1027,14 +1024,7 @@ VnStore(Error * ec, Volume * vp, Vnode * vnp,
        Log("VnStore: can't open index file!\n");
        goto error_encountered;
     }
-    if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
-       Log("VnStore: can't seek on index file! fdp=%"AFS_PTR_FMT
-           " offset=%d, errno=%d\n",
-           fdP, (int) offset, errno);
-       goto error_encountered;
-    }
-
-    nBytes = FDH_WRITE(fdP, &vnp->disk, vcp->diskSize);
+    nBytes = FDH_PWRITE(fdP, &vnp->disk, vcp->diskSize, offset);
     if (nBytes != vcp->diskSize) {
        /* Don't force volume offline if the inumber is out of
         * range or the inode table is full.
@@ -1042,7 +1032,7 @@ VnStore(Error * ec, Volume * vp, Vnode * vnp,
        FDH_REALLYCLOSE(fdP);
        if (nBytes == BAD_IGET) {
            Log("VnStore: bad inumber %s\n",
-               PrintInode(NULL,
+               PrintInode(stmp,
                           vp->vnodeIndex[class].handle->ih_ino));
            *ec = VIO;
            VOL_LOCK;
@@ -1080,7 +1070,7 @@ VnStore(Error * ec, Volume * vp, Vnode * vnp,
     VnChangeState_r(vnp, VN_STATE_ERROR);
     VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
 #else
-    assert(1 == 2);
+    osi_Assert(1 == 2);
 #endif
 }
 
@@ -1328,10 +1318,10 @@ VPutVnode_r(Error * ec, Vnode * vnp)
     struct VnodeClassInfo *vcp;
 
     *ec = 0;
-    assert(Vn_refcount(vnp) != 0);
+    osi_Assert(Vn_refcount(vnp) != 0);
     class = vnodeIdToClass(Vn_id(vnp));
     vcp = &VnodeClassInfo[class];
-    assert(vnp->disk.vnodeMagic == vcp->magic);
+    osi_Assert(vnp->disk.vnodeMagic == vcp->magic);
     VNLog(200, 2, Vn_id(vnp), (intptr_t) vnp, 0, 0);
 
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -1360,7 +1350,7 @@ VPutVnode_r(Error * ec, Vnode * vnp)
        if (vnp->changed_oldTime || vnp->changed_newTime || vnp->delete) {
            Volume *vp = Vn_volume(vnp);
            afs_uint32 now = FT_ApproxTime();
-           assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
+           osi_Assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
 
            if (vnp->delete) {
                /* No longer any directory entries for this vnode. Free the Vnode */
@@ -1382,7 +1372,7 @@ VPutVnode_r(Error * ec, Vnode * vnp)
 #ifdef AFS_DEMAND_ATTACH_FS
                VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
 #else
-               assert(V_needsSalvaged(vp));
+               osi_Assert(V_needsSalvaged(vp));
                *ec = VSALVAGE;
 #endif
            } else {
@@ -1396,8 +1386,9 @@ VPutVnode_r(Error * ec, Vnode * vnp)
                if (vnp->delete && !*ec) {
                    if (Vn_volume(vnp)->header->diskstuff.filecount-- < 1)
                        Vn_volume(vnp)->header->diskstuff.filecount = 0;
-                   VFreeBitMapEntry_r(ec, &vp->vnodeIndex[class],
-                                      vnodeIdToBitNumber(Vn_id(vnp)));
+                   VFreeBitMapEntry_r(ec, vp, &vp->vnodeIndex[class],
+                                      vnodeIdToBitNumber(Vn_id(vnp)),
+                                      VOL_FREE_BITMAP_WAIT);
                }
            }
            vcp->writes++;
@@ -1469,10 +1460,10 @@ VVnodeWriteToRead_r(Error * ec, Vnode * vnp)
 #endif /* AFS_PTHREAD_ENV */
 
     *ec = 0;
-    assert(Vn_refcount(vnp) != 0);
+    osi_Assert(Vn_refcount(vnp) != 0);
     class = vnodeIdToClass(Vn_id(vnp));
     vcp = &VnodeClassInfo[class];
-    assert(vnp->disk.vnodeMagic == vcp->magic);
+    osi_Assert(vnp->disk.vnodeMagic == vcp->magic);
     VNLog(300, 2, Vn_id(vnp), (intptr_t) vnp, 0, 0);
 
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -1506,7 +1497,7 @@ VVnodeWriteToRead_r(Error * ec, Vnode * vnp)
     if (vnp->changed_oldTime || vnp->changed_newTime) {
        Volume *vp = Vn_volume(vnp);
        afs_uint32 now = FT_ApproxTime();
-       assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
+       osi_Assert(Vn_cacheCheck(vnp) == vp->cacheCheck);
        if (vnp->changed_newTime)
            vnp->disk.serverModifyTime = now;
        if (vnp->changed_newTime)
@@ -1517,7 +1508,7 @@ VVnodeWriteToRead_r(Error * ec, Vnode * vnp)
 #ifdef AFS_DEMAND_ATTACH_FS
            VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0);
 #else
-           assert(V_needsSalvaged(vp));
+           osi_Assert(V_needsSalvaged(vp));
            *ec = VSALVAGE;
 #endif
        } else {
@@ -1661,7 +1652,7 @@ VCloseVnodeFiles_r(Volume * vp)
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     /* XXX need better error handling here */
-    assert(VInvalidateVnodesByVolume_r(vp,
+    osi_Assert(VInvalidateVnodesByVolume_r(vp,
                                       &ih_vec,
                                       &vec_len) == 0);
 
@@ -1725,7 +1716,7 @@ VReleaseVnodeFiles_r(Volume * vp)
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     /* XXX need better error handling here */
-    assert(VInvalidateVnodesByVolume_r(vp,
+    osi_Assert(VInvalidateVnodesByVolume_r(vp,
                                       &ih_vec,
                                       &vec_len) == 0);