Solaris: shutdown in freevfs, not unmount
[openafs.git] / src / afs / SOLARIS / osi_vfsops.c
index 4a19d33..d090111 100644 (file)
 #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 stuff */
 #include "h/modctl.h"
 #include "h/syscall.h"
+#if defined(AFS_SUN511_ENV)
+#include <sys/vfs_opreg.h>
+#endif
 #include <sys/kobj.h>
 
-
+#ifdef AFS_SUN58_ENV
+# include <sys/mount.h>
+#endif
 
 struct vfs *afs_globalVFS = 0;
 struct vcache *afs_globalVp = 0;
@@ -36,22 +39,26 @@ int afsfstype = 0;
 
 int
 afs_mount(struct vfs *afsp, struct vnode *amvp, struct mounta *uap,
-         struct AFS_UCRED *credp)
+         afs_ucred_t *credp)
 {
 
     AFS_GLOCK();
 
     AFS_STATCNT(afs_mount);
 
-    if (!suser(credp)) {
+#if defined(AFS_SUN510_ENV)
+    if (secpolicy_fs_mount(credp, amvp, afsp) != 0) {
+#else
+    if (!afs_osi_suser(credp)) {
+#endif
        AFS_GUNLOCK();
-       return EPERM;
+       return (EPERM);
     }
     afsp->vfs_fstype = afsfstype;
 
     if (afs_globalVFS) {       /* Don't allow remounts. */
        AFS_GUNLOCK();
-       return EBUSY;
+       return (EBUSY);
     }
 
     afs_globalVFS = afsp;
@@ -66,32 +73,85 @@ afs_mount(struct vfs *afsp, struct vnode *amvp, struct mounta *uap,
 
 #if defined(AFS_SUN58_ENV)
 int
-afs_unmount(struct vfs *afsp, int flag, struct AFS_UCRED *credp)
+afs_unmount(struct vfs *afsp, int flag, afs_ucred_t *credp)
 #else
 int
-afs_unmount(struct vfs *afsp, struct AFS_UCRED *credp)
+afs_unmount(struct vfs *afsp, afs_ucred_t *credp)
 #endif
 {
+    struct vcache *rootvp = NULL;
+
     AFS_GLOCK();
     AFS_STATCNT(afs_unmount);
 
-    if (!suser(credp)) {
+#if defined(AFS_SUN510_ENV)
+    if (secpolicy_fs_unmount(credp, afsp) != 0) {
+#else
+    if (!afs_osi_suser(credp)) {
+#endif
+        AFS_GUNLOCK();
+        return (EPERM);
+    }
+
+#ifdef AFS_SUN58_ENV
+    if (flag & MS_FORCE) {
+       AFS_GUNLOCK();
+       return ENOTSUP;
+    }
+
+    /* We should have one reference from the caller, and one reference for the
+     * root vnode; any more and someone is still referencing something */
+    if (afsp->vfs_count > 2) {
+       AFS_GUNLOCK();
+       return EBUSY;
+    }
+
+    /* The root vnode should have one ref for the mount; any more, and someone
+     * else is using the root vnode */
+    if (afs_globalVp && VREFCOUNT_GT(afs_globalVp, 1)) {
        AFS_GUNLOCK();
-       return EPERM;
+       return EBUSY;
     }
+
+    afsp->vfs_flag |= VFS_UNMOUNTED;
+#endif /* AFS_SUN58_ENV */
+
+    /* release the root vnode, which should be the last reference to us
+     * besides the caller of afs_unmount */
+    rootvp = afs_globalVp;
+    afs_globalVp = NULL;
+    AFS_RELE(rootvp);
+
+#ifndef AFS_SUN58_ENV
+    /* shutdown now, since afs_freevfs() will not be called */
     afs_globalVFS = 0;
     afs_shutdown();
+#endif /* !AFS_SUN58_ENV */
 
     AFS_GUNLOCK();
     return 0;
 }
 
+#ifdef AFS_SUN58_ENV
+void
+afs_freevfs(struct vfs *afsp)
+{
+    AFS_GLOCK();
+
+    afs_globalVFS = 0;
+    afs_shutdown();
+
+    AFS_GUNLOCK();
+}
+#endif /* AFS_SUN58_ENV */
+
 int
 afs_root(struct vfs *afsp, struct vnode **avpp)
 {
     register afs_int32 code = 0;
     struct vrequest treq;
     register struct vcache *tvp = 0;
+    struct vcache *gvp;
     struct proc *proc = ttoproc(curthread);
     struct vnode *vp = afsp->vfs_vnodecovered;
     int locked = 0;
@@ -107,7 +167,8 @@ afs_root(struct vfs *afsp, struct vnode **avpp)
 
     AFS_STATCNT(afs_root);
 
-    if (afs_globalVp && (afs_globalVp->states & CStatd)) {
+again:
+    if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
        tvp = afs_globalVp;
     } else {
        if (MUTEX_HELD(&vp->v_lock)) {
@@ -117,8 +178,9 @@ afs_root(struct vfs *afsp, struct vnode **avpp)
        }
 
        if (afs_globalVp) {
-           afs_PutVCache(afs_globalVp);
+           gvp = afs_globalVp;
            afs_globalVp = NULL;
+           afs_PutVCache(gvp);
        }
 
        if (!(code = afs_InitReq(&treq, proc->p_cred))
@@ -126,6 +188,12 @@ afs_root(struct vfs *afsp, struct vnode **avpp)
            tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
            /* we really want this to stay around */
            if (tvp) {
+               if (afs_globalVp) {
+                   /* someone else got there before us! */
+                   afs_PutVCache(tvp);
+                   tvp = 0;
+                   goto again;
+               }
                afs_globalVp = tvp;
            } else
                code = ENOENT;
@@ -181,7 +249,7 @@ afs_statvfs(struct vfs *afsp, struct statvfs *abp)
 }
 
 int
-afs_sync(struct vfs *afsp, short flags, struct AFS_UCRED *credp)
+afs_sync(struct vfs *afsp, short flags, afs_ucred_t *credp)
 {
     return 0;
 }
@@ -237,16 +305,32 @@ afs_swapvp(struct vfs *afsp, struct vnode **avpp, char *nm)
 }
 
 
-#ifdef AFS_SUN510_ENV
-struct fs_operation_def afs_vfsops_template[] = {
-    { VFSNAME_MOUNT,           afs_mount },
-    { VFSNAME_UNMOUNT,         afs_unmount },
-    { VFSNAME_ROOT,            afs_root },
-    { VFSNAME_STATVFS,         afs_statvfs },
-    { VFSNAME_SYNC,            afs_sync },
-    { VFSNAME_VGET,            afs_vget },
-    { VFSNAME_MOUNTROOT,       afs_mountroot },
-    { VFSNAME_FREEVFS,         fs_freevfs },
+#if defined(AFS_SUN511_ENV)
+/* The following list must always be NULL-terminated */
+static const fs_operation_def_t afs_vfsops_template[] = {
+    VFSNAME_MOUNT,             { .vfs_mount = afs_mount },
+    VFSNAME_UNMOUNT,           { .vfs_unmount = afs_unmount },
+    VFSNAME_ROOT,              { .vfs_root = afs_root },
+    VFSNAME_STATVFS,           { .vfs_statvfs = afs_statvfs },
+    VFSNAME_SYNC,              { .vfs_sync = afs_sync },
+    VFSNAME_VGET,              { .vfs_vget = afs_vget },
+    VFSNAME_MOUNTROOT,         { .vfs_mountroot = afs_mountroot },
+    VFSNAME_FREEVFS,           { .vfs_freevfs = afs_freevfs },
+    NULL,                      NULL
+};
+struct vfsops *afs_vfsopsp;
+#elif defined(AFS_SUN510_ENV)
+/* The following list must always be NULL-terminated */
+const fs_operation_def_t afs_vfsops_template[] = {
+    VFSNAME_MOUNT,             afs_mount,
+    VFSNAME_UNMOUNT,           afs_unmount,
+    VFSNAME_ROOT,              afs_root,
+    VFSNAME_STATVFS,           afs_statvfs,
+    VFSNAME_SYNC,              afs_sync,
+    VFSNAME_VGET,              afs_vget,
+    VFSNAME_MOUNTROOT,         afs_mountroot,
+    VFSNAME_FREEVFS,           afs_freevfs,
+    NULL,                      NULL
 };
 struct vfsops *afs_vfsopsp;
 #else
@@ -260,7 +344,7 @@ struct vfsops Afs_vfsops = {
     afs_mountroot,
     afs_swapvp,
 #if defined(AFS_SUN58_ENV)
-    fs_freevfs,
+    afs_freevfs,
 #endif
 };
 #endif
@@ -277,7 +361,6 @@ void (*ufs_itimes_nolockp) ();
 int (*afs_orig_ioctl) (), (*afs_orig_ioctl32) ();
 int (*afs_orig_setgroups) (), (*afs_orig_setgroups32) ();
 
-struct streamtab *udp_infop = 0;
 #ifndef AFS_SUN510_ENV
 struct ill_s *ill_g_headp = 0;
 #endif
@@ -297,6 +380,20 @@ int (*nfs_checkauth) ();
 
 extern Afs_syscall();
 
+static void *
+do_mod_lookup(const char * mod, const char * sym)
+{
+    void * ptr;
+
+    ptr = modlookup(mod, sym);
+    if (ptr == NULL) {
+        afs_warn("modlookup failed for symbol '%s' in module '%s'\n",
+                sym, mod);
+    }
+
+    return ptr;
+}
+
 #ifdef AFS_SUN510_ENV
 afsinit(int fstype, char *dummy)
 #else
@@ -318,64 +415,49 @@ afsinit(struct vfssw *vfsswp, int fstype)
     afs_orig_ioctl32 = sysent32[SYS_ioctl].sy_call;
     sysent32[SYS_setgroups].sy_callc = afs_xsetgroups;
     sysent32[SYS_ioctl].sy_call = afs_xioctl;
-#endif
+#endif /* AFS_SUN57_64BIT_ENV */
 
 #ifdef AFS_SUN510_ENV
     vfs_setfsops(fstype, afs_vfsops_template, &afs_vfsopsp);
     afsfstype = fstype;
     vn_make_ops("afs", afs_vnodeops_template, &afs_ops);
-#else
+#else /* !AFS_SUN510_ENV */
     vfsswp->vsw_vfsops = &Afs_vfsops;
     afsfstype = fstype;
-#endif
+#endif /* !AFS_SUN510_ENV */
 
 
-#if    !defined(AFS_NONFSTRANS)
-    nfs_rfsdisptab_v2 = (int (*)())modlookup("nfssrv", "rfsdisptab_v2");
-    if (!nfs_rfsdisptab_v2) {
-       afs_warn("warning : rfsdisptab_v2 NOT FOUND\n");
-    }
-    if (nfs_rfsdisptab_v2) {
-       nfs_acldisptab_v2 = (int (*)())modlookup("nfssrv", "acldisptab_v2");
-       if (!nfs_acldisptab_v2) {
-           afs_warn("warning : acldisptab_v2 NOT FOUND\n");
-       } else {
+#if !defined(AFS_NONFSTRANS)
+    nfs_rfsdisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v2");
+    if (nfs_rfsdisptab_v2 != NULL) {
+       nfs_acldisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v2");
+       if (nfs_acldisptab_v2 != NULL) {
            afs_xlatorinit_v2(nfs_rfsdisptab_v2, nfs_acldisptab_v2);
        }
     }
-    nfs_rfsdisptab_v3 = (int (*)())modlookup("nfssrv", "rfsdisptab_v3");
-    if (!nfs_rfsdisptab_v3) {
-       afs_warn("warning : rfsdisptab_v3 NOT FOUND\n");
-    }
-    if (nfs_rfsdisptab_v3) {
-       nfs_acldisptab_v3 = (int (*)())modlookup("nfssrv", "acldisptab_v3");
-       if (!nfs_acldisptab_v3) {
-           afs_warn("warning : acldisptab_v3 NOT FOUND\n");
-       } else {
+    nfs_rfsdisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v3");
+    if (nfs_rfsdisptab_v3 != NULL) {
+       nfs_acldisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v3");
+       if (nfs_acldisptab_v3 != NULL) {
            afs_xlatorinit_v3(nfs_rfsdisptab_v3, nfs_acldisptab_v3);
        }
     }
 
-    nfs_checkauth = (int (*)())modlookup("nfssrv", "checkauth");
-    if (!nfs_checkauth)
-       afs_warn("nfs_checkauth not initialised");
-#endif
-    ufs_iallocp = (int (*)())modlookup("ufs", "ufs_ialloc");
-    ufs_iupdatp = (void (*)())modlookup("ufs", "ufs_iupdat");
-    ufs_igetp = (int (*)())modlookup("ufs", "ufs_iget");
-    ufs_itimes_nolockp = (void (*)())modlookup("ufs", "ufs_itimes_nolock");
-    udp_infop = (struct streamtab *)modlookup("udp", "udpinfo");
-#ifdef AFS_SUN510_ENV
-    if (!ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp || !ufs_igetp
-       || !udp_infop)
-       afs_warn("AFS to UFS mapping cannot be fully initialised\n");
-#else
-    ill_g_headp = (struct ill_s *)modlookup("ip", "ill_g_head");
+    nfs_checkauth = (int (*)()) do_mod_lookup("nfssrv", "checkauth");
+#endif /* !AFS_NONFSTRANS */
 
-    if (!ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp || !ufs_igetp
-       || !udp_infop || !ill_g_headp)
+    ufs_iallocp = (int (*)()) do_mod_lookup("ufs", "ufs_ialloc");
+    ufs_iupdatp = (void (*)()) do_mod_lookup("ufs", "ufs_iupdat");
+    ufs_igetp = (int (*)()) do_mod_lookup("ufs", "ufs_iget");
+    ufs_itimes_nolockp = (void (*)()) do_mod_lookup("ufs", "ufs_itimes_nolock");
+
+    if (!ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp || !ufs_igetp) {
        afs_warn("AFS to UFS mapping cannot be fully initialised\n");
-#endif
+    }
+
+#if !defined(AFS_SUN510_ENV)
+    ill_g_headp = (struct ill_s *) do_mod_lookup("ip", "ill_g_head");
+#endif /* !AFS_SUN510_ENV */
 
     afs_sinited = 1;
     return 0;
@@ -383,12 +465,22 @@ afsinit(struct vfssw *vfsswp, int fstype)
 }
 
 #ifdef AFS_SUN510_ENV
+#ifdef AFS_SUN511_ENV
+static vfsdef_t afs_vfsdef = {
+    VFSDEF_VERSION,
+    "afs",
+    afsinit,
+    0,
+    NULL
+};
+#else
 static struct vfsdef_v3 afs_vfsdef = {
     VFSDEF_VERSION,
     "afs",
     afsinit,
     0
 };
+#endif
 #else
 static struct vfssw afs_vfw = {
     "afs",