nfsd4: make recall callback an asynchronous rpc
[safe/jmp/linux-2.6] / fs / nfsd / nfs4callback.c
index 32ffea0..f4fab69 100644 (file)
@@ -38,6 +38,8 @@
 #include <linux/inet.h>
 #include <linux/errno.h>
 #include <linux/delay.h>
+#include <linux/sched.h>
+#include <linux/kthread.h>
 #include <linux/sunrpc/xdr.h>
 #include <linux/sunrpc/svc.h>
 #include <linux/sunrpc/clnt.h>
@@ -51,9 +53,6 @@
 #define NFSPROC4_CB_NULL 0
 #define NFSPROC4_CB_COMPOUND 1
 
-/* declarations */
-static const struct rpc_call_ops nfs4_cb_null_ops;
-
 /* Index of predefined Linux callback client operations */
 
 enum {
@@ -74,7 +73,7 @@ enum nfs_cb_opnum4 {
 #define op_enc_sz                      1
 #define op_dec_sz                      2
 #define enc_nfs4_fh_sz                 (1 + (NFS4_FHSIZE >> 2))
-#define enc_stateid_sz                 16
+#define enc_stateid_sz                 (NFS4_STATEID_SIZE >> 2)
 #define NFS4_enc_cb_recall_sz          (cb_compound_enc_hdr_sz +       \
                                        1 + enc_stateid_sz +            \
                                        enc_nfs4_fh_sz)
@@ -102,7 +101,7 @@ xdr_writemem(__be32 *p, const void *ptr, int nbytes)
 } while (0)
 #define RESERVE_SPACE(nbytes)   do {                            \
        p = xdr_reserve_space(xdr, nbytes);                     \
-       if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
+       if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
        BUG_ON(!p);                                             \
 } while (0)
 
@@ -132,7 +131,7 @@ xdr_error:                                      \
        p = xdr_inline_decode(xdr, nbytes); \
        if (!p) { \
                dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
-                       __FUNCTION__, __LINE__); \
+                       __func__, __LINE__); \
                return -EIO; \
        } \
 } while (0)
@@ -216,17 +215,18 @@ encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
 }
 
 static int
-encode_cb_recall(struct xdr_stream *xdr, struct nfs4_cb_recall *cb_rec)
+encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp)
 {
        __be32 *p;
-       int len = cb_rec->cbr_fhlen;
+       int len = dp->dl_fh.fh_size;
 
-       RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
+       RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
        WRITE32(OP_CB_RECALL);
-       WRITEMEM(&cb_rec->cbr_stateid, sizeof(stateid_t));
-       WRITE32(cb_rec->cbr_trunc);
+       WRITE32(dp->dl_stateid.si_generation);
+       WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
+       WRITE32(0); /* truncate optimization not implemented */
        WRITE32(len);
-       WRITEMEM(cb_rec->cbr_fhval, len);
+       WRITEMEM(&dp->dl_fh.fh_base, len);
        return 0;
 }
 
@@ -241,11 +241,11 @@ nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
 }
 
 static int
-nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_cb_recall *args)
+nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_delegation *args)
 {
        struct xdr_stream xdr;
        struct nfs4_cb_compound_hdr hdr = {
-               .ident = args->cbr_ident,
+               .ident = args->dl_ident,
                .nops   = 1,
        };
 
@@ -342,62 +342,54 @@ static struct rpc_version *       nfs_cb_version[] = {
        &nfs_cb_version4,
 };
 
-/*
- * Use the SETCLIENTID credential
- */
-static struct rpc_cred *
-nfsd4_lookupcred(struct nfs4_client *clp, int taskflags)
+static struct rpc_program cb_program;
+
+static struct rpc_stat cb_stats = {
+               .program        = &cb_program
+};
+
+#define NFS4_CALLBACK 0x40000000
+static struct rpc_program cb_program = {
+               .name           = "nfs4_cb",
+               .number         = NFS4_CALLBACK,
+               .nrvers         = ARRAY_SIZE(nfs_cb_version),
+               .version        = nfs_cb_version,
+               .stats          = &cb_stats,
+               .pipe_dir_name  = "/nfsd4_cb",
+};
+
+static int max_cb_time(void)
 {
-        struct auth_cred acred;
-       struct rpc_clnt *clnt = clp->cl_callback.cb_client;
-       struct rpc_cred *ret;
-
-        get_group_info(clp->cl_cred.cr_group_info);
-        acred.uid = clp->cl_cred.cr_uid;
-        acred.gid = clp->cl_cred.cr_gid;
-        acred.group_info = clp->cl_cred.cr_group_info;
-
-        dprintk("NFSD:     looking up %s cred\n",
-                clnt->cl_auth->au_ops->au_name);
-        ret = rpcauth_lookup_credcache(clnt->cl_auth, &acred, taskflags);
-        put_group_info(clp->cl_cred.cr_group_info);
-        return ret;
+       return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
 }
 
-/*
- * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
- */
-void
-nfsd4_probe_callback(struct nfs4_client *clp)
+/* Reference counting, callback cleanup, etc., all look racy as heck.
+ * And why is cb_set an atomic? */
+
+int setup_callback_client(struct nfs4_client *clp)
 {
        struct sockaddr_in      addr;
-       struct nfs4_callback    *cb = &clp->cl_callback;
+       struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
        struct rpc_timeout      timeparms = {
-               .to_initval     = (NFSD_LEASE_TIME/4) * HZ,
-               .to_retries     = 5,
-               .to_maxval      = (NFSD_LEASE_TIME/2) * HZ,
-               .to_exponential = 1,
+               .to_initval     = max_cb_time(),
+               .to_retries     = 0,
        };
-       struct rpc_program *    program = &cb->cb_program;
        struct rpc_create_args args = {
                .protocol       = IPPROTO_TCP,
                .address        = (struct sockaddr *)&addr,
                .addrsize       = sizeof(addr),
                .timeout        = &timeparms,
-               .program        = program,
+               .program        = &cb_program,
+               .prognumber     = cb->cb_prog,
                .version        = nfs_cb_version[1]->number,
-               .authflavor     = RPC_AUTH_UNIX,        /* XXX: need AUTH_GSS... */
-               .flags          = (RPC_CLNT_CREATE_NOPING),
-       };
-       struct rpc_message msg = {
-               .rpc_proc       = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
-               .rpc_argp       = clp,
+               .authflavor     = clp->cl_flavor,
+               .flags          = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
+               .client_name    = clp->cl_principal,
        };
-       char clientname[16];
-       int status;
+       struct rpc_clnt *client;
 
-       if (atomic_read(&cb->cb_set))
-               return;
+       if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
+               return -EINVAL;
 
        /* Initialize address */
        memset(&addr, 0, sizeof(addr));
@@ -405,134 +397,166 @@ nfsd4_probe_callback(struct nfs4_client *clp)
        addr.sin_port = htons(cb->cb_port);
        addr.sin_addr.s_addr = htonl(cb->cb_addr);
 
-       /* Initialize rpc_program */
-       program->name = "nfs4_cb";
-       program->number = cb->cb_prog;
-       program->nrvers = ARRAY_SIZE(nfs_cb_version);
-       program->version = nfs_cb_version;
-       program->stats = &cb->cb_stat;
+       /* Create RPC client */
+       client = rpc_create(&args);
+       if (IS_ERR(client)) {
+               dprintk("NFSD: couldn't create callback client: %ld\n",
+                       PTR_ERR(client));
+               return PTR_ERR(client);
+       }
+       cb->cb_client = client;
+       return 0;
+
+}
 
-       /* Initialize rpc_stat */
-       memset(program->stats, 0, sizeof(cb->cb_stat));
-       program->stats->program = program;
+static void warn_no_callback_path(struct nfs4_client *clp, int reason)
+{
+       dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
+               (int)clp->cl_name.len, clp->cl_name.data, reason);
+}
 
-       /* Just here to make some printk's more useful: */
-       snprintf(clientname, sizeof(clientname),
-               "%u.%u.%u.%u", NIPQUAD(addr.sin_addr));
-       args.servername = clientname;
+static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
+{
+       struct nfs4_client *clp = calldata;
 
-       /* Create RPC client */
-       cb->cb_client = rpc_create(&args);
-       if (IS_ERR(cb->cb_client)) {
-               dprintk("NFSD: couldn't create callback client\n");
-               goto out_err;
+       if (task->tk_status)
+               warn_no_callback_path(clp, task->tk_status);
+       else
+               atomic_set(&clp->cl_cb_conn.cb_set, 1);
+       put_nfs4_client(clp);
+}
+
+static const struct rpc_call_ops nfsd4_cb_probe_ops = {
+       .rpc_call_done = nfsd4_cb_probe_done,
+};
+
+static struct rpc_cred *lookup_cb_cred(struct nfs4_cb_conn *cb)
+{
+       struct auth_cred acred = {
+               .machine_cred = 1
+       };
+
+       /*
+        * Note in the gss case this doesn't actually have to wait for a
+        * gss upcall (or any calls to the client); this just creates a
+        * non-uptodate cred which the rpc state machine will fill in with
+        * a refresh_upcall later.
+        */
+       return rpcauth_lookup_credcache(cb->cb_client->cl_auth, &acred,
+                                                       RPCAUTH_LOOKUP_NEW);
+}
+
+void do_probe_callback(struct nfs4_client *clp)
+{
+       struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
+       struct rpc_message msg = {
+               .rpc_proc       = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
+               .rpc_argp       = clp,
+       };
+       struct rpc_cred *cred;
+       int status;
+
+       cred = lookup_cb_cred(cb);
+       if (IS_ERR(cred)) {
+               status = PTR_ERR(cred);
+               goto out;
        }
+       cb->cb_cred = cred;
+       msg.rpc_cred = cb->cb_cred;
+       status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_SOFT,
+                               &nfsd4_cb_probe_ops, (void *)clp);
+out:
+       if (status) {
+               warn_no_callback_path(clp, status);
+               put_nfs4_client(clp);
+       }
+}
 
-       /* Kick rpciod, put the call on the wire. */
-       if (rpciod_up() != 0)
-               goto out_clnt;
+/*
+ * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
+ */
+void
+nfsd4_probe_callback(struct nfs4_client *clp)
+{
+       int status;
+
+       BUG_ON(atomic_read(&clp->cl_cb_conn.cb_set));
+
+       status = setup_callback_client(clp);
+       if (status) {
+               warn_no_callback_path(clp, status);
+               return;
+       }
 
        /* the task holds a reference to the nfs4_client struct */
        atomic_inc(&clp->cl_count);
 
-       msg.rpc_cred = nfsd4_lookupcred(clp,0);
-       if (IS_ERR(msg.rpc_cred))
-               goto out_rpciod;
-       status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_ASYNC, &nfs4_cb_null_ops, NULL);
-       put_rpccred(msg.rpc_cred);
+       do_probe_callback(clp);
+}
+
+static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
+{
+       struct nfs4_delegation *dp = calldata;
+       struct nfs4_client *clp = dp->dl_client;
 
-       if (status != 0) {
-               dprintk("NFSD: asynchronous NFSPROC4_CB_NULL failed!\n");
-               goto out_rpciod;
+       switch (task->tk_status) {
+       case -EIO:
+               /* Network partition? */
+               atomic_set(&clp->cl_cb_conn.cb_set, 0);
+               warn_no_callback_path(clp, task->tk_status);
+       case -EBADHANDLE:
+       case -NFS4ERR_BAD_STATEID:
+               /* Race: client probably got cb_recall
+                * before open reply granting delegation */
+               break;
+       default:
+               /* success, or error we can't handle */
+               return;
+       }
+       if (dp->dl_retries--) {
+               rpc_delay(task, 2*HZ);
+               task->tk_status = 0;
+               rpc_restart_call(task);
+       } else {
+               atomic_set(&clp->cl_cb_conn.cb_set, 0);
+               warn_no_callback_path(clp, task->tk_status);
        }
-       return;
-
-out_rpciod:
-       atomic_dec(&clp->cl_count);
-       rpciod_down();
-out_clnt:
-       rpc_shutdown_client(cb->cb_client);
-out_err:
-       cb->cb_client = NULL;
-       dprintk("NFSD: warning: no callback path to client %.*s\n",
-               (int)clp->cl_name.len, clp->cl_name.data);
 }
 
-static void
-nfs4_cb_null(struct rpc_task *task, void *dummy)
+static void nfsd4_cb_recall_release(void *calldata)
 {
-       struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
-       struct nfs4_callback *cb = &clp->cl_callback;
-       __be32 addr = htonl(cb->cb_addr);
-
-       dprintk("NFSD: nfs4_cb_null task->tk_status %d\n", task->tk_status);
+       struct nfs4_delegation *dp = calldata;
+       struct nfs4_client *clp = dp->dl_client;
 
-       if (task->tk_status < 0) {
-               dprintk("NFSD: callback establishment to client %.*s failed\n",
-                       (int)clp->cl_name.len, clp->cl_name.data);
-               goto out;
-       }
-       atomic_set(&cb->cb_set, 1);
-       dprintk("NFSD: callback set to client %u.%u.%u.%u\n", NIPQUAD(addr));
-out:
+       nfs4_put_delegation(dp);
        put_nfs4_client(clp);
 }
 
-static const struct rpc_call_ops nfs4_cb_null_ops = {
-       .rpc_call_done = nfs4_cb_null,
+static const struct rpc_call_ops nfsd4_cb_recall_ops = {
+       .rpc_call_done = nfsd4_cb_recall_done,
+       .rpc_release = nfsd4_cb_recall_release,
 };
 
 /*
  * called with dp->dl_count inc'ed.
- * nfs4_lock_state() may or may not have been called.
  */
 void
 nfsd4_cb_recall(struct nfs4_delegation *dp)
 {
        struct nfs4_client *clp = dp->dl_client;
-       struct rpc_clnt *clnt = clp->cl_callback.cb_client;
-       struct nfs4_cb_recall *cbr = &dp->dl_recall;
+       struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
        struct rpc_message msg = {
                .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
-               .rpc_argp = cbr,
+               .rpc_argp = dp,
+               .rpc_cred = clp->cl_cb_conn.cb_cred
        };
-       int retries = 1;
-       int status = 0;
-
-       if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
-               return;
-
-       msg.rpc_cred = nfsd4_lookupcred(clp, 0);
-       if (IS_ERR(msg.rpc_cred))
-               goto out;
+       int status;
 
-       cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
-       cbr->cbr_dp = dp;
-
-       status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
-       while (retries--) {
-               switch (status) {
-                       case -EIO:
-                               /* Network partition? */
-                       case -EBADHANDLE:
-                       case -NFS4ERR_BAD_STATEID:
-                               /* Race: client probably got cb_recall
-                                * before open reply granting delegation */
-                               break;
-                       default:
-                               goto out_put_cred;
-               }
-               ssleep(2);
-               status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
+       dp->dl_retries = 1;
+       status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
+                               &nfsd4_cb_recall_ops, dp);
+       if (status) {
+               put_nfs4_client(clp);
+               nfs4_put_delegation(dp);
        }
-out_put_cred:
-       put_rpccred(msg.rpc_cred);
-out:
-       if (status == -EIO)
-               atomic_set(&clp->cl_callback.cb_set, 0);
-       /* Success or failure, now we're either waiting for lease expiration
-        * or deleg_return. */
-       dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
-       nfs4_put_delegation(dp);
-       return;
 }