svc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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/slab.h>
  22. #include <linux/sunrpc/types.h>
  23. #include <linux/sunrpc/xdr.h>
  24. #include <linux/sunrpc/stats.h>
  25. #include <linux/sunrpc/svcsock.h>
  26. #include <linux/sunrpc/clnt.h>
  27. #include <linux/sunrpc/bc_xprt.h>
  28. #include <trace/events/sunrpc.h>
  29. #define RPCDBG_FACILITY RPCDBG_SVCDSP
  30. static void svc_unregister(const struct svc_serv *serv, struct net *net);
  31. #define svc_serv_is_pooled(serv) ((serv)->sv_ops->svo_function)
  32. #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
  33. /*
  34. * Structure for mapping cpus to pools and vice versa.
  35. * Setup once during sunrpc initialisation.
  36. */
  37. struct svc_pool_map svc_pool_map = {
  38. .mode = SVC_POOL_DEFAULT
  39. };
  40. EXPORT_SYMBOL_GPL(svc_pool_map);
  41. static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */
  42. static int
  43. param_set_pool_mode(const char *val, struct kernel_param *kp)
  44. {
  45. int *ip = (int *)kp->arg;
  46. struct svc_pool_map *m = &svc_pool_map;
  47. int err;
  48. mutex_lock(&svc_pool_map_mutex);
  49. err = -EBUSY;
  50. if (m->count)
  51. goto out;
  52. err = 0;
  53. if (!strncmp(val, "auto", 4))
  54. *ip = SVC_POOL_AUTO;
  55. else if (!strncmp(val, "global", 6))
  56. *ip = SVC_POOL_GLOBAL;
  57. else if (!strncmp(val, "percpu", 6))
  58. *ip = SVC_POOL_PERCPU;
  59. else if (!strncmp(val, "pernode", 7))
  60. *ip = SVC_POOL_PERNODE;
  61. else
  62. err = -EINVAL;
  63. out:
  64. mutex_unlock(&svc_pool_map_mutex);
  65. return err;
  66. }
  67. static int
  68. param_get_pool_mode(char *buf, struct kernel_param *kp)
  69. {
  70. int *ip = (int *)kp->arg;
  71. switch (*ip)
  72. {
  73. case SVC_POOL_AUTO:
  74. return strlcpy(buf, "auto", 20);
  75. case SVC_POOL_GLOBAL:
  76. return strlcpy(buf, "global", 20);
  77. case SVC_POOL_PERCPU:
  78. return strlcpy(buf, "percpu", 20);
  79. case SVC_POOL_PERNODE:
  80. return strlcpy(buf, "pernode", 20);
  81. default:
  82. return sprintf(buf, "%d", *ip);
  83. }
  84. }
  85. module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode,
  86. &svc_pool_map.mode, 0644);
  87. /*
  88. * Detect best pool mapping mode heuristically,
  89. * according to the machine's topology.
  90. */
  91. static int
  92. svc_pool_map_choose_mode(void)
  93. {
  94. unsigned int node;
  95. if (nr_online_nodes > 1) {
  96. /*
  97. * Actually have multiple NUMA nodes,
  98. * so split pools on NUMA node boundaries
  99. */
  100. return SVC_POOL_PERNODE;
  101. }
  102. node = first_online_node;
  103. if (nr_cpus_node(node) > 2) {
  104. /*
  105. * Non-trivial SMP, or CONFIG_NUMA on
  106. * non-NUMA hardware, e.g. with a generic
  107. * x86_64 kernel on Xeons. In this case we
  108. * want to divide the pools on cpu boundaries.
  109. */
  110. return SVC_POOL_PERCPU;
  111. }
  112. /* default: one global pool */
  113. return SVC_POOL_GLOBAL;
  114. }
  115. /*
  116. * Allocate the to_pool[] and pool_to[] arrays.
  117. * Returns 0 on success or an errno.
  118. */
  119. static int
  120. svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools)
  121. {
  122. m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  123. if (!m->to_pool)
  124. goto fail;
  125. m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  126. if (!m->pool_to)
  127. goto fail_free;
  128. return 0;
  129. fail_free:
  130. kfree(m->to_pool);
  131. m->to_pool = NULL;
  132. fail:
  133. return -ENOMEM;
  134. }
  135. /*
  136. * Initialise the pool map for SVC_POOL_PERCPU mode.
  137. * Returns number of pools or <0 on error.
  138. */
  139. static int
  140. svc_pool_map_init_percpu(struct svc_pool_map *m)
  141. {
  142. unsigned int maxpools = nr_cpu_ids;
  143. unsigned int pidx = 0;
  144. unsigned int cpu;
  145. int err;
  146. err = svc_pool_map_alloc_arrays(m, maxpools);
  147. if (err)
  148. return err;
  149. for_each_online_cpu(cpu) {
  150. BUG_ON(pidx >= maxpools);
  151. m->to_pool[cpu] = pidx;
  152. m->pool_to[pidx] = cpu;
  153. pidx++;
  154. }
  155. /* cpus brought online later all get mapped to pool0, sorry */
  156. return pidx;
  157. };
  158. /*
  159. * Initialise the pool map for SVC_POOL_PERNODE mode.
  160. * Returns number of pools or <0 on error.
  161. */
  162. static int
  163. svc_pool_map_init_pernode(struct svc_pool_map *m)
  164. {
  165. unsigned int maxpools = nr_node_ids;
  166. unsigned int pidx = 0;
  167. unsigned int node;
  168. int err;
  169. err = svc_pool_map_alloc_arrays(m, maxpools);
  170. if (err)
  171. return err;
  172. for_each_node_with_cpus(node) {
  173. /* some architectures (e.g. SN2) have cpuless nodes */
  174. BUG_ON(pidx > maxpools);
  175. m->to_pool[node] = pidx;
  176. m->pool_to[pidx] = node;
  177. pidx++;
  178. }
  179. /* nodes brought online later all get mapped to pool0, sorry */
  180. return pidx;
  181. }
  182. /*
  183. * Add a reference to the global map of cpus to pools (and
  184. * vice versa). Initialise the map if we're the first user.
  185. * Returns the number of pools.
  186. */
  187. unsigned int
  188. svc_pool_map_get(void)
  189. {
  190. struct svc_pool_map *m = &svc_pool_map;
  191. int npools = -1;
  192. mutex_lock(&svc_pool_map_mutex);
  193. if (m->count++) {
  194. mutex_unlock(&svc_pool_map_mutex);
  195. return m->npools;
  196. }
  197. if (m->mode == SVC_POOL_AUTO)
  198. m->mode = svc_pool_map_choose_mode();
  199. switch (m->mode) {
  200. case SVC_POOL_PERCPU:
  201. npools = svc_pool_map_init_percpu(m);
  202. break;
  203. case SVC_POOL_PERNODE:
  204. npools = svc_pool_map_init_pernode(m);
  205. break;
  206. }
  207. if (npools < 0) {
  208. /* default, or memory allocation failure */
  209. npools = 1;
  210. m->mode = SVC_POOL_GLOBAL;
  211. }
  212. m->npools = npools;
  213. mutex_unlock(&svc_pool_map_mutex);
  214. return m->npools;
  215. }
  216. EXPORT_SYMBOL_GPL(svc_pool_map_get);
  217. /*
  218. * Drop a reference to the global map of cpus to pools.
  219. * When the last reference is dropped, the map data is
  220. * freed; this allows the sysadmin to change the pool
  221. * mode using the pool_mode module option without
  222. * rebooting or re-loading sunrpc.ko.
  223. */
  224. void
  225. svc_pool_map_put(void)
  226. {
  227. struct svc_pool_map *m = &svc_pool_map;
  228. mutex_lock(&svc_pool_map_mutex);
  229. if (!--m->count) {
  230. kfree(m->to_pool);
  231. m->to_pool = NULL;
  232. kfree(m->pool_to);
  233. m->pool_to = NULL;
  234. m->npools = 0;
  235. }
  236. mutex_unlock(&svc_pool_map_mutex);
  237. }
  238. EXPORT_SYMBOL_GPL(svc_pool_map_put);
  239. static int svc_pool_map_get_node(unsigned int pidx)
  240. {
  241. const struct svc_pool_map *m = &svc_pool_map;
  242. if (m->count) {
  243. if (m->mode == SVC_POOL_PERCPU)
  244. return cpu_to_node(m->pool_to[pidx]);
  245. if (m->mode == SVC_POOL_PERNODE)
  246. return m->pool_to[pidx];
  247. }
  248. return NUMA_NO_NODE;
  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. WARN_ON_ONCE(m->count == 0);
  264. if (m->count == 0)
  265. return;
  266. switch (m->mode) {
  267. case SVC_POOL_PERCPU:
  268. {
  269. set_cpus_allowed_ptr(task, cpumask_of(node));
  270. break;
  271. }
  272. case SVC_POOL_PERNODE:
  273. {
  274. set_cpus_allowed_ptr(task, cpumask_of_node(node));
  275. break;
  276. }
  277. }
  278. }
  279. /*
  280. * Use the mapping mode to choose a pool for a given CPU.
  281. * Used when enqueueing an incoming RPC. Always returns
  282. * a non-NULL pool pointer.
  283. */
  284. struct svc_pool *
  285. svc_pool_for_cpu(struct svc_serv *serv, int cpu)
  286. {
  287. struct svc_pool_map *m = &svc_pool_map;
  288. unsigned int pidx = 0;
  289. /*
  290. * An uninitialised map happens in a pure client when
  291. * lockd is brought up, so silently treat it the
  292. * same as SVC_POOL_GLOBAL.
  293. */
  294. if (svc_serv_is_pooled(serv)) {
  295. switch (m->mode) {
  296. case SVC_POOL_PERCPU:
  297. pidx = m->to_pool[cpu];
  298. break;
  299. case SVC_POOL_PERNODE:
  300. pidx = m->to_pool[cpu_to_node(cpu)];
  301. break;
  302. }
  303. }
  304. return &serv->sv_pools[pidx % serv->sv_nrpools];
  305. }
  306. int svc_rpcb_setup(struct svc_serv *serv, struct net *net)
  307. {
  308. int err;
  309. err = rpcb_create_local(net);
  310. if (err)
  311. return err;
  312. /* Remove any stale portmap registrations */
  313. svc_unregister(serv, net);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(svc_rpcb_setup);
  317. void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net)
  318. {
  319. svc_unregister(serv, net);
  320. rpcb_put_local(net);
  321. }
  322. EXPORT_SYMBOL_GPL(svc_rpcb_cleanup);
  323. static int svc_uses_rpcbind(struct svc_serv *serv)
  324. {
  325. struct svc_program *progp;
  326. unsigned int i;
  327. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  328. for (i = 0; i < progp->pg_nvers; i++) {
  329. if (progp->pg_vers[i] == NULL)
  330. continue;
  331. if (progp->pg_vers[i]->vs_hidden == 0)
  332. return 1;
  333. }
  334. }
  335. return 0;
  336. }
  337. int svc_bind(struct svc_serv *serv, struct net *net)
  338. {
  339. if (!svc_uses_rpcbind(serv))
  340. return 0;
  341. return svc_rpcb_setup(serv, net);
  342. }
  343. EXPORT_SYMBOL_GPL(svc_bind);
  344. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  345. static void
  346. __svc_init_bc(struct svc_serv *serv)
  347. {
  348. INIT_LIST_HEAD(&serv->sv_cb_list);
  349. spin_lock_init(&serv->sv_cb_lock);
  350. init_waitqueue_head(&serv->sv_cb_waitq);
  351. }
  352. #else
  353. static void
  354. __svc_init_bc(struct svc_serv *serv)
  355. {
  356. }
  357. #endif
  358. /*
  359. * Create an RPC service
  360. */
  361. static struct svc_serv *
  362. __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
  363. struct svc_serv_ops *ops)
  364. {
  365. struct svc_serv *serv;
  366. unsigned int vers;
  367. unsigned int xdrsize;
  368. unsigned int i;
  369. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  370. return NULL;
  371. serv->sv_name = prog->pg_name;
  372. serv->sv_program = prog;
  373. serv->sv_nrthreads = 1;
  374. serv->sv_stats = prog->pg_stats;
  375. if (bufsize > RPCSVC_MAXPAYLOAD)
  376. bufsize = RPCSVC_MAXPAYLOAD;
  377. serv->sv_max_payload = bufsize? bufsize : 4096;
  378. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  379. serv->sv_ops = ops;
  380. xdrsize = 0;
  381. while (prog) {
  382. prog->pg_lovers = prog->pg_nvers-1;
  383. for (vers=0; vers<prog->pg_nvers ; vers++)
  384. if (prog->pg_vers[vers]) {
  385. prog->pg_hivers = vers;
  386. if (prog->pg_lovers > vers)
  387. prog->pg_lovers = vers;
  388. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  389. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  390. }
  391. prog = prog->pg_next;
  392. }
  393. serv->sv_xdrsize = xdrsize;
  394. INIT_LIST_HEAD(&serv->sv_tempsocks);
  395. INIT_LIST_HEAD(&serv->sv_permsocks);
  396. init_timer(&serv->sv_temptimer);
  397. spin_lock_init(&serv->sv_lock);
  398. __svc_init_bc(serv);
  399. serv->sv_nrpools = npools;
  400. serv->sv_pools =
  401. kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
  402. GFP_KERNEL);
  403. if (!serv->sv_pools) {
  404. kfree(serv);
  405. return NULL;
  406. }
  407. for (i = 0; i < serv->sv_nrpools; i++) {
  408. struct svc_pool *pool = &serv->sv_pools[i];
  409. dprintk("svc: initialising pool %u for %s\n",
  410. i, serv->sv_name);
  411. pool->sp_id = i;
  412. INIT_LIST_HEAD(&pool->sp_sockets);
  413. INIT_LIST_HEAD(&pool->sp_all_threads);
  414. spin_lock_init(&pool->sp_lock);
  415. }
  416. return serv;
  417. }
  418. struct svc_serv *
  419. svc_create(struct svc_program *prog, unsigned int bufsize,
  420. struct svc_serv_ops *ops)
  421. {
  422. return __svc_create(prog, bufsize, /*npools*/1, ops);
  423. }
  424. EXPORT_SYMBOL_GPL(svc_create);
  425. struct svc_serv *
  426. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  427. struct svc_serv_ops *ops)
  428. {
  429. struct svc_serv *serv;
  430. unsigned int npools = svc_pool_map_get();
  431. serv = __svc_create(prog, bufsize, npools, ops);
  432. if (!serv)
  433. goto out_err;
  434. return serv;
  435. out_err:
  436. svc_pool_map_put();
  437. return NULL;
  438. }
  439. EXPORT_SYMBOL_GPL(svc_create_pooled);
  440. void svc_shutdown_net(struct svc_serv *serv, struct net *net)
  441. {
  442. svc_close_net(serv, net);
  443. if (serv->sv_ops->svo_shutdown)
  444. serv->sv_ops->svo_shutdown(serv, net);
  445. }
  446. EXPORT_SYMBOL_GPL(svc_shutdown_net);
  447. /*
  448. * Destroy an RPC service. Should be called with appropriate locking to
  449. * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
  450. */
  451. void
  452. svc_destroy(struct svc_serv *serv)
  453. {
  454. dprintk("svc: svc_destroy(%s, %d)\n",
  455. serv->sv_program->pg_name,
  456. serv->sv_nrthreads);
  457. if (serv->sv_nrthreads) {
  458. if (--(serv->sv_nrthreads) != 0) {
  459. svc_sock_update_bufs(serv);
  460. return;
  461. }
  462. } else
  463. printk("svc_destroy: no threads for serv=%p!\n", serv);
  464. del_timer_sync(&serv->sv_temptimer);
  465. /*
  466. * The last user is gone and thus all sockets have to be destroyed to
  467. * the point. Check this.
  468. */
  469. BUG_ON(!list_empty(&serv->sv_permsocks));
  470. BUG_ON(!list_empty(&serv->sv_tempsocks));
  471. cache_clean_deferred(serv);
  472. if (svc_serv_is_pooled(serv))
  473. svc_pool_map_put();
  474. kfree(serv->sv_pools);
  475. kfree(serv);
  476. }
  477. EXPORT_SYMBOL_GPL(svc_destroy);
  478. /*
  479. * Allocate an RPC server's buffer space.
  480. * We allocate pages and place them in rq_argpages.
  481. */
  482. static int
  483. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node)
  484. {
  485. unsigned int pages, arghi;
  486. /* bc_xprt uses fore channel allocated buffers */
  487. if (svc_is_backchannel(rqstp))
  488. return 1;
  489. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  490. * We assume one is at most one page
  491. */
  492. arghi = 0;
  493. WARN_ON_ONCE(pages > RPCSVC_MAXPAGES);
  494. if (pages > RPCSVC_MAXPAGES)
  495. pages = RPCSVC_MAXPAGES;
  496. while (pages) {
  497. struct page *p = alloc_pages_node(node, GFP_KERNEL, 0);
  498. if (!p)
  499. break;
  500. rqstp->rq_pages[arghi++] = p;
  501. pages--;
  502. }
  503. return pages == 0;
  504. }
  505. /*
  506. * Release an RPC server buffer
  507. */
  508. static void
  509. svc_release_buffer(struct svc_rqst *rqstp)
  510. {
  511. unsigned int i;
  512. for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
  513. if (rqstp->rq_pages[i])
  514. put_page(rqstp->rq_pages[i]);
  515. }
  516. struct svc_rqst *
  517. svc_rqst_alloc(struct svc_serv *serv, struct svc_pool *pool, int node)
  518. {
  519. struct svc_rqst *rqstp;
  520. rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
  521. if (!rqstp)
  522. return rqstp;
  523. __set_bit(RQ_BUSY, &rqstp->rq_flags);
  524. spin_lock_init(&rqstp->rq_lock);
  525. rqstp->rq_server = serv;
  526. rqstp->rq_pool = pool;
  527. rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  528. if (!rqstp->rq_argp)
  529. goto out_enomem;
  530. rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  531. if (!rqstp->rq_resp)
  532. goto out_enomem;
  533. if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
  534. goto out_enomem;
  535. return rqstp;
  536. out_enomem:
  537. svc_rqst_free(rqstp);
  538. return NULL;
  539. }
  540. EXPORT_SYMBOL_GPL(svc_rqst_alloc);
  541. struct svc_rqst *
  542. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
  543. {
  544. struct svc_rqst *rqstp;
  545. rqstp = svc_rqst_alloc(serv, pool, node);
  546. if (!rqstp)
  547. return ERR_PTR(-ENOMEM);
  548. serv->sv_nrthreads++;
  549. spin_lock_bh(&pool->sp_lock);
  550. pool->sp_nrthreads++;
  551. list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
  552. spin_unlock_bh(&pool->sp_lock);
  553. return rqstp;
  554. }
  555. EXPORT_SYMBOL_GPL(svc_prepare_thread);
  556. /*
  557. * Choose a pool in which to create a new thread, for svc_set_num_threads
  558. */
  559. static inline struct svc_pool *
  560. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  561. {
  562. if (pool != NULL)
  563. return pool;
  564. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  565. }
  566. /*
  567. * Choose a thread to kill, for svc_set_num_threads
  568. */
  569. static inline struct task_struct *
  570. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  571. {
  572. unsigned int i;
  573. struct task_struct *task = NULL;
  574. if (pool != NULL) {
  575. spin_lock_bh(&pool->sp_lock);
  576. } else {
  577. /* choose a pool in round-robin fashion */
  578. for (i = 0; i < serv->sv_nrpools; i++) {
  579. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  580. spin_lock_bh(&pool->sp_lock);
  581. if (!list_empty(&pool->sp_all_threads))
  582. goto found_pool;
  583. spin_unlock_bh(&pool->sp_lock);
  584. }
  585. return NULL;
  586. }
  587. found_pool:
  588. if (!list_empty(&pool->sp_all_threads)) {
  589. struct svc_rqst *rqstp;
  590. /*
  591. * Remove from the pool->sp_all_threads list
  592. * so we don't try to kill it again.
  593. */
  594. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  595. set_bit(RQ_VICTIM, &rqstp->rq_flags);
  596. list_del_rcu(&rqstp->rq_all);
  597. task = rqstp->rq_task;
  598. }
  599. spin_unlock_bh(&pool->sp_lock);
  600. return task;
  601. }
  602. /*
  603. * Create or destroy enough new threads to make the number
  604. * of threads the given number. If `pool' is non-NULL, applies
  605. * only to threads in that pool, otherwise round-robins between
  606. * all pools. Caller must ensure that mutual exclusion between this and
  607. * server startup or shutdown.
  608. *
  609. * Destroying threads relies on the service threads filling in
  610. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  611. * has been created using svc_create_pooled().
  612. *
  613. * Based on code that used to be in nfsd_svc() but tweaked
  614. * to be pool-aware.
  615. */
  616. int
  617. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  618. {
  619. struct svc_rqst *rqstp;
  620. struct task_struct *task;
  621. struct svc_pool *chosen_pool;
  622. int error = 0;
  623. unsigned int state = serv->sv_nrthreads-1;
  624. int node;
  625. if (pool == NULL) {
  626. /* The -1 assumes caller has done a svc_get() */
  627. nrservs -= (serv->sv_nrthreads-1);
  628. } else {
  629. spin_lock_bh(&pool->sp_lock);
  630. nrservs -= pool->sp_nrthreads;
  631. spin_unlock_bh(&pool->sp_lock);
  632. }
  633. /* create new threads */
  634. while (nrservs > 0) {
  635. nrservs--;
  636. chosen_pool = choose_pool(serv, pool, &state);
  637. node = svc_pool_map_get_node(chosen_pool->sp_id);
  638. rqstp = svc_prepare_thread(serv, chosen_pool, node);
  639. if (IS_ERR(rqstp)) {
  640. error = PTR_ERR(rqstp);
  641. break;
  642. }
  643. __module_get(serv->sv_ops->svo_module);
  644. task = kthread_create_on_node(serv->sv_ops->svo_function, rqstp,
  645. node, "%s", serv->sv_name);
  646. if (IS_ERR(task)) {
  647. error = PTR_ERR(task);
  648. module_put(serv->sv_ops->svo_module);
  649. svc_exit_thread(rqstp);
  650. break;
  651. }
  652. rqstp->rq_task = task;
  653. if (serv->sv_nrpools > 1)
  654. svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
  655. svc_sock_update_bufs(serv);
  656. wake_up_process(task);
  657. }
  658. /* destroy old threads */
  659. while (nrservs < 0 &&
  660. (task = choose_victim(serv, pool, &state)) != NULL) {
  661. send_sig(SIGINT, task, 1);
  662. nrservs++;
  663. }
  664. return error;
  665. }
  666. EXPORT_SYMBOL_GPL(svc_set_num_threads);
  667. /*
  668. * Called from a server thread as it's exiting. Caller must hold the "service
  669. * mutex" for the service.
  670. */
  671. void
  672. svc_rqst_free(struct svc_rqst *rqstp)
  673. {
  674. svc_release_buffer(rqstp);
  675. kfree(rqstp->rq_resp);
  676. kfree(rqstp->rq_argp);
  677. kfree(rqstp->rq_auth_data);
  678. kfree_rcu(rqstp, rq_rcu_head);
  679. }
  680. EXPORT_SYMBOL_GPL(svc_rqst_free);
  681. void
  682. svc_exit_thread(struct svc_rqst *rqstp)
  683. {
  684. struct svc_serv *serv = rqstp->rq_server;
  685. struct svc_pool *pool = rqstp->rq_pool;
  686. spin_lock_bh(&pool->sp_lock);
  687. pool->sp_nrthreads--;
  688. if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags))
  689. list_del_rcu(&rqstp->rq_all);
  690. spin_unlock_bh(&pool->sp_lock);
  691. svc_rqst_free(rqstp);
  692. /* Release the server */
  693. if (serv)
  694. svc_destroy(serv);
  695. }
  696. EXPORT_SYMBOL_GPL(svc_exit_thread);
  697. /*
  698. * Register an "inet" protocol family netid with the local
  699. * rpcbind daemon via an rpcbind v4 SET request.
  700. *
  701. * No netconfig infrastructure is available in the kernel, so
  702. * we map IP_ protocol numbers to netids by hand.
  703. *
  704. * Returns zero on success; a negative errno value is returned
  705. * if any error occurs.
  706. */
  707. static int __svc_rpcb_register4(struct net *net, const u32 program,
  708. const u32 version,
  709. const unsigned short protocol,
  710. const unsigned short port)
  711. {
  712. const struct sockaddr_in sin = {
  713. .sin_family = AF_INET,
  714. .sin_addr.s_addr = htonl(INADDR_ANY),
  715. .sin_port = htons(port),
  716. };
  717. const char *netid;
  718. int error;
  719. switch (protocol) {
  720. case IPPROTO_UDP:
  721. netid = RPCBIND_NETID_UDP;
  722. break;
  723. case IPPROTO_TCP:
  724. netid = RPCBIND_NETID_TCP;
  725. break;
  726. default:
  727. return -ENOPROTOOPT;
  728. }
  729. error = rpcb_v4_register(net, program, version,
  730. (const struct sockaddr *)&sin, netid);
  731. /*
  732. * User space didn't support rpcbind v4, so retry this
  733. * registration request with the legacy rpcbind v2 protocol.
  734. */
  735. if (error == -EPROTONOSUPPORT)
  736. error = rpcb_register(net, program, version, protocol, port);
  737. return error;
  738. }
  739. #if IS_ENABLED(CONFIG_IPV6)
  740. /*
  741. * Register an "inet6" protocol family netid with the local
  742. * rpcbind daemon via an rpcbind v4 SET request.
  743. *
  744. * No netconfig infrastructure is available in the kernel, so
  745. * we map IP_ protocol numbers to netids by hand.
  746. *
  747. * Returns zero on success; a negative errno value is returned
  748. * if any error occurs.
  749. */
  750. static int __svc_rpcb_register6(struct net *net, const u32 program,
  751. const u32 version,
  752. const unsigned short protocol,
  753. const unsigned short port)
  754. {
  755. const struct sockaddr_in6 sin6 = {
  756. .sin6_family = AF_INET6,
  757. .sin6_addr = IN6ADDR_ANY_INIT,
  758. .sin6_port = htons(port),
  759. };
  760. const char *netid;
  761. int error;
  762. switch (protocol) {
  763. case IPPROTO_UDP:
  764. netid = RPCBIND_NETID_UDP6;
  765. break;
  766. case IPPROTO_TCP:
  767. netid = RPCBIND_NETID_TCP6;
  768. break;
  769. default:
  770. return -ENOPROTOOPT;
  771. }
  772. error = rpcb_v4_register(net, program, version,
  773. (const struct sockaddr *)&sin6, netid);
  774. /*
  775. * User space didn't support rpcbind version 4, so we won't
  776. * use a PF_INET6 listener.
  777. */
  778. if (error == -EPROTONOSUPPORT)
  779. error = -EAFNOSUPPORT;
  780. return error;
  781. }
  782. #endif /* IS_ENABLED(CONFIG_IPV6) */
  783. /*
  784. * Register a kernel RPC service via rpcbind version 4.
  785. *
  786. * Returns zero on success; a negative errno value is returned
  787. * if any error occurs.
  788. */
  789. static int __svc_register(struct net *net, const char *progname,
  790. const u32 program, const u32 version,
  791. const int family,
  792. const unsigned short protocol,
  793. const unsigned short port)
  794. {
  795. int error = -EAFNOSUPPORT;
  796. switch (family) {
  797. case PF_INET:
  798. error = __svc_rpcb_register4(net, program, version,
  799. protocol, port);
  800. break;
  801. #if IS_ENABLED(CONFIG_IPV6)
  802. case PF_INET6:
  803. error = __svc_rpcb_register6(net, program, version,
  804. protocol, port);
  805. #endif
  806. }
  807. return error;
  808. }
  809. /**
  810. * svc_register - register an RPC service with the local portmapper
  811. * @serv: svc_serv struct for the service to register
  812. * @net: net namespace for the service to register
  813. * @family: protocol family of service's listener socket
  814. * @proto: transport protocol number to advertise
  815. * @port: port to advertise
  816. *
  817. * Service is registered for any address in the passed-in protocol family
  818. */
  819. int svc_register(const struct svc_serv *serv, struct net *net,
  820. const int family, const unsigned short proto,
  821. const unsigned short port)
  822. {
  823. struct svc_program *progp;
  824. struct svc_version *vers;
  825. unsigned int i;
  826. int error = 0;
  827. WARN_ON_ONCE(proto == 0 && port == 0);
  828. if (proto == 0 && port == 0)
  829. return -EINVAL;
  830. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  831. for (i = 0; i < progp->pg_nvers; i++) {
  832. vers = progp->pg_vers[i];
  833. if (vers == NULL)
  834. continue;
  835. dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n",
  836. progp->pg_name,
  837. i,
  838. proto == IPPROTO_UDP? "udp" : "tcp",
  839. port,
  840. family,
  841. vers->vs_hidden ?
  842. " (but not telling portmap)" : "");
  843. if (vers->vs_hidden)
  844. continue;
  845. error = __svc_register(net, progp->pg_name, progp->pg_prog,
  846. i, family, proto, port);
  847. if (vers->vs_rpcb_optnl) {
  848. error = 0;
  849. continue;
  850. }
  851. if (error < 0) {
  852. printk(KERN_WARNING "svc: failed to register "
  853. "%sv%u RPC service (errno %d).\n",
  854. progp->pg_name, i, -error);
  855. break;
  856. }
  857. }
  858. }
  859. return error;
  860. }
  861. /*
  862. * If user space is running rpcbind, it should take the v4 UNSET
  863. * and clear everything for this [program, version]. If user space
  864. * is running portmap, it will reject the v4 UNSET, but won't have
  865. * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
  866. * in this case to clear all existing entries for [program, version].
  867. */
  868. static void __svc_unregister(struct net *net, const u32 program, const u32 version,
  869. const char *progname)
  870. {
  871. int error;
  872. error = rpcb_v4_register(net, program, version, NULL, "");
  873. /*
  874. * User space didn't support rpcbind v4, so retry this
  875. * request with the legacy rpcbind v2 protocol.
  876. */
  877. if (error == -EPROTONOSUPPORT)
  878. error = rpcb_register(net, program, version, 0, 0);
  879. dprintk("svc: %s(%sv%u), error %d\n",
  880. __func__, progname, version, error);
  881. }
  882. /*
  883. * All netids, bind addresses and ports registered for [program, version]
  884. * are removed from the local rpcbind database (if the service is not
  885. * hidden) to make way for a new instance of the service.
  886. *
  887. * The result of unregistration is reported via dprintk for those who want
  888. * verification of the result, but is otherwise not important.
  889. */
  890. static void svc_unregister(const struct svc_serv *serv, struct net *net)
  891. {
  892. struct svc_program *progp;
  893. unsigned long flags;
  894. unsigned int i;
  895. clear_thread_flag(TIF_SIGPENDING);
  896. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  897. for (i = 0; i < progp->pg_nvers; i++) {
  898. if (progp->pg_vers[i] == NULL)
  899. continue;
  900. if (progp->pg_vers[i]->vs_hidden)
  901. continue;
  902. dprintk("svc: attempting to unregister %sv%u\n",
  903. progp->pg_name, i);
  904. __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
  905. }
  906. }
  907. spin_lock_irqsave(&current->sighand->siglock, flags);
  908. recalc_sigpending();
  909. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  910. }
  911. /*
  912. * dprintk the given error with the address of the client that caused it.
  913. */
  914. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  915. static __printf(2, 3)
  916. void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  917. {
  918. struct va_format vaf;
  919. va_list args;
  920. char buf[RPC_MAX_ADDRBUFLEN];
  921. va_start(args, fmt);
  922. vaf.fmt = fmt;
  923. vaf.va = &args;
  924. dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
  925. va_end(args);
  926. }
  927. #else
  928. static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
  929. #endif
  930. /*
  931. * Common routine for processing the RPC request.
  932. */
  933. static int
  934. svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
  935. {
  936. struct svc_program *progp;
  937. struct svc_version *versp = NULL; /* compiler food */
  938. struct svc_procedure *procp = NULL;
  939. struct svc_serv *serv = rqstp->rq_server;
  940. kxdrproc_t xdr;
  941. __be32 *statp;
  942. u32 prog, vers, proc;
  943. __be32 auth_stat, rpc_stat;
  944. int auth_res;
  945. __be32 *reply_statp;
  946. rpc_stat = rpc_success;
  947. if (argv->iov_len < 6*4)
  948. goto err_short_len;
  949. /* Will be turned off only in gss privacy case: */
  950. set_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  951. /* Will be turned off only when NFSv4 Sessions are used */
  952. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  953. clear_bit(RQ_DROPME, &rqstp->rq_flags);
  954. /* Setup reply header */
  955. rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
  956. svc_putu32(resv, rqstp->rq_xid);
  957. vers = svc_getnl(argv);
  958. /* First words of reply: */
  959. svc_putnl(resv, 1); /* REPLY */
  960. if (vers != 2) /* RPC version number */
  961. goto err_bad_rpc;
  962. /* Save position in case we later decide to reject: */
  963. reply_statp = resv->iov_base + resv->iov_len;
  964. svc_putnl(resv, 0); /* ACCEPT */
  965. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  966. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  967. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  968. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  969. if (prog == progp->pg_prog)
  970. break;
  971. /*
  972. * Decode auth data, and add verifier to reply buffer.
  973. * We do this before anything else in order to get a decent
  974. * auth verifier.
  975. */
  976. auth_res = svc_authenticate(rqstp, &auth_stat);
  977. /* Also give the program a chance to reject this call: */
  978. if (auth_res == SVC_OK && progp) {
  979. auth_stat = rpc_autherr_badcred;
  980. auth_res = progp->pg_authenticate(rqstp);
  981. }
  982. switch (auth_res) {
  983. case SVC_OK:
  984. break;
  985. case SVC_GARBAGE:
  986. goto err_garbage;
  987. case SVC_SYSERR:
  988. rpc_stat = rpc_system_err;
  989. goto err_bad;
  990. case SVC_DENIED:
  991. goto err_bad_auth;
  992. case SVC_CLOSE:
  993. goto close;
  994. case SVC_DROP:
  995. goto dropit;
  996. case SVC_COMPLETE:
  997. goto sendit;
  998. }
  999. if (progp == NULL)
  1000. goto err_bad_prog;
  1001. if (vers >= progp->pg_nvers ||
  1002. !(versp = progp->pg_vers[vers]))
  1003. goto err_bad_vers;
  1004. procp = versp->vs_proc + proc;
  1005. if (proc >= versp->vs_nproc || !procp->pc_func)
  1006. goto err_bad_proc;
  1007. rqstp->rq_procinfo = procp;
  1008. /* Syntactic check complete */
  1009. serv->sv_stats->rpccnt++;
  1010. /* Build the reply header. */
  1011. statp = resv->iov_base +resv->iov_len;
  1012. svc_putnl(resv, RPC_SUCCESS);
  1013. /* Bump per-procedure stats counter */
  1014. procp->pc_count++;
  1015. /* Initialize storage for argp and resp */
  1016. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  1017. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  1018. /* un-reserve some of the out-queue now that we have a
  1019. * better idea of reply size
  1020. */
  1021. if (procp->pc_xdrressize)
  1022. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  1023. /* Call the function that processes the request. */
  1024. if (!versp->vs_dispatch) {
  1025. /* Decode arguments */
  1026. xdr = procp->pc_decode;
  1027. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  1028. goto err_garbage;
  1029. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  1030. /* Encode reply */
  1031. if (*statp == rpc_drop_reply ||
  1032. test_bit(RQ_DROPME, &rqstp->rq_flags)) {
  1033. if (procp->pc_release)
  1034. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1035. goto dropit;
  1036. }
  1037. if (*statp == rpc_autherr_badcred) {
  1038. if (procp->pc_release)
  1039. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1040. goto err_bad_auth;
  1041. }
  1042. if (*statp == rpc_success &&
  1043. (xdr = procp->pc_encode) &&
  1044. !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  1045. dprintk("svc: failed to encode reply\n");
  1046. /* serv->sv_stats->rpcsystemerr++; */
  1047. *statp = rpc_system_err;
  1048. }
  1049. } else {
  1050. dprintk("svc: calling dispatcher\n");
  1051. if (!versp->vs_dispatch(rqstp, statp)) {
  1052. /* Release reply info */
  1053. if (procp->pc_release)
  1054. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1055. goto dropit;
  1056. }
  1057. }
  1058. /* Check RPC status result */
  1059. if (*statp != rpc_success)
  1060. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  1061. /* Release reply info */
  1062. if (procp->pc_release)
  1063. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1064. if (procp->pc_encode == NULL)
  1065. goto dropit;
  1066. sendit:
  1067. if (svc_authorise(rqstp))
  1068. goto close;
  1069. return 1; /* Caller can now send it */
  1070. dropit:
  1071. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  1072. dprintk("svc: svc_process dropit\n");
  1073. return 0;
  1074. close:
  1075. if (test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
  1076. svc_close_xprt(rqstp->rq_xprt);
  1077. dprintk("svc: svc_process close\n");
  1078. return 0;
  1079. err_short_len:
  1080. svc_printk(rqstp, "short len %Zd, dropping request\n",
  1081. argv->iov_len);
  1082. goto close;
  1083. err_bad_rpc:
  1084. serv->sv_stats->rpcbadfmt++;
  1085. svc_putnl(resv, 1); /* REJECT */
  1086. svc_putnl(resv, 0); /* RPC_MISMATCH */
  1087. svc_putnl(resv, 2); /* Only RPCv2 supported */
  1088. svc_putnl(resv, 2);
  1089. goto sendit;
  1090. err_bad_auth:
  1091. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  1092. serv->sv_stats->rpcbadauth++;
  1093. /* Restore write pointer to location of accept status: */
  1094. xdr_ressize_check(rqstp, reply_statp);
  1095. svc_putnl(resv, 1); /* REJECT */
  1096. svc_putnl(resv, 1); /* AUTH_ERROR */
  1097. svc_putnl(resv, ntohl(auth_stat)); /* status */
  1098. goto sendit;
  1099. err_bad_prog:
  1100. dprintk("svc: unknown program %d\n", prog);
  1101. serv->sv_stats->rpcbadfmt++;
  1102. svc_putnl(resv, RPC_PROG_UNAVAIL);
  1103. goto sendit;
  1104. err_bad_vers:
  1105. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  1106. vers, prog, progp->pg_name);
  1107. serv->sv_stats->rpcbadfmt++;
  1108. svc_putnl(resv, RPC_PROG_MISMATCH);
  1109. svc_putnl(resv, progp->pg_lovers);
  1110. svc_putnl(resv, progp->pg_hivers);
  1111. goto sendit;
  1112. err_bad_proc:
  1113. svc_printk(rqstp, "unknown procedure (%d)\n", proc);
  1114. serv->sv_stats->rpcbadfmt++;
  1115. svc_putnl(resv, RPC_PROC_UNAVAIL);
  1116. goto sendit;
  1117. err_garbage:
  1118. svc_printk(rqstp, "failed to decode args\n");
  1119. rpc_stat = rpc_garbage_args;
  1120. err_bad:
  1121. serv->sv_stats->rpcbadfmt++;
  1122. svc_putnl(resv, ntohl(rpc_stat));
  1123. goto sendit;
  1124. }
  1125. /*
  1126. * Process the RPC request.
  1127. */
  1128. int
  1129. svc_process(struct svc_rqst *rqstp)
  1130. {
  1131. struct kvec *argv = &rqstp->rq_arg.head[0];
  1132. struct kvec *resv = &rqstp->rq_res.head[0];
  1133. struct svc_serv *serv = rqstp->rq_server;
  1134. u32 dir;
  1135. /*
  1136. * Setup response xdr_buf.
  1137. * Initially it has just one page
  1138. */
  1139. rqstp->rq_next_page = &rqstp->rq_respages[1];
  1140. resv->iov_base = page_address(rqstp->rq_respages[0]);
  1141. resv->iov_len = 0;
  1142. rqstp->rq_res.pages = rqstp->rq_respages + 1;
  1143. rqstp->rq_res.len = 0;
  1144. rqstp->rq_res.page_base = 0;
  1145. rqstp->rq_res.page_len = 0;
  1146. rqstp->rq_res.buflen = PAGE_SIZE;
  1147. rqstp->rq_res.tail[0].iov_base = NULL;
  1148. rqstp->rq_res.tail[0].iov_len = 0;
  1149. dir = svc_getnl(argv);
  1150. if (dir != 0) {
  1151. /* direction != CALL */
  1152. svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
  1153. serv->sv_stats->rpcbadfmt++;
  1154. goto out_drop;
  1155. }
  1156. /* Returns 1 for send, 0 for drop */
  1157. if (likely(svc_process_common(rqstp, argv, resv))) {
  1158. int ret = svc_send(rqstp);
  1159. trace_svc_process(rqstp, ret);
  1160. return ret;
  1161. }
  1162. out_drop:
  1163. trace_svc_process(rqstp, 0);
  1164. svc_drop(rqstp);
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL_GPL(svc_process);
  1168. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1169. /*
  1170. * Process a backchannel RPC request that arrived over an existing
  1171. * outbound connection
  1172. */
  1173. int
  1174. bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
  1175. struct svc_rqst *rqstp)
  1176. {
  1177. struct kvec *argv = &rqstp->rq_arg.head[0];
  1178. struct kvec *resv = &rqstp->rq_res.head[0];
  1179. struct rpc_task *task;
  1180. int proc_error;
  1181. int error;
  1182. dprintk("svc: %s(%p)\n", __func__, req);
  1183. /* Build the svc_rqst used by the common processing routine */
  1184. rqstp->rq_xprt = serv->sv_bc_xprt;
  1185. rqstp->rq_xid = req->rq_xid;
  1186. rqstp->rq_prot = req->rq_xprt->prot;
  1187. rqstp->rq_server = serv;
  1188. rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
  1189. memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
  1190. memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
  1191. memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
  1192. /* Adjust the argument buffer length */
  1193. rqstp->rq_arg.len = req->rq_private_buf.len;
  1194. if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
  1195. rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
  1196. rqstp->rq_arg.page_len = 0;
  1197. } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
  1198. rqstp->rq_arg.page_len)
  1199. rqstp->rq_arg.page_len = rqstp->rq_arg.len -
  1200. rqstp->rq_arg.head[0].iov_len;
  1201. else
  1202. rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
  1203. rqstp->rq_arg.page_len;
  1204. /* reset result send buffer "put" position */
  1205. resv->iov_len = 0;
  1206. /*
  1207. * Skip the next two words because they've already been
  1208. * processed in the transport
  1209. */
  1210. svc_getu32(argv); /* XID */
  1211. svc_getnl(argv); /* CALLDIR */
  1212. /* Parse and execute the bc call */
  1213. proc_error = svc_process_common(rqstp, argv, resv);
  1214. atomic_inc(&req->rq_xprt->bc_free_slots);
  1215. if (!proc_error) {
  1216. /* Processing error: drop the request */
  1217. xprt_free_bc_request(req);
  1218. return 0;
  1219. }
  1220. /* Finally, send the reply synchronously */
  1221. memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
  1222. task = rpc_run_bc_task(req);
  1223. if (IS_ERR(task)) {
  1224. error = PTR_ERR(task);
  1225. goto out;
  1226. }
  1227. WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
  1228. error = task->tk_status;
  1229. rpc_put_task(task);
  1230. out:
  1231. dprintk("svc: %s(), error=%d\n", __func__, error);
  1232. return error;
  1233. }
  1234. EXPORT_SYMBOL_GPL(bc_svc_process);
  1235. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1236. /*
  1237. * Return (transport-specific) limit on the rpc payload.
  1238. */
  1239. u32 svc_max_payload(const struct svc_rqst *rqstp)
  1240. {
  1241. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  1242. if (rqstp->rq_server->sv_max_payload < max)
  1243. max = rqstp->rq_server->sv_max_payload;
  1244. return max;
  1245. }
  1246. EXPORT_SYMBOL_GPL(svc_max_payload);