svc_xprt.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/svc_xprt.c
  4. *
  5. * Author: Tom Tucker <tom@opengridcomputing.com>
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/errno.h>
  10. #include <linux/freezer.h>
  11. #include <linux/slab.h>
  12. #include <net/sock.h>
  13. #include <linux/sunrpc/addr.h>
  14. #include <linux/sunrpc/stats.h>
  15. #include <linux/sunrpc/svc_xprt.h>
  16. #include <linux/sunrpc/svcsock.h>
  17. #include <linux/sunrpc/xprt.h>
  18. #include <linux/sunrpc/bc_xprt.h>
  19. #include <linux/module.h>
  20. #include <linux/netdevice.h>
  21. #include <trace/events/sunrpc.h>
  22. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  23. static unsigned int svc_rpc_per_connection_limit __read_mostly;
  24. module_param(svc_rpc_per_connection_limit, uint, 0644);
  25. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
  26. static int svc_deferred_recv(struct svc_rqst *rqstp);
  27. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  28. static void svc_age_temp_xprts(struct timer_list *t);
  29. static void svc_delete_xprt(struct svc_xprt *xprt);
  30. /* apparently the "standard" is that clients close
  31. * idle connections after 5 minutes, servers after
  32. * 6 minutes
  33. * http://nfsv4bat.org/Documents/ConnectAThon/1996/nfstcp.pdf
  34. */
  35. static int svc_conn_age_period = 6*60;
  36. /* List of registered transport classes */
  37. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  38. static LIST_HEAD(svc_xprt_class_list);
  39. /* SMP locking strategy:
  40. *
  41. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  42. * when both need to be taken (rare), svc_serv->sv_lock is first.
  43. * The "service mutex" protects svc_serv->sv_nrthread.
  44. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  45. * and the ->sk_info_authunix cache.
  46. *
  47. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  48. * enqueued multiply. During normal transport processing this bit
  49. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  50. * Providers should not manipulate this bit directly.
  51. *
  52. * Some flags can be set to certain values at any time
  53. * providing that certain rules are followed:
  54. *
  55. * XPT_CONN, XPT_DATA:
  56. * - Can be set or cleared at any time.
  57. * - After a set, svc_xprt_enqueue must be called to enqueue
  58. * the transport for processing.
  59. * - After a clear, the transport must be read/accepted.
  60. * If this succeeds, it must be set again.
  61. * XPT_CLOSE:
  62. * - Can set at any time. It is never cleared.
  63. * XPT_DEAD:
  64. * - Can only be set while XPT_BUSY is held which ensures
  65. * that no other thread will be using the transport or will
  66. * try to set XPT_DEAD.
  67. */
  68. /**
  69. * svc_reg_xprt_class - Register a server-side RPC transport class
  70. * @xcl: New transport class to be registered
  71. *
  72. * Returns zero on success; otherwise a negative errno is returned.
  73. */
  74. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  75. {
  76. struct svc_xprt_class *cl;
  77. int res = -EEXIST;
  78. INIT_LIST_HEAD(&xcl->xcl_list);
  79. spin_lock(&svc_xprt_class_lock);
  80. /* Make sure there isn't already a class with the same name */
  81. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  82. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  83. goto out;
  84. }
  85. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  86. res = 0;
  87. out:
  88. spin_unlock(&svc_xprt_class_lock);
  89. return res;
  90. }
  91. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  92. /**
  93. * svc_unreg_xprt_class - Unregister a server-side RPC transport class
  94. * @xcl: Transport class to be unregistered
  95. *
  96. */
  97. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  98. {
  99. spin_lock(&svc_xprt_class_lock);
  100. list_del_init(&xcl->xcl_list);
  101. spin_unlock(&svc_xprt_class_lock);
  102. }
  103. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  104. /**
  105. * svc_print_xprts - Format the transport list for printing
  106. * @buf: target buffer for formatted address
  107. * @maxlen: length of target buffer
  108. *
  109. * Fills in @buf with a string containing a list of transport names, each name
  110. * terminated with '\n'. If the buffer is too small, some entries may be
  111. * missing, but it is guaranteed that all lines in the output buffer are
  112. * complete.
  113. *
  114. * Returns positive length of the filled-in string.
  115. */
  116. int svc_print_xprts(char *buf, int maxlen)
  117. {
  118. struct svc_xprt_class *xcl;
  119. char tmpstr[80];
  120. int len = 0;
  121. buf[0] = '\0';
  122. spin_lock(&svc_xprt_class_lock);
  123. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  124. int slen;
  125. slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n",
  126. xcl->xcl_name, xcl->xcl_max_payload);
  127. if (slen >= sizeof(tmpstr) || len + slen >= maxlen)
  128. break;
  129. len += slen;
  130. strcat(buf, tmpstr);
  131. }
  132. spin_unlock(&svc_xprt_class_lock);
  133. return len;
  134. }
  135. /**
  136. * svc_xprt_deferred_close - Close a transport
  137. * @xprt: transport instance
  138. *
  139. * Used in contexts that need to defer the work of shutting down
  140. * the transport to an nfsd thread.
  141. */
  142. void svc_xprt_deferred_close(struct svc_xprt *xprt)
  143. {
  144. trace_svc_xprt_close(xprt);
  145. if (!test_and_set_bit(XPT_CLOSE, &xprt->xpt_flags))
  146. svc_xprt_enqueue(xprt);
  147. }
  148. EXPORT_SYMBOL_GPL(svc_xprt_deferred_close);
  149. static void svc_xprt_free(struct kref *kref)
  150. {
  151. struct svc_xprt *xprt =
  152. container_of(kref, struct svc_xprt, xpt_ref);
  153. struct module *owner = xprt->xpt_class->xcl_owner;
  154. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
  155. svcauth_unix_info_release(xprt);
  156. put_cred(xprt->xpt_cred);
  157. put_net_track(xprt->xpt_net, &xprt->ns_tracker);
  158. /* See comment on corresponding get in xs_setup_bc_tcp(): */
  159. if (xprt->xpt_bc_xprt)
  160. xprt_put(xprt->xpt_bc_xprt);
  161. if (xprt->xpt_bc_xps)
  162. xprt_switch_put(xprt->xpt_bc_xps);
  163. trace_svc_xprt_free(xprt);
  164. xprt->xpt_ops->xpo_free(xprt);
  165. module_put(owner);
  166. }
  167. void svc_xprt_put(struct svc_xprt *xprt)
  168. {
  169. kref_put(&xprt->xpt_ref, svc_xprt_free);
  170. }
  171. EXPORT_SYMBOL_GPL(svc_xprt_put);
  172. /*
  173. * Called by transport drivers to initialize the transport independent
  174. * portion of the transport instance.
  175. */
  176. void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl,
  177. struct svc_xprt *xprt, struct svc_serv *serv)
  178. {
  179. memset(xprt, 0, sizeof(*xprt));
  180. xprt->xpt_class = xcl;
  181. xprt->xpt_ops = xcl->xcl_ops;
  182. kref_init(&xprt->xpt_ref);
  183. xprt->xpt_server = serv;
  184. INIT_LIST_HEAD(&xprt->xpt_list);
  185. INIT_LIST_HEAD(&xprt->xpt_deferred);
  186. INIT_LIST_HEAD(&xprt->xpt_users);
  187. mutex_init(&xprt->xpt_mutex);
  188. spin_lock_init(&xprt->xpt_lock);
  189. set_bit(XPT_BUSY, &xprt->xpt_flags);
  190. xprt->xpt_net = get_net_track(net, &xprt->ns_tracker, GFP_ATOMIC);
  191. strcpy(xprt->xpt_remotebuf, "uninitialized");
  192. }
  193. EXPORT_SYMBOL_GPL(svc_xprt_init);
  194. /**
  195. * svc_xprt_received - start next receiver thread
  196. * @xprt: controlling transport
  197. *
  198. * The caller must hold the XPT_BUSY bit and must
  199. * not thereafter touch transport data.
  200. *
  201. * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
  202. * insufficient) data.
  203. */
  204. void svc_xprt_received(struct svc_xprt *xprt)
  205. {
  206. if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) {
  207. WARN_ONCE(1, "xprt=0x%p already busy!", xprt);
  208. return;
  209. }
  210. /* As soon as we clear busy, the xprt could be closed and
  211. * 'put', so we need a reference to call svc_xprt_enqueue with:
  212. */
  213. svc_xprt_get(xprt);
  214. smp_mb__before_atomic();
  215. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  216. svc_xprt_enqueue(xprt);
  217. svc_xprt_put(xprt);
  218. }
  219. EXPORT_SYMBOL_GPL(svc_xprt_received);
  220. void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new)
  221. {
  222. clear_bit(XPT_TEMP, &new->xpt_flags);
  223. spin_lock_bh(&serv->sv_lock);
  224. list_add(&new->xpt_list, &serv->sv_permsocks);
  225. spin_unlock_bh(&serv->sv_lock);
  226. svc_xprt_received(new);
  227. }
  228. static int _svc_xprt_create(struct svc_serv *serv, const char *xprt_name,
  229. struct net *net, struct sockaddr *sap,
  230. size_t len, int flags, const struct cred *cred)
  231. {
  232. struct svc_xprt_class *xcl;
  233. spin_lock(&svc_xprt_class_lock);
  234. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  235. struct svc_xprt *newxprt;
  236. unsigned short newport;
  237. if (strcmp(xprt_name, xcl->xcl_name))
  238. continue;
  239. if (!try_module_get(xcl->xcl_owner))
  240. goto err;
  241. spin_unlock(&svc_xprt_class_lock);
  242. newxprt = xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
  243. if (IS_ERR(newxprt)) {
  244. trace_svc_xprt_create_err(serv->sv_programs->pg_name,
  245. xcl->xcl_name, sap, len,
  246. newxprt);
  247. module_put(xcl->xcl_owner);
  248. return PTR_ERR(newxprt);
  249. }
  250. newxprt->xpt_cred = get_cred(cred);
  251. svc_add_new_perm_xprt(serv, newxprt);
  252. newport = svc_xprt_local_port(newxprt);
  253. return newport;
  254. }
  255. err:
  256. spin_unlock(&svc_xprt_class_lock);
  257. /* This errno is exposed to user space. Provide a reasonable
  258. * perror msg for a bad transport. */
  259. return -EPROTONOSUPPORT;
  260. }
  261. /**
  262. * svc_xprt_create_from_sa - Add a new listener to @serv from socket address
  263. * @serv: target RPC service
  264. * @xprt_name: transport class name
  265. * @net: network namespace
  266. * @sap: socket address pointer
  267. * @flags: SVC_SOCK flags
  268. * @cred: credential to bind to this transport
  269. *
  270. * Return local xprt port on success or %-EPROTONOSUPPORT on failure
  271. */
  272. int svc_xprt_create_from_sa(struct svc_serv *serv, const char *xprt_name,
  273. struct net *net, struct sockaddr *sap,
  274. int flags, const struct cred *cred)
  275. {
  276. size_t len;
  277. int err;
  278. switch (sap->sa_family) {
  279. case AF_INET:
  280. len = sizeof(struct sockaddr_in);
  281. break;
  282. #if IS_ENABLED(CONFIG_IPV6)
  283. case AF_INET6:
  284. len = sizeof(struct sockaddr_in6);
  285. break;
  286. #endif
  287. default:
  288. return -EAFNOSUPPORT;
  289. }
  290. err = _svc_xprt_create(serv, xprt_name, net, sap, len, flags, cred);
  291. if (err == -EPROTONOSUPPORT) {
  292. request_module("svc%s", xprt_name);
  293. err = _svc_xprt_create(serv, xprt_name, net, sap, len, flags,
  294. cred);
  295. }
  296. return err;
  297. }
  298. EXPORT_SYMBOL_GPL(svc_xprt_create_from_sa);
  299. /**
  300. * svc_xprt_create - Add a new listener to @serv
  301. * @serv: target RPC service
  302. * @xprt_name: transport class name
  303. * @net: network namespace
  304. * @family: network address family
  305. * @port: listener port
  306. * @flags: SVC_SOCK flags
  307. * @cred: credential to bind to this transport
  308. *
  309. * Return local xprt port on success or %-EPROTONOSUPPORT on failure
  310. */
  311. int svc_xprt_create(struct svc_serv *serv, const char *xprt_name,
  312. struct net *net, const int family,
  313. const unsigned short port, int flags,
  314. const struct cred *cred)
  315. {
  316. struct sockaddr_in sin = {
  317. .sin_family = AF_INET,
  318. .sin_addr.s_addr = htonl(INADDR_ANY),
  319. .sin_port = htons(port),
  320. };
  321. #if IS_ENABLED(CONFIG_IPV6)
  322. struct sockaddr_in6 sin6 = {
  323. .sin6_family = AF_INET6,
  324. .sin6_addr = IN6ADDR_ANY_INIT,
  325. .sin6_port = htons(port),
  326. };
  327. #endif
  328. struct sockaddr *sap;
  329. switch (family) {
  330. case PF_INET:
  331. sap = (struct sockaddr *)&sin;
  332. break;
  333. #if IS_ENABLED(CONFIG_IPV6)
  334. case PF_INET6:
  335. sap = (struct sockaddr *)&sin6;
  336. break;
  337. #endif
  338. default:
  339. return -EAFNOSUPPORT;
  340. }
  341. return svc_xprt_create_from_sa(serv, xprt_name, net, sap, flags, cred);
  342. }
  343. EXPORT_SYMBOL_GPL(svc_xprt_create);
  344. /*
  345. * Copy the local and remote xprt addresses to the rqstp structure
  346. */
  347. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  348. {
  349. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  350. rqstp->rq_addrlen = xprt->xpt_remotelen;
  351. /*
  352. * Destination address in request is needed for binding the
  353. * source address in RPC replies/callbacks later.
  354. */
  355. memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen);
  356. rqstp->rq_daddrlen = xprt->xpt_locallen;
  357. }
  358. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  359. /**
  360. * svc_print_addr - Format rq_addr field for printing
  361. * @rqstp: svc_rqst struct containing address to print
  362. * @buf: target buffer for formatted address
  363. * @len: length of target buffer
  364. *
  365. */
  366. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  367. {
  368. return __svc_print_addr(svc_addr(rqstp), buf, len);
  369. }
  370. EXPORT_SYMBOL_GPL(svc_print_addr);
  371. static bool svc_xprt_slots_in_range(struct svc_xprt *xprt)
  372. {
  373. unsigned int limit = svc_rpc_per_connection_limit;
  374. int nrqsts = atomic_read(&xprt->xpt_nr_rqsts);
  375. return limit == 0 || (nrqsts >= 0 && nrqsts < limit);
  376. }
  377. static bool svc_xprt_reserve_slot(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  378. {
  379. if (!test_bit(RQ_DATA, &rqstp->rq_flags)) {
  380. if (!svc_xprt_slots_in_range(xprt))
  381. return false;
  382. atomic_inc(&xprt->xpt_nr_rqsts);
  383. set_bit(RQ_DATA, &rqstp->rq_flags);
  384. }
  385. return true;
  386. }
  387. static void svc_xprt_release_slot(struct svc_rqst *rqstp)
  388. {
  389. struct svc_xprt *xprt = rqstp->rq_xprt;
  390. if (test_and_clear_bit(RQ_DATA, &rqstp->rq_flags)) {
  391. atomic_dec(&xprt->xpt_nr_rqsts);
  392. smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */
  393. svc_xprt_enqueue(xprt);
  394. }
  395. }
  396. static bool svc_xprt_ready(struct svc_xprt *xprt)
  397. {
  398. unsigned long xpt_flags;
  399. /*
  400. * If another cpu has recently updated xpt_flags,
  401. * sk_sock->flags, xpt_reserved, or xpt_nr_rqsts, we need to
  402. * know about it; otherwise it's possible that both that cpu and
  403. * this one could call svc_xprt_enqueue() without either
  404. * svc_xprt_enqueue() recognizing that the conditions below
  405. * are satisfied, and we could stall indefinitely:
  406. */
  407. smp_rmb();
  408. xpt_flags = READ_ONCE(xprt->xpt_flags);
  409. trace_svc_xprt_enqueue(xprt, xpt_flags);
  410. if (xpt_flags & BIT(XPT_BUSY))
  411. return false;
  412. if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE) | BIT(XPT_HANDSHAKE)))
  413. return true;
  414. if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) {
  415. if (xprt->xpt_ops->xpo_has_wspace(xprt) &&
  416. svc_xprt_slots_in_range(xprt))
  417. return true;
  418. trace_svc_xprt_no_write_space(xprt);
  419. return false;
  420. }
  421. return false;
  422. }
  423. /**
  424. * svc_xprt_enqueue - Queue a transport on an idle nfsd thread
  425. * @xprt: transport with data pending
  426. *
  427. */
  428. void svc_xprt_enqueue(struct svc_xprt *xprt)
  429. {
  430. struct svc_pool *pool;
  431. if (!svc_xprt_ready(xprt))
  432. return;
  433. /* Mark transport as busy. It will remain in this state until
  434. * the provider calls svc_xprt_received. We update XPT_BUSY
  435. * atomically because it also guards against trying to enqueue
  436. * the transport twice.
  437. */
  438. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  439. return;
  440. pool = svc_pool_for_cpu(xprt->xpt_server);
  441. percpu_counter_inc(&pool->sp_sockets_queued);
  442. xprt->xpt_qtime = ktime_get();
  443. lwq_enqueue(&xprt->xpt_ready, &pool->sp_xprts);
  444. svc_pool_wake_idle_thread(pool);
  445. }
  446. EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
  447. /*
  448. * Dequeue the first transport, if there is one.
  449. */
  450. static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
  451. {
  452. struct svc_xprt *xprt = NULL;
  453. xprt = lwq_dequeue(&pool->sp_xprts, struct svc_xprt, xpt_ready);
  454. if (xprt)
  455. svc_xprt_get(xprt);
  456. return xprt;
  457. }
  458. /**
  459. * svc_reserve - change the space reserved for the reply to a request.
  460. * @rqstp: The request in question
  461. * @space: new max space to reserve
  462. *
  463. * Each request reserves some space on the output queue of the transport
  464. * to make sure the reply fits. This function reduces that reserved
  465. * space to be the amount of space used already, plus @space.
  466. *
  467. */
  468. void svc_reserve(struct svc_rqst *rqstp, int space)
  469. {
  470. struct svc_xprt *xprt = rqstp->rq_xprt;
  471. space += rqstp->rq_res.head[0].iov_len;
  472. if (xprt && space < rqstp->rq_reserved) {
  473. atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
  474. rqstp->rq_reserved = space;
  475. smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */
  476. svc_xprt_enqueue(xprt);
  477. }
  478. }
  479. EXPORT_SYMBOL_GPL(svc_reserve);
  480. static void free_deferred(struct svc_xprt *xprt, struct svc_deferred_req *dr)
  481. {
  482. if (!dr)
  483. return;
  484. xprt->xpt_ops->xpo_release_ctxt(xprt, dr->xprt_ctxt);
  485. kfree(dr);
  486. }
  487. static void svc_xprt_release(struct svc_rqst *rqstp)
  488. {
  489. struct svc_xprt *xprt = rqstp->rq_xprt;
  490. xprt->xpt_ops->xpo_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
  491. rqstp->rq_xprt_ctxt = NULL;
  492. free_deferred(xprt, rqstp->rq_deferred);
  493. rqstp->rq_deferred = NULL;
  494. svc_rqst_release_pages(rqstp);
  495. rqstp->rq_res.page_len = 0;
  496. rqstp->rq_res.page_base = 0;
  497. /* Reset response buffer and release
  498. * the reservation.
  499. * But first, check that enough space was reserved
  500. * for the reply, otherwise we have a bug!
  501. */
  502. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  503. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  504. rqstp->rq_reserved,
  505. rqstp->rq_res.len);
  506. rqstp->rq_res.head[0].iov_len = 0;
  507. svc_reserve(rqstp, 0);
  508. svc_xprt_release_slot(rqstp);
  509. rqstp->rq_xprt = NULL;
  510. svc_xprt_put(xprt);
  511. }
  512. /**
  513. * svc_wake_up - Wake up a service thread for non-transport work
  514. * @serv: RPC service
  515. *
  516. * Some svc_serv's will have occasional work to do, even when a xprt is not
  517. * waiting to be serviced. This function is there to "kick" a task in one of
  518. * those services so that it can wake up and do that work. Note that we only
  519. * bother with pool 0 as we don't need to wake up more than one thread for
  520. * this purpose.
  521. */
  522. void svc_wake_up(struct svc_serv *serv)
  523. {
  524. struct svc_pool *pool = &serv->sv_pools[0];
  525. set_bit(SP_TASK_PENDING, &pool->sp_flags);
  526. svc_pool_wake_idle_thread(pool);
  527. }
  528. EXPORT_SYMBOL_GPL(svc_wake_up);
  529. int svc_port_is_privileged(struct sockaddr *sin)
  530. {
  531. switch (sin->sa_family) {
  532. case AF_INET:
  533. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  534. < PROT_SOCK;
  535. case AF_INET6:
  536. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  537. < PROT_SOCK;
  538. default:
  539. return 0;
  540. }
  541. }
  542. /*
  543. * Make sure that we don't have too many connections that have not yet
  544. * demonstrated that they have access to the NFS server. If we have,
  545. * something must be dropped. It's not clear what will happen if we allow
  546. * "too many" connections, but when dealing with network-facing software,
  547. * we have to code defensively. Here we do that by imposing hard limits.
  548. *
  549. * There's no point in trying to do random drop here for DoS
  550. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  551. * attacker can easily beat that.
  552. *
  553. * The only somewhat efficient mechanism would be if drop old
  554. * connections from the same IP first. But right now we don't even
  555. * record the client IP in svc_sock.
  556. */
  557. static void svc_check_conn_limits(struct svc_serv *serv)
  558. {
  559. if (serv->sv_tmpcnt > XPT_MAX_TMP_CONN) {
  560. struct svc_xprt *xprt = NULL, *xprti;
  561. spin_lock_bh(&serv->sv_lock);
  562. if (!list_empty(&serv->sv_tempsocks)) {
  563. /*
  564. * Always select the oldest connection. It's not fair,
  565. * but nor is life.
  566. */
  567. list_for_each_entry_reverse(xprti, &serv->sv_tempsocks,
  568. xpt_list) {
  569. if (!test_bit(XPT_PEER_VALID, &xprti->xpt_flags)) {
  570. xprt = xprti;
  571. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  572. svc_xprt_get(xprt);
  573. break;
  574. }
  575. }
  576. }
  577. spin_unlock_bh(&serv->sv_lock);
  578. if (xprt) {
  579. svc_xprt_enqueue(xprt);
  580. svc_xprt_put(xprt);
  581. }
  582. }
  583. }
  584. static bool svc_alloc_arg(struct svc_rqst *rqstp)
  585. {
  586. struct xdr_buf *arg = &rqstp->rq_arg;
  587. unsigned long pages, filled, ret;
  588. pages = rqstp->rq_maxpages;
  589. for (filled = 0; filled < pages; filled = ret) {
  590. ret = alloc_pages_bulk(GFP_KERNEL, pages, rqstp->rq_pages);
  591. if (ret > filled)
  592. /* Made progress, don't sleep yet */
  593. continue;
  594. set_current_state(TASK_IDLE);
  595. if (svc_thread_should_stop(rqstp)) {
  596. set_current_state(TASK_RUNNING);
  597. return false;
  598. }
  599. trace_svc_alloc_arg_err(pages, ret);
  600. memalloc_retry_wait(GFP_KERNEL);
  601. }
  602. rqstp->rq_page_end = &rqstp->rq_pages[pages];
  603. rqstp->rq_pages[pages] = NULL; /* this might be seen in nfsd_splice_actor() */
  604. /* Make arg->head point to first page and arg->pages point to rest */
  605. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  606. arg->head[0].iov_len = PAGE_SIZE;
  607. arg->pages = rqstp->rq_pages + 1;
  608. arg->page_base = 0;
  609. /* save at least one page for response */
  610. arg->page_len = (pages-2)*PAGE_SIZE;
  611. arg->len = (pages-1)*PAGE_SIZE;
  612. arg->tail[0].iov_len = 0;
  613. rqstp->rq_xid = xdr_zero;
  614. return true;
  615. }
  616. static bool
  617. svc_thread_should_sleep(struct svc_rqst *rqstp)
  618. {
  619. struct svc_pool *pool = rqstp->rq_pool;
  620. /* did someone call svc_wake_up? */
  621. if (test_bit(SP_TASK_PENDING, &pool->sp_flags))
  622. return false;
  623. /* was a socket queued? */
  624. if (!lwq_empty(&pool->sp_xprts))
  625. return false;
  626. /* are we shutting down? */
  627. if (svc_thread_should_stop(rqstp))
  628. return false;
  629. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  630. if (svc_is_backchannel(rqstp)) {
  631. if (!lwq_empty(&rqstp->rq_server->sv_cb_list))
  632. return false;
  633. }
  634. #endif
  635. return true;
  636. }
  637. static bool svc_schedule_timeout(long timeo)
  638. {
  639. return schedule_timeout(timeo ? timeo : MAX_SCHEDULE_TIMEOUT) == 0;
  640. }
  641. static bool svc_thread_wait_for_work(struct svc_rqst *rqstp, long timeo)
  642. {
  643. struct svc_pool *pool = rqstp->rq_pool;
  644. bool did_timeout = false;
  645. if (svc_thread_should_sleep(rqstp)) {
  646. set_current_state(TASK_IDLE | TASK_FREEZABLE);
  647. llist_add(&rqstp->rq_idle, &pool->sp_idle_threads);
  648. if (likely(svc_thread_should_sleep(rqstp)))
  649. did_timeout = svc_schedule_timeout(timeo);
  650. while (!llist_del_first_this(&pool->sp_idle_threads,
  651. &rqstp->rq_idle)) {
  652. /* Work just became available. This thread can only
  653. * handle it after removing rqstp from the idle
  654. * list. If that attempt failed, some other thread
  655. * must have queued itself after finding no
  656. * work to do, so that thread has taken responsibly
  657. * for this new work. This thread can safely sleep
  658. * until woken again.
  659. */
  660. did_timeout = svc_schedule_timeout(timeo);
  661. set_current_state(TASK_IDLE | TASK_FREEZABLE);
  662. }
  663. __set_current_state(TASK_RUNNING);
  664. } else {
  665. cond_resched();
  666. }
  667. try_to_freeze();
  668. return did_timeout;
  669. }
  670. static void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt)
  671. {
  672. spin_lock_bh(&serv->sv_lock);
  673. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  674. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  675. serv->sv_tmpcnt++;
  676. if (serv->sv_temptimer.function == NULL) {
  677. /* setup timer to age temp transports */
  678. serv->sv_temptimer.function = svc_age_temp_xprts;
  679. mod_timer(&serv->sv_temptimer,
  680. jiffies + svc_conn_age_period * HZ);
  681. }
  682. spin_unlock_bh(&serv->sv_lock);
  683. svc_xprt_received(newxpt);
  684. }
  685. static void svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  686. {
  687. struct svc_serv *serv = rqstp->rq_server;
  688. int len = 0;
  689. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  690. if (test_and_clear_bit(XPT_KILL_TEMP, &xprt->xpt_flags))
  691. xprt->xpt_ops->xpo_kill_temp_xprt(xprt);
  692. svc_delete_xprt(xprt);
  693. /* Leave XPT_BUSY set on the dead xprt: */
  694. goto out;
  695. }
  696. if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  697. struct svc_xprt *newxpt;
  698. /*
  699. * We know this module_get will succeed because the
  700. * listener holds a reference too
  701. */
  702. __module_get(xprt->xpt_class->xcl_owner);
  703. svc_check_conn_limits(xprt->xpt_server);
  704. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  705. if (newxpt) {
  706. newxpt->xpt_cred = get_cred(xprt->xpt_cred);
  707. svc_add_new_temp_xprt(serv, newxpt);
  708. trace_svc_xprt_accept(newxpt, serv->sv_name);
  709. } else {
  710. module_put(xprt->xpt_class->xcl_owner);
  711. }
  712. svc_xprt_received(xprt);
  713. } else if (test_bit(XPT_HANDSHAKE, &xprt->xpt_flags)) {
  714. xprt->xpt_ops->xpo_handshake(xprt);
  715. svc_xprt_received(xprt);
  716. } else if (svc_xprt_reserve_slot(rqstp, xprt)) {
  717. /* XPT_DATA|XPT_DEFERRED case: */
  718. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  719. if (rqstp->rq_deferred)
  720. len = svc_deferred_recv(rqstp);
  721. else
  722. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  723. rqstp->rq_reserved = serv->sv_max_mesg;
  724. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  725. if (len <= 0)
  726. goto out;
  727. trace_svc_xdr_recvfrom(&rqstp->rq_arg);
  728. clear_bit(XPT_OLD, &xprt->xpt_flags);
  729. rqstp->rq_chandle.defer = svc_defer;
  730. if (serv->sv_stats)
  731. serv->sv_stats->netcnt++;
  732. percpu_counter_inc(&rqstp->rq_pool->sp_messages_arrived);
  733. rqstp->rq_stime = ktime_get();
  734. svc_process(rqstp);
  735. } else
  736. svc_xprt_received(xprt);
  737. out:
  738. rqstp->rq_res.len = 0;
  739. svc_xprt_release(rqstp);
  740. }
  741. static void svc_thread_wake_next(struct svc_rqst *rqstp)
  742. {
  743. if (!svc_thread_should_sleep(rqstp))
  744. /* More work pending after I dequeued some,
  745. * wake another worker
  746. */
  747. svc_pool_wake_idle_thread(rqstp->rq_pool);
  748. }
  749. /**
  750. * svc_recv - Receive and process the next request on any transport
  751. * @rqstp: an idle RPC service thread
  752. * @timeo: timeout (in jiffies) (0 means infinite timeout)
  753. *
  754. * This code is carefully organised not to touch any cachelines in
  755. * the shared svc_serv structure, only cachelines in the local
  756. * svc_pool.
  757. *
  758. * If the timeout is 0, then the sleep will never time out.
  759. *
  760. * Returns -ETIMEDOUT if idle for an extended period
  761. * -EBUSY if there is more work to do than available threads
  762. * 0 otherwise.
  763. */
  764. int svc_recv(struct svc_rqst *rqstp, long timeo)
  765. {
  766. struct svc_pool *pool = rqstp->rq_pool;
  767. bool did_timeout;
  768. int ret = 0;
  769. if (!svc_alloc_arg(rqstp))
  770. return ret;
  771. did_timeout = svc_thread_wait_for_work(rqstp, timeo);
  772. if (did_timeout && svc_thread_should_sleep(rqstp) &&
  773. pool->sp_nrthrmin && pool->sp_nrthreads > pool->sp_nrthrmin)
  774. ret = -ETIMEDOUT;
  775. clear_bit(SP_TASK_PENDING, &pool->sp_flags);
  776. if (svc_thread_should_stop(rqstp)) {
  777. svc_thread_wake_next(rqstp);
  778. return ret;
  779. }
  780. rqstp->rq_xprt = svc_xprt_dequeue(pool);
  781. if (rqstp->rq_xprt) {
  782. struct svc_xprt *xprt = rqstp->rq_xprt;
  783. svc_thread_wake_next(rqstp);
  784. /* Normally we will wait up to 5 seconds for any required
  785. * cache information to be provided. When there are no
  786. * idle threads, we reduce the wait time.
  787. */
  788. if (pool->sp_idle_threads.first) {
  789. rqstp->rq_chandle.thread_wait = 5 * HZ;
  790. } else {
  791. rqstp->rq_chandle.thread_wait = 1 * HZ;
  792. /*
  793. * No idle threads: signal -EBUSY so the caller
  794. * can consider spawning another thread. Use
  795. * SP_TASK_STARTING to limit this signal to one
  796. * thread at a time; the caller clears this flag
  797. * after starting a new thread.
  798. */
  799. if (!did_timeout && timeo &&
  800. !test_and_set_bit(SP_TASK_STARTING,
  801. &pool->sp_flags))
  802. ret = -EBUSY;
  803. }
  804. trace_svc_xprt_dequeue(rqstp);
  805. svc_handle_xprt(rqstp, xprt);
  806. }
  807. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  808. if (svc_is_backchannel(rqstp)) {
  809. struct svc_serv *serv = rqstp->rq_server;
  810. struct rpc_rqst *req;
  811. req = lwq_dequeue(&serv->sv_cb_list,
  812. struct rpc_rqst, rq_bc_list);
  813. if (req) {
  814. svc_thread_wake_next(rqstp);
  815. svc_process_bc(req, rqstp);
  816. }
  817. }
  818. #endif
  819. return ret;
  820. }
  821. EXPORT_SYMBOL_GPL(svc_recv);
  822. /**
  823. * svc_send - Return reply to client
  824. * @rqstp: RPC transaction context
  825. *
  826. */
  827. void svc_send(struct svc_rqst *rqstp)
  828. {
  829. struct svc_xprt *xprt;
  830. struct xdr_buf *xb;
  831. int status;
  832. xprt = rqstp->rq_xprt;
  833. /* calculate over-all length */
  834. xb = &rqstp->rq_res;
  835. xb->len = xb->head[0].iov_len +
  836. xb->page_len +
  837. xb->tail[0].iov_len;
  838. trace_svc_xdr_sendto(rqstp->rq_xid, xb);
  839. trace_svc_stats_latency(rqstp);
  840. status = xprt->xpt_ops->xpo_sendto(rqstp);
  841. trace_svc_send(rqstp, status);
  842. }
  843. /*
  844. * Timer function to close old temporary transports, using
  845. * a mark-and-sweep algorithm.
  846. */
  847. static void svc_age_temp_xprts(struct timer_list *t)
  848. {
  849. struct svc_serv *serv = timer_container_of(serv, t, sv_temptimer);
  850. struct svc_xprt *xprt;
  851. struct list_head *le, *next;
  852. dprintk("svc_age_temp_xprts\n");
  853. if (!spin_trylock_bh(&serv->sv_lock)) {
  854. /* busy, try again 1 sec later */
  855. dprintk("svc_age_temp_xprts: busy\n");
  856. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  857. return;
  858. }
  859. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  860. xprt = list_entry(le, struct svc_xprt, xpt_list);
  861. /* First time through, just mark it OLD. Second time
  862. * through, close it. */
  863. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  864. continue;
  865. if (kref_read(&xprt->xpt_ref) > 1 ||
  866. test_bit(XPT_BUSY, &xprt->xpt_flags))
  867. continue;
  868. list_del_init(le);
  869. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  870. dprintk("queuing xprt %p for closing\n", xprt);
  871. /* a thread will dequeue and close it soon */
  872. svc_xprt_enqueue(xprt);
  873. }
  874. spin_unlock_bh(&serv->sv_lock);
  875. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  876. }
  877. /* Close temporary transports whose xpt_local matches server_addr immediately
  878. * instead of waiting for them to be picked up by the timer.
  879. *
  880. * This is meant to be called from a notifier_block that runs when an ip
  881. * address is deleted.
  882. */
  883. void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr)
  884. {
  885. struct svc_xprt *xprt;
  886. struct list_head *le, *next;
  887. LIST_HEAD(to_be_closed);
  888. spin_lock_bh(&serv->sv_lock);
  889. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  890. xprt = list_entry(le, struct svc_xprt, xpt_list);
  891. if (rpc_cmp_addr(server_addr, (struct sockaddr *)
  892. &xprt->xpt_local)) {
  893. dprintk("svc_age_temp_xprts_now: found %p\n", xprt);
  894. list_move(le, &to_be_closed);
  895. }
  896. }
  897. spin_unlock_bh(&serv->sv_lock);
  898. while (!list_empty(&to_be_closed)) {
  899. le = to_be_closed.next;
  900. list_del_init(le);
  901. xprt = list_entry(le, struct svc_xprt, xpt_list);
  902. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  903. set_bit(XPT_KILL_TEMP, &xprt->xpt_flags);
  904. dprintk("svc_age_temp_xprts_now: queuing xprt %p for closing\n",
  905. xprt);
  906. svc_xprt_enqueue(xprt);
  907. }
  908. }
  909. EXPORT_SYMBOL_GPL(svc_age_temp_xprts_now);
  910. static void call_xpt_users(struct svc_xprt *xprt)
  911. {
  912. struct svc_xpt_user *u;
  913. spin_lock(&xprt->xpt_lock);
  914. while (!list_empty(&xprt->xpt_users)) {
  915. u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
  916. list_del_init(&u->list);
  917. u->callback(u);
  918. }
  919. spin_unlock(&xprt->xpt_lock);
  920. }
  921. /*
  922. * Remove a dead transport
  923. */
  924. static void svc_delete_xprt(struct svc_xprt *xprt)
  925. {
  926. struct svc_serv *serv = xprt->xpt_server;
  927. struct svc_deferred_req *dr;
  928. /* unregister with rpcbind for when transport type is TCP or UDP.
  929. */
  930. if (test_bit(XPT_RPCB_UNREG, &xprt->xpt_flags)) {
  931. struct svc_sock *svsk = container_of(xprt, struct svc_sock,
  932. sk_xprt);
  933. struct socket *sock = svsk->sk_sock;
  934. if (svc_register(serv, xprt->xpt_net, sock->sk->sk_family,
  935. sock->sk->sk_protocol, 0) < 0)
  936. pr_warn("failed to unregister %s with rpcbind\n",
  937. xprt->xpt_class->xcl_name);
  938. }
  939. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  940. return;
  941. trace_svc_xprt_detach(xprt);
  942. xprt->xpt_ops->xpo_detach(xprt);
  943. if (xprt->xpt_bc_xprt)
  944. xprt->xpt_bc_xprt->ops->close(xprt->xpt_bc_xprt);
  945. spin_lock_bh(&serv->sv_lock);
  946. list_del_init(&xprt->xpt_list);
  947. if (test_bit(XPT_TEMP, &xprt->xpt_flags) &&
  948. !test_bit(XPT_PEER_VALID, &xprt->xpt_flags))
  949. serv->sv_tmpcnt--;
  950. spin_unlock_bh(&serv->sv_lock);
  951. while ((dr = svc_deferred_dequeue(xprt)) != NULL)
  952. free_deferred(xprt, dr);
  953. call_xpt_users(xprt);
  954. svc_xprt_put(xprt);
  955. }
  956. /**
  957. * svc_xprt_close - Close a client connection
  958. * @xprt: transport to disconnect
  959. *
  960. */
  961. void svc_xprt_close(struct svc_xprt *xprt)
  962. {
  963. trace_svc_xprt_close(xprt);
  964. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  965. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  966. /* someone else will have to effect the close */
  967. return;
  968. /*
  969. * We expect svc_close_xprt() to work even when no threads are
  970. * running (e.g., while configuring the server before starting
  971. * any threads), so if the transport isn't busy, we delete
  972. * it ourself:
  973. */
  974. svc_delete_xprt(xprt);
  975. }
  976. EXPORT_SYMBOL_GPL(svc_xprt_close);
  977. static int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net)
  978. {
  979. struct svc_xprt *xprt;
  980. int ret = 0;
  981. spin_lock_bh(&serv->sv_lock);
  982. list_for_each_entry(xprt, xprt_list, xpt_list) {
  983. if (xprt->xpt_net != net)
  984. continue;
  985. ret++;
  986. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  987. svc_xprt_enqueue(xprt);
  988. }
  989. spin_unlock_bh(&serv->sv_lock);
  990. return ret;
  991. }
  992. static void svc_clean_up_xprts(struct svc_serv *serv, struct net *net)
  993. {
  994. struct svc_xprt *xprt;
  995. int i;
  996. for (i = 0; i < serv->sv_nrpools; i++) {
  997. struct svc_pool *pool = &serv->sv_pools[i];
  998. struct llist_node *q, **t1, *t2;
  999. q = lwq_dequeue_all(&pool->sp_xprts);
  1000. lwq_for_each_safe(xprt, t1, t2, &q, xpt_ready) {
  1001. if (xprt->xpt_net == net) {
  1002. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  1003. svc_delete_xprt(xprt);
  1004. xprt = NULL;
  1005. }
  1006. }
  1007. if (q)
  1008. lwq_enqueue_batch(q, &pool->sp_xprts);
  1009. }
  1010. }
  1011. /**
  1012. * svc_xprt_destroy_all - Destroy transports associated with @serv
  1013. * @serv: RPC service to be shut down
  1014. * @net: target network namespace
  1015. * @unregister: true if it is OK to unregister the destroyed xprts
  1016. *
  1017. * Server threads may still be running (especially in the case where the
  1018. * service is still running in other network namespaces).
  1019. *
  1020. * So we shut down sockets the same way we would on a running server, by
  1021. * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do
  1022. * the close. In the case there are no such other threads,
  1023. * threads running, svc_clean_up_xprts() does a simple version of a
  1024. * server's main event loop, and in the case where there are other
  1025. * threads, we may need to wait a little while and then check again to
  1026. * see if they're done.
  1027. */
  1028. void svc_xprt_destroy_all(struct svc_serv *serv, struct net *net,
  1029. bool unregister)
  1030. {
  1031. int delay = 0;
  1032. while (svc_close_list(serv, &serv->sv_permsocks, net) +
  1033. svc_close_list(serv, &serv->sv_tempsocks, net)) {
  1034. svc_clean_up_xprts(serv, net);
  1035. msleep(delay++);
  1036. }
  1037. if (unregister)
  1038. svc_rpcb_cleanup(serv, net);
  1039. }
  1040. EXPORT_SYMBOL_GPL(svc_xprt_destroy_all);
  1041. /*
  1042. * Handle defer and revisit of requests
  1043. */
  1044. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  1045. {
  1046. struct svc_deferred_req *dr =
  1047. container_of(dreq, struct svc_deferred_req, handle);
  1048. struct svc_xprt *xprt = dr->xprt;
  1049. spin_lock(&xprt->xpt_lock);
  1050. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  1051. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  1052. spin_unlock(&xprt->xpt_lock);
  1053. trace_svc_defer_drop(dr);
  1054. free_deferred(xprt, dr);
  1055. svc_xprt_put(xprt);
  1056. return;
  1057. }
  1058. dr->xprt = NULL;
  1059. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  1060. spin_unlock(&xprt->xpt_lock);
  1061. trace_svc_defer_queue(dr);
  1062. svc_xprt_enqueue(xprt);
  1063. svc_xprt_put(xprt);
  1064. }
  1065. /*
  1066. * Save the request off for later processing. The request buffer looks
  1067. * like this:
  1068. *
  1069. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  1070. *
  1071. * This code can only handle requests that consist of an xprt-header
  1072. * and rpc-header.
  1073. */
  1074. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  1075. {
  1076. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  1077. struct svc_deferred_req *dr;
  1078. if (rqstp->rq_arg.page_len || !test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags))
  1079. return NULL; /* if more than a page, give up FIXME */
  1080. if (rqstp->rq_deferred) {
  1081. dr = rqstp->rq_deferred;
  1082. rqstp->rq_deferred = NULL;
  1083. } else {
  1084. size_t skip;
  1085. size_t size;
  1086. /* FIXME maybe discard if size too large */
  1087. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  1088. dr = kmalloc(size, GFP_KERNEL);
  1089. if (dr == NULL)
  1090. return NULL;
  1091. dr->handle.owner = rqstp->rq_server;
  1092. dr->prot = rqstp->rq_prot;
  1093. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  1094. dr->addrlen = rqstp->rq_addrlen;
  1095. dr->daddr = rqstp->rq_daddr;
  1096. dr->argslen = rqstp->rq_arg.len >> 2;
  1097. /* back up head to the start of the buffer and copy */
  1098. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1099. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  1100. dr->argslen << 2);
  1101. }
  1102. dr->xprt_ctxt = rqstp->rq_xprt_ctxt;
  1103. rqstp->rq_xprt_ctxt = NULL;
  1104. trace_svc_defer(rqstp);
  1105. svc_xprt_get(rqstp->rq_xprt);
  1106. dr->xprt = rqstp->rq_xprt;
  1107. set_bit(RQ_DROPME, &rqstp->rq_flags);
  1108. dr->handle.revisit = svc_revisit;
  1109. return &dr->handle;
  1110. }
  1111. /*
  1112. * recv data from a deferred request into an active one
  1113. */
  1114. static noinline int svc_deferred_recv(struct svc_rqst *rqstp)
  1115. {
  1116. struct svc_deferred_req *dr = rqstp->rq_deferred;
  1117. trace_svc_defer_recv(dr);
  1118. /* setup iov_base past transport header */
  1119. rqstp->rq_arg.head[0].iov_base = dr->args;
  1120. /* The iov_len does not include the transport header bytes */
  1121. rqstp->rq_arg.head[0].iov_len = dr->argslen << 2;
  1122. rqstp->rq_arg.page_len = 0;
  1123. /* The rq_arg.len includes the transport header bytes */
  1124. rqstp->rq_arg.len = dr->argslen << 2;
  1125. rqstp->rq_prot = dr->prot;
  1126. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  1127. rqstp->rq_addrlen = dr->addrlen;
  1128. /* Save off transport header len in case we get deferred again */
  1129. rqstp->rq_daddr = dr->daddr;
  1130. rqstp->rq_respages = rqstp->rq_pages;
  1131. rqstp->rq_xprt_ctxt = dr->xprt_ctxt;
  1132. dr->xprt_ctxt = NULL;
  1133. svc_xprt_received(rqstp->rq_xprt);
  1134. return dr->argslen << 2;
  1135. }
  1136. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  1137. {
  1138. struct svc_deferred_req *dr = NULL;
  1139. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  1140. return NULL;
  1141. spin_lock(&xprt->xpt_lock);
  1142. if (!list_empty(&xprt->xpt_deferred)) {
  1143. dr = list_entry(xprt->xpt_deferred.next,
  1144. struct svc_deferred_req,
  1145. handle.recent);
  1146. list_del_init(&dr->handle.recent);
  1147. } else
  1148. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  1149. spin_unlock(&xprt->xpt_lock);
  1150. return dr;
  1151. }
  1152. /**
  1153. * svc_find_listener - find an RPC transport instance
  1154. * @serv: pointer to svc_serv to search
  1155. * @xcl_name: C string containing transport's class name
  1156. * @net: owner net pointer
  1157. * @sa: sockaddr containing address
  1158. *
  1159. * Return the transport instance pointer for the endpoint accepting
  1160. * connections/peer traffic from the specified transport class,
  1161. * and matching sockaddr.
  1162. */
  1163. struct svc_xprt *svc_find_listener(struct svc_serv *serv, const char *xcl_name,
  1164. struct net *net, const struct sockaddr *sa)
  1165. {
  1166. struct svc_xprt *xprt;
  1167. struct svc_xprt *found = NULL;
  1168. spin_lock_bh(&serv->sv_lock);
  1169. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1170. if (xprt->xpt_net != net)
  1171. continue;
  1172. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  1173. continue;
  1174. if (!rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local))
  1175. continue;
  1176. found = xprt;
  1177. svc_xprt_get(xprt);
  1178. break;
  1179. }
  1180. spin_unlock_bh(&serv->sv_lock);
  1181. return found;
  1182. }
  1183. EXPORT_SYMBOL_GPL(svc_find_listener);
  1184. /**
  1185. * svc_find_xprt - find an RPC transport instance
  1186. * @serv: pointer to svc_serv to search
  1187. * @xcl_name: C string containing transport's class name
  1188. * @net: owner net pointer
  1189. * @af: Address family of transport's local address
  1190. * @port: transport's IP port number
  1191. *
  1192. * Return the transport instance pointer for the endpoint accepting
  1193. * connections/peer traffic from the specified transport class,
  1194. * address family and port.
  1195. *
  1196. * Specifying 0 for the address family or port is effectively a
  1197. * wild-card, and will result in matching the first transport in the
  1198. * service's list that has a matching class name.
  1199. */
  1200. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  1201. struct net *net, const sa_family_t af,
  1202. const unsigned short port)
  1203. {
  1204. struct svc_xprt *xprt;
  1205. struct svc_xprt *found = NULL;
  1206. /* Sanity check the args */
  1207. if (serv == NULL || xcl_name == NULL)
  1208. return found;
  1209. spin_lock_bh(&serv->sv_lock);
  1210. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1211. if (xprt->xpt_net != net)
  1212. continue;
  1213. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  1214. continue;
  1215. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  1216. continue;
  1217. if (port != 0 && port != svc_xprt_local_port(xprt))
  1218. continue;
  1219. found = xprt;
  1220. svc_xprt_get(xprt);
  1221. break;
  1222. }
  1223. spin_unlock_bh(&serv->sv_lock);
  1224. return found;
  1225. }
  1226. EXPORT_SYMBOL_GPL(svc_find_xprt);
  1227. static int svc_one_xprt_name(const struct svc_xprt *xprt,
  1228. char *pos, int remaining)
  1229. {
  1230. int len;
  1231. len = snprintf(pos, remaining, "%s %u\n",
  1232. xprt->xpt_class->xcl_name,
  1233. svc_xprt_local_port(xprt));
  1234. if (len >= remaining)
  1235. return -ENAMETOOLONG;
  1236. return len;
  1237. }
  1238. /**
  1239. * svc_xprt_names - format a buffer with a list of transport names
  1240. * @serv: pointer to an RPC service
  1241. * @buf: pointer to a buffer to be filled in
  1242. * @buflen: length of buffer to be filled in
  1243. *
  1244. * Fills in @buf with a string containing a list of transport names,
  1245. * each name terminated with '\n'.
  1246. *
  1247. * Returns positive length of the filled-in string on success; otherwise
  1248. * a negative errno value is returned if an error occurs.
  1249. */
  1250. int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
  1251. {
  1252. struct svc_xprt *xprt;
  1253. int len, totlen;
  1254. char *pos;
  1255. /* Sanity check args */
  1256. if (!serv)
  1257. return 0;
  1258. spin_lock_bh(&serv->sv_lock);
  1259. pos = buf;
  1260. totlen = 0;
  1261. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1262. len = svc_one_xprt_name(xprt, pos, buflen - totlen);
  1263. if (len < 0) {
  1264. *buf = '\0';
  1265. totlen = len;
  1266. }
  1267. if (len <= 0)
  1268. break;
  1269. pos += len;
  1270. totlen += len;
  1271. }
  1272. spin_unlock_bh(&serv->sv_lock);
  1273. return totlen;
  1274. }
  1275. EXPORT_SYMBOL_GPL(svc_xprt_names);
  1276. /*----------------------------------------------------------------------------*/
  1277. static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
  1278. {
  1279. unsigned int pidx = (unsigned int)*pos;
  1280. struct svc_info *si = m->private;
  1281. dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
  1282. mutex_lock(si->mutex);
  1283. if (!pidx)
  1284. return SEQ_START_TOKEN;
  1285. if (!si->serv)
  1286. return NULL;
  1287. return pidx > si->serv->sv_nrpools ? NULL
  1288. : &si->serv->sv_pools[pidx - 1];
  1289. }
  1290. static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
  1291. {
  1292. struct svc_pool *pool = p;
  1293. struct svc_info *si = m->private;
  1294. struct svc_serv *serv = si->serv;
  1295. dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
  1296. if (!serv) {
  1297. pool = NULL;
  1298. } else if (p == SEQ_START_TOKEN) {
  1299. pool = &serv->sv_pools[0];
  1300. } else {
  1301. unsigned int pidx = (pool - &serv->sv_pools[0]);
  1302. if (pidx < serv->sv_nrpools-1)
  1303. pool = &serv->sv_pools[pidx+1];
  1304. else
  1305. pool = NULL;
  1306. }
  1307. ++*pos;
  1308. return pool;
  1309. }
  1310. static void svc_pool_stats_stop(struct seq_file *m, void *p)
  1311. {
  1312. struct svc_info *si = m->private;
  1313. mutex_unlock(si->mutex);
  1314. }
  1315. static int svc_pool_stats_show(struct seq_file *m, void *p)
  1316. {
  1317. struct svc_pool *pool = p;
  1318. if (p == SEQ_START_TOKEN) {
  1319. seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
  1320. return 0;
  1321. }
  1322. seq_printf(m, "%u %llu %llu %llu 0\n",
  1323. pool->sp_id,
  1324. percpu_counter_sum_positive(&pool->sp_messages_arrived),
  1325. percpu_counter_sum_positive(&pool->sp_sockets_queued),
  1326. percpu_counter_sum_positive(&pool->sp_threads_woken));
  1327. return 0;
  1328. }
  1329. static const struct seq_operations svc_pool_stats_seq_ops = {
  1330. .start = svc_pool_stats_start,
  1331. .next = svc_pool_stats_next,
  1332. .stop = svc_pool_stats_stop,
  1333. .show = svc_pool_stats_show,
  1334. };
  1335. int svc_pool_stats_open(struct svc_info *info, struct file *file)
  1336. {
  1337. struct seq_file *seq;
  1338. int err;
  1339. err = seq_open(file, &svc_pool_stats_seq_ops);
  1340. if (err)
  1341. return err;
  1342. seq = file->private_data;
  1343. seq->private = info;
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL(svc_pool_stats_open);
  1347. /*----------------------------------------------------------------------------*/