svc.c 33 KB

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