svc.c 29 KB

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