svc.c 33 KB

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