svc.c 40 KB

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