svc.c 34 KB

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