svc.c 33 KB

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