fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / sunrpc / pmap_clnt.c
index 8139ce6..76e59e9 100644 (file)
@@ -1,12 +1,13 @@
 /*
- * linux/net/sunrpc/pmap.c
+ * linux/net/sunrpc/pmap_clnt.c
  *
- * Portmapper client.
+ * In-kernel RPC portmapper client.
+ *
+ * Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
  *
  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  */
 
-#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/socket.h>
 #include <linux/kernel.h>
@@ -14,7 +15,6 @@
 #include <linux/uio.h>
 #include <linux/in.h>
 #include <linux/sunrpc/clnt.h>
-#include <linux/sunrpc/xprt.h>
 #include <linux/sunrpc/sched.h>
 
 #ifdef RPC_DEBUG
 #define PMAP_UNSET             2
 #define PMAP_GETPORT           3
 
+struct portmap_args {
+       u32                     pm_prog;
+       u32                     pm_vers;
+       u32                     pm_prot;
+       unsigned short          pm_port;
+       struct rpc_xprt *       pm_xprt;
+};
+
 static struct rpc_procinfo     pmap_procedures[];
 static struct rpc_clnt *       pmap_create(char *, struct sockaddr_in *, int, int);
-static void                    pmap_getport_done(struct rpc_task *);
+static void                    pmap_getport_done(struct rpc_task *, void *);
 static struct rpc_program      pmap_program;
-static DEFINE_SPINLOCK(pmap_lock);
 
-/*
- * Obtain the port for a given RPC service on a given host. This one can
- * be called for an ongoing RPC request.
- */
-void
-rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
+static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
 {
-       struct rpc_portmap *map = clnt->cl_pmap;
-       struct sockaddr_in *sap = &clnt->cl_xprt->addr;
+       struct portmap_args *map = calldata;
        struct rpc_message msg = {
                .rpc_proc       = &pmap_procedures[PMAP_GETPORT],
                .rpc_argp       = map,
-               .rpc_resp       = &clnt->cl_port,
-               .rpc_cred       = NULL
+               .rpc_resp       = &map->pm_port,
        };
+
+       rpc_call_setup(task, &msg, 0);
+}
+
+static inline struct portmap_args *pmap_map_alloc(void)
+{
+       return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
+}
+
+static inline void pmap_map_free(struct portmap_args *map)
+{
+       kfree(map);
+}
+
+static void pmap_map_release(void *data)
+{
+       struct portmap_args *map = data;
+
+       xprt_put(map->pm_xprt);
+       pmap_map_free(map);
+}
+
+static const struct rpc_call_ops pmap_getport_ops = {
+       .rpc_call_prepare       = pmap_getport_prepare,
+       .rpc_call_done          = pmap_getport_done,
+       .rpc_release            = pmap_map_release,
+};
+
+static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status)
+{
+       xprt_clear_binding(xprt);
+       rpc_wake_up_status(&xprt->binding, status);
+}
+
+/**
+ * rpc_getport - obtain the port for a given RPC service on a given host
+ * @task: task that is waiting for portmapper request
+ *
+ * This one can be called for an ongoing RPC request, and can be used in
+ * an async (rpciod) context.
+ */
+void rpc_getport(struct rpc_task *task)
+{
+       struct rpc_clnt *clnt = task->tk_client;
+       struct rpc_xprt *xprt = task->tk_xprt;
+       struct sockaddr_in addr;
+       struct portmap_args *map;
        struct rpc_clnt *pmap_clnt;
-       struct rpc_task *child;
+       struct rpc_task *child;
+       int status;
 
-       dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
+       dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
                        task->tk_pid, clnt->cl_server,
-                       map->pm_prog, map->pm_vers, map->pm_prot);
+                       clnt->cl_prog, clnt->cl_vers, xprt->prot);
 
        /* Autobind on cloned rpc clients is discouraged */
        BUG_ON(clnt->cl_parent != clnt);
 
-       spin_lock(&pmap_lock);
-       if (map->pm_binding) {
-               rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL);
-               spin_unlock(&pmap_lock);
-               return;
-       }
-       map->pm_binding = 1;
-       spin_unlock(&pmap_lock);
-
-       pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0);
-       if (IS_ERR(pmap_clnt)) {
-               task->tk_status = PTR_ERR(pmap_clnt);
-               goto bailout;
-       }
-       task->tk_status = 0;
-
-       /*
-        * Note: rpc_new_child will release client after a failure.
-        */
-       if (!(child = rpc_new_child(pmap_clnt, task)))
+       status = -EACCES;               /* tell caller to check again */
+       if (xprt_test_and_set_binding(xprt))
+               goto bailout_nowake;
+
+       /* Put self on queue before sending rpcbind request, in case
+        * pmap_getport_done completes before we return from rpc_run_task */
+       rpc_sleep_on(&xprt->binding, task, NULL, NULL);
+
+       /* Someone else may have bound if we slept */
+       status = 0;
+       if (xprt_bound(xprt))
+               goto bailout_nofree;
+
+       status = -ENOMEM;
+       map = pmap_map_alloc();
+       if (!map)
+               goto bailout_nofree;
+       map->pm_prog = clnt->cl_prog;
+       map->pm_vers = clnt->cl_vers;
+       map->pm_prot = xprt->prot;
+       map->pm_port = 0;
+       map->pm_xprt = xprt_get(xprt);
+
+       rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
+       pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
+       status = PTR_ERR(pmap_clnt);
+       if (IS_ERR(pmap_clnt))
                goto bailout;
 
-       /* Setup the call info struct */
-       rpc_call_setup(child, &msg, 0);
+       status = -EIO;
+       child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
+       if (IS_ERR(child))
+               goto bailout_nofree;
+       rpc_put_task(child);
 
-       /* ... and run the child task */
-       rpc_run_child(task, child, pmap_getport_done);
+       task->tk_xprt->stat.bind_count++;
        return;
 
 bailout:
-       spin_lock(&pmap_lock);
-       map->pm_binding = 0;
-       rpc_wake_up(&map->pm_bindwait);
-       spin_unlock(&pmap_lock);
-       rpc_exit(task, -EIO);
+       pmap_map_free(map);
+       xprt_put(xprt);
+bailout_nofree:
+       pmap_wake_portmap_waiters(xprt, status);
+bailout_nowake:
+       task->tk_status = status;
 }
 
 #ifdef CONFIG_ROOT_NFS
-int
-rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
+/**
+ * rpc_getport_external - obtain the port for a given RPC service on a given host
+ * @sin: address of remote peer
+ * @prog: RPC program number to bind
+ * @vers: RPC version number to bind
+ * @prot: transport protocol to use to make this request
+ *
+ * This one is called from outside the RPC client in a synchronous task context.
+ */
+int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
 {
-       struct rpc_portmap map = {
+       struct portmap_args map = {
                .pm_prog        = prog,
                .pm_vers        = vers,
                .pm_prot        = prot,
                .pm_port        = 0
        };
+       struct rpc_message msg = {
+               .rpc_proc       = &pmap_procedures[PMAP_GETPORT],
+               .rpc_argp       = &map,
+               .rpc_resp       = &map.pm_port,
+       };
        struct rpc_clnt *pmap_clnt;
        char            hostname[32];
        int             status;
 
-       dprintk("RPC:      rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
+       dprintk("RPC:      rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
                        NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
 
        sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
@@ -116,7 +187,7 @@ rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
                return PTR_ERR(pmap_clnt);
 
        /* Setup the call info struct */
-       status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
+       status = rpc_call_sync(pmap_clnt, &msg, 0);
 
        if (status >= 0) {
                if (map.pm_port != 0)
@@ -127,50 +198,68 @@ rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
 }
 #endif
 
-static void
-pmap_getport_done(struct rpc_task *task)
+/*
+ * Portmapper child task invokes this callback via tk_exit.
+ */
+static void pmap_getport_done(struct rpc_task *child, void *data)
 {
-       struct rpc_clnt *clnt = task->tk_client;
-       struct rpc_xprt *xprt = task->tk_xprt;
-       struct rpc_portmap *map = clnt->cl_pmap;
-
-       dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
-                       task->tk_pid, task->tk_status, clnt->cl_port);
-
-       xprt->ops->set_port(xprt, 0);
-       if (task->tk_status < 0) {
-               /* Make the calling task exit with an error */
-               task->tk_action = rpc_exit_task;
-       } else if (clnt->cl_port == 0) {
-               /* Program not registered */
-               rpc_exit(task, -EACCES);
+       struct portmap_args *map = data;
+       struct rpc_xprt *xprt = map->pm_xprt;
+       int status = child->tk_status;
+
+       if (status < 0) {
+               /* Portmapper not available */
+               xprt->ops->set_port(xprt, 0);
+       } else if (map->pm_port == 0) {
+               /* Requested RPC service wasn't registered */
+               xprt->ops->set_port(xprt, 0);
+               status = -EACCES;
        } else {
-               xprt->ops->set_port(xprt, clnt->cl_port);
-               clnt->cl_port = htons(clnt->cl_port);
+               /* Succeeded */
+               xprt->ops->set_port(xprt, map->pm_port);
+               xprt_set_bound(xprt);
+               status = 0;
        }
-       spin_lock(&pmap_lock);
-       map->pm_binding = 0;
-       rpc_wake_up(&map->pm_bindwait);
-       spin_unlock(&pmap_lock);
+
+       dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
+                       child->tk_pid, status, map->pm_port);
+
+       pmap_wake_portmap_waiters(xprt, status);
 }
 
-/*
- * Set or unset a port registration with the local portmapper.
+/**
+ * rpc_register - set or unset a port registration with the local portmapper
+ * @prog: RPC program number to bind
+ * @vers: RPC version number to bind
+ * @prot: transport protocol to use to make this request
+ * @port: port value to register
+ * @okay: result code
+ *
  * port == 0 means unregister, port != 0 means register.
  */
-int
-rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
+int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
 {
-       struct sockaddr_in      sin;
-       struct rpc_portmap      map;
+       struct sockaddr_in      sin = {
+               .sin_family     = AF_INET,
+               .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+       };
+       struct portmap_args     map = {
+               .pm_prog        = prog,
+               .pm_vers        = vers,
+               .pm_prot        = prot,
+               .pm_port        = port,
+       };
+       struct rpc_message msg = {
+               .rpc_proc       = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
+               .rpc_argp       = &map,
+               .rpc_resp       = okay,
+       };
        struct rpc_clnt         *pmap_clnt;
        int error = 0;
 
-       dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
+       dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
                        prog, vers, prot, port);
 
-       sin.sin_family = AF_INET;
-       sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
        if (IS_ERR(pmap_clnt)) {
                error = PTR_ERR(pmap_clnt);
@@ -178,13 +267,7 @@ rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
                return error;
        }
 
-       map.pm_prog = prog;
-       map.pm_vers = vers;
-       map.pm_prot = prot;
-       map.pm_port = port;
-
-       error = rpc_call(pmap_clnt, port? PMAP_SET : PMAP_UNSET,
-                                       &map, okay, 0);
+       error = rpc_call_sync(pmap_clnt, &msg, 0);
 
        if (error < 0) {
                printk(KERN_WARNING
@@ -197,38 +280,32 @@ rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
        return error;
 }
 
-static struct rpc_clnt *
-pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
+static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
 {
-       struct rpc_xprt *xprt;
-       struct rpc_clnt *clnt;
-
-       /* printk("pmap: create xprt\n"); */
-       xprt = xprt_create_proto(proto, srvaddr, NULL);
-       if (IS_ERR(xprt))
-               return (struct rpc_clnt *)xprt;
-       xprt->ops->set_port(xprt, RPC_PMAP_PORT);
+       struct rpc_create_args args = {
+               .protocol       = proto,
+               .address        = (struct sockaddr *)srvaddr,
+               .addrsize       = sizeof(*srvaddr),
+               .servername     = hostname,
+               .program        = &pmap_program,
+               .version        = RPC_PMAP_VERSION,
+               .authflavor     = RPC_AUTH_UNIX,
+               .flags          = (RPC_CLNT_CREATE_ONESHOT |
+                                  RPC_CLNT_CREATE_NOPING),
+       };
+
+       srvaddr->sin_port = htons(RPC_PMAP_PORT);
        if (!privileged)
-               xprt->resvport = 0;
-
-       /* printk("pmap: create clnt\n"); */
-       clnt = rpc_new_client(xprt, hostname,
-                               &pmap_program, RPC_PMAP_VERSION,
-                               RPC_AUTH_UNIX);
-       if (!IS_ERR(clnt)) {
-               clnt->cl_softrtry = 1;
-               clnt->cl_oneshot  = 1;
-       }
-       return clnt;
+               args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
+       return rpc_create(&args);
 }
 
 /*
  * XDR encode/decode functions for PMAP
  */
-static int
-xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
+static int xdr_encode_mapping(struct rpc_rqst *req, __be32 *p, struct portmap_args *map)
 {
-       dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
+       dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
                map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
        *p++ = htonl(map->pm_prog);
        *p++ = htonl(map->pm_vers);
@@ -239,15 +316,13 @@ xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
        return 0;
 }
 
-static int
-xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
+static int xdr_decode_port(struct rpc_rqst *req, __be32 *p, unsigned short *portp)
 {
        *portp = (unsigned short) ntohl(*p++);
        return 0;
 }
 
-static int
-xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
+static int xdr_decode_bool(struct rpc_rqst *req, __be32 *p, unsigned int *boolp)
 {
        *boolp = (unsigned int) ntohl(*p++);
        return 0;
@@ -260,6 +335,8 @@ static struct rpc_procinfo  pmap_procedures[] = {
          .p_decode             = (kxdrproc_t) xdr_decode_bool,
          .p_bufsiz             = 4,
          .p_count              = 1,
+         .p_statidx            = PMAP_SET,
+         .p_name               = "SET",
        },
 [PMAP_UNSET] = {
          .p_proc               = PMAP_UNSET,
@@ -267,6 +344,8 @@ static struct rpc_procinfo  pmap_procedures[] = {
          .p_decode             = (kxdrproc_t) xdr_decode_bool,
          .p_bufsiz             = 4,
          .p_count              = 1,
+         .p_statidx            = PMAP_UNSET,
+         .p_name               = "UNSET",
        },
 [PMAP_GETPORT] = {
          .p_proc               = PMAP_GETPORT,
@@ -274,6 +353,8 @@ static struct rpc_procinfo  pmap_procedures[] = {
          .p_decode             = (kxdrproc_t) xdr_decode_port,
          .p_bufsiz             = 4,
          .p_count              = 1,
+         .p_statidx            = PMAP_GETPORT,
+         .p_name               = "GETPORT",
        },
 };