afs: clarify cold and warm shutdown logic
[openafs.git] / src / afs / FBSD / osi_vfsops.c
index 5820173..d15620c 100644 (file)
@@ -1,64 +1,95 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID("$Header$");
 
-#include <afs/sysincludes.h>            /* Standard vendor system headers */
-#include <afsincludes.h>            /* Afs-based standard headers */
-#include <afs/afs_stats.h>              /* statistics */
+#include <afs/sysincludes.h>   /* Standard vendor system headers */
+#include <afsincludes.h>       /* Afs-based standard headers */
+#include <afs/afs_stats.h>     /* statistics */
 #include <sys/malloc.h>
 #include <sys/namei.h>
 #include <sys/conf.h>
+#include <sys/module.h>
+#include <sys/sysproto.h>
 #include <sys/syscall.h>
+#include <sys/sysent.h>
 
-struct vcache *afs_globalVp = 0;
-struct mount *afs_globalVFS = 0;
-int afs_pbuf_freecnt=-1;
-int
-afs_quotactl()
+struct vcache *afs_globalVp = NULL;
+struct mount *afs_globalVFS = NULL;
+int afs_pbuf_freecnt = -1;
+
+extern int Afs_xsetgroups();
+extern int afs_xioctl();
+
+static struct syscall_helper_data afs_syscalls[] = {
+    {
+       .syscall_no = AFS_SYSCALL,
+       .new_sysent = {
+           .sy_narg = 5,
+           .sy_call = (sy_call_t *)afs3_syscall,
+           .sy_auevent = AUE_NULL,
+       },
+    },
+    SYSCALL_INIT_LAST
+};
+
+static int
+afs_init(struct vfsconf *vfc)
 {
-       return EOPNOTSUPP;
+    int code;
+#if defined(FBSD_SYSCALL_REGISTER_TAKES_FLAGS)
+    code = syscall_helper_register(afs_syscalls, 0);
+#else
+    code = syscall_helper_register(afs_syscalls);
+#endif
+    if (code) {
+       printf("AFS_SYSCALL in use, error %i. aborting\n", code);
+       return code;
+    }
+    osi_Init();
+    afs_pbuf_freecnt = nswbuf / 2 + 1;
+    return 0;
 }
 
-int
-afs_fhtovp(mp, fhp, vpp)
-struct mount *mp;
-struct fid *fhp;
-struct vnode **vpp;
+static int
+afs_uninit(struct vfsconf *vfc)
 {
+    if (afs_globalVFS)
+       return EBUSY;
 
-       return (EINVAL);
+    return syscall_helper_unregister(afs_syscalls);
 }
 
-int
-afs_vptofh(vp, fhp)
-struct vnode *vp;
-struct fid *fhp;
+static int
+afs_statfs(struct mount *mp, struct statfs *abp)
 {
+    AFS_GLOCK();
+    AFS_STATCNT(afs_statfs);
 
-       return (EINVAL);
-}
+    abp->f_bsize = mp->vfs_bsize;
+    abp->f_iosize = mp->vfs_bsize;
 
-int
-afs_start(mp, flags, p)
-struct mount *mp;
-int flags;
-struct proc *p;
-{
-    afs_pbuf_freecnt = nswbuf / 2 + 1;
-    return (0);                         /* nothing to do. ? */
+    abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
+       abp->f_ffree = AFS_VFS_FAKEFREE;
+
+    abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
+    abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
+    if (abp != &mp->mnt_stat) {
+       abp->f_type = mp->mnt_vfc->vfc_typenum;
+       memcpy((caddr_t) & abp->f_mntonname[0],
+              (caddr_t) mp->mnt_stat.f_mntonname, MNAMELEN);
+       memcpy((caddr_t) & abp->f_mntfromname[0],
+              (caddr_t) mp->mnt_stat.f_mntfromname, MNAMELEN);
+    }
+
+    AFS_GUNLOCK();
+    return 0;
 }
 
-int
-afs_mount(mp, path, data, ndp, p)
-register struct mount *mp;
-char *path;
-caddr_t data;
-struct nameidata *ndp;
-struct proc *p;
+static int
+afs_omount(struct mount *mp, char *path, caddr_t data)
 {
     /* 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)
@@ -67,187 +98,183 @@ struct proc *p;
     AFS_GLOCK();
     AFS_STATCNT(afs_mount);
 
-    if (afs_globalVFS) { /* Don't allow remounts. */
+    if (afs_globalVFS) {       /* Don't allow remounts. */
        AFS_GUNLOCK();
-       return (EBUSY);
+       return EBUSY;
     }
 
     afs_globalVFS = mp;
     mp->vfs_bsize = 8192;
     vfs_getnewfsid(mp);
-    mp->mnt_stat.f_iosize=8192;
-    
-    (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN-1, &size);
+    /*
+     * This is kind of ugly, as the interlock has grown to encompass
+     * more fields over time and there's not a good way to group the
+     * code without duplication.
+     */
+    MNT_ILOCK(mp);
+    mp->mnt_flag &= ~MNT_LOCAL;
+#if __FreeBSD_version < 1000021
+    mp->mnt_kern_flag |= MNTK_MPSAFE; /* solid steel */
+#endif
+    /*
+     * XXX mnt_stat "is considered stable as long as a ref is held".
+     * We should check that we hold the only ref.
+     */
+    mp->mnt_stat.f_iosize = 8192;
+
+    if (path != NULL)
+       copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
+    else
+       bcopy("/afs", mp->mnt_stat.f_mntonname, size = 4);
     memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
     memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
     strcpy(mp->mnt_stat.f_mntfromname, "AFS");
     /* null terminated string "AFS" will fit, just leave it be. */
     strcpy(mp->mnt_stat.f_fstypename, "afs");
+    MNT_IUNLOCK(mp);
     AFS_GUNLOCK();
-    (void) afs_statfs(mp, &mp->mnt_stat, p);
+    afs_statfs(mp, &mp->mnt_stat);
+
     return 0;
 }
 
-int
-afs_unmount(mp, flags, p)
-struct mount *mp;
-int flags;
-struct proc *p;
+static int
+afs_mount(struct mount *mp)
+{
+    return afs_omount(mp, NULL, NULL);
+}
+
+static int
+#if __FreeBSD_version >= 1000004
+afs_cmount(struct mntarg *ma, void *data, uint64_t flags)
+#else
+afs_cmount(struct mntarg *ma, void *data, int flags)
+#endif
+{
+    return kernel_mount(ma, flags);
+}
+
+static int
+afs_unmount(struct mount *mp, int flags)
 {
-    
+    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();
+
+    if (!error) {
+       /*
+        * Release any remaining vnodes on this mount point. The second
+        * argument is how many refs we hold on the root vnode. Since we
+        * released our reference to the root vnode up above, give 0.
+        */
+       error = vflush(mp, 0, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
+    }
+    if (error)
+       goto out;
     AFS_GLOCK();
     AFS_STATCNT(afs_unmount);
     afs_globalVFS = 0;
-    afs_shutdown();
+    afs_shutdown(AFS_WARM);
     AFS_GUNLOCK();
 
-    return 0;
+out:
+    return error;
 }
 
-int
-afs_root(struct mount *mp,
-             struct vnode **vpp)
+static int
+afs_root(struct mount *mp, int flags, struct vnode **vpp)
 {
     int error;
     struct vrequest treq;
-    register struct vcache *tvp = 0;
-#ifdef AFS_FBSD50_ENV
+    struct vcache *tvp = 0;
+    struct vcache *gvp;
     struct thread *td = curthread;
-    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);
-    if (afs_globalVp && (afs_globalVp->states & CStatd)) {
+    crhold(cr);
+tryagain:
+    if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
        tvp = afs_globalVp;
-        error=0;
+       error = 0;
     } else {
-       if (afs_globalVp) {
-           afs_PutVCache(afs_globalVp);
-           afs_globalVp = NULL;
-       }
-
-       if (!(error = afs_InitReq(&treq, &cr)) &&
-           !(error = afs_CheckInit())) {
-           tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
+       if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
+           tvp = afs_GetVCache(&afs_rootFid, &treq);
            /* we really want this to stay around */
            if (tvp) {
-               afs_globalVp = tvp;
+               gvp = afs_globalVp;
+               afs_globalVp = tvp;
+               if (gvp) {
+                   afs_PutVCache(gvp);
+                   if (tvp != afs_globalVp) {
+                       /* someone raced us and won */
+                       afs_PutVCache(tvp);
+                       goto tryagain;
+                   }
+               }
            } else
-               error = ENOENT;
+               error = EIO;
        }
     }
     if (tvp) {
-        osi_vnhold(tvp,0);
-    AFS_GUNLOCK();
-#ifdef AFS_FBSD50_ENV
-        vn_lock(AFSTOV(tvp), LK_EXCLUSIVE | LK_RETRY, td);
-#else
-        vn_lock(AFSTOV(tvp), LK_EXCLUSIVE | LK_RETRY, p);
-#endif
-    AFS_GLOCK();
+       struct vnode *vp = AFSTOV(tvp);
+
+       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?
+        */
+       vp->v_vflag |= VV_ROOT;
+
        afs_globalVFS = mp;
-       *vpp = AFSTOV(tvp);
-        tvp->v.v_flag |= VROOT;
+       *vpp = vp;
     }
 
-    afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *vpp,
+    afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, tvp ? AFSTOV(tvp) : NULL,
               ICL_TYPE_INT32, error);
     AFS_GUNLOCK();
+    crfree(cr);
     return error;
 }
 
-int
-afs_vget(mp, lfl, vp)
-struct mount *mp;
-struct vnode *vp;
-int lfl;
-{
-    int error;
-
-    printf("vget called. help!\n");
-    if (vp->v_usecount < 0) {
-       vprint("bad usecount", vp);
-       panic("afs_vget");
-    }
-#ifdef AFS_FBSD50_ENV
-    error = vget(vp, lfl, curthread);
-#else
-    error = vget(vp, lfl, curproc);
-#endif
-    if (!error)
-       insmntque(vp, afs_globalVFS);   /* take off free list */
-    return error;
-}
-
-int afs_statfs(struct mount *mp, struct statfs *abp, struct proc *p)
+static int
+afs_sync(struct mount *mp, int waitfor)
 {
-    AFS_GLOCK();
-    AFS_STATCNT(afs_statfs);
-
-#if 0
-    abp->f_type = MOUNT_AFS;
-#endif
-    abp->f_bsize = mp->vfs_bsize;
-    abp->f_iosize = mp->vfs_bsize;
-
-    /* Fake a high number below to satisfy programs that use the statfs call
-     * to make sure that there's enough space in the device partition before
-     * storing something there.
-     */
-    abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
-       abp->f_ffree  = 2000000;
-
-    abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
-    abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
-    if (abp != &mp->mnt_stat) {
-       abp->f_type = mp->mnt_vfc->vfc_typenum;
-       memcpy((caddr_t)&abp->f_mntonname[0], (caddr_t)mp->mnt_stat.f_mntonname, MNAMELEN);
-       memcpy((caddr_t)&abp->f_mntfromname[0], (caddr_t)mp->mnt_stat.f_mntfromname, MNAMELEN);
-    }
-
-    AFS_GUNLOCK();
     return 0;
 }
 
-int afs_sync(mp, waitfor, cred, p) 
-struct mount *mp;
-int waitfor;
-struct ucred *cred;
-struct prioc *p;
-{
-return 0;
-}
-
-int afs_sysctl() {
-   return EOPNOTSUPP;
-}
-
-
-int afs_init(struct vfsconf *vfc) {
-   return 0;
-}
-
 struct vfsops afs_vfsops = {
-  afs_mount,
-  afs_start,
-  afs_unmount,
-  afs_root,
-  afs_quotactl,
-  afs_statfs,
-  afs_sync,
-  afs_vget,
-  afs_fhtovp,
-#ifdef AFS_FBSD50_ENV
-  vfs_stdcheckexp,
-#endif
-  afs_vptofh,
-  afs_init,
-#ifdef AFS_FBSD50_ENV
-  vfs_stduninit,
-#endif
-  afs_sysctl
+       .vfs_init =             afs_init,
+       .vfs_mount =            afs_mount,
+       .vfs_cmount =           afs_cmount,
+       .vfs_root =             afs_root,
+       .vfs_statfs =           afs_statfs,
+       .vfs_sync =             afs_sync,
+       .vfs_uninit =           afs_uninit,
+       .vfs_unmount =          afs_unmount,
+       .vfs_sysctl =           vfs_stdsysctl,
 };