svc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. if (bufsize > RPCSVC_MAXPAYLOAD)
  248. bufsize = RPCSVC_MAXPAYLOAD;
  249. serv->sv_max_payload = bufsize? bufsize : 4096;
  250. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  251. serv->sv_shutdown = shutdown;
  252. xdrsize = 0;
  253. while (prog) {
  254. prog->pg_lovers = prog->pg_nvers-1;
  255. for (vers=0; vers<prog->pg_nvers ; vers++)
  256. if (prog->pg_vers[vers]) {
  257. prog->pg_hivers = vers;
  258. if (prog->pg_lovers > vers)
  259. prog->pg_lovers = vers;
  260. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  261. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  262. }
  263. prog = prog->pg_next;
  264. }
  265. serv->sv_xdrsize = xdrsize;
  266. INIT_LIST_HEAD(&serv->sv_tempsocks);
  267. INIT_LIST_HEAD(&serv->sv_permsocks);
  268. init_timer(&serv->sv_temptimer);
  269. spin_lock_init(&serv->sv_lock);
  270. serv->sv_nrpools = npools;
  271. serv->sv_pools =
  272. kcalloc(sizeof(struct svc_pool), serv->sv_nrpools,
  273. GFP_KERNEL);
  274. if (!serv->sv_pools) {
  275. kfree(serv);
  276. return NULL;
  277. }
  278. for (i = 0; i < serv->sv_nrpools; i++) {
  279. struct svc_pool *pool = &serv->sv_pools[i];
  280. dprintk("initialising pool %u for %s\n",
  281. i, serv->sv_name);
  282. pool->sp_id = i;
  283. INIT_LIST_HEAD(&pool->sp_threads);
  284. INIT_LIST_HEAD(&pool->sp_sockets);
  285. INIT_LIST_HEAD(&pool->sp_all_threads);
  286. spin_lock_init(&pool->sp_lock);
  287. }
  288. /* Remove any stale portmap registrations */
  289. svc_register(serv, 0, 0);
  290. return serv;
  291. }
  292. struct svc_serv *
  293. svc_create(struct svc_program *prog, unsigned int bufsize,
  294. void (*shutdown)(struct svc_serv *serv))
  295. {
  296. return __svc_create(prog, bufsize, /*npools*/1, shutdown);
  297. }
  298. struct svc_serv *
  299. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  300. void (*shutdown)(struct svc_serv *serv),
  301. svc_thread_fn func, int sig, struct module *mod)
  302. {
  303. struct svc_serv *serv;
  304. unsigned int npools = svc_pool_map_init();
  305. serv = __svc_create(prog, bufsize, npools, shutdown);
  306. if (serv != NULL) {
  307. serv->sv_function = func;
  308. serv->sv_kill_signal = sig;
  309. serv->sv_module = mod;
  310. }
  311. return serv;
  312. }
  313. /*
  314. * Destroy an RPC service. Should be called with the BKL held
  315. */
  316. void
  317. svc_destroy(struct svc_serv *serv)
  318. {
  319. struct svc_sock *svsk;
  320. dprintk("RPC: svc_destroy(%s, %d)\n",
  321. serv->sv_program->pg_name,
  322. serv->sv_nrthreads);
  323. if (serv->sv_nrthreads) {
  324. if (--(serv->sv_nrthreads) != 0) {
  325. svc_sock_update_bufs(serv);
  326. return;
  327. }
  328. } else
  329. printk("svc_destroy: no threads for serv=%p!\n", serv);
  330. del_timer_sync(&serv->sv_temptimer);
  331. while (!list_empty(&serv->sv_tempsocks)) {
  332. svsk = list_entry(serv->sv_tempsocks.next,
  333. struct svc_sock,
  334. sk_list);
  335. svc_delete_socket(svsk);
  336. }
  337. if (serv->sv_shutdown)
  338. serv->sv_shutdown(serv);
  339. while (!list_empty(&serv->sv_permsocks)) {
  340. svsk = list_entry(serv->sv_permsocks.next,
  341. struct svc_sock,
  342. sk_list);
  343. svc_delete_socket(svsk);
  344. }
  345. cache_clean_deferred(serv);
  346. /* Unregister service with the portmapper */
  347. svc_register(serv, 0, 0);
  348. kfree(serv->sv_pools);
  349. kfree(serv);
  350. }
  351. /*
  352. * Allocate an RPC server's buffer space.
  353. * We allocate pages and place them in rq_argpages.
  354. */
  355. static int
  356. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
  357. {
  358. int pages;
  359. int arghi;
  360. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  361. * We assume one is at most one page
  362. */
  363. arghi = 0;
  364. BUG_ON(pages > RPCSVC_MAXPAGES);
  365. while (pages) {
  366. struct page *p = alloc_page(GFP_KERNEL);
  367. if (!p)
  368. break;
  369. rqstp->rq_pages[arghi++] = p;
  370. pages--;
  371. }
  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. int i;
  381. for (i=0; i<ARRAY_SIZE(rqstp->rq_pages); i++)
  382. if (rqstp->rq_pages[i])
  383. put_page(rqstp->rq_pages[i]);
  384. }
  385. /*
  386. * Create a thread in the given pool. Caller must hold BKL.
  387. * On a NUMA or SMP machine, with a multi-pool serv, the thread
  388. * will be restricted to run on the cpus belonging to the pool.
  389. */
  390. static int
  391. __svc_create_thread(svc_thread_fn func, struct svc_serv *serv,
  392. struct svc_pool *pool)
  393. {
  394. struct svc_rqst *rqstp;
  395. int error = -ENOMEM;
  396. int have_oldmask = 0;
  397. cpumask_t oldmask;
  398. rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
  399. if (!rqstp)
  400. goto out;
  401. init_waitqueue_head(&rqstp->rq_wait);
  402. if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  403. || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  404. || !svc_init_buffer(rqstp, serv->sv_max_mesg))
  405. goto out_thread;
  406. serv->sv_nrthreads++;
  407. spin_lock_bh(&pool->sp_lock);
  408. pool->sp_nrthreads++;
  409. list_add(&rqstp->rq_all, &pool->sp_all_threads);
  410. spin_unlock_bh(&pool->sp_lock);
  411. rqstp->rq_server = serv;
  412. rqstp->rq_pool = pool;
  413. if (serv->sv_nrpools > 1)
  414. have_oldmask = svc_pool_map_set_cpumask(pool->sp_id, &oldmask);
  415. error = kernel_thread((int (*)(void *)) func, rqstp, 0);
  416. if (have_oldmask)
  417. set_cpus_allowed(current, oldmask);
  418. if (error < 0)
  419. goto out_thread;
  420. svc_sock_update_bufs(serv);
  421. error = 0;
  422. out:
  423. return error;
  424. out_thread:
  425. svc_exit_thread(rqstp);
  426. goto out;
  427. }
  428. /*
  429. * Create a thread in the default pool. Caller must hold BKL.
  430. */
  431. int
  432. svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
  433. {
  434. return __svc_create_thread(func, serv, &serv->sv_pools[0]);
  435. }
  436. /*
  437. * Choose a pool in which to create a new thread, for svc_set_num_threads
  438. */
  439. static inline struct svc_pool *
  440. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  441. {
  442. if (pool != NULL)
  443. return pool;
  444. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  445. }
  446. /*
  447. * Choose a thread to kill, for svc_set_num_threads
  448. */
  449. static inline struct task_struct *
  450. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  451. {
  452. unsigned int i;
  453. struct task_struct *task = NULL;
  454. if (pool != NULL) {
  455. spin_lock_bh(&pool->sp_lock);
  456. } else {
  457. /* choose a pool in round-robin fashion */
  458. for (i = 0; i < serv->sv_nrpools; i++) {
  459. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  460. spin_lock_bh(&pool->sp_lock);
  461. if (!list_empty(&pool->sp_all_threads))
  462. goto found_pool;
  463. spin_unlock_bh(&pool->sp_lock);
  464. }
  465. return NULL;
  466. }
  467. found_pool:
  468. if (!list_empty(&pool->sp_all_threads)) {
  469. struct svc_rqst *rqstp;
  470. /*
  471. * Remove from the pool->sp_all_threads list
  472. * so we don't try to kill it again.
  473. */
  474. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  475. list_del_init(&rqstp->rq_all);
  476. task = rqstp->rq_task;
  477. }
  478. spin_unlock_bh(&pool->sp_lock);
  479. return task;
  480. }
  481. /*
  482. * Create or destroy enough new threads to make the number
  483. * of threads the given number. If `pool' is non-NULL, applies
  484. * only to threads in that pool, otherwise round-robins between
  485. * all pools. Must be called with a svc_get() reference and
  486. * the BKL held.
  487. *
  488. * Destroying threads relies on the service threads filling in
  489. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  490. * has been created using svc_create_pooled().
  491. *
  492. * Based on code that used to be in nfsd_svc() but tweaked
  493. * to be pool-aware.
  494. */
  495. int
  496. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  497. {
  498. struct task_struct *victim;
  499. int error = 0;
  500. unsigned int state = serv->sv_nrthreads-1;
  501. if (pool == NULL) {
  502. /* The -1 assumes caller has done a svc_get() */
  503. nrservs -= (serv->sv_nrthreads-1);
  504. } else {
  505. spin_lock_bh(&pool->sp_lock);
  506. nrservs -= pool->sp_nrthreads;
  507. spin_unlock_bh(&pool->sp_lock);
  508. }
  509. /* create new threads */
  510. while (nrservs > 0) {
  511. nrservs--;
  512. __module_get(serv->sv_module);
  513. error = __svc_create_thread(serv->sv_function, serv,
  514. choose_pool(serv, pool, &state));
  515. if (error < 0) {
  516. module_put(serv->sv_module);
  517. break;
  518. }
  519. }
  520. /* destroy old threads */
  521. while (nrservs < 0 &&
  522. (victim = choose_victim(serv, pool, &state)) != NULL) {
  523. send_sig(serv->sv_kill_signal, victim, 1);
  524. nrservs++;
  525. }
  526. return error;
  527. }
  528. /*
  529. * Called from a server thread as it's exiting. Caller must hold BKL.
  530. */
  531. void
  532. svc_exit_thread(struct svc_rqst *rqstp)
  533. {
  534. struct svc_serv *serv = rqstp->rq_server;
  535. struct svc_pool *pool = rqstp->rq_pool;
  536. svc_release_buffer(rqstp);
  537. kfree(rqstp->rq_resp);
  538. kfree(rqstp->rq_argp);
  539. kfree(rqstp->rq_auth_data);
  540. spin_lock_bh(&pool->sp_lock);
  541. pool->sp_nrthreads--;
  542. list_del(&rqstp->rq_all);
  543. spin_unlock_bh(&pool->sp_lock);
  544. kfree(rqstp);
  545. /* Release the server */
  546. if (serv)
  547. svc_destroy(serv);
  548. }
  549. /*
  550. * Register an RPC service with the local portmapper.
  551. * To unregister a service, call this routine with
  552. * proto and port == 0.
  553. */
  554. int
  555. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  556. {
  557. struct svc_program *progp;
  558. unsigned long flags;
  559. int i, error = 0, dummy;
  560. if (!port)
  561. clear_thread_flag(TIF_SIGPENDING);
  562. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  563. for (i = 0; i < progp->pg_nvers; i++) {
  564. if (progp->pg_vers[i] == NULL)
  565. continue;
  566. dprintk("RPC: svc_register(%s, %s, %d, %d)%s\n",
  567. progp->pg_name,
  568. proto == IPPROTO_UDP? "udp" : "tcp",
  569. port,
  570. i,
  571. progp->pg_vers[i]->vs_hidden?
  572. " (but not telling portmap)" : "");
  573. if (progp->pg_vers[i]->vs_hidden)
  574. continue;
  575. error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
  576. if (error < 0)
  577. break;
  578. if (port && !dummy) {
  579. error = -EACCES;
  580. break;
  581. }
  582. }
  583. }
  584. if (!port) {
  585. spin_lock_irqsave(&current->sighand->siglock, flags);
  586. recalc_sigpending();
  587. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  588. }
  589. return error;
  590. }
  591. /*
  592. * Process the RPC request.
  593. */
  594. int
  595. svc_process(struct svc_rqst *rqstp)
  596. {
  597. struct svc_program *progp;
  598. struct svc_version *versp = NULL; /* compiler food */
  599. struct svc_procedure *procp = NULL;
  600. struct kvec * argv = &rqstp->rq_arg.head[0];
  601. struct kvec * resv = &rqstp->rq_res.head[0];
  602. struct svc_serv *serv = rqstp->rq_server;
  603. kxdrproc_t xdr;
  604. __be32 *statp;
  605. u32 dir, prog, vers, proc;
  606. __be32 auth_stat, rpc_stat;
  607. int auth_res;
  608. __be32 *reply_statp;
  609. rpc_stat = rpc_success;
  610. if (argv->iov_len < 6*4)
  611. goto err_short_len;
  612. /* setup response xdr_buf.
  613. * Initially it has just one page
  614. */
  615. rqstp->rq_resused = 1;
  616. resv->iov_base = page_address(rqstp->rq_respages[0]);
  617. resv->iov_len = 0;
  618. rqstp->rq_res.pages = rqstp->rq_respages + 1;
  619. rqstp->rq_res.len = 0;
  620. rqstp->rq_res.page_base = 0;
  621. rqstp->rq_res.page_len = 0;
  622. rqstp->rq_res.buflen = PAGE_SIZE;
  623. rqstp->rq_res.tail[0].iov_base = NULL;
  624. rqstp->rq_res.tail[0].iov_len = 0;
  625. /* Will be turned off only in gss privacy case: */
  626. rqstp->rq_sendfile_ok = 1;
  627. /* tcp needs a space for the record length... */
  628. if (rqstp->rq_prot == IPPROTO_TCP)
  629. svc_putnl(resv, 0);
  630. rqstp->rq_xid = svc_getu32(argv);
  631. svc_putu32(resv, rqstp->rq_xid);
  632. dir = svc_getnl(argv);
  633. vers = svc_getnl(argv);
  634. /* First words of reply: */
  635. svc_putnl(resv, 1); /* REPLY */
  636. if (dir != 0) /* direction != CALL */
  637. goto err_bad_dir;
  638. if (vers != 2) /* RPC version number */
  639. goto err_bad_rpc;
  640. /* Save position in case we later decide to reject: */
  641. reply_statp = resv->iov_base + resv->iov_len;
  642. svc_putnl(resv, 0); /* ACCEPT */
  643. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  644. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  645. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  646. progp = serv->sv_program;
  647. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  648. if (prog == progp->pg_prog)
  649. break;
  650. /*
  651. * Decode auth data, and add verifier to reply buffer.
  652. * We do this before anything else in order to get a decent
  653. * auth verifier.
  654. */
  655. auth_res = svc_authenticate(rqstp, &auth_stat);
  656. /* Also give the program a chance to reject this call: */
  657. if (auth_res == SVC_OK && progp) {
  658. auth_stat = rpc_autherr_badcred;
  659. auth_res = progp->pg_authenticate(rqstp);
  660. }
  661. switch (auth_res) {
  662. case SVC_OK:
  663. break;
  664. case SVC_GARBAGE:
  665. rpc_stat = rpc_garbage_args;
  666. goto err_bad;
  667. case SVC_SYSERR:
  668. rpc_stat = rpc_system_err;
  669. goto err_bad;
  670. case SVC_DENIED:
  671. goto err_bad_auth;
  672. case SVC_DROP:
  673. goto dropit;
  674. case SVC_COMPLETE:
  675. goto sendit;
  676. }
  677. if (progp == NULL)
  678. goto err_bad_prog;
  679. if (vers >= progp->pg_nvers ||
  680. !(versp = progp->pg_vers[vers]))
  681. goto err_bad_vers;
  682. procp = versp->vs_proc + proc;
  683. if (proc >= versp->vs_nproc || !procp->pc_func)
  684. goto err_bad_proc;
  685. rqstp->rq_server = serv;
  686. rqstp->rq_procinfo = procp;
  687. /* Syntactic check complete */
  688. serv->sv_stats->rpccnt++;
  689. /* Build the reply header. */
  690. statp = resv->iov_base +resv->iov_len;
  691. svc_putnl(resv, RPC_SUCCESS);
  692. /* Bump per-procedure stats counter */
  693. procp->pc_count++;
  694. /* Initialize storage for argp and resp */
  695. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  696. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  697. /* un-reserve some of the out-queue now that we have a
  698. * better idea of reply size
  699. */
  700. if (procp->pc_xdrressize)
  701. svc_reserve(rqstp, procp->pc_xdrressize<<2);
  702. /* Call the function that processes the request. */
  703. if (!versp->vs_dispatch) {
  704. /* Decode arguments */
  705. xdr = procp->pc_decode;
  706. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  707. goto err_garbage;
  708. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  709. /* Encode reply */
  710. if (*statp == rpc_drop_reply) {
  711. if (procp->pc_release)
  712. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  713. goto dropit;
  714. }
  715. if (*statp == rpc_success && (xdr = procp->pc_encode)
  716. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  717. dprintk("svc: failed to encode reply\n");
  718. /* serv->sv_stats->rpcsystemerr++; */
  719. *statp = rpc_system_err;
  720. }
  721. } else {
  722. dprintk("svc: calling dispatcher\n");
  723. if (!versp->vs_dispatch(rqstp, statp)) {
  724. /* Release reply info */
  725. if (procp->pc_release)
  726. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  727. goto dropit;
  728. }
  729. }
  730. /* Check RPC status result */
  731. if (*statp != rpc_success)
  732. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  733. /* Release reply info */
  734. if (procp->pc_release)
  735. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  736. if (procp->pc_encode == NULL)
  737. goto dropit;
  738. sendit:
  739. if (svc_authorise(rqstp))
  740. goto dropit;
  741. return svc_send(rqstp);
  742. dropit:
  743. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  744. dprintk("svc: svc_process dropit\n");
  745. svc_drop(rqstp);
  746. return 0;
  747. err_short_len:
  748. #ifdef RPC_PARANOIA
  749. printk("svc: short len %Zd, dropping request\n", argv->iov_len);
  750. #endif
  751. goto dropit; /* drop request */
  752. err_bad_dir:
  753. #ifdef RPC_PARANOIA
  754. printk("svc: bad direction %d, dropping request\n", dir);
  755. #endif
  756. serv->sv_stats->rpcbadfmt++;
  757. goto dropit; /* drop request */
  758. err_bad_rpc:
  759. serv->sv_stats->rpcbadfmt++;
  760. svc_putnl(resv, 1); /* REJECT */
  761. svc_putnl(resv, 0); /* RPC_MISMATCH */
  762. svc_putnl(resv, 2); /* Only RPCv2 supported */
  763. svc_putnl(resv, 2);
  764. goto sendit;
  765. err_bad_auth:
  766. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  767. serv->sv_stats->rpcbadauth++;
  768. /* Restore write pointer to location of accept status: */
  769. xdr_ressize_check(rqstp, reply_statp);
  770. svc_putnl(resv, 1); /* REJECT */
  771. svc_putnl(resv, 1); /* AUTH_ERROR */
  772. svc_putnl(resv, ntohl(auth_stat)); /* status */
  773. goto sendit;
  774. err_bad_prog:
  775. dprintk("svc: unknown program %d\n", prog);
  776. serv->sv_stats->rpcbadfmt++;
  777. svc_putnl(resv, RPC_PROG_UNAVAIL);
  778. goto sendit;
  779. err_bad_vers:
  780. #ifdef RPC_PARANOIA
  781. printk("svc: unknown version (%d)\n", vers);
  782. #endif
  783. serv->sv_stats->rpcbadfmt++;
  784. svc_putnl(resv, RPC_PROG_MISMATCH);
  785. svc_putnl(resv, progp->pg_lovers);
  786. svc_putnl(resv, progp->pg_hivers);
  787. goto sendit;
  788. err_bad_proc:
  789. #ifdef RPC_PARANOIA
  790. printk("svc: unknown procedure (%d)\n", proc);
  791. #endif
  792. serv->sv_stats->rpcbadfmt++;
  793. svc_putnl(resv, RPC_PROC_UNAVAIL);
  794. goto sendit;
  795. err_garbage:
  796. #ifdef RPC_PARANOIA
  797. printk("svc: failed to decode args\n");
  798. #endif
  799. rpc_stat = rpc_garbage_args;
  800. err_bad:
  801. serv->sv_stats->rpcbadfmt++;
  802. svc_putnl(resv, ntohl(rpc_stat));
  803. goto sendit;
  804. }
  805. /*
  806. * Return (transport-specific) limit on the rpc payload.
  807. */
  808. u32 svc_max_payload(const struct svc_rqst *rqstp)
  809. {
  810. int max = RPCSVC_MAXPAYLOAD_TCP;
  811. if (rqstp->rq_sock->sk_sock->type == SOCK_DGRAM)
  812. max = RPCSVC_MAXPAYLOAD_UDP;
  813. if (rqstp->rq_server->sv_max_payload < max)
  814. max = rqstp->rq_server->sv_max_payload;
  815. return max;
  816. }
  817. EXPORT_SYMBOL_GPL(svc_max_payload);