svc.c 33 KB

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