svc_xprt.c 39 KB

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