svc.c 24 KB

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