freebsd: properly track vcache references
[openafs.git] / src / afs / FBSD / osi_vfsops.c
index d3e6c57..b956b80 100644 (file)
@@ -189,19 +189,33 @@ afs_unmount(struct mount *mp, int flags, struct thread *p)
 {
     int error = 0;
 
+    AFS_GLOCK();
+    if (afs_globalVp &&
+       ((flags & MNT_FORCE) || !VREFCOUNT_GT(afs_globalVp, 1))) {
+       /* Put back afs_root's ref */
+       struct vcache *gvp = afs_globalVp;
+       afs_globalVp = NULL;
+       afs_PutVCache(gvp);
+    }
+    if (afs_globalVp)
+       error = EBUSY;
+    AFS_GUNLOCK();
+
     /*
      * Release any remaining vnodes on this mount point.
      * The `1' means that we hold one extra reference on
      * the root vnode (this is just a guess right now).
      * This has to be done outside the global lock.
      */
+    if (!error) {
 #if defined(AFS_FBSD80_ENV)
-    error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
 #elif defined(AFS_FBSD53_ENV)
-    error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
 #else
-    error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
 #endif
+    }
     if (error)
        goto out;
     AFS_GLOCK();
@@ -228,6 +242,7 @@ afs_root(struct mount *mp, struct vnode **vpp)
     int error;
     struct vrequest treq;
     struct vcache *tvp = 0;
+    struct vcache *gvp;
 #if !defined(AFS_FBSD53_ENV) || defined(AFS_FBSD80_ENV)
     struct thread *td = curthread;
 #endif
@@ -236,22 +251,26 @@ afs_root(struct mount *mp, struct vnode **vpp)
     AFS_GLOCK();
     AFS_STATCNT(afs_root);
     crhold(cr);
+tryagain:
     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
        tvp = afs_globalVp;
        error = 0;
     } else {
-tryagain:
-       if (afs_globalVp) {
-           afs_PutVCache(afs_globalVp);
-           /* vrele() needed here or not? */
-           afs_globalVp = NULL;
-       }
        if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
            tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
            /* we really want this to stay around */
-           if (tvp)
+           if (tvp) {
+               gvp = afs_globalVp;
                afs_globalVp = tvp;
-           else
+               if (gvp) {
+                   afs_PutVCache(gvp);
+                   if (tvp != afs_globalVp) {
+                       /* someone raced us and won */
+                       afs_PutVCache(tvp);
+                       goto tryagain;
+                   }
+               }
+           } else
                error = ENOENT;
        }
     }
@@ -260,16 +279,23 @@ tryagain:
 
        ASSERT_VI_UNLOCKED(vp, "afs_root");
        AFS_GUNLOCK();
+       error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
+       AFS_GLOCK();
+       /* we dropped the glock, so re-check everything it had serialized */
+       if (!afs_globalVp || !(afs_globalVp->f.states & CStatd) ||
+               tvp != afs_globalVp) {
+           vput(vp);
+           afs_PutVCache(tvp);
+           goto tryagain;
+       }
+       if (error != 0)
+           goto tryagain;
        /*
         * I'm uncomfortable about this.  Shouldn't this happen at a
         * higher level, and shouldn't we busy the top-level directory
         * to prevent recycling?
         */
-       error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
        vp->v_vflag |= VV_ROOT;
-       AFS_GLOCK();
-       if (error != 0)
-               goto tryagain;
 
        afs_globalVFS = mp;
        *vpp = vp;