svc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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);
  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. BUG_ON(m->count == 0);
  279. switch (m->mode) {
  280. case SVC_POOL_PERCPU:
  281. {
  282. set_cpus_allowed_ptr(task, cpumask_of(node));
  283. break;
  284. }
  285. case SVC_POOL_PERNODE:
  286. {
  287. set_cpus_allowed_ptr(task, cpumask_of_node(node));
  288. break;
  289. }
  290. }
  291. }
  292. /*
  293. * Use the mapping mode to choose a pool for a given CPU.
  294. * Used when enqueueing an incoming RPC. Always returns
  295. * a non-NULL pool pointer.
  296. */
  297. struct svc_pool *
  298. svc_pool_for_cpu(struct svc_serv *serv, int cpu)
  299. {
  300. struct svc_pool_map *m = &svc_pool_map;
  301. unsigned int pidx = 0;
  302. /*
  303. * An uninitialised map happens in a pure client when
  304. * lockd is brought up, so silently treat it the
  305. * same as SVC_POOL_GLOBAL.
  306. */
  307. if (svc_serv_is_pooled(serv)) {
  308. switch (m->mode) {
  309. case SVC_POOL_PERCPU:
  310. pidx = m->to_pool[cpu];
  311. break;
  312. case SVC_POOL_PERNODE:
  313. pidx = m->to_pool[cpu_to_node(cpu)];
  314. break;
  315. }
  316. }
  317. return &serv->sv_pools[pidx % serv->sv_nrpools];
  318. }
  319. static int svc_rpcb_setup(struct svc_serv *serv)
  320. {
  321. int err;
  322. err = rpcb_create_local();
  323. if (err)
  324. return err;
  325. /* Remove any stale portmap registrations */
  326. svc_unregister(serv);
  327. return 0;
  328. }
  329. void svc_rpcb_cleanup(struct svc_serv *serv)
  330. {
  331. svc_unregister(serv);
  332. rpcb_put_local();
  333. }
  334. EXPORT_SYMBOL_GPL(svc_rpcb_cleanup);
  335. static int svc_uses_rpcbind(struct svc_serv *serv)
  336. {
  337. struct svc_program *progp;
  338. unsigned int i;
  339. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  340. for (i = 0; i < progp->pg_nvers; i++) {
  341. if (progp->pg_vers[i] == NULL)
  342. continue;
  343. if (progp->pg_vers[i]->vs_hidden == 0)
  344. return 1;
  345. }
  346. }
  347. return 0;
  348. }
  349. /*
  350. * Create an RPC service
  351. */
  352. static struct svc_serv *
  353. __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
  354. void (*shutdown)(struct svc_serv *serv))
  355. {
  356. struct svc_serv *serv;
  357. unsigned int vers;
  358. unsigned int xdrsize;
  359. unsigned int i;
  360. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  361. return NULL;
  362. serv->sv_name = prog->pg_name;
  363. serv->sv_program = prog;
  364. serv->sv_nrthreads = 1;
  365. serv->sv_stats = prog->pg_stats;
  366. if (bufsize > RPCSVC_MAXPAYLOAD)
  367. bufsize = RPCSVC_MAXPAYLOAD;
  368. serv->sv_max_payload = bufsize? bufsize : 4096;
  369. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  370. serv->sv_shutdown = shutdown;
  371. xdrsize = 0;
  372. while (prog) {
  373. prog->pg_lovers = prog->pg_nvers-1;
  374. for (vers=0; vers<prog->pg_nvers ; vers++)
  375. if (prog->pg_vers[vers]) {
  376. prog->pg_hivers = vers;
  377. if (prog->pg_lovers > vers)
  378. prog->pg_lovers = vers;
  379. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  380. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  381. }
  382. prog = prog->pg_next;
  383. }
  384. serv->sv_xdrsize = xdrsize;
  385. INIT_LIST_HEAD(&serv->sv_tempsocks);
  386. INIT_LIST_HEAD(&serv->sv_permsocks);
  387. init_timer(&serv->sv_temptimer);
  388. spin_lock_init(&serv->sv_lock);
  389. serv->sv_nrpools = npools;
  390. serv->sv_pools =
  391. kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
  392. GFP_KERNEL);
  393. if (!serv->sv_pools) {
  394. kfree(serv);
  395. return NULL;
  396. }
  397. for (i = 0; i < serv->sv_nrpools; i++) {
  398. struct svc_pool *pool = &serv->sv_pools[i];
  399. dprintk("svc: initialising pool %u for %s\n",
  400. i, serv->sv_name);
  401. pool->sp_id = i;
  402. INIT_LIST_HEAD(&pool->sp_threads);
  403. INIT_LIST_HEAD(&pool->sp_sockets);
  404. INIT_LIST_HEAD(&pool->sp_all_threads);
  405. spin_lock_init(&pool->sp_lock);
  406. }
  407. if (svc_uses_rpcbind(serv)) {
  408. if (svc_rpcb_setup(serv) < 0) {
  409. kfree(serv->sv_pools);
  410. kfree(serv);
  411. return NULL;
  412. }
  413. if (!serv->sv_shutdown)
  414. serv->sv_shutdown = svc_rpcb_cleanup;
  415. }
  416. return serv;
  417. }
  418. struct svc_serv *
  419. svc_create(struct svc_program *prog, unsigned int bufsize,
  420. void (*shutdown)(struct svc_serv *serv))
  421. {
  422. return __svc_create(prog, bufsize, /*npools*/1, shutdown);
  423. }
  424. EXPORT_SYMBOL_GPL(svc_create);
  425. struct svc_serv *
  426. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  427. void (*shutdown)(struct svc_serv *serv),
  428. svc_thread_fn func, struct module *mod)
  429. {
  430. struct svc_serv *serv;
  431. unsigned int npools = svc_pool_map_get();
  432. serv = __svc_create(prog, bufsize, npools, shutdown);
  433. if (serv != NULL) {
  434. serv->sv_function = func;
  435. serv->sv_module = mod;
  436. }
  437. return serv;
  438. }
  439. EXPORT_SYMBOL_GPL(svc_create_pooled);
  440. /*
  441. * Destroy an RPC service. Should be called with appropriate locking to
  442. * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
  443. */
  444. void
  445. svc_destroy(struct svc_serv *serv)
  446. {
  447. dprintk("svc: svc_destroy(%s, %d)\n",
  448. serv->sv_program->pg_name,
  449. serv->sv_nrthreads);
  450. if (serv->sv_nrthreads) {
  451. if (--(serv->sv_nrthreads) != 0) {
  452. svc_sock_update_bufs(serv);
  453. return;
  454. }
  455. } else
  456. printk("svc_destroy: no threads for serv=%p!\n", serv);
  457. del_timer_sync(&serv->sv_temptimer);
  458. /*
  459. * The set of xprts (contained in the sv_tempsocks and
  460. * sv_permsocks lists) is now constant, since it is modified
  461. * only by accepting new sockets (done by service threads in
  462. * svc_recv) or aging old ones (done by sv_temptimer), or
  463. * configuration changes (excluded by whatever locking the
  464. * caller is using--nfsd_mutex in the case of nfsd). So it's
  465. * safe to traverse those lists and shut everything down:
  466. */
  467. svc_close_all(serv);
  468. if (serv->sv_shutdown)
  469. serv->sv_shutdown(serv);
  470. cache_clean_deferred(serv);
  471. if (svc_serv_is_pooled(serv))
  472. svc_pool_map_put();
  473. kfree(serv->sv_pools);
  474. kfree(serv);
  475. }
  476. EXPORT_SYMBOL_GPL(svc_destroy);
  477. /*
  478. * Allocate an RPC server's buffer space.
  479. * We allocate pages and place them in rq_argpages.
  480. */
  481. static int
  482. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node)
  483. {
  484. unsigned int pages, arghi;
  485. /* bc_xprt uses fore channel allocated buffers */
  486. if (svc_is_backchannel(rqstp))
  487. return 1;
  488. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  489. * We assume one is at most one page
  490. */
  491. arghi = 0;
  492. BUG_ON(pages > RPCSVC_MAXPAGES);
  493. while (pages) {
  494. struct page *p = alloc_pages_node(node, GFP_KERNEL, 0);
  495. if (!p)
  496. break;
  497. rqstp->rq_pages[arghi++] = p;
  498. pages--;
  499. }
  500. return pages == 0;
  501. }
  502. /*
  503. * Release an RPC server buffer
  504. */
  505. static void
  506. svc_release_buffer(struct svc_rqst *rqstp)
  507. {
  508. unsigned int i;
  509. for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
  510. if (rqstp->rq_pages[i])
  511. put_page(rqstp->rq_pages[i]);
  512. }
  513. struct svc_rqst *
  514. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
  515. {
  516. struct svc_rqst *rqstp;
  517. rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
  518. if (!rqstp)
  519. goto out_enomem;
  520. init_waitqueue_head(&rqstp->rq_wait);
  521. serv->sv_nrthreads++;
  522. spin_lock_bh(&pool->sp_lock);
  523. pool->sp_nrthreads++;
  524. list_add(&rqstp->rq_all, &pool->sp_all_threads);
  525. spin_unlock_bh(&pool->sp_lock);
  526. rqstp->rq_server = serv;
  527. rqstp->rq_pool = pool;
  528. rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  529. if (!rqstp->rq_argp)
  530. goto out_thread;
  531. rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  532. if (!rqstp->rq_resp)
  533. goto out_thread;
  534. if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
  535. goto out_thread;
  536. return rqstp;
  537. out_thread:
  538. svc_exit_thread(rqstp);
  539. out_enomem:
  540. return ERR_PTR(-ENOMEM);
  541. }
  542. EXPORT_SYMBOL_GPL(svc_prepare_thread);
  543. /*
  544. * Choose a pool in which to create a new thread, for svc_set_num_threads
  545. */
  546. static inline struct svc_pool *
  547. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  548. {
  549. if (pool != NULL)
  550. return pool;
  551. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  552. }
  553. /*
  554. * Choose a thread to kill, for svc_set_num_threads
  555. */
  556. static inline struct task_struct *
  557. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  558. {
  559. unsigned int i;
  560. struct task_struct *task = NULL;
  561. if (pool != NULL) {
  562. spin_lock_bh(&pool->sp_lock);
  563. } else {
  564. /* choose a pool in round-robin fashion */
  565. for (i = 0; i < serv->sv_nrpools; i++) {
  566. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  567. spin_lock_bh(&pool->sp_lock);
  568. if (!list_empty(&pool->sp_all_threads))
  569. goto found_pool;
  570. spin_unlock_bh(&pool->sp_lock);
  571. }
  572. return NULL;
  573. }
  574. found_pool:
  575. if (!list_empty(&pool->sp_all_threads)) {
  576. struct svc_rqst *rqstp;
  577. /*
  578. * Remove from the pool->sp_all_threads list
  579. * so we don't try to kill it again.
  580. */
  581. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  582. list_del_init(&rqstp->rq_all);
  583. task = rqstp->rq_task;
  584. }
  585. spin_unlock_bh(&pool->sp_lock);
  586. return task;
  587. }
  588. /*
  589. * Create or destroy enough new threads to make the number
  590. * of threads the given number. If `pool' is non-NULL, applies
  591. * only to threads in that pool, otherwise round-robins between
  592. * all pools. Caller must ensure that mutual exclusion between this and
  593. * server startup or shutdown.
  594. *
  595. * Destroying threads relies on the service threads filling in
  596. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  597. * has been created using svc_create_pooled().
  598. *
  599. * Based on code that used to be in nfsd_svc() but tweaked
  600. * to be pool-aware.
  601. */
  602. int
  603. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  604. {
  605. struct svc_rqst *rqstp;
  606. struct task_struct *task;
  607. struct svc_pool *chosen_pool;
  608. int error = 0;
  609. unsigned int state = serv->sv_nrthreads-1;
  610. int node;
  611. if (pool == NULL) {
  612. /* The -1 assumes caller has done a svc_get() */
  613. nrservs -= (serv->sv_nrthreads-1);
  614. } else {
  615. spin_lock_bh(&pool->sp_lock);
  616. nrservs -= pool->sp_nrthreads;
  617. spin_unlock_bh(&pool->sp_lock);
  618. }
  619. /* create new threads */
  620. while (nrservs > 0) {
  621. nrservs--;
  622. chosen_pool = choose_pool(serv, pool, &state);
  623. node = svc_pool_map_get_node(chosen_pool->sp_id);
  624. rqstp = svc_prepare_thread(serv, chosen_pool, node);
  625. if (IS_ERR(rqstp)) {
  626. error = PTR_ERR(rqstp);
  627. break;
  628. }
  629. __module_get(serv->sv_module);
  630. task = kthread_create_on_node(serv->sv_function, rqstp,
  631. node, serv->sv_name);
  632. if (IS_ERR(task)) {
  633. error = PTR_ERR(task);
  634. module_put(serv->sv_module);
  635. svc_exit_thread(rqstp);
  636. break;
  637. }
  638. rqstp->rq_task = task;
  639. if (serv->sv_nrpools > 1)
  640. svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
  641. svc_sock_update_bufs(serv);
  642. wake_up_process(task);
  643. }
  644. /* destroy old threads */
  645. while (nrservs < 0 &&
  646. (task = choose_victim(serv, pool, &state)) != NULL) {
  647. send_sig(SIGINT, task, 1);
  648. nrservs++;
  649. }
  650. return error;
  651. }
  652. EXPORT_SYMBOL_GPL(svc_set_num_threads);
  653. /*
  654. * Called from a server thread as it's exiting. Caller must hold the BKL or
  655. * the "service mutex", whichever is appropriate for the service.
  656. */
  657. void
  658. svc_exit_thread(struct svc_rqst *rqstp)
  659. {
  660. struct svc_serv *serv = rqstp->rq_server;
  661. struct svc_pool *pool = rqstp->rq_pool;
  662. svc_release_buffer(rqstp);
  663. kfree(rqstp->rq_resp);
  664. kfree(rqstp->rq_argp);
  665. kfree(rqstp->rq_auth_data);
  666. spin_lock_bh(&pool->sp_lock);
  667. pool->sp_nrthreads--;
  668. list_del(&rqstp->rq_all);
  669. spin_unlock_bh(&pool->sp_lock);
  670. kfree(rqstp);
  671. /* Release the server */
  672. if (serv)
  673. svc_destroy(serv);
  674. }
  675. EXPORT_SYMBOL_GPL(svc_exit_thread);
  676. /*
  677. * Register an "inet" protocol family netid with the local
  678. * rpcbind daemon via an rpcbind v4 SET request.
  679. *
  680. * No netconfig infrastructure is available in the kernel, so
  681. * we map IP_ protocol numbers to netids by hand.
  682. *
  683. * Returns zero on success; a negative errno value is returned
  684. * if any error occurs.
  685. */
  686. static int __svc_rpcb_register4(const u32 program, const u32 version,
  687. const unsigned short protocol,
  688. const unsigned short port)
  689. {
  690. const struct sockaddr_in sin = {
  691. .sin_family = AF_INET,
  692. .sin_addr.s_addr = htonl(INADDR_ANY),
  693. .sin_port = htons(port),
  694. };
  695. const char *netid;
  696. int error;
  697. switch (protocol) {
  698. case IPPROTO_UDP:
  699. netid = RPCBIND_NETID_UDP;
  700. break;
  701. case IPPROTO_TCP:
  702. netid = RPCBIND_NETID_TCP;
  703. break;
  704. default:
  705. return -ENOPROTOOPT;
  706. }
  707. error = rpcb_v4_register(program, version,
  708. (const struct sockaddr *)&sin, netid);
  709. /*
  710. * User space didn't support rpcbind v4, so retry this
  711. * registration request with the legacy rpcbind v2 protocol.
  712. */
  713. if (error == -EPROTONOSUPPORT)
  714. error = rpcb_register(program, version, protocol, port);
  715. return error;
  716. }
  717. #if IS_ENABLED(CONFIG_IPV6)
  718. /*
  719. * Register an "inet6" protocol family netid with the local
  720. * rpcbind daemon via an rpcbind v4 SET request.
  721. *
  722. * No netconfig infrastructure is available in the kernel, so
  723. * we map IP_ protocol numbers to netids by hand.
  724. *
  725. * Returns zero on success; a negative errno value is returned
  726. * if any error occurs.
  727. */
  728. static int __svc_rpcb_register6(const u32 program, const u32 version,
  729. const unsigned short protocol,
  730. const unsigned short port)
  731. {
  732. const struct sockaddr_in6 sin6 = {
  733. .sin6_family = AF_INET6,
  734. .sin6_addr = IN6ADDR_ANY_INIT,
  735. .sin6_port = htons(port),
  736. };
  737. const char *netid;
  738. int error;
  739. switch (protocol) {
  740. case IPPROTO_UDP:
  741. netid = RPCBIND_NETID_UDP6;
  742. break;
  743. case IPPROTO_TCP:
  744. netid = RPCBIND_NETID_TCP6;
  745. break;
  746. default:
  747. return -ENOPROTOOPT;
  748. }
  749. error = rpcb_v4_register(program, version,
  750. (const struct sockaddr *)&sin6, netid);
  751. /*
  752. * User space didn't support rpcbind version 4, so we won't
  753. * use a PF_INET6 listener.
  754. */
  755. if (error == -EPROTONOSUPPORT)
  756. error = -EAFNOSUPPORT;
  757. return error;
  758. }
  759. #endif /* IS_ENABLED(CONFIG_IPV6) */
  760. /*
  761. * Register a kernel RPC service via rpcbind version 4.
  762. *
  763. * Returns zero on success; a negative errno value is returned
  764. * if any error occurs.
  765. */
  766. static int __svc_register(const char *progname,
  767. const u32 program, const u32 version,
  768. const int family,
  769. const unsigned short protocol,
  770. const unsigned short port)
  771. {
  772. int error = -EAFNOSUPPORT;
  773. switch (family) {
  774. case PF_INET:
  775. error = __svc_rpcb_register4(program, version,
  776. protocol, port);
  777. break;
  778. #if IS_ENABLED(CONFIG_IPV6)
  779. case PF_INET6:
  780. error = __svc_rpcb_register6(program, version,
  781. protocol, port);
  782. #endif
  783. }
  784. if (error < 0)
  785. printk(KERN_WARNING "svc: failed to register %sv%u RPC "
  786. "service (errno %d).\n", progname, version, -error);
  787. return error;
  788. }
  789. /**
  790. * svc_register - register an RPC service with the local portmapper
  791. * @serv: svc_serv struct for the service to register
  792. * @family: protocol family of service's listener socket
  793. * @proto: transport protocol number to advertise
  794. * @port: port to advertise
  795. *
  796. * Service is registered for any address in the passed-in protocol family
  797. */
  798. int svc_register(const struct svc_serv *serv, const int family,
  799. const unsigned short proto, const unsigned short port)
  800. {
  801. struct svc_program *progp;
  802. unsigned int i;
  803. int error = 0;
  804. BUG_ON(proto == 0 && port == 0);
  805. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  806. for (i = 0; i < progp->pg_nvers; i++) {
  807. if (progp->pg_vers[i] == NULL)
  808. continue;
  809. dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n",
  810. progp->pg_name,
  811. i,
  812. proto == IPPROTO_UDP? "udp" : "tcp",
  813. port,
  814. family,
  815. progp->pg_vers[i]->vs_hidden?
  816. " (but not telling portmap)" : "");
  817. if (progp->pg_vers[i]->vs_hidden)
  818. continue;
  819. error = __svc_register(progp->pg_name, progp->pg_prog,
  820. i, family, proto, port);
  821. if (error < 0)
  822. break;
  823. }
  824. }
  825. return error;
  826. }
  827. /*
  828. * If user space is running rpcbind, it should take the v4 UNSET
  829. * and clear everything for this [program, version]. If user space
  830. * is running portmap, it will reject the v4 UNSET, but won't have
  831. * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
  832. * in this case to clear all existing entries for [program, version].
  833. */
  834. static void __svc_unregister(const u32 program, const u32 version,
  835. const char *progname)
  836. {
  837. int error;
  838. error = rpcb_v4_register(program, version, NULL, "");
  839. /*
  840. * User space didn't support rpcbind v4, so retry this
  841. * request with the legacy rpcbind v2 protocol.
  842. */
  843. if (error == -EPROTONOSUPPORT)
  844. error = rpcb_register(program, version, 0, 0);
  845. dprintk("svc: %s(%sv%u), error %d\n",
  846. __func__, progname, version, error);
  847. }
  848. /*
  849. * All netids, bind addresses and ports registered for [program, version]
  850. * are removed from the local rpcbind database (if the service is not
  851. * hidden) to make way for a new instance of the service.
  852. *
  853. * The result of unregistration is reported via dprintk for those who want
  854. * verification of the result, but is otherwise not important.
  855. */
  856. static void svc_unregister(const struct svc_serv *serv)
  857. {
  858. struct svc_program *progp;
  859. unsigned long flags;
  860. unsigned int i;
  861. clear_thread_flag(TIF_SIGPENDING);
  862. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  863. for (i = 0; i < progp->pg_nvers; i++) {
  864. if (progp->pg_vers[i] == NULL)
  865. continue;
  866. if (progp->pg_vers[i]->vs_hidden)
  867. continue;
  868. dprintk("svc: attempting to unregister %sv%u\n",
  869. progp->pg_name, i);
  870. __svc_unregister(progp->pg_prog, i, progp->pg_name);
  871. }
  872. }
  873. spin_lock_irqsave(&current->sighand->siglock, flags);
  874. recalc_sigpending();
  875. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  876. }
  877. /*
  878. * Printk the given error with the address of the client that caused it.
  879. */
  880. static __printf(2, 3)
  881. int svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  882. {
  883. va_list args;
  884. int r;
  885. char buf[RPC_MAX_ADDRBUFLEN];
  886. if (!net_ratelimit())
  887. return 0;
  888. printk(KERN_WARNING "svc: %s: ",
  889. svc_print_addr(rqstp, buf, sizeof(buf)));
  890. va_start(args, fmt);
  891. r = vprintk(fmt, args);
  892. va_end(args);
  893. return r;
  894. }
  895. /*
  896. * Common routine for processing the RPC request.
  897. */
  898. static int
  899. svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
  900. {
  901. struct svc_program *progp;
  902. struct svc_version *versp = NULL; /* compiler food */
  903. struct svc_procedure *procp = NULL;
  904. struct svc_serv *serv = rqstp->rq_server;
  905. kxdrproc_t xdr;
  906. __be32 *statp;
  907. u32 prog, vers, proc;
  908. __be32 auth_stat, rpc_stat;
  909. int auth_res;
  910. __be32 *reply_statp;
  911. rpc_stat = rpc_success;
  912. if (argv->iov_len < 6*4)
  913. goto err_short_len;
  914. /* Will be turned off only in gss privacy case: */
  915. rqstp->rq_splice_ok = 1;
  916. /* Will be turned off only when NFSv4 Sessions are used */
  917. rqstp->rq_usedeferral = 1;
  918. rqstp->rq_dropme = false;
  919. /* Setup reply header */
  920. rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
  921. svc_putu32(resv, rqstp->rq_xid);
  922. vers = svc_getnl(argv);
  923. /* First words of reply: */
  924. svc_putnl(resv, 1); /* REPLY */
  925. if (vers != 2) /* RPC version number */
  926. goto err_bad_rpc;
  927. /* Save position in case we later decide to reject: */
  928. reply_statp = resv->iov_base + resv->iov_len;
  929. svc_putnl(resv, 0); /* ACCEPT */
  930. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  931. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  932. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  933. progp = serv->sv_program;
  934. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  935. if (prog == progp->pg_prog)
  936. break;
  937. /*
  938. * Decode auth data, and add verifier to reply buffer.
  939. * We do this before anything else in order to get a decent
  940. * auth verifier.
  941. */
  942. auth_res = svc_authenticate(rqstp, &auth_stat);
  943. /* Also give the program a chance to reject this call: */
  944. if (auth_res == SVC_OK && progp) {
  945. auth_stat = rpc_autherr_badcred;
  946. auth_res = progp->pg_authenticate(rqstp);
  947. }
  948. switch (auth_res) {
  949. case SVC_OK:
  950. break;
  951. case SVC_GARBAGE:
  952. goto err_garbage;
  953. case SVC_SYSERR:
  954. rpc_stat = rpc_system_err;
  955. goto err_bad;
  956. case SVC_DENIED:
  957. goto err_bad_auth;
  958. case SVC_CLOSE:
  959. if (test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
  960. svc_close_xprt(rqstp->rq_xprt);
  961. case SVC_DROP:
  962. goto dropit;
  963. case SVC_COMPLETE:
  964. goto sendit;
  965. }
  966. if (progp == NULL)
  967. goto err_bad_prog;
  968. if (vers >= progp->pg_nvers ||
  969. !(versp = progp->pg_vers[vers]))
  970. goto err_bad_vers;
  971. procp = versp->vs_proc + proc;
  972. if (proc >= versp->vs_nproc || !procp->pc_func)
  973. goto err_bad_proc;
  974. rqstp->rq_procinfo = procp;
  975. /* Syntactic check complete */
  976. serv->sv_stats->rpccnt++;
  977. /* Build the reply header. */
  978. statp = resv->iov_base +resv->iov_len;
  979. svc_putnl(resv, RPC_SUCCESS);
  980. /* Bump per-procedure stats counter */
  981. procp->pc_count++;
  982. /* Initialize storage for argp and resp */
  983. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  984. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  985. /* un-reserve some of the out-queue now that we have a
  986. * better idea of reply size
  987. */
  988. if (procp->pc_xdrressize)
  989. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  990. /* Call the function that processes the request. */
  991. if (!versp->vs_dispatch) {
  992. /* Decode arguments */
  993. xdr = procp->pc_decode;
  994. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  995. goto err_garbage;
  996. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  997. /* Encode reply */
  998. if (rqstp->rq_dropme) {
  999. if (procp->pc_release)
  1000. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1001. goto dropit;
  1002. }
  1003. if (*statp == rpc_success &&
  1004. (xdr = procp->pc_encode) &&
  1005. !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  1006. dprintk("svc: failed to encode reply\n");
  1007. /* serv->sv_stats->rpcsystemerr++; */
  1008. *statp = rpc_system_err;
  1009. }
  1010. } else {
  1011. dprintk("svc: calling dispatcher\n");
  1012. if (!versp->vs_dispatch(rqstp, statp)) {
  1013. /* Release reply info */
  1014. if (procp->pc_release)
  1015. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1016. goto dropit;
  1017. }
  1018. }
  1019. /* Check RPC status result */
  1020. if (*statp != rpc_success)
  1021. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  1022. /* Release reply info */
  1023. if (procp->pc_release)
  1024. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  1025. if (procp->pc_encode == NULL)
  1026. goto dropit;
  1027. sendit:
  1028. if (svc_authorise(rqstp))
  1029. goto dropit;
  1030. return 1; /* Caller can now send it */
  1031. dropit:
  1032. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  1033. dprintk("svc: svc_process dropit\n");
  1034. return 0;
  1035. err_short_len:
  1036. svc_printk(rqstp, "short len %Zd, dropping request\n",
  1037. argv->iov_len);
  1038. goto dropit; /* drop request */
  1039. err_bad_rpc:
  1040. serv->sv_stats->rpcbadfmt++;
  1041. svc_putnl(resv, 1); /* REJECT */
  1042. svc_putnl(resv, 0); /* RPC_MISMATCH */
  1043. svc_putnl(resv, 2); /* Only RPCv2 supported */
  1044. svc_putnl(resv, 2);
  1045. goto sendit;
  1046. err_bad_auth:
  1047. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  1048. serv->sv_stats->rpcbadauth++;
  1049. /* Restore write pointer to location of accept status: */
  1050. xdr_ressize_check(rqstp, reply_statp);
  1051. svc_putnl(resv, 1); /* REJECT */
  1052. svc_putnl(resv, 1); /* AUTH_ERROR */
  1053. svc_putnl(resv, ntohl(auth_stat)); /* status */
  1054. goto sendit;
  1055. err_bad_prog:
  1056. dprintk("svc: unknown program %d\n", prog);
  1057. serv->sv_stats->rpcbadfmt++;
  1058. svc_putnl(resv, RPC_PROG_UNAVAIL);
  1059. goto sendit;
  1060. err_bad_vers:
  1061. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  1062. vers, prog, progp->pg_name);
  1063. serv->sv_stats->rpcbadfmt++;
  1064. svc_putnl(resv, RPC_PROG_MISMATCH);
  1065. svc_putnl(resv, progp->pg_lovers);
  1066. svc_putnl(resv, progp->pg_hivers);
  1067. goto sendit;
  1068. err_bad_proc:
  1069. svc_printk(rqstp, "unknown procedure (%d)\n", proc);
  1070. serv->sv_stats->rpcbadfmt++;
  1071. svc_putnl(resv, RPC_PROC_UNAVAIL);
  1072. goto sendit;
  1073. err_garbage:
  1074. svc_printk(rqstp, "failed to decode args\n");
  1075. rpc_stat = rpc_garbage_args;
  1076. err_bad:
  1077. serv->sv_stats->rpcbadfmt++;
  1078. svc_putnl(resv, ntohl(rpc_stat));
  1079. goto sendit;
  1080. }
  1081. EXPORT_SYMBOL_GPL(svc_process);
  1082. /*
  1083. * Process the RPC request.
  1084. */
  1085. int
  1086. svc_process(struct svc_rqst *rqstp)
  1087. {
  1088. struct kvec *argv = &rqstp->rq_arg.head[0];
  1089. struct kvec *resv = &rqstp->rq_res.head[0];
  1090. struct svc_serv *serv = rqstp->rq_server;
  1091. u32 dir;
  1092. /*
  1093. * Setup response xdr_buf.
  1094. * Initially it has just one page
  1095. */
  1096. rqstp->rq_resused = 1;
  1097. resv->iov_base = page_address(rqstp->rq_respages[0]);
  1098. resv->iov_len = 0;
  1099. rqstp->rq_res.pages = rqstp->rq_respages + 1;
  1100. rqstp->rq_res.len = 0;
  1101. rqstp->rq_res.page_base = 0;
  1102. rqstp->rq_res.page_len = 0;
  1103. rqstp->rq_res.buflen = PAGE_SIZE;
  1104. rqstp->rq_res.tail[0].iov_base = NULL;
  1105. rqstp->rq_res.tail[0].iov_len = 0;
  1106. rqstp->rq_xid = svc_getu32(argv);
  1107. dir = svc_getnl(argv);
  1108. if (dir != 0) {
  1109. /* direction != CALL */
  1110. svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
  1111. serv->sv_stats->rpcbadfmt++;
  1112. svc_drop(rqstp);
  1113. return 0;
  1114. }
  1115. /* Returns 1 for send, 0 for drop */
  1116. if (svc_process_common(rqstp, argv, resv))
  1117. return svc_send(rqstp);
  1118. else {
  1119. svc_drop(rqstp);
  1120. return 0;
  1121. }
  1122. }
  1123. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1124. /*
  1125. * Process a backchannel RPC request that arrived over an existing
  1126. * outbound connection
  1127. */
  1128. int
  1129. bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
  1130. struct svc_rqst *rqstp)
  1131. {
  1132. struct kvec *argv = &rqstp->rq_arg.head[0];
  1133. struct kvec *resv = &rqstp->rq_res.head[0];
  1134. /* Build the svc_rqst used by the common processing routine */
  1135. rqstp->rq_xprt = serv->sv_bc_xprt;
  1136. rqstp->rq_xid = req->rq_xid;
  1137. rqstp->rq_prot = req->rq_xprt->prot;
  1138. rqstp->rq_server = serv;
  1139. rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
  1140. memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
  1141. memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
  1142. memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
  1143. /* reset result send buffer "put" position */
  1144. resv->iov_len = 0;
  1145. if (rqstp->rq_prot != IPPROTO_TCP) {
  1146. printk(KERN_ERR "No support for Non-TCP transports!\n");
  1147. BUG();
  1148. }
  1149. /*
  1150. * Skip the next two words because they've already been
  1151. * processed in the trasport
  1152. */
  1153. svc_getu32(argv); /* XID */
  1154. svc_getnl(argv); /* CALLDIR */
  1155. /* Returns 1 for send, 0 for drop */
  1156. if (svc_process_common(rqstp, argv, resv)) {
  1157. memcpy(&req->rq_snd_buf, &rqstp->rq_res,
  1158. sizeof(req->rq_snd_buf));
  1159. return bc_send(req);
  1160. } else {
  1161. /* Nothing to do to drop request */
  1162. return 0;
  1163. }
  1164. }
  1165. EXPORT_SYMBOL_GPL(bc_svc_process);
  1166. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1167. /*
  1168. * Return (transport-specific) limit on the rpc payload.
  1169. */
  1170. u32 svc_max_payload(const struct svc_rqst *rqstp)
  1171. {
  1172. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  1173. if (rqstp->rq_server->sv_max_payload < max)
  1174. max = rqstp->rq_server->sv_max_payload;
  1175. return max;
  1176. }
  1177. EXPORT_SYMBOL_GPL(svc_max_payload);