Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/svc.c |
| 3 | * |
| 4 | * High-level RPC service routines |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/linkage.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/net.h> |
| 13 | #include <linux/in.h> |
| 14 | #include <linux/mm.h> |
| 15 | |
| 16 | #include <linux/sunrpc/types.h> |
| 17 | #include <linux/sunrpc/xdr.h> |
| 18 | #include <linux/sunrpc/stats.h> |
| 19 | #include <linux/sunrpc/svcsock.h> |
| 20 | #include <linux/sunrpc/clnt.h> |
| 21 | |
| 22 | #define RPCDBG_FACILITY RPCDBG_SVCDSP |
| 23 | #define RPC_PARANOIA 1 |
| 24 | |
| 25 | /* |
| 26 | * Create an RPC service |
| 27 | */ |
| 28 | struct svc_serv * |
| 29 | svc_create(struct svc_program *prog, unsigned int bufsize) |
| 30 | { |
| 31 | struct svc_serv *serv; |
| 32 | int vers; |
| 33 | unsigned int xdrsize; |
| 34 | |
| 35 | if (!(serv = (struct svc_serv *) kmalloc(sizeof(*serv), GFP_KERNEL))) |
| 36 | return NULL; |
| 37 | memset(serv, 0, sizeof(*serv)); |
| 38 | serv->sv_program = prog; |
| 39 | serv->sv_nrthreads = 1; |
| 40 | serv->sv_stats = prog->pg_stats; |
| 41 | serv->sv_bufsz = bufsize? bufsize : 4096; |
| 42 | prog->pg_lovers = prog->pg_nvers-1; |
| 43 | xdrsize = 0; |
| 44 | for (vers=0; vers<prog->pg_nvers ; vers++) |
| 45 | if (prog->pg_vers[vers]) { |
| 46 | prog->pg_hivers = vers; |
| 47 | if (prog->pg_lovers > vers) |
| 48 | prog->pg_lovers = vers; |
| 49 | if (prog->pg_vers[vers]->vs_xdrsize > xdrsize) |
| 50 | xdrsize = prog->pg_vers[vers]->vs_xdrsize; |
| 51 | } |
| 52 | serv->sv_xdrsize = xdrsize; |
| 53 | INIT_LIST_HEAD(&serv->sv_threads); |
| 54 | INIT_LIST_HEAD(&serv->sv_sockets); |
| 55 | INIT_LIST_HEAD(&serv->sv_tempsocks); |
| 56 | INIT_LIST_HEAD(&serv->sv_permsocks); |
| 57 | spin_lock_init(&serv->sv_lock); |
| 58 | |
| 59 | serv->sv_name = prog->pg_name; |
| 60 | |
| 61 | /* Remove any stale portmap registrations */ |
| 62 | svc_register(serv, 0, 0); |
| 63 | |
| 64 | return serv; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Destroy an RPC service |
| 69 | */ |
| 70 | void |
| 71 | svc_destroy(struct svc_serv *serv) |
| 72 | { |
| 73 | struct svc_sock *svsk; |
| 74 | |
| 75 | dprintk("RPC: svc_destroy(%s, %d)\n", |
| 76 | serv->sv_program->pg_name, |
| 77 | serv->sv_nrthreads); |
| 78 | |
| 79 | if (serv->sv_nrthreads) { |
| 80 | if (--(serv->sv_nrthreads) != 0) { |
| 81 | svc_sock_update_bufs(serv); |
| 82 | return; |
| 83 | } |
| 84 | } else |
| 85 | printk("svc_destroy: no threads for serv=%p!\n", serv); |
| 86 | |
| 87 | while (!list_empty(&serv->sv_tempsocks)) { |
| 88 | svsk = list_entry(serv->sv_tempsocks.next, |
| 89 | struct svc_sock, |
| 90 | sk_list); |
| 91 | svc_delete_socket(svsk); |
| 92 | } |
| 93 | while (!list_empty(&serv->sv_permsocks)) { |
| 94 | svsk = list_entry(serv->sv_permsocks.next, |
| 95 | struct svc_sock, |
| 96 | sk_list); |
| 97 | svc_delete_socket(svsk); |
| 98 | } |
| 99 | |
| 100 | cache_clean_deferred(serv); |
| 101 | |
| 102 | /* Unregister service with the portmapper */ |
| 103 | svc_register(serv, 0, 0); |
| 104 | kfree(serv); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Allocate an RPC server's buffer space. |
| 109 | * We allocate pages and place them in rq_argpages. |
| 110 | */ |
| 111 | static int |
| 112 | svc_init_buffer(struct svc_rqst *rqstp, unsigned int size) |
| 113 | { |
| 114 | int pages; |
| 115 | int arghi; |
| 116 | |
| 117 | if (size > RPCSVC_MAXPAYLOAD) |
| 118 | size = RPCSVC_MAXPAYLOAD; |
| 119 | pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE; |
| 120 | rqstp->rq_argused = 0; |
| 121 | rqstp->rq_resused = 0; |
| 122 | arghi = 0; |
| 123 | if (pages > RPCSVC_MAXPAGES) |
| 124 | BUG(); |
| 125 | while (pages) { |
| 126 | struct page *p = alloc_page(GFP_KERNEL); |
| 127 | if (!p) |
| 128 | break; |
| 129 | rqstp->rq_argpages[arghi++] = p; |
| 130 | pages--; |
| 131 | } |
| 132 | rqstp->rq_arghi = arghi; |
| 133 | return ! pages; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Release an RPC server buffer |
| 138 | */ |
| 139 | static void |
| 140 | svc_release_buffer(struct svc_rqst *rqstp) |
| 141 | { |
| 142 | while (rqstp->rq_arghi) |
| 143 | put_page(rqstp->rq_argpages[--rqstp->rq_arghi]); |
| 144 | while (rqstp->rq_resused) { |
| 145 | if (rqstp->rq_respages[--rqstp->rq_resused] == NULL) |
| 146 | continue; |
| 147 | put_page(rqstp->rq_respages[rqstp->rq_resused]); |
| 148 | } |
| 149 | rqstp->rq_argused = 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Create a server thread |
| 154 | */ |
| 155 | int |
| 156 | svc_create_thread(svc_thread_fn func, struct svc_serv *serv) |
| 157 | { |
| 158 | struct svc_rqst *rqstp; |
| 159 | int error = -ENOMEM; |
| 160 | |
| 161 | rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL); |
| 162 | if (!rqstp) |
| 163 | goto out; |
| 164 | |
| 165 | memset(rqstp, 0, sizeof(*rqstp)); |
| 166 | init_waitqueue_head(&rqstp->rq_wait); |
| 167 | |
| 168 | if (!(rqstp->rq_argp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
| 169 | || !(rqstp->rq_resp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
| 170 | || !svc_init_buffer(rqstp, serv->sv_bufsz)) |
| 171 | goto out_thread; |
| 172 | |
| 173 | serv->sv_nrthreads++; |
| 174 | rqstp->rq_server = serv; |
| 175 | error = kernel_thread((int (*)(void *)) func, rqstp, 0); |
| 176 | if (error < 0) |
| 177 | goto out_thread; |
| 178 | svc_sock_update_bufs(serv); |
| 179 | error = 0; |
| 180 | out: |
| 181 | return error; |
| 182 | |
| 183 | out_thread: |
| 184 | svc_exit_thread(rqstp); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Destroy an RPC server thread |
| 190 | */ |
| 191 | void |
| 192 | svc_exit_thread(struct svc_rqst *rqstp) |
| 193 | { |
| 194 | struct svc_serv *serv = rqstp->rq_server; |
| 195 | |
| 196 | svc_release_buffer(rqstp); |
| 197 | if (rqstp->rq_resp) |
| 198 | kfree(rqstp->rq_resp); |
| 199 | if (rqstp->rq_argp) |
| 200 | kfree(rqstp->rq_argp); |
| 201 | if (rqstp->rq_auth_data) |
| 202 | kfree(rqstp->rq_auth_data); |
| 203 | kfree(rqstp); |
| 204 | |
| 205 | /* Release the server */ |
| 206 | if (serv) |
| 207 | svc_destroy(serv); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Register an RPC service with the local portmapper. |
| 212 | * To unregister a service, call this routine with |
| 213 | * proto and port == 0. |
| 214 | */ |
| 215 | int |
| 216 | svc_register(struct svc_serv *serv, int proto, unsigned short port) |
| 217 | { |
| 218 | struct svc_program *progp; |
| 219 | unsigned long flags; |
| 220 | int i, error = 0, dummy; |
| 221 | |
| 222 | progp = serv->sv_program; |
| 223 | |
| 224 | dprintk("RPC: svc_register(%s, %s, %d)\n", |
| 225 | progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port); |
| 226 | |
| 227 | if (!port) |
| 228 | clear_thread_flag(TIF_SIGPENDING); |
| 229 | |
| 230 | for (i = 0; i < progp->pg_nvers; i++) { |
| 231 | if (progp->pg_vers[i] == NULL) |
| 232 | continue; |
| 233 | error = rpc_register(progp->pg_prog, i, proto, port, &dummy); |
| 234 | if (error < 0) |
| 235 | break; |
| 236 | if (port && !dummy) { |
| 237 | error = -EACCES; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (!port) { |
| 243 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 244 | recalc_sigpending(); |
| 245 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 246 | } |
| 247 | |
| 248 | return error; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Process the RPC request. |
| 253 | */ |
| 254 | int |
| 255 | svc_process(struct svc_serv *serv, struct svc_rqst *rqstp) |
| 256 | { |
| 257 | struct svc_program *progp; |
| 258 | struct svc_version *versp = NULL; /* compiler food */ |
| 259 | struct svc_procedure *procp = NULL; |
| 260 | struct kvec * argv = &rqstp->rq_arg.head[0]; |
| 261 | struct kvec * resv = &rqstp->rq_res.head[0]; |
| 262 | kxdrproc_t xdr; |
| 263 | u32 *statp; |
| 264 | u32 dir, prog, vers, proc, |
| 265 | auth_stat, rpc_stat; |
| 266 | int auth_res; |
| 267 | u32 *accept_statp; |
| 268 | |
| 269 | rpc_stat = rpc_success; |
| 270 | |
| 271 | if (argv->iov_len < 6*4) |
| 272 | goto err_short_len; |
| 273 | |
| 274 | /* setup response xdr_buf. |
| 275 | * Initially it has just one page |
| 276 | */ |
| 277 | svc_take_page(rqstp); /* must succeed */ |
| 278 | resv->iov_base = page_address(rqstp->rq_respages[0]); |
| 279 | resv->iov_len = 0; |
| 280 | rqstp->rq_res.pages = rqstp->rq_respages+1; |
| 281 | rqstp->rq_res.len = 0; |
| 282 | rqstp->rq_res.page_base = 0; |
| 283 | rqstp->rq_res.page_len = 0; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame^] | 284 | rqstp->rq_res.buflen = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | rqstp->rq_res.tail[0].iov_len = 0; |
| 286 | /* tcp needs a space for the record length... */ |
| 287 | if (rqstp->rq_prot == IPPROTO_TCP) |
| 288 | svc_putu32(resv, 0); |
| 289 | |
| 290 | rqstp->rq_xid = svc_getu32(argv); |
| 291 | svc_putu32(resv, rqstp->rq_xid); |
| 292 | |
| 293 | dir = ntohl(svc_getu32(argv)); |
| 294 | vers = ntohl(svc_getu32(argv)); |
| 295 | |
| 296 | /* First words of reply: */ |
| 297 | svc_putu32(resv, xdr_one); /* REPLY */ |
| 298 | |
| 299 | if (dir != 0) /* direction != CALL */ |
| 300 | goto err_bad_dir; |
| 301 | if (vers != 2) /* RPC version number */ |
| 302 | goto err_bad_rpc; |
| 303 | |
| 304 | /* Save position in case we later decide to reject: */ |
| 305 | accept_statp = resv->iov_base + resv->iov_len; |
| 306 | |
| 307 | svc_putu32(resv, xdr_zero); /* ACCEPT */ |
| 308 | |
| 309 | rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */ |
| 310 | rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */ |
| 311 | rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */ |
| 312 | |
| 313 | progp = serv->sv_program; |
| 314 | /* |
| 315 | * Decode auth data, and add verifier to reply buffer. |
| 316 | * We do this before anything else in order to get a decent |
| 317 | * auth verifier. |
| 318 | */ |
| 319 | auth_res = svc_authenticate(rqstp, &auth_stat); |
| 320 | /* Also give the program a chance to reject this call: */ |
| 321 | if (auth_res == SVC_OK) { |
| 322 | auth_stat = rpc_autherr_badcred; |
| 323 | auth_res = progp->pg_authenticate(rqstp); |
| 324 | } |
| 325 | switch (auth_res) { |
| 326 | case SVC_OK: |
| 327 | break; |
| 328 | case SVC_GARBAGE: |
| 329 | rpc_stat = rpc_garbage_args; |
| 330 | goto err_bad; |
| 331 | case SVC_SYSERR: |
| 332 | rpc_stat = rpc_system_err; |
| 333 | goto err_bad; |
| 334 | case SVC_DENIED: |
| 335 | goto err_bad_auth; |
| 336 | case SVC_DROP: |
| 337 | goto dropit; |
| 338 | case SVC_COMPLETE: |
| 339 | goto sendit; |
| 340 | } |
| 341 | |
| 342 | if (prog != progp->pg_prog) |
| 343 | goto err_bad_prog; |
| 344 | |
| 345 | if (vers >= progp->pg_nvers || |
| 346 | !(versp = progp->pg_vers[vers])) |
| 347 | goto err_bad_vers; |
| 348 | |
| 349 | procp = versp->vs_proc + proc; |
| 350 | if (proc >= versp->vs_nproc || !procp->pc_func) |
| 351 | goto err_bad_proc; |
| 352 | rqstp->rq_server = serv; |
| 353 | rqstp->rq_procinfo = procp; |
| 354 | |
| 355 | /* Syntactic check complete */ |
| 356 | serv->sv_stats->rpccnt++; |
| 357 | |
| 358 | /* Build the reply header. */ |
| 359 | statp = resv->iov_base +resv->iov_len; |
| 360 | svc_putu32(resv, rpc_success); /* RPC_SUCCESS */ |
| 361 | |
| 362 | /* Bump per-procedure stats counter */ |
| 363 | procp->pc_count++; |
| 364 | |
| 365 | /* Initialize storage for argp and resp */ |
| 366 | memset(rqstp->rq_argp, 0, procp->pc_argsize); |
| 367 | memset(rqstp->rq_resp, 0, procp->pc_ressize); |
| 368 | |
| 369 | /* un-reserve some of the out-queue now that we have a |
| 370 | * better idea of reply size |
| 371 | */ |
| 372 | if (procp->pc_xdrressize) |
| 373 | svc_reserve(rqstp, procp->pc_xdrressize<<2); |
| 374 | |
| 375 | /* Call the function that processes the request. */ |
| 376 | if (!versp->vs_dispatch) { |
| 377 | /* Decode arguments */ |
| 378 | xdr = procp->pc_decode; |
| 379 | if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp)) |
| 380 | goto err_garbage; |
| 381 | |
| 382 | *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); |
| 383 | |
| 384 | /* Encode reply */ |
| 385 | if (*statp == rpc_success && (xdr = procp->pc_encode) |
| 386 | && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) { |
| 387 | dprintk("svc: failed to encode reply\n"); |
| 388 | /* serv->sv_stats->rpcsystemerr++; */ |
| 389 | *statp = rpc_system_err; |
| 390 | } |
| 391 | } else { |
| 392 | dprintk("svc: calling dispatcher\n"); |
| 393 | if (!versp->vs_dispatch(rqstp, statp)) { |
| 394 | /* Release reply info */ |
| 395 | if (procp->pc_release) |
| 396 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 397 | goto dropit; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* Check RPC status result */ |
| 402 | if (*statp != rpc_success) |
| 403 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; |
| 404 | |
| 405 | /* Release reply info */ |
| 406 | if (procp->pc_release) |
| 407 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 408 | |
| 409 | if (procp->pc_encode == NULL) |
| 410 | goto dropit; |
| 411 | |
| 412 | sendit: |
| 413 | if (svc_authorise(rqstp)) |
| 414 | goto dropit; |
| 415 | return svc_send(rqstp); |
| 416 | |
| 417 | dropit: |
| 418 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ |
| 419 | dprintk("svc: svc_process dropit\n"); |
| 420 | svc_drop(rqstp); |
| 421 | return 0; |
| 422 | |
| 423 | err_short_len: |
| 424 | #ifdef RPC_PARANOIA |
| 425 | printk("svc: short len %Zd, dropping request\n", argv->iov_len); |
| 426 | #endif |
| 427 | goto dropit; /* drop request */ |
| 428 | |
| 429 | err_bad_dir: |
| 430 | #ifdef RPC_PARANOIA |
| 431 | printk("svc: bad direction %d, dropping request\n", dir); |
| 432 | #endif |
| 433 | serv->sv_stats->rpcbadfmt++; |
| 434 | goto dropit; /* drop request */ |
| 435 | |
| 436 | err_bad_rpc: |
| 437 | serv->sv_stats->rpcbadfmt++; |
| 438 | svc_putu32(resv, xdr_one); /* REJECT */ |
| 439 | svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */ |
| 440 | svc_putu32(resv, xdr_two); /* Only RPCv2 supported */ |
| 441 | svc_putu32(resv, xdr_two); |
| 442 | goto sendit; |
| 443 | |
| 444 | err_bad_auth: |
| 445 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); |
| 446 | serv->sv_stats->rpcbadauth++; |
| 447 | /* Restore write pointer to location of accept status: */ |
| 448 | xdr_ressize_check(rqstp, accept_statp); |
| 449 | svc_putu32(resv, xdr_one); /* REJECT */ |
| 450 | svc_putu32(resv, xdr_one); /* AUTH_ERROR */ |
| 451 | svc_putu32(resv, auth_stat); /* status */ |
| 452 | goto sendit; |
| 453 | |
| 454 | err_bad_prog: |
| 455 | #ifdef RPC_PARANOIA |
| 456 | if (prog != 100227 || progp->pg_prog != 100003) |
| 457 | printk("svc: unknown program %d (me %d)\n", prog, progp->pg_prog); |
| 458 | /* else it is just a Solaris client seeing if ACLs are supported */ |
| 459 | #endif |
| 460 | serv->sv_stats->rpcbadfmt++; |
| 461 | svc_putu32(resv, rpc_prog_unavail); |
| 462 | goto sendit; |
| 463 | |
| 464 | err_bad_vers: |
| 465 | #ifdef RPC_PARANOIA |
| 466 | printk("svc: unknown version (%d)\n", vers); |
| 467 | #endif |
| 468 | serv->sv_stats->rpcbadfmt++; |
| 469 | svc_putu32(resv, rpc_prog_mismatch); |
| 470 | svc_putu32(resv, htonl(progp->pg_lovers)); |
| 471 | svc_putu32(resv, htonl(progp->pg_hivers)); |
| 472 | goto sendit; |
| 473 | |
| 474 | err_bad_proc: |
| 475 | #ifdef RPC_PARANOIA |
| 476 | printk("svc: unknown procedure (%d)\n", proc); |
| 477 | #endif |
| 478 | serv->sv_stats->rpcbadfmt++; |
| 479 | svc_putu32(resv, rpc_proc_unavail); |
| 480 | goto sendit; |
| 481 | |
| 482 | err_garbage: |
| 483 | #ifdef RPC_PARANOIA |
| 484 | printk("svc: failed to decode args\n"); |
| 485 | #endif |
| 486 | rpc_stat = rpc_garbage_args; |
| 487 | err_bad: |
| 488 | serv->sv_stats->rpcbadfmt++; |
| 489 | svc_putu32(resv, rpc_stat); |
| 490 | goto sendit; |
| 491 | } |