rpc: store pointer to pipe inode in gss upcall message
[safe/jmp/linux-2.6] / net / sunrpc / auth_gss / auth_gss.c
index 5080908..fe06acd 100644 (file)
@@ -33,8 +33,6 @@
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * $Id$
  */
 
 
 #include <linux/sunrpc/gss_api.h>
 #include <asm/uaccess.h>
 
-static struct rpc_authops authgss_ops;
+static const struct rpc_authops authgss_ops;
 
-static struct rpc_credops gss_credops;
+static const struct rpc_credops gss_credops;
+static const struct rpc_credops gss_nullops;
 
 #ifdef RPC_DEBUG
 # define RPCDBG_FACILITY       RPCDBG_AUTH
 #endif
 
-#define NFS_NGROUPS    16
-
-#define GSS_CRED_EXPIRE                (60 * HZ)       /* XXX: reasonable? */
-#define GSS_CRED_SLACK         1024            /* XXX: unused */
+#define GSS_CRED_SLACK         1024
 /* length of a krb5 verifier (48), plus data added before arguments when
  * using integrity (two 4-byte integers): */
 #define GSS_VERF_SLACK         100
 
-/* XXX this define must match the gssd define
-* as it is passed to gssd to signal the use of
-* machine creds should be part of the shared rpc interface */
-
-#define CA_RUN_AS_MACHINE  0x00000200
-
-/* dump the buffer in `emacs-hexl' style */
-#define isprint(c)      ((c > 0x1f) && (c < 0x7f))
-
-static DEFINE_RWLOCK(gss_ctx_lock);
-
 struct gss_auth {
+       struct kref kref;
        struct rpc_auth rpc_auth;
        struct gss_api_mech *mech;
        enum rpc_gss_svc service;
@@ -89,7 +75,14 @@ struct gss_auth {
        struct dentry *dentry;
 };
 
-static void gss_destroy_ctx(struct gss_cl_ctx *);
+/* pipe_version >= 0 if and only if someone has a pipe open. */
+static int pipe_version = -1;
+static atomic_t pipe_users = ATOMIC_INIT(0);
+static DEFINE_SPINLOCK(pipe_version_lock);
+static struct rpc_wait_queue pipe_version_rpc_waitqueue;
+static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
+
+static void gss_free_ctx(struct gss_cl_ctx *);
 static struct rpc_pipe_ops gss_upcall_ops;
 
 static inline struct gss_cl_ctx *
@@ -103,35 +96,26 @@ static inline void
 gss_put_ctx(struct gss_cl_ctx *ctx)
 {
        if (atomic_dec_and_test(&ctx->count))
-               gss_destroy_ctx(ctx);
+               gss_free_ctx(ctx);
 }
 
+/* gss_cred_set_ctx:
+ * called by gss_upcall_callback and gss_create_upcall in order
+ * to set the gss context. The actual exchange of an old context
+ * and a new one is protected by the inode->i_lock.
+ */
 static void
 gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
 {
        struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
-       struct gss_cl_ctx *old;
-       write_lock(&gss_ctx_lock);
-       old = gss_cred->gc_ctx;
-       gss_cred->gc_ctx = ctx;
-       cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
-       cred->cr_flags &= ~RPCAUTH_CRED_NEW;
-       write_unlock(&gss_ctx_lock);
-       if (old)
-               gss_put_ctx(old);
-}
-
-static int
-gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
-{
-       struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
-       int res = 0;
 
-       read_lock(&gss_ctx_lock);
-       if ((cred->cr_flags & RPCAUTH_CRED_UPTODATE) && gss_cred->gc_ctx)
-               res = 1;
-       read_unlock(&gss_ctx_lock);
-       return res;
+       if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
+               return;
+       gss_get_ctx(ctx);
+       rcu_assign_pointer(gss_cred->gc_ctx, ctx);
+       set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
+       smp_mb__before_clear_bit();
+       clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
 }
 
 static const void *
@@ -156,7 +140,7 @@ simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
        q = (const void *)((const char *)p + len);
        if (unlikely(q > end || q < p))
                return ERR_PTR(-EFAULT);
-       dest->data = kmemdup(p, len, GFP_KERNEL);
+       dest->data = kmemdup(p, len, GFP_NOFS);
        if (unlikely(dest->data == NULL))
                return ERR_PTR(-ENOMEM);
        dest->len = len;
@@ -169,10 +153,10 @@ gss_cred_get_ctx(struct rpc_cred *cred)
        struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
        struct gss_cl_ctx *ctx = NULL;
 
-       read_lock(&gss_ctx_lock);
+       rcu_read_lock();
        if (gss_cred->gc_ctx)
                ctx = gss_get_ctx(gss_cred->gc_ctx);
-       read_unlock(&gss_ctx_lock);
+       rcu_read_unlock();
        return ctx;
 }
 
@@ -181,7 +165,7 @@ gss_alloc_context(void)
 {
        struct gss_cl_ctx *ctx;
 
-       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
        if (ctx != NULL) {
                ctx->gc_proc = RPC_GSS_PROC_DATA;
                ctx->gc_seq = 1;        /* NetApp 6.4R1 doesn't accept seq. no. 0 */
@@ -250,19 +234,44 @@ struct gss_upcall_msg {
        struct rpc_pipe_msg msg;
        struct list_head list;
        struct gss_auth *auth;
+       struct rpc_inode *inode;
        struct rpc_wait_queue rpc_waitqueue;
        wait_queue_head_t waitqueue;
        struct gss_cl_ctx *ctx;
 };
 
+static int get_pipe_version(void)
+{
+       int ret;
+
+       spin_lock(&pipe_version_lock);
+       if (pipe_version >= 0) {
+               atomic_inc(&pipe_users);
+               ret = 0;
+       } else
+               ret = -EAGAIN;
+       spin_unlock(&pipe_version_lock);
+       return ret;
+}
+
+static void put_pipe_version(void)
+{
+       if (atomic_dec_and_lock(&pipe_users, &pipe_version_lock)) {
+               pipe_version = -1;
+               spin_unlock(&pipe_version_lock);
+       }
+}
+
 static void
 gss_release_msg(struct gss_upcall_msg *gss_msg)
 {
        if (!atomic_dec_and_test(&gss_msg->count))
                return;
+       put_pipe_version();
        BUG_ON(!list_empty(&gss_msg->list));
        if (gss_msg->ctx != NULL)
                gss_put_ctx(gss_msg->ctx);
+       rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
        kfree(gss_msg);
 }
 
@@ -281,15 +290,15 @@ __gss_find_upcall(struct rpc_inode *rpci, uid_t uid)
        return NULL;
 }
 
-/* Try to add a upcall to the pipefs queue.
+/* Try to add an upcall to the pipefs queue.
  * If an upcall owned by our uid already exists, then we return a reference
  * to that upcall instead of adding the new upcall.
  */
 static inline struct gss_upcall_msg *
 gss_add_msg(struct gss_auth *gss_auth, struct gss_upcall_msg *gss_msg)
 {
-       struct inode *inode = gss_auth->dentry->d_inode;
-       struct rpc_inode *rpci = RPC_I(inode);
+       struct rpc_inode *rpci = gss_msg->inode;
+       struct inode *inode = &rpci->vfs_inode;
        struct gss_upcall_msg *old;
 
        spin_lock(&inode->i_lock);
@@ -315,8 +324,7 @@ __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
 static void
 gss_unhash_msg(struct gss_upcall_msg *gss_msg)
 {
-       struct gss_auth *gss_auth = gss_msg->auth;
-       struct inode *inode = gss_auth->dentry->d_inode;
+       struct inode *inode = &gss_msg->inode->vfs_inode;
 
        if (list_empty(&gss_msg->list))
                return;
@@ -332,13 +340,13 @@ gss_upcall_callback(struct rpc_task *task)
        struct gss_cred *gss_cred = container_of(task->tk_msg.rpc_cred,
                        struct gss_cred, gc_base);
        struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
-       struct inode *inode = gss_msg->auth->dentry->d_inode;
+       struct inode *inode = &gss_msg->inode->vfs_inode;
 
+       spin_lock(&inode->i_lock);
        if (gss_msg->ctx)
-               gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_get_ctx(gss_msg->ctx));
+               gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_msg->ctx);
        else
                task->tk_status = gss_msg->msg.errno;
-       spin_lock(&inode->i_lock);
        gss_cred->gc_upcall = NULL;
        rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
        spin_unlock(&inode->i_lock);
@@ -349,32 +357,47 @@ static inline struct gss_upcall_msg *
 gss_alloc_msg(struct gss_auth *gss_auth, uid_t uid)
 {
        struct gss_upcall_msg *gss_msg;
+       int vers;
 
-       gss_msg = kzalloc(sizeof(*gss_msg), GFP_KERNEL);
-       if (gss_msg != NULL) {
-               INIT_LIST_HEAD(&gss_msg->list);
-               rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
-               init_waitqueue_head(&gss_msg->waitqueue);
-               atomic_set(&gss_msg->count, 1);
-               gss_msg->msg.data = &gss_msg->uid;
-               gss_msg->msg.len = sizeof(gss_msg->uid);
-               gss_msg->uid = uid;
-               gss_msg->auth = gss_auth;
+       gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
+       if (gss_msg == NULL)
+               return ERR_PTR(-ENOMEM);
+       vers = get_pipe_version();
+       if (vers < 0) {
+               kfree(gss_msg);
+               return ERR_PTR(vers);
        }
+       gss_msg->inode = RPC_I(gss_auth->dentry->d_inode);
+       INIT_LIST_HEAD(&gss_msg->list);
+       rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
+       init_waitqueue_head(&gss_msg->waitqueue);
+       atomic_set(&gss_msg->count, 1);
+       gss_msg->msg.data = &gss_msg->uid;
+       gss_msg->msg.len = sizeof(gss_msg->uid);
+       gss_msg->uid = uid;
+       gss_msg->auth = gss_auth;
        return gss_msg;
 }
 
 static struct gss_upcall_msg *
 gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
 {
+       struct gss_cred *gss_cred = container_of(cred,
+                       struct gss_cred, gc_base);
        struct gss_upcall_msg *gss_new, *gss_msg;
+       uid_t uid = cred->cr_uid;
 
-       gss_new = gss_alloc_msg(gss_auth, cred->cr_uid);
-       if (gss_new == NULL)
-               return ERR_PTR(-ENOMEM);
+       /* Special case: rpc.gssd assumes that uid == 0 implies machine creds */
+       if (gss_cred->gc_machine_cred != 0)
+               uid = 0;
+
+       gss_new = gss_alloc_msg(gss_auth, uid);
+       if (IS_ERR(gss_new))
+               return gss_new;
        gss_msg = gss_add_msg(gss_auth, gss_new);
        if (gss_msg == gss_new) {
-               int res = rpc_queue_upcall(gss_auth->dentry->d_inode, &gss_new->msg);
+               struct inode *inode = &gss_new->inode->vfs_inode;
+               int res = rpc_queue_upcall(inode, &gss_new->msg);
                if (res) {
                        gss_unhash_msg(gss_new);
                        gss_msg = ERR_PTR(res);
@@ -384,6 +407,18 @@ gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cr
        return gss_msg;
 }
 
+static void warn_gssd(void)
+{
+       static unsigned long ratelimit;
+       unsigned long now = jiffies;
+
+       if (time_after(now, ratelimit)) {
+               printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
+                               "Please check user daemon is running.\n");
+               ratelimit = now + 15*HZ;
+       }
+}
+
 static inline int
 gss_refresh_upcall(struct rpc_task *task)
 {
@@ -393,25 +428,38 @@ gss_refresh_upcall(struct rpc_task *task)
        struct gss_cred *gss_cred = container_of(cred,
                        struct gss_cred, gc_base);
        struct gss_upcall_msg *gss_msg;
-       struct inode *inode = gss_auth->dentry->d_inode;
+       struct inode *inode;
        int err = 0;
 
        dprintk("RPC: %5u gss_refresh_upcall for uid %u\n", task->tk_pid,
                                                                cred->cr_uid);
        gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
+       if (IS_ERR(gss_msg) == -EAGAIN) {
+               /* XXX: warning on the first, under the assumption we
+                * shouldn't normally hit this case on a refresh. */
+               warn_gssd();
+               task->tk_timeout = 15*HZ;
+               rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
+               return 0;
+       }
        if (IS_ERR(gss_msg)) {
                err = PTR_ERR(gss_msg);
                goto out;
        }
+       inode = &gss_msg->inode->vfs_inode;
        spin_lock(&inode->i_lock);
        if (gss_cred->gc_upcall != NULL)
-               rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL, NULL);
-       else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
+               rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
+       else if (gss_msg->ctx != NULL) {
+               gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_msg->ctx);
+               gss_cred->gc_upcall = NULL;
+               rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
+       } else if (gss_msg->msg.errno >= 0) {
                task->tk_timeout = 0;
                gss_cred->gc_upcall = gss_msg;
                /* gss_upcall_callback will release the reference to gss_upcall_msg */
                atomic_inc(&gss_msg->count);
-               rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback, NULL);
+               rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
        } else
                err = gss_msg->msg.errno;
        spin_unlock(&inode->i_lock);
@@ -425,23 +473,33 @@ out:
 static inline int
 gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
 {
-       struct inode *inode = gss_auth->dentry->d_inode;
+       struct inode *inode;
        struct rpc_cred *cred = &gss_cred->gc_base;
        struct gss_upcall_msg *gss_msg;
        DEFINE_WAIT(wait);
        int err = 0;
 
        dprintk("RPC:       gss_upcall for uid %u\n", cred->cr_uid);
+retry:
        gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
+       if (PTR_ERR(gss_msg) == -EAGAIN) {
+               err = wait_event_interruptible_timeout(pipe_version_waitqueue,
+                               pipe_version >= 0, 15*HZ);
+               if (err)
+                       goto out;
+               if (pipe_version < 0)
+                       warn_gssd();
+               goto retry;
+       }
        if (IS_ERR(gss_msg)) {
                err = PTR_ERR(gss_msg);
                goto out;
        }
+       inode = &gss_msg->inode->vfs_inode;
        for (;;) {
                prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_INTERRUPTIBLE);
                spin_lock(&inode->i_lock);
                if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
-                       spin_unlock(&inode->i_lock);
                        break;
                }
                spin_unlock(&inode->i_lock);
@@ -452,9 +510,10 @@ gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
                schedule();
        }
        if (gss_msg->ctx)
-               gss_cred_set_ctx(cred, gss_get_ctx(gss_msg->ctx));
+               gss_cred_set_ctx(cred, gss_msg->ctx);
        else
                err = gss_msg->msg.errno;
+       spin_unlock(&inode->i_lock);
 out_intr:
        finish_wait(&gss_msg->waitqueue, &wait);
        gss_release_msg(gss_msg);
@@ -469,16 +528,15 @@ gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
                char __user *dst, size_t buflen)
 {
        char *data = (char *)msg->data + msg->copied;
-       ssize_t mlen = msg->len;
-       ssize_t left;
+       size_t mlen = min(msg->len, buflen);
+       unsigned long left;
 
-       if (mlen > buflen)
-               mlen = buflen;
        left = copy_to_user(dst, data, mlen);
-       if (left < 0) {
-               msg->errno = left;
-               return left;
+       if (left == mlen) {
+               msg->errno = -EFAULT;
+               return -EFAULT;
        }
+
        mlen -= left;
        msg->copied += mlen;
        msg->errno = 0;
@@ -492,7 +550,6 @@ gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
 {
        const void *p, *end;
        void *buf;
-       struct rpc_clnt *clnt;
        struct gss_upcall_msg *gss_msg;
        struct inode *inode = filp->f_path.dentry->d_inode;
        struct gss_cl_ctx *ctx;
@@ -502,11 +559,10 @@ gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
        if (mlen > MSG_BUF_MAXSIZE)
                goto out;
        err = -ENOMEM;
-       buf = kmalloc(mlen, GFP_KERNEL);
+       buf = kmalloc(mlen, GFP_NOFS);
        if (!buf)
                goto out;
 
-       clnt = RPC_I(inode)->private;
        err = -EFAULT;
        if (copy_from_user(buf, src, mlen))
                goto err;
@@ -537,7 +593,7 @@ gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
        p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
        if (IS_ERR(p)) {
                err = PTR_ERR(p);
-               gss_msg->msg.errno = (err == -EACCES) ? -EACCES : -EAGAIN;
+               gss_msg->msg.errno = (err == -EAGAIN) ? -EAGAIN : -EACCES;
                goto err_release_msg;
        }
        gss_msg->ctx = gss_get_ctx(ctx);
@@ -557,6 +613,20 @@ out:
        return err;
 }
 
+static int
+gss_pipe_open(struct inode *inode)
+{
+       spin_lock(&pipe_version_lock);
+       if (pipe_version < 0) {
+               pipe_version = 0;
+               rpc_wake_up(&pipe_version_rpc_waitqueue);
+               wake_up(&pipe_version_waitqueue);
+       }
+       atomic_inc(&pipe_users);
+       spin_unlock(&pipe_version_lock);
+       return 0;
+}
+
 static void
 gss_pipe_release(struct inode *inode)
 {
@@ -576,27 +646,22 @@ gss_pipe_release(struct inode *inode)
                spin_lock(&inode->i_lock);
        }
        spin_unlock(&inode->i_lock);
+
+       put_pipe_version();
 }
 
 static void
 gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
 {
        struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
-       static unsigned long ratelimit;
 
        if (msg->errno < 0) {
                dprintk("RPC:       gss_pipe_destroy_msg releasing msg %p\n",
                                gss_msg);
                atomic_inc(&gss_msg->count);
                gss_unhash_msg(gss_msg);
-               if (msg->errno == -ETIMEDOUT) {
-                       unsigned long now = jiffies;
-                       if (time_after(now, ratelimit)) {
-                               printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
-                                                   "Please check user daemon is running!\n");
-                               ratelimit = now + 15*HZ;
-                       }
-               }
+               if (msg->errno == -ETIMEDOUT)
+                       warn_gssd();
                gss_release_msg(gss_msg);
        }
 }
@@ -622,8 +687,8 @@ gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
        err = -EINVAL;
        gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
        if (!gss_auth->mech) {
-               printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
-                               __FUNCTION__, flavor);
+               printk(KERN_WARNING "%s: Pseudoflavor %d not found!\n",
+                               __func__, flavor);
                goto err_free;
        }
        gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
@@ -635,10 +700,7 @@ gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
        auth->au_ops = &authgss_ops;
        auth->au_flavor = flavor;
        atomic_set(&auth->au_count, 1);
-
-       err = rpcauth_init_credcache(auth, GSS_CRED_EXPIRE);
-       if (err)
-               goto err_put_mech;
+       kref_init(&gss_auth->kref);
 
        gss_auth->dentry = rpc_mkpipe(clnt->cl_dentry, gss_auth->mech->gm_name,
                        clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
@@ -647,7 +709,13 @@ gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
                goto err_put_mech;
        }
 
+       err = rpcauth_init_credcache(auth);
+       if (err)
+               goto err_unlink_pipe;
+
        return auth;
+err_unlink_pipe:
+       rpc_unlink(gss_auth->dentry);
 err_put_mech:
        gss_mech_put(gss_auth->mech);
 err_free:
@@ -658,6 +726,24 @@ out_dec:
 }
 
 static void
+gss_free(struct gss_auth *gss_auth)
+{
+       rpc_unlink(gss_auth->dentry);
+       gss_mech_put(gss_auth->mech);
+
+       kfree(gss_auth);
+       module_put(THIS_MODULE);
+}
+
+static void
+gss_free_callback(struct kref *kref)
+{
+       struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
+
+       gss_free(gss_auth);
+}
+
+static void
 gss_destroy(struct rpc_auth *auth)
 {
        struct gss_auth *gss_auth;
@@ -665,41 +751,110 @@ gss_destroy(struct rpc_auth *auth)
        dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
                        auth, auth->au_flavor);
 
+       rpcauth_destroy_credcache(auth);
+
        gss_auth = container_of(auth, struct gss_auth, rpc_auth);
-       rpc_unlink(gss_auth->dentry);
-       gss_auth->dentry = NULL;
-       gss_mech_put(gss_auth->mech);
+       kref_put(&gss_auth->kref, gss_free_callback);
+}
 
-       rpcauth_free_credcache(auth);
-       kfree(gss_auth);
-       module_put(THIS_MODULE);
+/*
+ * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
+ * to the server with the GSS control procedure field set to
+ * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
+ * all RPCSEC_GSS state associated with that context.
+ */
+static int
+gss_destroying_context(struct rpc_cred *cred)
+{
+       struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
+       struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
+       struct rpc_task *task;
+
+       if (gss_cred->gc_ctx == NULL ||
+           test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
+               return 0;
+
+       gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
+       cred->cr_ops = &gss_nullops;
+
+       /* Take a reference to ensure the cred will be destroyed either
+        * by the RPC call or by the put_rpccred() below */
+       get_rpccred(cred);
+
+       task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
+       if (!IS_ERR(task))
+               rpc_put_task(task);
+
+       put_rpccred(cred);
+       return 1;
 }
 
-/* gss_destroy_cred (and gss_destroy_ctx) are used to clean up after failure
+/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
  * to create a new cred or context, so they check that things have been
  * allocated before freeing them. */
 static void
-gss_destroy_ctx(struct gss_cl_ctx *ctx)
+gss_do_free_ctx(struct gss_cl_ctx *ctx)
 {
-       dprintk("RPC:       gss_destroy_ctx\n");
-
-       if (ctx->gc_gss_ctx)
-               gss_delete_sec_context(&ctx->gc_gss_ctx);
+       dprintk("RPC:       gss_free_ctx\n");
 
        kfree(ctx->gc_wire_ctx.data);
        kfree(ctx);
 }
 
 static void
-gss_destroy_cred(struct rpc_cred *rc)
+gss_free_ctx_callback(struct rcu_head *head)
+{
+       struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
+       gss_do_free_ctx(ctx);
+}
+
+static void
+gss_free_ctx(struct gss_cl_ctx *ctx)
+{
+       struct gss_ctx *gc_gss_ctx;
+
+       gc_gss_ctx = rcu_dereference(ctx->gc_gss_ctx);
+       rcu_assign_pointer(ctx->gc_gss_ctx, NULL);
+       call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
+       if (gc_gss_ctx)
+               gss_delete_sec_context(&gc_gss_ctx);
+}
+
+static void
+gss_free_cred(struct gss_cred *gss_cred)
+{
+       dprintk("RPC:       gss_free_cred %p\n", gss_cred);
+       kfree(gss_cred);
+}
+
+static void
+gss_free_cred_callback(struct rcu_head *head)
+{
+       struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
+       gss_free_cred(gss_cred);
+}
+
+static void
+gss_destroy_nullcred(struct rpc_cred *cred)
 {
-       struct gss_cred *cred = container_of(rc, struct gss_cred, gc_base);
+       struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
+       struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
+       struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
+
+       rcu_assign_pointer(gss_cred->gc_ctx, NULL);
+       call_rcu(&cred->cr_rcu, gss_free_cred_callback);
+       if (ctx)
+               gss_put_ctx(ctx);
+       kref_put(&gss_auth->kref, gss_free_callback);
+}
 
-       dprintk("RPC:       gss_destroy_cred \n");
+static void
+gss_destroy_cred(struct rpc_cred *cred)
+{
 
-       if (cred->gc_ctx)
-               gss_put_ctx(cred->gc_ctx);
-       kfree(cred);
+       if (gss_destroying_context(cred))
+               return;
+       gss_destroy_nullcred(cred);
 }
 
 /*
@@ -721,20 +876,18 @@ gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
        dprintk("RPC:       gss_create_cred for uid %d, flavor %d\n",
                acred->uid, auth->au_flavor);
 
-       if (!(cred = kzalloc(sizeof(*cred), GFP_KERNEL)))
+       if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
                goto out_err;
 
-       atomic_set(&cred->gc_count, 1);
-       cred->gc_uid = acred->uid;
+       rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
        /*
         * Note: in order to force a call to call_refresh(), we deliberately
         * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
         */
-       cred->gc_flags = 0;
-       cred->gc_base.cr_auth = auth;
-       cred->gc_base.cr_ops = &gss_credops;
-       cred->gc_base.cr_flags = RPCAUTH_CRED_NEW;
+       cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
        cred->gc_service = gss_auth->service;
+       cred->gc_machine_cred = acred->machine_cred;
+       kref_get(&gss_auth->kref);
        return &cred->gc_base;
 
 out_err:
@@ -760,17 +913,16 @@ gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
 {
        struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
 
-       /*
-        * If the searchflags have set RPCAUTH_LOOKUP_NEW, then
-        * we don't really care if the credential has expired or not,
-        * since the caller should be prepared to reinitialise it.
-        */
-       if ((flags & RPCAUTH_LOOKUP_NEW) && (rc->cr_flags & RPCAUTH_CRED_NEW))
+       if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
                goto out;
        /* Don't match with creds that have expired. */
-       if (gss_cred->gc_ctx && time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
+       if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
+               return 0;
+       if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
                return 0;
 out:
+       if (acred->machine_cred != gss_cred->gc_machine_cred)
+               return 0;
        return (rc->cr_uid == acred->uid);
 }
 
@@ -821,7 +973,7 @@ gss_marshal(struct rpc_task *task, __be32 *p)
        mic.data = (u8 *)(p + 1);
        maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
        if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
        } else if (maj_stat != 0) {
                printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
                goto out_put_ctx;
@@ -834,16 +986,55 @@ out_put_ctx:
        return NULL;
 }
 
+static int gss_renew_cred(struct rpc_task *task)
+{
+       struct rpc_cred *oldcred = task->tk_msg.rpc_cred;
+       struct gss_cred *gss_cred = container_of(oldcred,
+                                                struct gss_cred,
+                                                gc_base);
+       struct rpc_auth *auth = oldcred->cr_auth;
+       struct auth_cred acred = {
+               .uid = oldcred->cr_uid,
+               .machine_cred = gss_cred->gc_machine_cred,
+       };
+       struct rpc_cred *new;
+
+       new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
+       if (IS_ERR(new))
+               return PTR_ERR(new);
+       task->tk_msg.rpc_cred = new;
+       put_rpccred(oldcred);
+       return 0;
+}
+
 /*
 * Refresh credentials. XXX - finish
 */
 static int
 gss_refresh(struct rpc_task *task)
 {
+       struct rpc_cred *cred = task->tk_msg.rpc_cred;
+       int ret = 0;
+
+       if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
+                       !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
+               ret = gss_renew_cred(task);
+               if (ret < 0)
+                       goto out;
+               cred = task->tk_msg.rpc_cred;
+       }
 
-       if (!gss_cred_is_uptodate_ctx(task->tk_msg.rpc_cred))
-               return gss_refresh_upcall(task);
-       return 0;
+       if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
+               ret = gss_refresh_upcall(task);
+out:
+       return ret;
+}
+
+/* Dummy refresh routine: used only when destroying the context */
+static int
+gss_refresh_null(struct rpc_task *task)
+{
+       return -EACCES;
 }
 
 static __be32 *
@@ -874,12 +1065,15 @@ gss_validate(struct rpc_task *task, __be32 *p)
 
        maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
        if (maj_stat == GSS_S_CONTEXT_EXPIRED)
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
-       if (maj_stat)
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
+       if (maj_stat) {
+               dprintk("RPC: %5u gss_validate: gss_verify_mic returned "
+                               "error 0x%08x\n", task->tk_pid, maj_stat);
                goto out_bad;
+       }
        /* We leave it to unwrap to calculate au_rslack. For now we just
         * calculate the length of the verifier: */
-       task->tk_auth->au_verfsize = XDR_QUADLEN(len) + 2;
+       cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
        gss_put_ctx(ctx);
        dprintk("RPC: %5u gss_validate: gss_verify_mic succeeded.\n",
                        task->tk_pid);
@@ -928,7 +1122,7 @@ gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
        maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
        status = -EIO; /* XXX? */
        if (maj_stat == GSS_S_CONTEXT_EXPIRED)
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
        else if (maj_stat)
                return status;
        q = xdr_encode_opaque(p, NULL, mic.len);
@@ -1027,7 +1221,7 @@ gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
        /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
         * done anyway, so it's safe to put the request on the wire: */
        if (maj_stat == GSS_S_CONTEXT_EXPIRED)
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
        else if (maj_stat)
                return status;
 
@@ -1114,7 +1308,7 @@ gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
 
        maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
        if (maj_stat == GSS_S_CONTEXT_EXPIRED)
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
        if (maj_stat != GSS_S_COMPLETE)
                return status;
        return 0;
@@ -1139,7 +1333,7 @@ gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
 
        maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
        if (maj_stat == GSS_S_CONTEXT_EXPIRED)
-               cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
+               clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
        if (maj_stat != GSS_S_COMPLETE)
                return status;
        if (ntohl(*(*p)++) != rqstp->rq_seqno)
@@ -1179,7 +1373,7 @@ gss_unwrap_resp(struct rpc_task *task,
                        break;
        }
        /* take into account extra slack for integrity and privacy cases: */
-       task->tk_auth->au_rslack = task->tk_auth->au_verfsize + (p - savedp)
+       cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
                                                + (savedlen - head->iov_len);
 out_decode:
        status = decode(rqstp, p, obj);
@@ -1190,22 +1384,21 @@ out:
        return status;
 }
 
-static struct rpc_authops authgss_ops = {
+static const struct rpc_authops authgss_ops = {
        .owner          = THIS_MODULE,
        .au_flavor      = RPC_AUTH_GSS,
-#ifdef RPC_DEBUG
        .au_name        = "RPCSEC_GSS",
-#endif
        .create         = gss_create,
        .destroy        = gss_destroy,
        .lookup_cred    = gss_lookup_cred,
        .crcreate       = gss_create_cred
 };
 
-static struct rpc_credops gss_credops = {
+static const struct rpc_credops gss_credops = {
        .cr_name        = "AUTH_GSS",
        .crdestroy      = gss_destroy_cred,
        .cr_init        = gss_cred_init,
+       .crbind         = rpcauth_generic_bind_cred,
        .crmatch        = gss_match,
        .crmarshal      = gss_marshal,
        .crrefresh      = gss_refresh,
@@ -1214,10 +1407,23 @@ static struct rpc_credops gss_credops = {
        .crunwrap_resp  = gss_unwrap_resp,
 };
 
+static const struct rpc_credops gss_nullops = {
+       .cr_name        = "AUTH_GSS",
+       .crdestroy      = gss_destroy_nullcred,
+       .crbind         = rpcauth_generic_bind_cred,
+       .crmatch        = gss_match,
+       .crmarshal      = gss_marshal,
+       .crrefresh      = gss_refresh_null,
+       .crvalidate     = gss_validate,
+       .crwrap_req     = gss_wrap_req,
+       .crunwrap_resp  = gss_unwrap_resp,
+};
+
 static struct rpc_pipe_ops gss_upcall_ops = {
        .upcall         = gss_pipe_upcall,
        .downcall       = gss_pipe_downcall,
        .destroy_msg    = gss_pipe_destroy_msg,
+       .open_pipe      = gss_pipe_open,
        .release_pipe   = gss_pipe_release,
 };
 
@@ -1234,6 +1440,7 @@ static int __init init_rpcsec_gss(void)
        err = gss_svc_init();
        if (err)
                goto out_unregister;
+       rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
        return 0;
 out_unregister:
        rpcauth_unregister(&authgss_ops);