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