SUNRPC: display human-readable procedure name in rpc_iostats output
[linux-2.6.git] / net / sunrpc / pmap_clnt.c
1 /*
2  * linux/net/sunrpc/pmap.c
3  *
4  * Portmapper client.
5  *
6  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/uio.h>
15 #include <linux/in.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/xprt.h>
18 #include <linux/sunrpc/sched.h>
19
20 #ifdef RPC_DEBUG
21 # define RPCDBG_FACILITY        RPCDBG_PMAP
22 #endif
23
24 #define PMAP_SET                1
25 #define PMAP_UNSET              2
26 #define PMAP_GETPORT            3
27
28 static struct rpc_procinfo      pmap_procedures[];
29 static struct rpc_clnt *        pmap_create(char *, struct sockaddr_in *, int, int);
30 static void                     pmap_getport_done(struct rpc_task *);
31 static struct rpc_program       pmap_program;
32 static DEFINE_SPINLOCK(pmap_lock);
33
34 /*
35  * Obtain the port for a given RPC service on a given host. This one can
36  * be called for an ongoing RPC request.
37  */
38 void
39 rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
40 {
41         struct rpc_portmap *map = clnt->cl_pmap;
42         struct sockaddr_in *sap = &clnt->cl_xprt->addr;
43         struct rpc_message msg = {
44                 .rpc_proc       = &pmap_procedures[PMAP_GETPORT],
45                 .rpc_argp       = map,
46                 .rpc_resp       = &clnt->cl_port,
47                 .rpc_cred       = NULL
48         };
49         struct rpc_clnt *pmap_clnt;
50         struct rpc_task *child;
51
52         dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
53                         task->tk_pid, clnt->cl_server,
54                         map->pm_prog, map->pm_vers, map->pm_prot);
55
56         /* Autobind on cloned rpc clients is discouraged */
57         BUG_ON(clnt->cl_parent != clnt);
58
59         spin_lock(&pmap_lock);
60         if (map->pm_binding) {
61                 rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL);
62                 spin_unlock(&pmap_lock);
63                 return;
64         }
65         map->pm_binding = 1;
66         spin_unlock(&pmap_lock);
67
68         pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0);
69         if (IS_ERR(pmap_clnt)) {
70                 task->tk_status = PTR_ERR(pmap_clnt);
71                 goto bailout;
72         }
73         task->tk_status = 0;
74
75         /*
76          * Note: rpc_new_child will release client after a failure.
77          */
78         if (!(child = rpc_new_child(pmap_clnt, task)))
79                 goto bailout;
80
81         /* Setup the call info struct */
82         rpc_call_setup(child, &msg, 0);
83
84         /* ... and run the child task */
85         task->tk_xprt->stat.bind_count++;
86         rpc_run_child(task, child, pmap_getport_done);
87         return;
88
89 bailout:
90         spin_lock(&pmap_lock);
91         map->pm_binding = 0;
92         rpc_wake_up(&map->pm_bindwait);
93         spin_unlock(&pmap_lock);
94         rpc_exit(task, -EIO);
95 }
96
97 #ifdef CONFIG_ROOT_NFS
98 int
99 rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
100 {
101         struct rpc_portmap map = {
102                 .pm_prog        = prog,
103                 .pm_vers        = vers,
104                 .pm_prot        = prot,
105                 .pm_port        = 0
106         };
107         struct rpc_clnt *pmap_clnt;
108         char            hostname[32];
109         int             status;
110
111         dprintk("RPC:      rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
112                         NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
113
114         sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
115         pmap_clnt = pmap_create(hostname, sin, prot, 0);
116         if (IS_ERR(pmap_clnt))
117                 return PTR_ERR(pmap_clnt);
118
119         /* Setup the call info struct */
120         status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
121
122         if (status >= 0) {
123                 if (map.pm_port != 0)
124                         return map.pm_port;
125                 status = -EACCES;
126         }
127         return status;
128 }
129 #endif
130
131 static void
132 pmap_getport_done(struct rpc_task *task)
133 {
134         struct rpc_clnt *clnt = task->tk_client;
135         struct rpc_xprt *xprt = task->tk_xprt;
136         struct rpc_portmap *map = clnt->cl_pmap;
137
138         dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
139                         task->tk_pid, task->tk_status, clnt->cl_port);
140
141         xprt->ops->set_port(xprt, 0);
142         if (task->tk_status < 0) {
143                 /* Make the calling task exit with an error */
144                 task->tk_action = rpc_exit_task;
145         } else if (clnt->cl_port == 0) {
146                 /* Program not registered */
147                 rpc_exit(task, -EACCES);
148         } else {
149                 xprt->ops->set_port(xprt, clnt->cl_port);
150                 clnt->cl_port = htons(clnt->cl_port);
151         }
152         spin_lock(&pmap_lock);
153         map->pm_binding = 0;
154         rpc_wake_up(&map->pm_bindwait);
155         spin_unlock(&pmap_lock);
156 }
157
158 /*
159  * Set or unset a port registration with the local portmapper.
160  * port == 0 means unregister, port != 0 means register.
161  */
162 int
163 rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
164 {
165         struct sockaddr_in      sin;
166         struct rpc_portmap      map;
167         struct rpc_clnt         *pmap_clnt;
168         int error = 0;
169
170         dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
171                         prog, vers, prot, port);
172
173         sin.sin_family = AF_INET;
174         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
175         pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
176         if (IS_ERR(pmap_clnt)) {
177                 error = PTR_ERR(pmap_clnt);
178                 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
179                 return error;
180         }
181
182         map.pm_prog = prog;
183         map.pm_vers = vers;
184         map.pm_prot = prot;
185         map.pm_port = port;
186
187         error = rpc_call(pmap_clnt, port? PMAP_SET : PMAP_UNSET,
188                                         &map, okay, 0);
189
190         if (error < 0) {
191                 printk(KERN_WARNING
192                         "RPC: failed to contact portmap (errno %d).\n",
193                         error);
194         }
195         dprintk("RPC: registration status %d/%d\n", error, *okay);
196
197         /* Client deleted automatically because cl_oneshot == 1 */
198         return error;
199 }
200
201 static struct rpc_clnt *
202 pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
203 {
204         struct rpc_xprt *xprt;
205         struct rpc_clnt *clnt;
206
207         /* printk("pmap: create xprt\n"); */
208         xprt = xprt_create_proto(proto, srvaddr, NULL);
209         if (IS_ERR(xprt))
210                 return (struct rpc_clnt *)xprt;
211         xprt->ops->set_port(xprt, RPC_PMAP_PORT);
212         if (!privileged)
213                 xprt->resvport = 0;
214
215         /* printk("pmap: create clnt\n"); */
216         clnt = rpc_new_client(xprt, hostname,
217                                 &pmap_program, RPC_PMAP_VERSION,
218                                 RPC_AUTH_UNIX);
219         if (!IS_ERR(clnt)) {
220                 clnt->cl_softrtry = 1;
221                 clnt->cl_oneshot  = 1;
222         }
223         return clnt;
224 }
225
226 /*
227  * XDR encode/decode functions for PMAP
228  */
229 static int
230 xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
231 {
232         dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
233                 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
234         *p++ = htonl(map->pm_prog);
235         *p++ = htonl(map->pm_vers);
236         *p++ = htonl(map->pm_prot);
237         *p++ = htonl(map->pm_port);
238
239         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
240         return 0;
241 }
242
243 static int
244 xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
245 {
246         *portp = (unsigned short) ntohl(*p++);
247         return 0;
248 }
249
250 static int
251 xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
252 {
253         *boolp = (unsigned int) ntohl(*p++);
254         return 0;
255 }
256
257 static struct rpc_procinfo      pmap_procedures[] = {
258 [PMAP_SET] = {
259           .p_proc               = PMAP_SET,
260           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
261           .p_decode             = (kxdrproc_t) xdr_decode_bool,
262           .p_bufsiz             = 4,
263           .p_count              = 1,
264           .p_statidx            = PMAP_SET,
265           .p_name               = "SET",
266         },
267 [PMAP_UNSET] = {
268           .p_proc               = PMAP_UNSET,
269           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
270           .p_decode             = (kxdrproc_t) xdr_decode_bool,
271           .p_bufsiz             = 4,
272           .p_count              = 1,
273           .p_statidx            = PMAP_UNSET,
274           .p_name               = "UNSET",
275         },
276 [PMAP_GETPORT] = {
277           .p_proc               = PMAP_GETPORT,
278           .p_encode             = (kxdrproc_t) xdr_encode_mapping,
279           .p_decode             = (kxdrproc_t) xdr_decode_port,
280           .p_bufsiz             = 4,
281           .p_count              = 1,
282           .p_statidx            = PMAP_GETPORT,
283           .p_name               = "GETPORT",
284         },
285 };
286
287 static struct rpc_version       pmap_version2 = {
288         .number         = 2,
289         .nrprocs        = 4,
290         .procs          = pmap_procedures
291 };
292
293 static struct rpc_version *     pmap_version[] = {
294         NULL,
295         NULL,
296         &pmap_version2
297 };
298
299 static struct rpc_stat          pmap_stats;
300
301 static struct rpc_program       pmap_program = {
302         .name           = "portmap",
303         .number         = RPC_PMAP_PROGRAM,
304         .nrvers         = ARRAY_SIZE(pmap_version),
305         .version        = pmap_version,
306         .stats          = &pmap_stats,
307 };