svc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. * Multiple threads pools and NUMAisation
  9. * Copyright (c) 2006 Silicon Graphics, Inc.
  10. * by Greg Banks <gnb@melbourne.sgi.com>
  11. */
  12. #include <linux/linkage.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/net.h>
  16. #include <linux/in.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/sunrpc/types.h>
  21. #include <linux/sunrpc/xdr.h>
  22. #include <linux/sunrpc/stats.h>
  23. #include <linux/sunrpc/svcsock.h>
  24. #include <linux/sunrpc/clnt.h>
  25. #define RPCDBG_FACILITY RPCDBG_SVCDSP
  26. #define RPC_PARANOIA 1
  27. /*
  28. * Mode for mapping cpus to pools.
  29. */
  30. enum {
  31. SVC_POOL_NONE = -1, /* uninitialised, choose one of the others */
  32. SVC_POOL_GLOBAL, /* no mapping, just a single global pool
  33. * (legacy & UP mode) */
  34. SVC_POOL_PERCPU, /* one pool per cpu */
  35. SVC_POOL_PERNODE /* one pool per numa node */
  36. };
  37. /*
  38. * Structure for mapping cpus to pools and vice versa.
  39. * Setup once during sunrpc initialisation.
  40. */
  41. static struct svc_pool_map {
  42. int mode; /* Note: int not enum to avoid
  43. * warnings about "enumeration value
  44. * not handled in switch" */
  45. unsigned int npools;
  46. unsigned int *pool_to; /* maps pool id to cpu or node */
  47. unsigned int *to_pool; /* maps cpu or node to pool id */
  48. } svc_pool_map = {
  49. .mode = SVC_POOL_NONE
  50. };
  51. /*
  52. * Detect best pool mapping mode heuristically,
  53. * according to the machine's topology.
  54. */
  55. static int
  56. svc_pool_map_choose_mode(void)
  57. {
  58. unsigned int node;
  59. if (num_online_nodes() > 1) {
  60. /*
  61. * Actually have multiple NUMA nodes,
  62. * so split pools on NUMA node boundaries
  63. */
  64. return SVC_POOL_PERNODE;
  65. }
  66. node = any_online_node(node_online_map);
  67. if (nr_cpus_node(node) > 2) {
  68. /*
  69. * Non-trivial SMP, or CONFIG_NUMA on
  70. * non-NUMA hardware, e.g. with a generic
  71. * x86_64 kernel on Xeons. In this case we
  72. * want to divide the pools on cpu boundaries.
  73. */
  74. return SVC_POOL_PERCPU;
  75. }
  76. /* default: one global pool */
  77. return SVC_POOL_GLOBAL;
  78. }
  79. /*
  80. * Allocate the to_pool[] and pool_to[] arrays.
  81. * Returns 0 on success or an errno.
  82. */
  83. static int
  84. svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools)
  85. {
  86. m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  87. if (!m->to_pool)
  88. goto fail;
  89. m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  90. if (!m->pool_to)
  91. goto fail_free;
  92. return 0;
  93. fail_free:
  94. kfree(m->to_pool);
  95. fail:
  96. return -ENOMEM;
  97. }
  98. /*
  99. * Initialise the pool map for SVC_POOL_PERCPU mode.
  100. * Returns number of pools or <0 on error.
  101. */
  102. static int
  103. svc_pool_map_init_percpu(struct svc_pool_map *m)
  104. {
  105. unsigned int maxpools = highest_possible_processor_id()+1;
  106. unsigned int pidx = 0;
  107. unsigned int cpu;
  108. int err;
  109. err = svc_pool_map_alloc_arrays(m, maxpools);
  110. if (err)
  111. return err;
  112. for_each_online_cpu(cpu) {
  113. BUG_ON(pidx > maxpools);
  114. m->to_pool[cpu] = pidx;
  115. m->pool_to[pidx] = cpu;
  116. pidx++;
  117. }
  118. /* cpus brought online later all get mapped to pool0, sorry */
  119. return pidx;
  120. };
  121. /*
  122. * Initialise the pool map for SVC_POOL_PERNODE mode.
  123. * Returns number of pools or <0 on error.
  124. */
  125. static int
  126. svc_pool_map_init_pernode(struct svc_pool_map *m)
  127. {
  128. unsigned int maxpools = highest_possible_node_id()+1;
  129. unsigned int pidx = 0;
  130. unsigned int node;
  131. int err;
  132. err = svc_pool_map_alloc_arrays(m, maxpools);
  133. if (err)
  134. return err;
  135. for_each_node_with_cpus(node) {
  136. /* some architectures (e.g. SN2) have cpuless nodes */
  137. BUG_ON(pidx > maxpools);
  138. m->to_pool[node] = pidx;
  139. m->pool_to[pidx] = node;
  140. pidx++;
  141. }
  142. /* nodes brought online later all get mapped to pool0, sorry */
  143. return pidx;
  144. }
  145. /*
  146. * Build the global map of cpus to pools and vice versa.
  147. */
  148. static unsigned int
  149. svc_pool_map_init(void)
  150. {
  151. struct svc_pool_map *m = &svc_pool_map;
  152. int npools = -1;
  153. if (m->mode != SVC_POOL_NONE)
  154. return m->npools;
  155. m->mode = svc_pool_map_choose_mode();
  156. switch (m->mode) {
  157. case SVC_POOL_PERCPU:
  158. npools = svc_pool_map_init_percpu(m);
  159. break;
  160. case SVC_POOL_PERNODE:
  161. npools = svc_pool_map_init_pernode(m);
  162. break;
  163. }
  164. if (npools < 0) {
  165. /* default, or memory allocation failure */
  166. npools = 1;
  167. m->mode = SVC_POOL_GLOBAL;
  168. }
  169. m->npools = npools;
  170. return m->npools;
  171. }
  172. /*
  173. * Set the current thread's cpus_allowed mask so that it
  174. * will only run on cpus in the given pool.
  175. *
  176. * Returns 1 and fills in oldmask iff a cpumask was applied.
  177. */
  178. static inline int
  179. svc_pool_map_set_cpumask(unsigned int pidx, cpumask_t *oldmask)
  180. {
  181. struct svc_pool_map *m = &svc_pool_map;
  182. unsigned int node; /* or cpu */
  183. /*
  184. * The caller checks for sv_nrpools > 1, which
  185. * implies that we've been initialized and the
  186. * map mode is not NONE.
  187. */
  188. BUG_ON(m->mode == SVC_POOL_NONE);
  189. switch (m->mode)
  190. {
  191. default:
  192. return 0;
  193. case SVC_POOL_PERCPU:
  194. node = m->pool_to[pidx];
  195. *oldmask = current->cpus_allowed;
  196. set_cpus_allowed(current, cpumask_of_cpu(node));
  197. return 1;
  198. case SVC_POOL_PERNODE:
  199. node = m->pool_to[pidx];
  200. *oldmask = current->cpus_allowed;
  201. set_cpus_allowed(current, node_to_cpumask(node));
  202. return 1;
  203. }
  204. }
  205. /*
  206. * Use the mapping mode to choose a pool for a given CPU.
  207. * Used when enqueueing an incoming RPC. Always returns
  208. * a non-NULL pool pointer.
  209. */
  210. struct svc_pool *
  211. svc_pool_for_cpu(struct svc_serv *serv, int cpu)
  212. {
  213. struct svc_pool_map *m = &svc_pool_map;
  214. unsigned int pidx = 0;
  215. /*
  216. * SVC_POOL_NONE happens in a pure client when
  217. * lockd is brought up, so silently treat it the
  218. * same as SVC_POOL_GLOBAL.
  219. */
  220. switch (m->mode) {
  221. case SVC_POOL_PERCPU:
  222. pidx = m->to_pool[cpu];
  223. break;
  224. case SVC_POOL_PERNODE:
  225. pidx = m->to_pool[cpu_to_node(cpu)];
  226. break;
  227. }
  228. return &serv->sv_pools[pidx % serv->sv_nrpools];
  229. }
  230. /*
  231. * Create an RPC service
  232. */
  233. static struct svc_serv *
  234. __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
  235. void (*shutdown)(struct svc_serv *serv))
  236. {
  237. struct svc_serv *serv;
  238. int vers;
  239. unsigned int xdrsize;
  240. unsigned int i;
  241. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  242. return NULL;
  243. serv->sv_name = prog->pg_name;
  244. serv->sv_program = prog;
  245. serv->sv_nrthreads = 1;
  246. serv->sv_stats = prog->pg_stats;
  247. serv->sv_bufsz = bufsize? bufsize : 4096;
  248. serv->sv_shutdown = shutdown;
  249. xdrsize = 0;
  250. while (prog) {
  251. prog->pg_lovers = prog->pg_nvers-1;
  252. for (vers=0; vers<prog->pg_nvers ; vers++)
  253. if (prog->pg_vers[vers]) {
  254. prog->pg_hivers = vers;
  255. if (prog->pg_lovers > vers)
  256. prog->pg_lovers = vers;
  257. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  258. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  259. }
  260. prog = prog->pg_next;
  261. }
  262. serv->sv_xdrsize = xdrsize;
  263. INIT_LIST_HEAD(&serv->sv_tempsocks);
  264. INIT_LIST_HEAD(&serv->sv_permsocks);
  265. init_timer(&serv->sv_temptimer);
  266. spin_lock_init(&serv->sv_lock);
  267. serv->sv_nrpools = npools;
  268. serv->sv_pools =
  269. kcalloc(sizeof(struct svc_pool), serv->sv_nrpools,
  270. GFP_KERNEL);
  271. if (!serv->sv_pools) {
  272. kfree(serv);
  273. return NULL;
  274. }
  275. for (i = 0; i < serv->sv_nrpools; i++) {
  276. struct svc_pool *pool = &serv->sv_pools[i];
  277. dprintk("initialising pool %u for %s\n",
  278. i, serv->sv_name);
  279. pool->sp_id = i;
  280. INIT_LIST_HEAD(&pool->sp_threads);
  281. INIT_LIST_HEAD(&pool->sp_sockets);
  282. INIT_LIST_HEAD(&pool->sp_all_threads);
  283. spin_lock_init(&pool->sp_lock);
  284. }
  285. /* Remove any stale portmap registrations */
  286. svc_register(serv, 0, 0);
  287. return serv;
  288. }
  289. struct svc_serv *
  290. svc_create(struct svc_program *prog, unsigned int bufsize,
  291. void (*shutdown)(struct svc_serv *serv))
  292. {
  293. return __svc_create(prog, bufsize, /*npools*/1, shutdown);
  294. }
  295. struct svc_serv *
  296. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  297. void (*shutdown)(struct svc_serv *serv),
  298. svc_thread_fn func, int sig, struct module *mod)
  299. {
  300. struct svc_serv *serv;
  301. unsigned int npools = svc_pool_map_init();
  302. serv = __svc_create(prog, bufsize, npools, shutdown);
  303. if (serv != NULL) {
  304. serv->sv_function = func;
  305. serv->sv_kill_signal = sig;
  306. serv->sv_module = mod;
  307. }
  308. return serv;
  309. }
  310. /*
  311. * Destroy an RPC service. Should be called with the BKL held
  312. */
  313. void
  314. svc_destroy(struct svc_serv *serv)
  315. {
  316. struct svc_sock *svsk;
  317. dprintk("RPC: svc_destroy(%s, %d)\n",
  318. serv->sv_program->pg_name,
  319. serv->sv_nrthreads);
  320. if (serv->sv_nrthreads) {
  321. if (--(serv->sv_nrthreads) != 0) {
  322. svc_sock_update_bufs(serv);
  323. return;
  324. }
  325. } else
  326. printk("svc_destroy: no threads for serv=%p!\n", serv);
  327. del_timer_sync(&serv->sv_temptimer);
  328. while (!list_empty(&serv->sv_tempsocks)) {
  329. svsk = list_entry(serv->sv_tempsocks.next,
  330. struct svc_sock,
  331. sk_list);
  332. svc_delete_socket(svsk);
  333. }
  334. if (serv->sv_shutdown)
  335. serv->sv_shutdown(serv);
  336. while (!list_empty(&serv->sv_permsocks)) {
  337. svsk = list_entry(serv->sv_permsocks.next,
  338. struct svc_sock,
  339. sk_list);
  340. svc_delete_socket(svsk);
  341. }
  342. cache_clean_deferred(serv);
  343. /* Unregister service with the portmapper */
  344. svc_register(serv, 0, 0);
  345. kfree(serv->sv_pools);
  346. kfree(serv);
  347. }
  348. /*
  349. * Allocate an RPC server's buffer space.
  350. * We allocate pages and place them in rq_argpages.
  351. */
  352. static int
  353. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
  354. {
  355. int pages;
  356. int arghi;
  357. if (size > RPCSVC_MAXPAYLOAD)
  358. size = RPCSVC_MAXPAYLOAD;
  359. pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
  360. rqstp->rq_argused = 0;
  361. rqstp->rq_resused = 0;
  362. arghi = 0;
  363. BUG_ON(pages > RPCSVC_MAXPAGES);
  364. while (pages) {
  365. struct page *p = alloc_page(GFP_KERNEL);
  366. if (!p)
  367. break;
  368. rqstp->rq_argpages[arghi++] = p;
  369. pages--;
  370. }
  371. rqstp->rq_arghi = arghi;
  372. return ! pages;
  373. }
  374. /*
  375. * Release an RPC server buffer
  376. */
  377. static void
  378. svc_release_buffer(struct svc_rqst *rqstp)
  379. {
  380. while (rqstp->rq_arghi)
  381. put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
  382. while (rqstp->rq_resused) {
  383. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  384. continue;
  385. put_page(rqstp->rq_respages[rqstp->rq_resused]);
  386. }
  387. rqstp->rq_argused = 0;
  388. }
  389. /*
  390. * Create a thread in the given pool. Caller must hold BKL.
  391. * On a NUMA or SMP machine, with a multi-pool serv, the thread
  392. * will be restricted to run on the cpus belonging to the pool.
  393. */
  394. static int
  395. __svc_create_thread(svc_thread_fn func, struct svc_serv *serv,
  396. struct svc_pool *pool)
  397. {
  398. struct svc_rqst *rqstp;
  399. int error = -ENOMEM;
  400. int have_oldmask = 0;
  401. cpumask_t oldmask;
  402. rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
  403. if (!rqstp)
  404. goto out;
  405. init_waitqueue_head(&rqstp->rq_wait);
  406. if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  407. || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  408. || !svc_init_buffer(rqstp, serv->sv_bufsz))
  409. goto out_thread;
  410. serv->sv_nrthreads++;
  411. spin_lock_bh(&pool->sp_lock);
  412. pool->sp_nrthreads++;
  413. list_add(&rqstp->rq_all, &pool->sp_all_threads);
  414. spin_unlock_bh(&pool->sp_lock);
  415. rqstp->rq_server = serv;
  416. rqstp->rq_pool = pool;
  417. if (serv->sv_nrpools > 1)
  418. have_oldmask = svc_pool_map_set_cpumask(pool->sp_id, &oldmask);
  419. error = kernel_thread((int (*)(void *)) func, rqstp, 0);
  420. if (have_oldmask)
  421. set_cpus_allowed(current, oldmask);
  422. if (error < 0)
  423. goto out_thread;
  424. svc_sock_update_bufs(serv);
  425. error = 0;
  426. out:
  427. return error;
  428. out_thread:
  429. svc_exit_thread(rqstp);
  430. goto out;
  431. }
  432. /*
  433. * Create a thread in the default pool. Caller must hold BKL.
  434. */
  435. int
  436. svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
  437. {
  438. return __svc_create_thread(func, serv, &serv->sv_pools[0]);
  439. }
  440. /*
  441. * Choose a pool in which to create a new thread, for svc_set_num_threads
  442. */
  443. static inline struct svc_pool *
  444. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  445. {
  446. if (pool != NULL)
  447. return pool;
  448. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  449. }
  450. /*
  451. * Choose a thread to kill, for svc_set_num_threads
  452. */
  453. static inline struct task_struct *
  454. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  455. {
  456. unsigned int i;
  457. struct task_struct *task = NULL;
  458. if (pool != NULL) {
  459. spin_lock_bh(&pool->sp_lock);
  460. } else {
  461. /* choose a pool in round-robin fashion */
  462. for (i = 0; i < serv->sv_nrpools; i++) {
  463. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  464. spin_lock_bh(&pool->sp_lock);
  465. if (!list_empty(&pool->sp_all_threads))
  466. goto found_pool;
  467. spin_unlock_bh(&pool->sp_lock);
  468. }
  469. return NULL;
  470. }
  471. found_pool:
  472. if (!list_empty(&pool->sp_all_threads)) {
  473. struct svc_rqst *rqstp;
  474. /*
  475. * Remove from the pool->sp_all_threads list
  476. * so we don't try to kill it again.
  477. */
  478. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  479. list_del_init(&rqstp->rq_all);
  480. task = rqstp->rq_task;
  481. }
  482. spin_unlock_bh(&pool->sp_lock);
  483. return task;
  484. }
  485. /*
  486. * Create or destroy enough new threads to make the number
  487. * of threads the given number. If `pool' is non-NULL, applies
  488. * only to threads in that pool, otherwise round-robins between
  489. * all pools. Must be called with a svc_get() reference and
  490. * the BKL held.
  491. *
  492. * Destroying threads relies on the service threads filling in
  493. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  494. * has been created using svc_create_pooled().
  495. *
  496. * Based on code that used to be in nfsd_svc() but tweaked
  497. * to be pool-aware.
  498. */
  499. int
  500. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  501. {
  502. struct task_struct *victim;
  503. int error = 0;
  504. unsigned int state = serv->sv_nrthreads-1;
  505. if (pool == NULL) {
  506. /* The -1 assumes caller has done a svc_get() */
  507. nrservs -= (serv->sv_nrthreads-1);
  508. } else {
  509. spin_lock_bh(&pool->sp_lock);
  510. nrservs -= pool->sp_nrthreads;
  511. spin_unlock_bh(&pool->sp_lock);
  512. }
  513. /* create new threads */
  514. while (nrservs > 0) {
  515. nrservs--;
  516. __module_get(serv->sv_module);
  517. error = __svc_create_thread(serv->sv_function, serv,
  518. choose_pool(serv, pool, &state));
  519. if (error < 0) {
  520. module_put(serv->sv_module);
  521. break;
  522. }
  523. }
  524. /* destroy old threads */
  525. while (nrservs < 0 &&
  526. (victim = choose_victim(serv, pool, &state)) != NULL) {
  527. send_sig(serv->sv_kill_signal, victim, 1);
  528. nrservs++;
  529. }
  530. return error;
  531. }
  532. /*
  533. * Called from a server thread as it's exiting. Caller must hold BKL.
  534. */
  535. void
  536. svc_exit_thread(struct svc_rqst *rqstp)
  537. {
  538. struct svc_serv *serv = rqstp->rq_server;
  539. struct svc_pool *pool = rqstp->rq_pool;
  540. svc_release_buffer(rqstp);
  541. kfree(rqstp->rq_resp);
  542. kfree(rqstp->rq_argp);
  543. kfree(rqstp->rq_auth_data);
  544. spin_lock_bh(&pool->sp_lock);
  545. pool->sp_nrthreads--;
  546. list_del(&rqstp->rq_all);
  547. spin_unlock_bh(&pool->sp_lock);
  548. kfree(rqstp);
  549. /* Release the server */
  550. if (serv)
  551. svc_destroy(serv);
  552. }
  553. /*
  554. * Register an RPC service with the local portmapper.
  555. * To unregister a service, call this routine with
  556. * proto and port == 0.
  557. */
  558. int
  559. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  560. {
  561. struct svc_program *progp;
  562. unsigned long flags;
  563. int i, error = 0, dummy;
  564. progp = serv->sv_program;
  565. dprintk("RPC: svc_register(%s, %s, %d)\n",
  566. progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
  567. if (!port)
  568. clear_thread_flag(TIF_SIGPENDING);
  569. for (i = 0; i < progp->pg_nvers; i++) {
  570. if (progp->pg_vers[i] == NULL)
  571. continue;
  572. error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
  573. if (error < 0)
  574. break;
  575. if (port && !dummy) {
  576. error = -EACCES;
  577. break;
  578. }
  579. }
  580. if (!port) {
  581. spin_lock_irqsave(&current->sighand->siglock, flags);
  582. recalc_sigpending();
  583. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  584. }
  585. return error;
  586. }
  587. /*
  588. * Process the RPC request.
  589. */
  590. int
  591. svc_process(struct svc_rqst *rqstp)
  592. {
  593. struct svc_program *progp;
  594. struct svc_version *versp = NULL; /* compiler food */
  595. struct svc_procedure *procp = NULL;
  596. struct kvec * argv = &rqstp->rq_arg.head[0];
  597. struct kvec * resv = &rqstp->rq_res.head[0];
  598. struct svc_serv *serv = rqstp->rq_server;
  599. kxdrproc_t xdr;
  600. __be32 *statp;
  601. u32 dir, prog, vers, proc;
  602. __be32 auth_stat, rpc_stat;
  603. int auth_res;
  604. __be32 *accept_statp;
  605. rpc_stat = rpc_success;
  606. if (argv->iov_len < 6*4)
  607. goto err_short_len;
  608. /* setup response xdr_buf.
  609. * Initially it has just one page
  610. */
  611. svc_take_page(rqstp); /* must succeed */
  612. resv->iov_base = page_address(rqstp->rq_respages[0]);
  613. resv->iov_len = 0;
  614. rqstp->rq_res.pages = rqstp->rq_respages+1;
  615. rqstp->rq_res.len = 0;
  616. rqstp->rq_res.page_base = 0;
  617. rqstp->rq_res.page_len = 0;
  618. rqstp->rq_res.buflen = PAGE_SIZE;
  619. rqstp->rq_res.tail[0].iov_base = NULL;
  620. rqstp->rq_res.tail[0].iov_len = 0;
  621. /* Will be turned off only in gss privacy case: */
  622. rqstp->rq_sendfile_ok = 1;
  623. /* tcp needs a space for the record length... */
  624. if (rqstp->rq_prot == IPPROTO_TCP)
  625. svc_putnl(resv, 0);
  626. rqstp->rq_xid = svc_getu32(argv);
  627. svc_putu32(resv, rqstp->rq_xid);
  628. dir = svc_getnl(argv);
  629. vers = svc_getnl(argv);
  630. /* First words of reply: */
  631. svc_putnl(resv, 1); /* REPLY */
  632. if (dir != 0) /* direction != CALL */
  633. goto err_bad_dir;
  634. if (vers != 2) /* RPC version number */
  635. goto err_bad_rpc;
  636. /* Save position in case we later decide to reject: */
  637. accept_statp = resv->iov_base + resv->iov_len;
  638. svc_putnl(resv, 0); /* ACCEPT */
  639. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  640. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  641. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  642. progp = serv->sv_program;
  643. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  644. if (prog == progp->pg_prog)
  645. break;
  646. /*
  647. * Decode auth data, and add verifier to reply buffer.
  648. * We do this before anything else in order to get a decent
  649. * auth verifier.
  650. */
  651. auth_res = svc_authenticate(rqstp, &auth_stat);
  652. /* Also give the program a chance to reject this call: */
  653. if (auth_res == SVC_OK && progp) {
  654. auth_stat = rpc_autherr_badcred;
  655. auth_res = progp->pg_authenticate(rqstp);
  656. }
  657. switch (auth_res) {
  658. case SVC_OK:
  659. break;
  660. case SVC_GARBAGE:
  661. rpc_stat = rpc_garbage_args;
  662. goto err_bad;
  663. case SVC_SYSERR:
  664. rpc_stat = rpc_system_err;
  665. goto err_bad;
  666. case SVC_DENIED:
  667. goto err_bad_auth;
  668. case SVC_DROP:
  669. goto dropit;
  670. case SVC_COMPLETE:
  671. goto sendit;
  672. }
  673. if (progp == NULL)
  674. goto err_bad_prog;
  675. if (vers >= progp->pg_nvers ||
  676. !(versp = progp->pg_vers[vers]))
  677. goto err_bad_vers;
  678. procp = versp->vs_proc + proc;
  679. if (proc >= versp->vs_nproc || !procp->pc_func)
  680. goto err_bad_proc;
  681. rqstp->rq_server = serv;
  682. rqstp->rq_procinfo = procp;
  683. /* Syntactic check complete */
  684. serv->sv_stats->rpccnt++;
  685. /* Build the reply header. */
  686. statp = resv->iov_base +resv->iov_len;
  687. svc_putnl(resv, RPC_SUCCESS);
  688. /* Bump per-procedure stats counter */
  689. procp->pc_count++;
  690. /* Initialize storage for argp and resp */
  691. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  692. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  693. /* un-reserve some of the out-queue now that we have a
  694. * better idea of reply size
  695. */
  696. if (procp->pc_xdrressize)
  697. svc_reserve(rqstp, procp->pc_xdrressize<<2);
  698. /* Call the function that processes the request. */
  699. if (!versp->vs_dispatch) {
  700. /* Decode arguments */
  701. xdr = procp->pc_decode;
  702. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  703. goto err_garbage;
  704. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  705. /* Encode reply */
  706. if (*statp == rpc_success && (xdr = procp->pc_encode)
  707. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  708. dprintk("svc: failed to encode reply\n");
  709. /* serv->sv_stats->rpcsystemerr++; */
  710. *statp = rpc_system_err;
  711. }
  712. } else {
  713. dprintk("svc: calling dispatcher\n");
  714. if (!versp->vs_dispatch(rqstp, statp)) {
  715. /* Release reply info */
  716. if (procp->pc_release)
  717. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  718. goto dropit;
  719. }
  720. }
  721. /* Check RPC status result */
  722. if (*statp != rpc_success)
  723. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  724. /* Release reply info */
  725. if (procp->pc_release)
  726. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  727. if (procp->pc_encode == NULL)
  728. goto dropit;
  729. sendit:
  730. if (svc_authorise(rqstp))
  731. goto dropit;
  732. return svc_send(rqstp);
  733. dropit:
  734. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  735. dprintk("svc: svc_process dropit\n");
  736. svc_drop(rqstp);
  737. return 0;
  738. err_short_len:
  739. #ifdef RPC_PARANOIA
  740. printk("svc: short len %Zd, dropping request\n", argv->iov_len);
  741. #endif
  742. goto dropit; /* drop request */
  743. err_bad_dir:
  744. #ifdef RPC_PARANOIA
  745. printk("svc: bad direction %d, dropping request\n", dir);
  746. #endif
  747. serv->sv_stats->rpcbadfmt++;
  748. goto dropit; /* drop request */
  749. err_bad_rpc:
  750. serv->sv_stats->rpcbadfmt++;
  751. svc_putnl(resv, 1); /* REJECT */
  752. svc_putnl(resv, 0); /* RPC_MISMATCH */
  753. svc_putnl(resv, 2); /* Only RPCv2 supported */
  754. svc_putnl(resv, 2);
  755. goto sendit;
  756. err_bad_auth:
  757. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  758. serv->sv_stats->rpcbadauth++;
  759. /* Restore write pointer to location of accept status: */
  760. xdr_ressize_check(rqstp, accept_statp);
  761. svc_putnl(resv, 1); /* REJECT */
  762. svc_putnl(resv, 1); /* AUTH_ERROR */
  763. svc_putnl(resv, ntohl(auth_stat)); /* status */
  764. goto sendit;
  765. err_bad_prog:
  766. dprintk("svc: unknown program %d\n", prog);
  767. serv->sv_stats->rpcbadfmt++;
  768. svc_putnl(resv, RPC_PROG_UNAVAIL);
  769. goto sendit;
  770. err_bad_vers:
  771. #ifdef RPC_PARANOIA
  772. printk("svc: unknown version (%d)\n", vers);
  773. #endif
  774. serv->sv_stats->rpcbadfmt++;
  775. svc_putnl(resv, RPC_PROG_MISMATCH);
  776. svc_putnl(resv, progp->pg_lovers);
  777. svc_putnl(resv, progp->pg_hivers);
  778. goto sendit;
  779. err_bad_proc:
  780. #ifdef RPC_PARANOIA
  781. printk("svc: unknown procedure (%d)\n", proc);
  782. #endif
  783. serv->sv_stats->rpcbadfmt++;
  784. svc_putnl(resv, RPC_PROC_UNAVAIL);
  785. goto sendit;
  786. err_garbage:
  787. #ifdef RPC_PARANOIA
  788. printk("svc: failed to decode args\n");
  789. #endif
  790. rpc_stat = rpc_garbage_args;
  791. err_bad:
  792. serv->sv_stats->rpcbadfmt++;
  793. svc_putnl(resv, ntohl(rpc_stat));
  794. goto sendit;
  795. }