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> |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/sunrpc/types.h> |
| 19 | #include <linux/sunrpc/xdr.h> |
| 20 | #include <linux/sunrpc/stats.h> |
| 21 | #include <linux/sunrpc/svcsock.h> |
| 22 | #include <linux/sunrpc/clnt.h> |
| 23 | |
| 24 | #define RPCDBG_FACILITY RPCDBG_SVCDSP |
| 25 | #define RPC_PARANOIA 1 |
| 26 | |
| 27 | /* |
| 28 | * Create an RPC service |
| 29 | */ |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 30 | static struct svc_serv * |
| 31 | __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 32 | void (*shutdown)(struct svc_serv *serv)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | struct svc_serv *serv; |
| 35 | int vers; |
| 36 | unsigned int xdrsize; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 37 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 39 | if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return NULL; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 41 | serv->sv_name = prog->pg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | serv->sv_program = prog; |
| 43 | serv->sv_nrthreads = 1; |
| 44 | serv->sv_stats = prog->pg_stats; |
| 45 | serv->sv_bufsz = bufsize? bufsize : 4096; |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 46 | serv->sv_shutdown = shutdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | xdrsize = 0; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 48 | while (prog) { |
| 49 | prog->pg_lovers = prog->pg_nvers-1; |
| 50 | for (vers=0; vers<prog->pg_nvers ; vers++) |
| 51 | if (prog->pg_vers[vers]) { |
| 52 | prog->pg_hivers = vers; |
| 53 | if (prog->pg_lovers > vers) |
| 54 | prog->pg_lovers = vers; |
| 55 | if (prog->pg_vers[vers]->vs_xdrsize > xdrsize) |
| 56 | xdrsize = prog->pg_vers[vers]->vs_xdrsize; |
| 57 | } |
| 58 | prog = prog->pg_next; |
| 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | serv->sv_xdrsize = xdrsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | INIT_LIST_HEAD(&serv->sv_tempsocks); |
| 62 | INIT_LIST_HEAD(&serv->sv_permsocks); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 63 | init_timer(&serv->sv_temptimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | spin_lock_init(&serv->sv_lock); |
| 65 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 66 | serv->sv_nrpools = npools; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 67 | serv->sv_pools = |
| 68 | kcalloc(sizeof(struct svc_pool), serv->sv_nrpools, |
| 69 | GFP_KERNEL); |
| 70 | if (!serv->sv_pools) { |
| 71 | kfree(serv); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 76 | struct svc_pool *pool = &serv->sv_pools[i]; |
| 77 | |
| 78 | dprintk("initialising pool %u for %s\n", |
| 79 | i, serv->sv_name); |
| 80 | |
| 81 | pool->sp_id = i; |
| 82 | INIT_LIST_HEAD(&pool->sp_threads); |
| 83 | INIT_LIST_HEAD(&pool->sp_sockets); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 84 | INIT_LIST_HEAD(&pool->sp_all_threads); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 85 | spin_lock_init(&pool->sp_lock); |
| 86 | } |
| 87 | |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* Remove any stale portmap registrations */ |
| 90 | svc_register(serv, 0, 0); |
| 91 | |
| 92 | return serv; |
| 93 | } |
| 94 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 95 | struct svc_serv * |
| 96 | svc_create(struct svc_program *prog, unsigned int bufsize, |
| 97 | void (*shutdown)(struct svc_serv *serv)) |
| 98 | { |
| 99 | return __svc_create(prog, bufsize, /*npools*/1, shutdown); |
| 100 | } |
| 101 | |
| 102 | struct svc_serv * |
| 103 | svc_create_pooled(struct svc_program *prog, unsigned int bufsize, |
| 104 | void (*shutdown)(struct svc_serv *serv), |
| 105 | svc_thread_fn func, int sig, struct module *mod) |
| 106 | { |
| 107 | struct svc_serv *serv; |
| 108 | |
| 109 | serv = __svc_create(prog, bufsize, /*npools*/1, shutdown); |
| 110 | |
| 111 | if (serv != NULL) { |
| 112 | serv->sv_function = func; |
| 113 | serv->sv_kill_signal = sig; |
| 114 | serv->sv_module = mod; |
| 115 | } |
| 116 | |
| 117 | return serv; |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 121 | * Destroy an RPC service. Should be called with the BKL held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | */ |
| 123 | void |
| 124 | svc_destroy(struct svc_serv *serv) |
| 125 | { |
| 126 | struct svc_sock *svsk; |
| 127 | |
| 128 | dprintk("RPC: svc_destroy(%s, %d)\n", |
| 129 | serv->sv_program->pg_name, |
| 130 | serv->sv_nrthreads); |
| 131 | |
| 132 | if (serv->sv_nrthreads) { |
| 133 | if (--(serv->sv_nrthreads) != 0) { |
| 134 | svc_sock_update_bufs(serv); |
| 135 | return; |
| 136 | } |
| 137 | } else |
| 138 | printk("svc_destroy: no threads for serv=%p!\n", serv); |
| 139 | |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 140 | del_timer_sync(&serv->sv_temptimer); |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | while (!list_empty(&serv->sv_tempsocks)) { |
| 143 | svsk = list_entry(serv->sv_tempsocks.next, |
| 144 | struct svc_sock, |
| 145 | sk_list); |
| 146 | svc_delete_socket(svsk); |
| 147 | } |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 148 | if (serv->sv_shutdown) |
| 149 | serv->sv_shutdown(serv); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | while (!list_empty(&serv->sv_permsocks)) { |
| 152 | svsk = list_entry(serv->sv_permsocks.next, |
| 153 | struct svc_sock, |
| 154 | sk_list); |
| 155 | svc_delete_socket(svsk); |
| 156 | } |
| 157 | |
| 158 | cache_clean_deferred(serv); |
| 159 | |
| 160 | /* Unregister service with the portmapper */ |
| 161 | svc_register(serv, 0, 0); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 162 | kfree(serv->sv_pools); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | kfree(serv); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Allocate an RPC server's buffer space. |
| 168 | * We allocate pages and place them in rq_argpages. |
| 169 | */ |
| 170 | static int |
| 171 | svc_init_buffer(struct svc_rqst *rqstp, unsigned int size) |
| 172 | { |
| 173 | int pages; |
| 174 | int arghi; |
| 175 | |
| 176 | if (size > RPCSVC_MAXPAYLOAD) |
| 177 | size = RPCSVC_MAXPAYLOAD; |
| 178 | pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE; |
| 179 | rqstp->rq_argused = 0; |
| 180 | rqstp->rq_resused = 0; |
| 181 | arghi = 0; |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 182 | BUG_ON(pages > RPCSVC_MAXPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | while (pages) { |
| 184 | struct page *p = alloc_page(GFP_KERNEL); |
| 185 | if (!p) |
| 186 | break; |
| 187 | rqstp->rq_argpages[arghi++] = p; |
| 188 | pages--; |
| 189 | } |
| 190 | rqstp->rq_arghi = arghi; |
| 191 | return ! pages; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Release an RPC server buffer |
| 196 | */ |
| 197 | static void |
| 198 | svc_release_buffer(struct svc_rqst *rqstp) |
| 199 | { |
| 200 | while (rqstp->rq_arghi) |
| 201 | put_page(rqstp->rq_argpages[--rqstp->rq_arghi]); |
| 202 | while (rqstp->rq_resused) { |
| 203 | if (rqstp->rq_respages[--rqstp->rq_resused] == NULL) |
| 204 | continue; |
| 205 | put_page(rqstp->rq_respages[rqstp->rq_resused]); |
| 206 | } |
| 207 | rqstp->rq_argused = 0; |
| 208 | } |
| 209 | |
| 210 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 211 | * Create a thread in the given pool. Caller must hold BKL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | */ |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 213 | static int |
| 214 | __svc_create_thread(svc_thread_fn func, struct svc_serv *serv, |
| 215 | struct svc_pool *pool) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | struct svc_rqst *rqstp; |
| 218 | int error = -ENOMEM; |
| 219 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 220 | rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (!rqstp) |
| 222 | goto out; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | init_waitqueue_head(&rqstp->rq_wait); |
| 225 | |
Jesper Juhl | 12fe2c5 | 2006-01-10 13:08:21 -0800 | [diff] [blame] | 226 | if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
| 227 | || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | || !svc_init_buffer(rqstp, serv->sv_bufsz)) |
| 229 | goto out_thread; |
| 230 | |
| 231 | serv->sv_nrthreads++; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 232 | spin_lock_bh(&pool->sp_lock); |
| 233 | pool->sp_nrthreads++; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 234 | list_add(&rqstp->rq_all, &pool->sp_all_threads); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 235 | spin_unlock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | rqstp->rq_server = serv; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 237 | rqstp->rq_pool = pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | error = kernel_thread((int (*)(void *)) func, rqstp, 0); |
| 239 | if (error < 0) |
| 240 | goto out_thread; |
| 241 | svc_sock_update_bufs(serv); |
| 242 | error = 0; |
| 243 | out: |
| 244 | return error; |
| 245 | |
| 246 | out_thread: |
| 247 | svc_exit_thread(rqstp); |
| 248 | goto out; |
| 249 | } |
| 250 | |
| 251 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 252 | * Create a thread in the default pool. Caller must hold BKL. |
| 253 | */ |
| 254 | int |
| 255 | svc_create_thread(svc_thread_fn func, struct svc_serv *serv) |
| 256 | { |
| 257 | return __svc_create_thread(func, serv, &serv->sv_pools[0]); |
| 258 | } |
| 259 | |
| 260 | /* |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 261 | * Choose a pool in which to create a new thread, for svc_set_num_threads |
| 262 | */ |
| 263 | static inline struct svc_pool * |
| 264 | choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 265 | { |
| 266 | if (pool != NULL) |
| 267 | return pool; |
| 268 | |
| 269 | return &serv->sv_pools[(*state)++ % serv->sv_nrpools]; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Choose a thread to kill, for svc_set_num_threads |
| 274 | */ |
| 275 | static inline struct task_struct * |
| 276 | choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 277 | { |
| 278 | unsigned int i; |
| 279 | struct task_struct *task = NULL; |
| 280 | |
| 281 | if (pool != NULL) { |
| 282 | spin_lock_bh(&pool->sp_lock); |
| 283 | } else { |
| 284 | /* choose a pool in round-robin fashion */ |
| 285 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 286 | pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; |
| 287 | spin_lock_bh(&pool->sp_lock); |
| 288 | if (!list_empty(&pool->sp_all_threads)) |
| 289 | goto found_pool; |
| 290 | spin_unlock_bh(&pool->sp_lock); |
| 291 | } |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | found_pool: |
| 296 | if (!list_empty(&pool->sp_all_threads)) { |
| 297 | struct svc_rqst *rqstp; |
| 298 | |
| 299 | /* |
| 300 | * Remove from the pool->sp_all_threads list |
| 301 | * so we don't try to kill it again. |
| 302 | */ |
| 303 | rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all); |
| 304 | list_del_init(&rqstp->rq_all); |
| 305 | task = rqstp->rq_task; |
| 306 | } |
| 307 | spin_unlock_bh(&pool->sp_lock); |
| 308 | |
| 309 | return task; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Create or destroy enough new threads to make the number |
| 314 | * of threads the given number. If `pool' is non-NULL, applies |
| 315 | * only to threads in that pool, otherwise round-robins between |
| 316 | * all pools. Must be called with a svc_get() reference and |
| 317 | * the BKL held. |
| 318 | * |
| 319 | * Destroying threads relies on the service threads filling in |
| 320 | * rqstp->rq_task, which only the nfs ones do. Assumes the serv |
| 321 | * has been created using svc_create_pooled(). |
| 322 | * |
| 323 | * Based on code that used to be in nfsd_svc() but tweaked |
| 324 | * to be pool-aware. |
| 325 | */ |
| 326 | int |
| 327 | svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 328 | { |
| 329 | struct task_struct *victim; |
| 330 | int error = 0; |
| 331 | unsigned int state = serv->sv_nrthreads-1; |
| 332 | |
| 333 | if (pool == NULL) { |
| 334 | /* The -1 assumes caller has done a svc_get() */ |
| 335 | nrservs -= (serv->sv_nrthreads-1); |
| 336 | } else { |
| 337 | spin_lock_bh(&pool->sp_lock); |
| 338 | nrservs -= pool->sp_nrthreads; |
| 339 | spin_unlock_bh(&pool->sp_lock); |
| 340 | } |
| 341 | |
| 342 | /* create new threads */ |
| 343 | while (nrservs > 0) { |
| 344 | nrservs--; |
| 345 | __module_get(serv->sv_module); |
| 346 | error = __svc_create_thread(serv->sv_function, serv, |
| 347 | choose_pool(serv, pool, &state)); |
| 348 | if (error < 0) { |
| 349 | module_put(serv->sv_module); |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | /* destroy old threads */ |
| 354 | while (nrservs < 0 && |
| 355 | (victim = choose_victim(serv, pool, &state)) != NULL) { |
| 356 | send_sig(serv->sv_kill_signal, victim, 1); |
| 357 | nrservs++; |
| 358 | } |
| 359 | |
| 360 | return error; |
| 361 | } |
| 362 | |
| 363 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 364 | * Called from a server thread as it's exiting. Caller must hold BKL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | */ |
| 366 | void |
| 367 | svc_exit_thread(struct svc_rqst *rqstp) |
| 368 | { |
| 369 | struct svc_serv *serv = rqstp->rq_server; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 370 | struct svc_pool *pool = rqstp->rq_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | svc_release_buffer(rqstp); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 373 | kfree(rqstp->rq_resp); |
| 374 | kfree(rqstp->rq_argp); |
| 375 | kfree(rqstp->rq_auth_data); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 376 | |
| 377 | spin_lock_bh(&pool->sp_lock); |
| 378 | pool->sp_nrthreads--; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame^] | 379 | list_del(&rqstp->rq_all); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 380 | spin_unlock_bh(&pool->sp_lock); |
| 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | kfree(rqstp); |
| 383 | |
| 384 | /* Release the server */ |
| 385 | if (serv) |
| 386 | svc_destroy(serv); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Register an RPC service with the local portmapper. |
| 391 | * To unregister a service, call this routine with |
| 392 | * proto and port == 0. |
| 393 | */ |
| 394 | int |
| 395 | svc_register(struct svc_serv *serv, int proto, unsigned short port) |
| 396 | { |
| 397 | struct svc_program *progp; |
| 398 | unsigned long flags; |
| 399 | int i, error = 0, dummy; |
| 400 | |
| 401 | progp = serv->sv_program; |
| 402 | |
| 403 | dprintk("RPC: svc_register(%s, %s, %d)\n", |
| 404 | progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port); |
| 405 | |
| 406 | if (!port) |
| 407 | clear_thread_flag(TIF_SIGPENDING); |
| 408 | |
| 409 | for (i = 0; i < progp->pg_nvers; i++) { |
| 410 | if (progp->pg_vers[i] == NULL) |
| 411 | continue; |
| 412 | error = rpc_register(progp->pg_prog, i, proto, port, &dummy); |
| 413 | if (error < 0) |
| 414 | break; |
| 415 | if (port && !dummy) { |
| 416 | error = -EACCES; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (!port) { |
| 422 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 423 | recalc_sigpending(); |
| 424 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 425 | } |
| 426 | |
| 427 | return error; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Process the RPC request. |
| 432 | */ |
| 433 | int |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 434 | svc_process(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | struct svc_program *progp; |
| 437 | struct svc_version *versp = NULL; /* compiler food */ |
| 438 | struct svc_procedure *procp = NULL; |
| 439 | struct kvec * argv = &rqstp->rq_arg.head[0]; |
| 440 | struct kvec * resv = &rqstp->rq_res.head[0]; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 441 | struct svc_serv *serv = rqstp->rq_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | kxdrproc_t xdr; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 443 | __be32 *statp; |
| 444 | u32 dir, prog, vers, proc; |
| 445 | __be32 auth_stat, rpc_stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | int auth_res; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 447 | __be32 *accept_statp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | rpc_stat = rpc_success; |
| 450 | |
| 451 | if (argv->iov_len < 6*4) |
| 452 | goto err_short_len; |
| 453 | |
| 454 | /* setup response xdr_buf. |
| 455 | * Initially it has just one page |
| 456 | */ |
| 457 | svc_take_page(rqstp); /* must succeed */ |
| 458 | resv->iov_base = page_address(rqstp->rq_respages[0]); |
| 459 | resv->iov_len = 0; |
| 460 | rqstp->rq_res.pages = rqstp->rq_respages+1; |
| 461 | rqstp->rq_res.len = 0; |
| 462 | rqstp->rq_res.page_base = 0; |
| 463 | rqstp->rq_res.page_len = 0; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 464 | rqstp->rq_res.buflen = PAGE_SIZE; |
J. Bruce Fields | 7c9fdcf | 2006-06-30 01:56:19 -0700 | [diff] [blame] | 465 | rqstp->rq_res.tail[0].iov_base = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | rqstp->rq_res.tail[0].iov_len = 0; |
J. Bruce Fields | 5c04c46 | 2006-06-30 01:56:19 -0700 | [diff] [blame] | 467 | /* Will be turned off only in gss privacy case: */ |
| 468 | rqstp->rq_sendfile_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* tcp needs a space for the record length... */ |
| 470 | if (rqstp->rq_prot == IPPROTO_TCP) |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 471 | svc_putnl(resv, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | rqstp->rq_xid = svc_getu32(argv); |
| 474 | svc_putu32(resv, rqstp->rq_xid); |
| 475 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 476 | dir = svc_getnl(argv); |
| 477 | vers = svc_getnl(argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | /* First words of reply: */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 480 | svc_putnl(resv, 1); /* REPLY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | if (dir != 0) /* direction != CALL */ |
| 483 | goto err_bad_dir; |
| 484 | if (vers != 2) /* RPC version number */ |
| 485 | goto err_bad_rpc; |
| 486 | |
| 487 | /* Save position in case we later decide to reject: */ |
| 488 | accept_statp = resv->iov_base + resv->iov_len; |
| 489 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 490 | svc_putnl(resv, 0); /* ACCEPT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 492 | rqstp->rq_prog = prog = svc_getnl(argv); /* program number */ |
| 493 | rqstp->rq_vers = vers = svc_getnl(argv); /* version number */ |
| 494 | rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | progp = serv->sv_program; |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 497 | |
| 498 | for (progp = serv->sv_program; progp; progp = progp->pg_next) |
| 499 | if (prog == progp->pg_prog) |
| 500 | break; |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Decode auth data, and add verifier to reply buffer. |
| 504 | * We do this before anything else in order to get a decent |
| 505 | * auth verifier. |
| 506 | */ |
| 507 | auth_res = svc_authenticate(rqstp, &auth_stat); |
| 508 | /* Also give the program a chance to reject this call: */ |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 509 | if (auth_res == SVC_OK && progp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | auth_stat = rpc_autherr_badcred; |
| 511 | auth_res = progp->pg_authenticate(rqstp); |
| 512 | } |
| 513 | switch (auth_res) { |
| 514 | case SVC_OK: |
| 515 | break; |
| 516 | case SVC_GARBAGE: |
| 517 | rpc_stat = rpc_garbage_args; |
| 518 | goto err_bad; |
| 519 | case SVC_SYSERR: |
| 520 | rpc_stat = rpc_system_err; |
| 521 | goto err_bad; |
| 522 | case SVC_DENIED: |
| 523 | goto err_bad_auth; |
| 524 | case SVC_DROP: |
| 525 | goto dropit; |
| 526 | case SVC_COMPLETE: |
| 527 | goto sendit; |
| 528 | } |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 529 | |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 530 | if (progp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | goto err_bad_prog; |
| 532 | |
| 533 | if (vers >= progp->pg_nvers || |
| 534 | !(versp = progp->pg_vers[vers])) |
| 535 | goto err_bad_vers; |
| 536 | |
| 537 | procp = versp->vs_proc + proc; |
| 538 | if (proc >= versp->vs_nproc || !procp->pc_func) |
| 539 | goto err_bad_proc; |
| 540 | rqstp->rq_server = serv; |
| 541 | rqstp->rq_procinfo = procp; |
| 542 | |
| 543 | /* Syntactic check complete */ |
| 544 | serv->sv_stats->rpccnt++; |
| 545 | |
| 546 | /* Build the reply header. */ |
| 547 | statp = resv->iov_base +resv->iov_len; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 548 | svc_putnl(resv, RPC_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* Bump per-procedure stats counter */ |
| 551 | procp->pc_count++; |
| 552 | |
| 553 | /* Initialize storage for argp and resp */ |
| 554 | memset(rqstp->rq_argp, 0, procp->pc_argsize); |
| 555 | memset(rqstp->rq_resp, 0, procp->pc_ressize); |
| 556 | |
| 557 | /* un-reserve some of the out-queue now that we have a |
| 558 | * better idea of reply size |
| 559 | */ |
| 560 | if (procp->pc_xdrressize) |
| 561 | svc_reserve(rqstp, procp->pc_xdrressize<<2); |
| 562 | |
| 563 | /* Call the function that processes the request. */ |
| 564 | if (!versp->vs_dispatch) { |
| 565 | /* Decode arguments */ |
| 566 | xdr = procp->pc_decode; |
| 567 | if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp)) |
| 568 | goto err_garbage; |
| 569 | |
| 570 | *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); |
| 571 | |
| 572 | /* Encode reply */ |
| 573 | if (*statp == rpc_success && (xdr = procp->pc_encode) |
| 574 | && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) { |
| 575 | dprintk("svc: failed to encode reply\n"); |
| 576 | /* serv->sv_stats->rpcsystemerr++; */ |
| 577 | *statp = rpc_system_err; |
| 578 | } |
| 579 | } else { |
| 580 | dprintk("svc: calling dispatcher\n"); |
| 581 | if (!versp->vs_dispatch(rqstp, statp)) { |
| 582 | /* Release reply info */ |
| 583 | if (procp->pc_release) |
| 584 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 585 | goto dropit; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* Check RPC status result */ |
| 590 | if (*statp != rpc_success) |
| 591 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; |
| 592 | |
| 593 | /* Release reply info */ |
| 594 | if (procp->pc_release) |
| 595 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 596 | |
| 597 | if (procp->pc_encode == NULL) |
| 598 | goto dropit; |
| 599 | |
| 600 | sendit: |
| 601 | if (svc_authorise(rqstp)) |
| 602 | goto dropit; |
| 603 | return svc_send(rqstp); |
| 604 | |
| 605 | dropit: |
| 606 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ |
| 607 | dprintk("svc: svc_process dropit\n"); |
| 608 | svc_drop(rqstp); |
| 609 | return 0; |
| 610 | |
| 611 | err_short_len: |
| 612 | #ifdef RPC_PARANOIA |
| 613 | printk("svc: short len %Zd, dropping request\n", argv->iov_len); |
| 614 | #endif |
| 615 | goto dropit; /* drop request */ |
| 616 | |
| 617 | err_bad_dir: |
| 618 | #ifdef RPC_PARANOIA |
| 619 | printk("svc: bad direction %d, dropping request\n", dir); |
| 620 | #endif |
| 621 | serv->sv_stats->rpcbadfmt++; |
| 622 | goto dropit; /* drop request */ |
| 623 | |
| 624 | err_bad_rpc: |
| 625 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 626 | svc_putnl(resv, 1); /* REJECT */ |
| 627 | svc_putnl(resv, 0); /* RPC_MISMATCH */ |
| 628 | svc_putnl(resv, 2); /* Only RPCv2 supported */ |
| 629 | svc_putnl(resv, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | goto sendit; |
| 631 | |
| 632 | err_bad_auth: |
| 633 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); |
| 634 | serv->sv_stats->rpcbadauth++; |
| 635 | /* Restore write pointer to location of accept status: */ |
| 636 | xdr_ressize_check(rqstp, accept_statp); |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 637 | svc_putnl(resv, 1); /* REJECT */ |
| 638 | svc_putnl(resv, 1); /* AUTH_ERROR */ |
| 639 | svc_putnl(resv, ntohl(auth_stat)); /* status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | goto sendit; |
| 641 | |
| 642 | err_bad_prog: |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 643 | dprintk("svc: unknown program %d\n", prog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 645 | svc_putnl(resv, RPC_PROG_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | goto sendit; |
| 647 | |
| 648 | err_bad_vers: |
| 649 | #ifdef RPC_PARANOIA |
| 650 | printk("svc: unknown version (%d)\n", vers); |
| 651 | #endif |
| 652 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 653 | svc_putnl(resv, RPC_PROG_MISMATCH); |
| 654 | svc_putnl(resv, progp->pg_lovers); |
| 655 | svc_putnl(resv, progp->pg_hivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | goto sendit; |
| 657 | |
| 658 | err_bad_proc: |
| 659 | #ifdef RPC_PARANOIA |
| 660 | printk("svc: unknown procedure (%d)\n", proc); |
| 661 | #endif |
| 662 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 663 | svc_putnl(resv, RPC_PROC_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | goto sendit; |
| 665 | |
| 666 | err_garbage: |
| 667 | #ifdef RPC_PARANOIA |
| 668 | printk("svc: failed to decode args\n"); |
| 669 | #endif |
| 670 | rpc_stat = rpc_garbage_args; |
| 671 | err_bad: |
| 672 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 673 | svc_putnl(resv, ntohl(rpc_stat)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | goto sendit; |
| 675 | } |