freebsd: properly track vcache references
[openafs.git] / src / afs / FBSD / osi_vfsops.c
index 37c6437..b956b80 100644 (file)
@@ -17,69 +17,96 @@ struct vcache *afs_globalVp = NULL;
 struct mount *afs_globalVFS = NULL;
 int afs_pbuf_freecnt = -1;
 
-#ifdef AFS_FBSD50_ENV
-#define        THREAD_OR_PROC struct thread *p
-#else
-#define        THREAD_OR_PROC struct proc *p
-#endif
-
-extern int afs3_syscall();
 extern int Afs_xsetgroups();
 extern int afs_xioctl();
 
+#if !defined(AFS_FBSD90_ENV) && !defined(AFS_FBSD82_ENV)
 static sy_call_t *old_handler;
+#else
+static struct sysent old_sysent;
 
+static struct sysent afs_sysent = {
+    5,                 /* int sy_narg */
+    (sy_call_t *) afs3_syscall,        /* sy_call_t *sy_call */
+#ifdef AFS_FBSD60_ENV
+    AUE_NULL,          /* au_event_t sy_auevent */
+#ifdef AFS_FBSD70_ENV
+    NULL,              /* systrace_args_funt_t sy_systrace_args_func */
+    0,                 /* u_int32_t sy_entry */
+    0,                 /* u_int32_t sy_return */
+#ifdef AFS_FBSD90_ENV
+    0,                 /* u_int32_t sy_flags */
+    0                  /* u_int32_t sy_thrcnt */
+#endif
+#endif
+#endif /* FBSD60 */
+};
+#endif /* FBSD90 */
 
 int
 afs_init(struct vfsconf *vfc)
 {
+    int code;
+    int offset = AFS_SYSCALL;
+#if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
+    code = syscall_register(&offset, &afs_sysent, &old_sysent);
+    if (code) {
+       printf("AFS_SYSCALL in use, error %i. aborting\n", code);
+       return code;
+    }
+#else
     if (sysent[AFS_SYSCALL].sy_call != nosys
-       && sysent[AFS_SYSCALL].sy_call != lkmnosys) {
-       printf("AFS_SYSCALL in use. aborting\n");
-       return EBUSY;
+        && sysent[AFS_SYSCALL].sy_call != lkmnosys) {
+        printf("AFS_SYSCALL in use. aborting\n");
+        return EBUSY;
     }
+#endif
     osi_Init();
     afs_pbuf_freecnt = nswbuf / 2 + 1;
-#if 0
-    sysent[SYS_setgroups].sy_call = Afs_xsetgroups;
-    sysent[SYS_ioctl].sy_call = afs_xioctl;
-#endif
+#if !defined(AFS_FBSD90_ENV) && !defined(AFS_FBSD82_ENV)
     old_handler = sysent[AFS_SYSCALL].sy_call;
     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
     sysent[AFS_SYSCALL].sy_narg = 5;
+#endif
     return 0;
 }
 
 int
 afs_uninit(struct vfsconf *vfc)
 {
+#if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
+    int offset = AFS_SYSCALL;
+#endif
+
     if (afs_globalVFS)
        return EBUSY;
-#if 0
-    sysent[SYS_ioctl].sy_call = ioctl;
-    sysent[SYS_setgroups].sy_call = setgroups;
-#endif
+#if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
+    syscall_deregister(&offset, &old_sysent);
+#else
     sysent[AFS_SYSCALL].sy_narg = 0;
     sysent[AFS_SYSCALL].sy_call = old_handler;
+#endif
     return 0;
 }
 
 int
-afs_start(struct mount *mp, int flags, THREAD_OR_PROC)
+afs_start(struct mount *mp, int flags, struct thread *p)
 {
     return (0);                        /* nothing to do. ? */
 }
 
 int
-#ifdef AFS_FBSD53_ENV
+#if defined(AFS_FBSD80_ENV)
+afs_omount(struct mount *mp, char *path, caddr_t data)
+#elif defined(AFS_FBSD53_ENV)
 afs_omount(struct mount *mp, char *path, caddr_t data, struct thread *p)
 #else
 afs_omount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
-       THREAD_OR_PROC)
+       struct thread *p)
 #endif
 {
     /* ndp contains the mounted-from device.  Just ignore it.
-     * we also don't care about our proc struct. */
+     * we also don't care about our thread struct. */
     size_t size;
 
     if (mp->mnt_flag & MNT_UPDATE)
@@ -116,30 +143,63 @@ afs_omount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
     MNT_IUNLOCK(mp);
 #endif
     AFS_GUNLOCK();
+#ifdef AFS_FBSD80_ENV
+    afs_statfs(mp, &mp->mnt_stat);
+#else
     afs_statfs(mp, &mp->mnt_stat, p);
+#endif
 
     return 0;
 }
 
 #ifdef AFS_FBSD53_ENV
 int
+#ifdef AFS_FBSD80_ENV
+afs_mount(struct mount *mp)
+#else
 afs_mount(struct mount *mp, struct thread *td)
+#endif
 {
+#ifdef AFS_FBSD80_ENV
+    return afs_omount(mp, NULL, NULL);
+#else
     return afs_omount(mp, NULL, NULL, td);
+#endif
 }
 #endif
 
 #ifdef AFS_FBSD60_ENV
 static int
+#ifdef AFS_FBSD80_ENV
+afs_cmount(struct mntarg *ma, void *data, int flags)
+#else
 afs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
+#endif
 {
     return kernel_mount(ma, flags);
 }
 #endif
 
 int
-afs_unmount(struct mount *mp, int flags, THREAD_OR_PROC)
+#ifdef AFS_FBSD80_ENV
+afs_unmount(struct mount *mp, int flags)
+#else
+afs_unmount(struct mount *mp, int flags, struct thread *p)
+#endif
 {
+    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.
@@ -147,24 +207,31 @@ afs_unmount(struct mount *mp, int flags, THREAD_OR_PROC)
      * 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)
-  /* do nothing */
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
 #elif defined(AFS_FBSD53_ENV)
-    vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
 #else
-    vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
+       error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
 #endif
+    }
+    if (error)
+       goto out;
     AFS_GLOCK();
     AFS_STATCNT(afs_unmount);
     afs_globalVFS = 0;
     afs_shutdown();
     AFS_GUNLOCK();
 
-    return 0;
+out:
+    return error;
 }
 
 int
-#if defined(AFS_FBSD60_ENV)
+#if defined(AFS_FBSD80_ENV)
+afs_root(struct mount *mp, int flags, struct vnode **vpp)
+#elif defined(AFS_FBSD60_ENV)
 afs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
 #elif defined(AFS_FBSD53_ENV)
 afs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
@@ -174,63 +241,61 @@ afs_root(struct mount *mp, struct vnode **vpp)
 {
     int error;
     struct vrequest treq;
-    register struct vcache *tvp = 0;
-#ifdef AFS_FBSD50_ENV
-#ifndef AFS_FBSD53_ENV
+    struct vcache *tvp = 0;
+    struct vcache *gvp;
+#if !defined(AFS_FBSD53_ENV) || defined(AFS_FBSD80_ENV)
     struct thread *td = curthread;
 #endif
-    struct ucred *cr = td->td_ucred;
-#else
-    struct proc *p = curproc;
-    struct ucred *cr = p->p_cred->pc_ucred;
-#endif
+    struct ucred *cr = osi_curcred();
 
     AFS_GLOCK();
     AFS_STATCNT(afs_root);
     crhold(cr);
+tryagain:
     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
        tvp = afs_globalVp;
        error = 0;
     } else {
-tryagain:
-#ifndef AFS_FBSD80_ENV
-       if (afs_globalVp) {
-           afs_PutVCache(afs_globalVp);
-           /* vrele() needed here or not? */
-           afs_globalVp = NULL;
-       }
-#endif
        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;
        }
     }
     if (tvp) {
        struct vnode *vp = AFSTOV(tvp);
 
-#ifdef AFS_FBSD50_ENV
        ASSERT_VI_UNLOCKED(vp, "afs_root");
-#endif
        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?
         */
-#ifdef AFS_FBSD50_ENV
-       error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
        vp->v_vflag |= VV_ROOT;
-#else
-       error = vget(vp, LK_EXCLUSIVE | LK_RETRY, p);
-       vp->v_flag |= VROOT;
-#endif
-       AFS_GLOCK();
-       if (error != 0)
-               goto tryagain;
 
        afs_globalVFS = mp;
        *vpp = vp;
@@ -244,7 +309,11 @@ tryagain:
 }
 
 int
-afs_statfs(struct mount *mp, struct statfs *abp, THREAD_OR_PROC)
+#ifdef AFS_FBSD80_ENV
+afs_statfs(struct mount *mp, struct statfs *abp)
+#else
+afs_statfs(struct mount *mp, struct statfs *abp, struct thread *p)
+#endif
 {
     AFS_GLOCK();
     AFS_STATCNT(afs_statfs);
@@ -274,10 +343,12 @@ afs_statfs(struct mount *mp, struct statfs *abp, THREAD_OR_PROC)
 }
 
 int
-#ifdef AFS_FBSD60_ENV
+#if defined(AFS_FBSD80_ENV)
+afs_sync(struct mount *mp, int waitfor)
+#elif defined(AFS_FBSD60_ENV)
 afs_sync(struct mount *mp, int waitfor, struct thread *td)
 #else
-afs_sync(struct mount *mp, int waitfor, struct ucred *cred, THREAD_OR_PROC)
+afs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *p)
 #endif
 {
     return 0;
@@ -314,8 +385,6 @@ struct vfsops afs_vfsops = {
     afs_init,
     afs_uninit,
     vfs_stdextattrctl,
-#ifdef AFS_FBSD50_ENV
     vfs_stdsysctl,
-#endif
 };
 #endif