svc.c 35 KB

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