svc.c 33 KB

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