afs: Clarify vcache->mvid accesses
[openafs.git] / src / afs / VNOPS / afs_vnop_remove.c
index b1c89cd..b5f53b4 100644 (file)
@@ -9,9 +9,7 @@
 
 /*
  * Implements:
- * afs_Wire (DUX)
  * FetchWholeEnchilada
- * afs_IsWired (DUX)
  * afsremove
  * afs_remove
  * afs_newname
@@ -20,8 +18,6 @@
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include "afs/sysincludes.h"   /* Standard vendor system headers */
 #include "afsincludes.h"       /* Afs-based standard headers */
@@ -35,92 +31,53 @@ extern afs_rwlock_t afs_xvcache;
 extern afs_rwlock_t afs_xcbhash;
 
 
-#ifdef AFS_OSF_ENV
-/*
- *  Wire down file in cache: prefetch all data, and turn on CWired flag
- *  so that callbacks/callback expirations are (temporarily) ignored
- *  and cache file(s) are kept in cache. File will be unwired when
- *  afs_inactive is called (ie no one has VN_HOLD on vnode), or when
- *  afs_IsWired notices that the file is no longer Active.
- */
-afs_Wire(avc, areq)
-#else /* AFS_OSF_ENV */
 static void
-FetchWholeEnchilada(register struct vcache *avc, struct vrequest *areq)
-#endif
+FetchWholeEnchilada(struct vcache *avc, struct vrequest *areq)
 {
-    register afs_int32 nextChunk;
-    register struct dcache *tdc;
+    afs_int32 nextChunk;
+    struct dcache *tdc;
     afs_size_t pos, offset, len;
 
     AFS_STATCNT(FetchWholeEnchilada);
-    if ((avc->states & CStatd) == 0)
+    if ((avc->f.states & CStatd) == 0)
        return;                 /* don't know size */
     for (nextChunk = 0; nextChunk < 1024; nextChunk++) {       /* sanity check on N chunks */
        pos = AFS_CHUNKTOBASE(nextChunk);
-#if    defined(AFS_OSF_ENV)
-       if (pos >= avc->m.Length)
-           break;              /* all done */
-#else /* AFS_OSF_ENV */
-       if (pos >= avc->m.Length)
+       if (pos >= avc->f.m.Length)
            return;             /* all done */
-#endif
        tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
        if (!tdc)
-#if    defined(AFS_OSF_ENV)
-           break;
-#else /* AFS_OSF_ENV */
            return;
-#endif
        afs_PutDCache(tdc);
     }
-#if defined(AFS_OSF_ENV)
-    avc->states |= CWired;
-#endif /* AFS_OSF_ENV */
 }
 
-#if    defined(AFS_OSF_ENV)
-/*
- *  Tests whether file is wired down, after unwiring the file if it
- *  is found to be inactive (ie not open and not being paged from).
- */
-afs_IsWired(register struct vcache *avc)
-{
-    if (avc->states & CWired) {
-       if (osi_Active(avc)) {
-           return 1;
-       }
-       avc->states &= ~CWired;
-    }
-    return 0;
-}
-#endif /* AFS_OSF_ENV */
-
 int
-afsremove(register struct vcache *adp, register struct dcache *tdc,
-         register struct vcache *tvc, char *aname, struct AFS_UCRED *acred,
+afsremove(struct vcache *adp, struct dcache *tdc,
+         struct vcache *tvc, char *aname, afs_ucred_t *acred,
          struct vrequest *treqp)
 {
-    register afs_int32 code = 0;
-    register struct afs_conn *tc;
+    afs_int32 code = 0;
+    struct afs_conn *tc;
     struct AFSFetchStatus OutDirStatus;
     struct AFSVolSync tsync;
+    struct rx_connection *rxconn;
     XSTATS_DECLS;
     if (!AFS_IS_DISCONNECTED) {
         do {
-           tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
+           tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK, &rxconn);
            if (tc) {
                XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
                RX_AFS_GUNLOCK();
                code =
-                   RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
+                   RXAFS_RemoveFile(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
                                     aname, &OutDirStatus, &tsync);
                RX_AFS_GLOCK();
                XSTATS_END_TIME;
            } else
                code = -1;
         } while (afs_Analyze
-                (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
+                (tc, rxconn, code, &adp->f.fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
                  SHARED_LOCK, NULL));
     }
 
@@ -138,7 +95,7 @@ afsremove(register struct vcache *adp, register struct dcache *tdc,
        if (code < 0) {
            ObtainWriteLock(&afs_xcbhash, 497);
            afs_DequeueCallback(adp);
-           adp->states &= ~CStatd;
+           adp->f.states &= ~CStatd;
            ReleaseWriteLock(&afs_xcbhash);
            osi_dnlc_purgedp(adp);
        }
@@ -167,23 +124,18 @@ afsremove(register struct vcache *adp, register struct dcache *tdc,
      * call FindVCache instead of GetVCache since if the file's really
      * gone, we won't be able to fetch the status info anyway.  */
     if (tvc) {
-#ifdef AFS_BOZONLOCK_ENV
-       afs_BozonLock(&tvc->pvnLock, tvc);
-       /* Since afs_TryToSmush will do a pvn_vptrunc */
-#endif
+       if (afs_mariner)
+           afs_MarinerLog("store$Removing", tvc);
        ObtainWriteLock(&tvc->lock, 141);
        /* note that callback will be broken on the deleted file if there are
         * still >0 links left to it, so we'll get the stat right */
-       tvc->m.LinkCount--;
-       tvc->states &= ~CUnique;        /* For the dfs xlator */
-       if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
+       tvc->f.m.LinkCount--;
+       tvc->f.states &= ~CUnique;      /* For the dfs xlator */
+       if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
            if (!AFS_NFSXLATORREQ(acred))
                afs_TryToSmush(tvc, acred, 0);
        }
        ReleaseWriteLock(&tvc->lock);
-#ifdef AFS_BOZONLOCK_ENV
-       afs_BozonUnlock(&tvc->pvnLock, tvc);
-#endif
        afs_PutVCache(tvc);
     }
     return (0);
@@ -215,13 +167,13 @@ char *Tnam1;
 /* Note that we don't set CDirty here, this is OK because the unlink
  * RPC is called synchronously */
 int
-afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
+afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
 {
-    struct vrequest treq;
-    register struct dcache *tdc;
+    struct vrequest *treq = NULL;
+    struct dcache *tdc;
     struct VenusFid unlinkFid;
-    register afs_int32 code;
-    register struct vcache *tvc;
+    afs_int32 code;
+    struct vcache *tvc;
     afs_size_t offset, len;
     struct afs_fakestat_state fakestate;
     OSI_VC_CONVERT(adp);
@@ -230,88 +182,54 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
               ICL_TYPE_STRING, aname);
 
-#ifdef AFS_OSF_ENV
-    tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
-#endif
 
-    if ((code = afs_InitReq(&treq, acred))) {
-#ifdef  AFS_OSF_ENV
-       afs_PutVCache(tvc);
-#endif
+    if ((code = afs_CreateReq(&treq, acred))) {
        return code;
     }
 
     afs_InitFakeStat(&fakestate);
-    code = afs_EvalFakeStat(&adp, &fakestate, &treq);
-    if (code) {
-       afs_PutFakeStat(&fakestate);
-#ifdef  AFS_OSF_ENV
-       afs_PutVCache(tvc);
-#endif
-       return code;
-    }
+    AFS_DISCON_LOCK();
+    code = afs_EvalFakeStat(&adp, &fakestate, treq);
+    if (code)
+       goto done;
 
     /* Check if this is dynroot */
     if (afs_IsDynroot(adp)) {
        code = afs_DynrootVOPRemove(adp, acred, aname);
-       afs_PutFakeStat(&fakestate);
-#ifdef  AFS_OSF_ENV
-       afs_PutVCache(tvc);
-#endif
-       return code;
+       goto done;
     }
     if (afs_IsDynrootMount(adp)) {
-       return ENOENT;
+       code = ENOENT;
+       goto done;
     }
 
     if (strlen(aname) > AFSNAMEMAX) {
-       afs_PutFakeStat(&fakestate);
-#ifdef  AFS_OSF_ENV
-       afs_PutVCache(tvc);
-#endif
-       return ENAMETOOLONG;
+       code = ENAMETOOLONG;
+       goto done;
     }
   tagain:
-    code = afs_VerifyVCache(adp, &treq);
-#ifdef AFS_OSF_ENV
-    tvc = VTOAFS(ndp->ni_vp);  /* should never be null */
-    if (code) {
-       afs_PutVCache(tvc);
-       afs_PutFakeStat(&fakestate);
-       return afs_CheckCode(code, &treq, 22);
-    }
-#else /* AFS_OSF_ENV */
+    code = afs_VerifyVCache(adp, treq);
     tvc = NULL;
     if (code) {
-       code = afs_CheckCode(code, &treq, 23);
-       afs_PutFakeStat(&fakestate);
-       return code;
+       code = afs_CheckCode(code, treq, 23);
+       goto done;
     }
-#endif
 
     /** If the volume is read-only, return error without making an RPC to the
       * fileserver
       */
-    if (adp->states & CRO) {
-#ifdef  AFS_OSF_ENV
-       afs_PutVCache(tvc);
-#endif
+    if (adp->f.states & CRO) {
        code = EROFS;
-       afs_PutFakeStat(&fakestate);
-       return code;
+       goto done;
     }
 
     /* If we're running disconnected without logging, go no further... */
     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
-#ifdef  AFS_OSF_ENV
-        afs_PutVCache(tvc);
-#endif
         code = ENETDOWN;
-        afs_PutFakeStat(&fakestate);
-        return code;
+       goto done;
     }
     
-    tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
+    tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);  /* test for error below */
     ObtainWriteLock(&adp->lock, 142);
     if (tdc)
        ObtainSharedLock(&tdc->lock, 638);
@@ -320,8 +238,8 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
      * Make sure that the data in the cache is current. We may have
      * received a callback while we were waiting for the write lock.
      */
-    if (!(adp->states & CStatd)
-       || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
+    if (!(adp->f.states & CStatd)
+       || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
        ReleaseWriteLock(&adp->lock);
        if (tdc) {
            ReleaseSharedLock(&tdc->lock);
@@ -343,11 +261,11 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
            if (code == 0) {
                afs_int32 cached = 0;
 
-               unlinkFid.Cell = adp->fid.Cell;
-               unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
+               unlinkFid.Cell = adp->f.fid.Cell;
+               unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
                if (unlinkFid.Fid.Unique == 0) {
                    tvc =
-                       afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
+                       afs_LookupVCache(&unlinkFid, treq, &cached, adp,
                                         aname);
                } else {
                    ObtainReadLock(&afs_xvcache);
@@ -357,9 +275,8 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
            }
        }
 
-#if defined(AFS_DISCON_ENV)
     if (AFS_IS_DISCON_RW) {
-       if (!adp->shVnode && !(adp->ddirty_flags & VDisconCreate)) {
+       if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
            /* Make shadow copy of parent dir. */
            afs_MakeShadowDir(adp, tdc);
        }
@@ -374,18 +291,17 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
 
        /* If we were locally created, then we don't need to do very
         * much beyond ensuring that we don't exist anymore */  
-       if (tvc->ddirty_flags & VDisconCreate) {
+       if (tvc->f.ddirty_flags & VDisconCreate) {
            afs_DisconRemoveDirty(tvc);
        } else {
            /* Add removed file vcache to dirty list. */
            afs_DisconAddDirty(tvc, VDisconRemove, 1);
         }
-       adp->m.LinkCount--;
+       adp->f.m.LinkCount--;
        ReleaseWriteLock(&tvc->lock);
        if (tdc)
            ObtainSharedLock(&tdc->lock, 714);
      }
-#endif
 
     if (tvc && osi_Active(tvc)) {
        /* about to delete whole file, prefetch it first */
@@ -393,11 +309,7 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
        if (tdc)
            ReleaseSharedLock(&tdc->lock);
        ObtainWriteLock(&tvc->lock, 143);
-#if    defined(AFS_OSF_ENV)
-       afs_Wire(tvc, &treq);
-#else /* AFS_OSF_ENV */
-       FetchWholeEnchilada(tvc, &treq);
-#endif
+       FetchWholeEnchilada(tvc, treq);
        ReleaseWriteLock(&tvc->lock);
        ObtainWriteLock(&adp->lock, 144);
        /* Technically I don't think we need this back, but let's hold it 
@@ -421,23 +333,23 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
 #endif
 #ifdef AFS_AIX_ENV
     if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
-       && !(tvc->states & CUnlinked)) {
+       && !(tvc->f.states & CUnlinked)) {
 #else
     if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
-       && !(tvc->states & CUnlinked)) {
+       && !(tvc->f.states & CUnlinked)) {
 #endif
        char *unlname = afs_newname();
 
        ReleaseWriteLock(&adp->lock);
        if (tdc)
            ReleaseSharedLock(&tdc->lock);
-       code = afsrename(adp, aname, adp, unlname, acred, &treq);
+       code = afsrename(adp, aname, adp, unlname, acred, treq);
        Tnam1 = unlname;
        if (!code) {
-           struct VenusFid *oldmvid = NULL;
-           if (tvc->mvid) 
-               oldmvid = tvc->mvid;
-           tvc->mvid = (struct VenusFid *)unlname;
+           void *oldmvid = NULL;
+           if (tvc->mvid.silly_name)
+               oldmvid = tvc->mvid.silly_name;
+           tvc->mvid.silly_name = unlname;
            if (oldmvid)
                osi_FreeSmallSpace(oldmvid);
            crhold(acred);
@@ -445,7 +357,13 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
                crfree(tvc->uncred);
            }
            tvc->uncred = acred;
-           tvc->states |= CUnlinked;
+           tvc->f.states |= CUnlinked;
+           /* if rename succeeded, remove should not */
+           ObtainWriteLock(&tvc->lock, 715);
+           if (tvc->f.ddirty_flags & VDisconRemove) {
+               tvc->f.ddirty_flags &= ~VDisconRemove;
+           }
+           ReleaseWriteLock(&tvc->lock);
        } else {
            osi_FreeSmallSpace(unlname);
        }
@@ -453,14 +371,17 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
            afs_PutDCache(tdc);
        afs_PutVCache(tvc);
     } else {
-       code = afsremove(adp, tdc, tvc, aname, acred, &treq);
+       code = afsremove(adp, tdc, tvc, aname, acred, treq);
     }
+    done:
     afs_PutFakeStat(&fakestate);
-#ifndef AFS_DARWIN80_ENV
+#if !defined(AFS_DARWIN80_ENV) && !defined(UKERNEL)
     /* we can't track by thread, it's not exported in the KPI; only do
        this on !macos */
     osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
 #endif
+    AFS_DISCON_UNLOCK();
+    afs_DestroyReq(treq);
     return code;
 }
 
@@ -471,14 +392,13 @@ afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
  * CAUTION -- may be called with avc unheld. */
 
 int
-afs_remunlink(register struct vcache *avc, register int doit)
+afs_remunlink(struct vcache *avc, int doit)
 {
-    struct AFS_UCRED *cred;
+    afs_ucred_t *cred;
     char *unlname;
     struct vcache *adp;
-    struct vrequest treq;
     struct VenusFid dirFid;
-    register struct dcache *tdc;
+    struct dcache *tdc;
     afs_int32 code = 0;
 
     if (NBObtainWriteLock(&avc->lock, 423))
@@ -490,36 +410,38 @@ afs_remunlink(register struct vcache *avc, register int doit)
     }
 #endif
 
-    if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
-       if ((code = afs_InitReq(&treq, avc->uncred))) {
+    if (avc->mvid.silly_name && (doit || (avc->f.states & CUnlinkedDel))) {
+       struct vrequest *treq = NULL;
+
+       if ((code = afs_CreateReq(&treq, avc->uncred))) {
            ReleaseWriteLock(&avc->lock);
        } else {
            /* Must bump the refCount because GetVCache may block.
             * Also clear mvid so no other thread comes here if we block.
             */
-           unlname = (char *)avc->mvid;
-           avc->mvid = NULL;
+           unlname = avc->mvid.silly_name;
+           avc->mvid.silly_name = NULL;
            cred = avc->uncred;
            avc->uncred = NULL;
 
 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
            VREF(AFSTOV(avc));
 #else
-           VN_HOLD(AFSTOV(avc));
+           AFS_FAST_HOLD(avc);
 #endif
 
            /* We'll only try this once. If it fails, just release the vnode.
             * Clear after doing hold so that NewVCache doesn't find us yet.
             */
-           avc->states &= ~(CUnlinked | CUnlinkedDel);
+           avc->f.states &= ~(CUnlinked | CUnlinkedDel);
 
            ReleaseWriteLock(&avc->lock);
 
-           dirFid.Cell = avc->fid.Cell;
-           dirFid.Fid.Volume = avc->fid.Fid.Volume;
-           dirFid.Fid.Vnode = avc->parentVnode;
-           dirFid.Fid.Unique = avc->parentUnique;
-           adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
+           dirFid.Cell = avc->f.fid.Cell;
+           dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
+           dirFid.Fid.Vnode = avc->f.parent.vnode;
+           dirFid.Fid.Unique = avc->f.parent.unique;
+           adp = afs_GetVCache(&dirFid, treq, NULL, NULL);
 
            if (adp) {
                tdc = afs_FindDCache(adp, (afs_size_t) 0);
@@ -528,7 +450,7 @@ afs_remunlink(register struct vcache *avc, register int doit)
                    ObtainSharedLock(&tdc->lock, 639);
 
                /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
-               code = afsremove(adp, tdc, avc, unlname, cred, &treq);
+               code = afsremove(adp, tdc, avc, unlname, cred, treq);
                afs_PutVCache(adp);
            } else {
                /* we failed - and won't be back to try again. */
@@ -536,6 +458,7 @@ afs_remunlink(register struct vcache *avc, register int doit)
            }
            osi_FreeSmallSpace(unlname);
            crfree(cred);
+           afs_DestroyReq(treq);
        }
     } else {
 #if defined(AFS_DARWIN80_ENV)