svc_xprt.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*
  2. * linux/net/sunrpc/svc_xprt.c
  3. *
  4. * Author: Tom Tucker <tom@opengridcomputing.com>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/errno.h>
  8. #include <linux/freezer.h>
  9. #include <linux/kthread.h>
  10. #include <linux/slab.h>
  11. #include <net/sock.h>
  12. #include <linux/sunrpc/stats.h>
  13. #include <linux/sunrpc/svc_xprt.h>
  14. #include <linux/sunrpc/svcsock.h>
  15. #include <linux/sunrpc/xprt.h>
  16. #include <linux/module.h>
  17. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  18. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
  19. static int svc_deferred_recv(struct svc_rqst *rqstp);
  20. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  21. static void svc_age_temp_xprts(unsigned long closure);
  22. static void svc_delete_xprt(struct svc_xprt *xprt);
  23. /* apparently the "standard" is that clients close
  24. * idle connections after 5 minutes, servers after
  25. * 6 minutes
  26. * http://www.connectathon.org/talks96/nfstcp.pdf
  27. */
  28. static int svc_conn_age_period = 6*60;
  29. /* List of registered transport classes */
  30. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  31. static LIST_HEAD(svc_xprt_class_list);
  32. /* SMP locking strategy:
  33. *
  34. * svc_pool->sp_lock protects most of the fields of that pool.
  35. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  36. * when both need to be taken (rare), svc_serv->sv_lock is first.
  37. * BKL protects svc_serv->sv_nrthread.
  38. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  39. * and the ->sk_info_authunix cache.
  40. *
  41. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  42. * enqueued multiply. During normal transport processing this bit
  43. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  44. * Providers should not manipulate this bit directly.
  45. *
  46. * Some flags can be set to certain values at any time
  47. * providing that certain rules are followed:
  48. *
  49. * XPT_CONN, XPT_DATA:
  50. * - Can be set or cleared at any time.
  51. * - After a set, svc_xprt_enqueue must be called to enqueue
  52. * the transport for processing.
  53. * - After a clear, the transport must be read/accepted.
  54. * If this succeeds, it must be set again.
  55. * XPT_CLOSE:
  56. * - Can set at any time. It is never cleared.
  57. * XPT_DEAD:
  58. * - Can only be set while XPT_BUSY is held which ensures
  59. * that no other thread will be using the transport or will
  60. * try to set XPT_DEAD.
  61. */
  62. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  63. {
  64. struct svc_xprt_class *cl;
  65. int res = -EEXIST;
  66. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  67. INIT_LIST_HEAD(&xcl->xcl_list);
  68. spin_lock(&svc_xprt_class_lock);
  69. /* Make sure there isn't already a class with the same name */
  70. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  71. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  72. goto out;
  73. }
  74. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  75. res = 0;
  76. out:
  77. spin_unlock(&svc_xprt_class_lock);
  78. return res;
  79. }
  80. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  81. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  82. {
  83. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  84. spin_lock(&svc_xprt_class_lock);
  85. list_del_init(&xcl->xcl_list);
  86. spin_unlock(&svc_xprt_class_lock);
  87. }
  88. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  89. /*
  90. * Format the transport list for printing
  91. */
  92. int svc_print_xprts(char *buf, int maxlen)
  93. {
  94. struct svc_xprt_class *xcl;
  95. char tmpstr[80];
  96. int len = 0;
  97. buf[0] = '\0';
  98. spin_lock(&svc_xprt_class_lock);
  99. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  100. int slen;
  101. sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
  102. slen = strlen(tmpstr);
  103. if (len + slen > maxlen)
  104. break;
  105. len += slen;
  106. strcat(buf, tmpstr);
  107. }
  108. spin_unlock(&svc_xprt_class_lock);
  109. return len;
  110. }
  111. static void svc_xprt_free(struct kref *kref)
  112. {
  113. struct svc_xprt *xprt =
  114. container_of(kref, struct svc_xprt, xpt_ref);
  115. struct module *owner = xprt->xpt_class->xcl_owner;
  116. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
  117. svcauth_unix_info_release(xprt);
  118. put_net(xprt->xpt_net);
  119. /* See comment on corresponding get in xs_setup_bc_tcp(): */
  120. if (xprt->xpt_bc_xprt)
  121. xprt_put(xprt->xpt_bc_xprt);
  122. xprt->xpt_ops->xpo_free(xprt);
  123. module_put(owner);
  124. }
  125. void svc_xprt_put(struct svc_xprt *xprt)
  126. {
  127. kref_put(&xprt->xpt_ref, svc_xprt_free);
  128. }
  129. EXPORT_SYMBOL_GPL(svc_xprt_put);
  130. /*
  131. * Called by transport drivers to initialize the transport independent
  132. * portion of the transport instance.
  133. */
  134. void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl,
  135. struct svc_xprt *xprt, struct svc_serv *serv)
  136. {
  137. memset(xprt, 0, sizeof(*xprt));
  138. xprt->xpt_class = xcl;
  139. xprt->xpt_ops = xcl->xcl_ops;
  140. kref_init(&xprt->xpt_ref);
  141. xprt->xpt_server = serv;
  142. INIT_LIST_HEAD(&xprt->xpt_list);
  143. INIT_LIST_HEAD(&xprt->xpt_ready);
  144. INIT_LIST_HEAD(&xprt->xpt_deferred);
  145. INIT_LIST_HEAD(&xprt->xpt_users);
  146. mutex_init(&xprt->xpt_mutex);
  147. spin_lock_init(&xprt->xpt_lock);
  148. set_bit(XPT_BUSY, &xprt->xpt_flags);
  149. rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending");
  150. xprt->xpt_net = get_net(net);
  151. }
  152. EXPORT_SYMBOL_GPL(svc_xprt_init);
  153. static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
  154. struct svc_serv *serv,
  155. struct net *net,
  156. const int family,
  157. const unsigned short port,
  158. int flags)
  159. {
  160. struct sockaddr_in sin = {
  161. .sin_family = AF_INET,
  162. .sin_addr.s_addr = htonl(INADDR_ANY),
  163. .sin_port = htons(port),
  164. };
  165. #if IS_ENABLED(CONFIG_IPV6)
  166. struct sockaddr_in6 sin6 = {
  167. .sin6_family = AF_INET6,
  168. .sin6_addr = IN6ADDR_ANY_INIT,
  169. .sin6_port = htons(port),
  170. };
  171. #endif
  172. struct sockaddr *sap;
  173. size_t len;
  174. switch (family) {
  175. case PF_INET:
  176. sap = (struct sockaddr *)&sin;
  177. len = sizeof(sin);
  178. break;
  179. #if IS_ENABLED(CONFIG_IPV6)
  180. case PF_INET6:
  181. sap = (struct sockaddr *)&sin6;
  182. len = sizeof(sin6);
  183. break;
  184. #endif
  185. default:
  186. return ERR_PTR(-EAFNOSUPPORT);
  187. }
  188. return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
  189. }
  190. /*
  191. * svc_xprt_received conditionally queues the transport for processing
  192. * by another thread. The caller must hold the XPT_BUSY bit and must
  193. * not thereafter touch transport data.
  194. *
  195. * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
  196. * insufficient) data.
  197. */
  198. static void svc_xprt_received(struct svc_xprt *xprt)
  199. {
  200. BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
  201. /* As soon as we clear busy, the xprt could be closed and
  202. * 'put', so we need a reference to call svc_xprt_enqueue with:
  203. */
  204. svc_xprt_get(xprt);
  205. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  206. svc_xprt_enqueue(xprt);
  207. svc_xprt_put(xprt);
  208. }
  209. void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new)
  210. {
  211. clear_bit(XPT_TEMP, &new->xpt_flags);
  212. spin_lock_bh(&serv->sv_lock);
  213. list_add(&new->xpt_list, &serv->sv_permsocks);
  214. spin_unlock_bh(&serv->sv_lock);
  215. svc_xprt_received(new);
  216. }
  217. int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
  218. struct net *net, const int family,
  219. const unsigned short port, int flags)
  220. {
  221. struct svc_xprt_class *xcl;
  222. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  223. spin_lock(&svc_xprt_class_lock);
  224. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  225. struct svc_xprt *newxprt;
  226. unsigned short newport;
  227. if (strcmp(xprt_name, xcl->xcl_name))
  228. continue;
  229. if (!try_module_get(xcl->xcl_owner))
  230. goto err;
  231. spin_unlock(&svc_xprt_class_lock);
  232. newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags);
  233. if (IS_ERR(newxprt)) {
  234. module_put(xcl->xcl_owner);
  235. return PTR_ERR(newxprt);
  236. }
  237. svc_add_new_perm_xprt(serv, newxprt);
  238. newport = svc_xprt_local_port(newxprt);
  239. return newport;
  240. }
  241. err:
  242. spin_unlock(&svc_xprt_class_lock);
  243. dprintk("svc: transport %s not found\n", xprt_name);
  244. /* This errno is exposed to user space. Provide a reasonable
  245. * perror msg for a bad transport. */
  246. return -EPROTONOSUPPORT;
  247. }
  248. EXPORT_SYMBOL_GPL(svc_create_xprt);
  249. /*
  250. * Copy the local and remote xprt addresses to the rqstp structure
  251. */
  252. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  253. {
  254. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  255. rqstp->rq_addrlen = xprt->xpt_remotelen;
  256. /*
  257. * Destination address in request is needed for binding the
  258. * source address in RPC replies/callbacks later.
  259. */
  260. memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen);
  261. rqstp->rq_daddrlen = xprt->xpt_locallen;
  262. }
  263. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  264. /**
  265. * svc_print_addr - Format rq_addr field for printing
  266. * @rqstp: svc_rqst struct containing address to print
  267. * @buf: target buffer for formatted address
  268. * @len: length of target buffer
  269. *
  270. */
  271. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  272. {
  273. return __svc_print_addr(svc_addr(rqstp), buf, len);
  274. }
  275. EXPORT_SYMBOL_GPL(svc_print_addr);
  276. /*
  277. * Queue up an idle server thread. Must have pool->sp_lock held.
  278. * Note: this is really a stack rather than a queue, so that we only
  279. * use as many different threads as we need, and the rest don't pollute
  280. * the cache.
  281. */
  282. static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  283. {
  284. list_add(&rqstp->rq_list, &pool->sp_threads);
  285. }
  286. /*
  287. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  288. */
  289. static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  290. {
  291. list_del(&rqstp->rq_list);
  292. }
  293. static bool svc_xprt_has_something_to_do(struct svc_xprt *xprt)
  294. {
  295. if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE)))
  296. return true;
  297. if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED)))
  298. return xprt->xpt_ops->xpo_has_wspace(xprt);
  299. return false;
  300. }
  301. /*
  302. * Queue up a transport with data pending. If there are idle nfsd
  303. * processes, wake 'em up.
  304. *
  305. */
  306. void svc_xprt_enqueue(struct svc_xprt *xprt)
  307. {
  308. struct svc_pool *pool;
  309. struct svc_rqst *rqstp;
  310. int cpu;
  311. if (!svc_xprt_has_something_to_do(xprt))
  312. return;
  313. cpu = get_cpu();
  314. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  315. put_cpu();
  316. spin_lock_bh(&pool->sp_lock);
  317. if (!list_empty(&pool->sp_threads) &&
  318. !list_empty(&pool->sp_sockets))
  319. printk(KERN_ERR
  320. "svc_xprt_enqueue: "
  321. "threads and transports both waiting??\n");
  322. pool->sp_stats.packets++;
  323. /* Mark transport as busy. It will remain in this state until
  324. * the provider calls svc_xprt_received. We update XPT_BUSY
  325. * atomically because it also guards against trying to enqueue
  326. * the transport twice.
  327. */
  328. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
  329. /* Don't enqueue transport while already enqueued */
  330. dprintk("svc: transport %p busy, not enqueued\n", xprt);
  331. goto out_unlock;
  332. }
  333. if (!list_empty(&pool->sp_threads)) {
  334. rqstp = list_entry(pool->sp_threads.next,
  335. struct svc_rqst,
  336. rq_list);
  337. dprintk("svc: transport %p served by daemon %p\n",
  338. xprt, rqstp);
  339. svc_thread_dequeue(pool, rqstp);
  340. if (rqstp->rq_xprt)
  341. printk(KERN_ERR
  342. "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
  343. rqstp, rqstp->rq_xprt);
  344. rqstp->rq_xprt = xprt;
  345. svc_xprt_get(xprt);
  346. pool->sp_stats.threads_woken++;
  347. wake_up(&rqstp->rq_wait);
  348. } else {
  349. dprintk("svc: transport %p put into queue\n", xprt);
  350. list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
  351. pool->sp_stats.sockets_queued++;
  352. }
  353. out_unlock:
  354. spin_unlock_bh(&pool->sp_lock);
  355. }
  356. EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
  357. /*
  358. * Dequeue the first transport. Must be called with the pool->sp_lock held.
  359. */
  360. static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
  361. {
  362. struct svc_xprt *xprt;
  363. if (list_empty(&pool->sp_sockets))
  364. return NULL;
  365. xprt = list_entry(pool->sp_sockets.next,
  366. struct svc_xprt, xpt_ready);
  367. list_del_init(&xprt->xpt_ready);
  368. dprintk("svc: transport %p dequeued, inuse=%d\n",
  369. xprt, atomic_read(&xprt->xpt_ref.refcount));
  370. return xprt;
  371. }
  372. /**
  373. * svc_reserve - change the space reserved for the reply to a request.
  374. * @rqstp: The request in question
  375. * @space: new max space to reserve
  376. *
  377. * Each request reserves some space on the output queue of the transport
  378. * to make sure the reply fits. This function reduces that reserved
  379. * space to be the amount of space used already, plus @space.
  380. *
  381. */
  382. void svc_reserve(struct svc_rqst *rqstp, int space)
  383. {
  384. space += rqstp->rq_res.head[0].iov_len;
  385. if (space < rqstp->rq_reserved) {
  386. struct svc_xprt *xprt = rqstp->rq_xprt;
  387. atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
  388. rqstp->rq_reserved = space;
  389. svc_xprt_enqueue(xprt);
  390. }
  391. }
  392. EXPORT_SYMBOL_GPL(svc_reserve);
  393. static void svc_xprt_release(struct svc_rqst *rqstp)
  394. {
  395. struct svc_xprt *xprt = rqstp->rq_xprt;
  396. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  397. kfree(rqstp->rq_deferred);
  398. rqstp->rq_deferred = NULL;
  399. svc_free_res_pages(rqstp);
  400. rqstp->rq_res.page_len = 0;
  401. rqstp->rq_res.page_base = 0;
  402. /* Reset response buffer and release
  403. * the reservation.
  404. * But first, check that enough space was reserved
  405. * for the reply, otherwise we have a bug!
  406. */
  407. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  408. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  409. rqstp->rq_reserved,
  410. rqstp->rq_res.len);
  411. rqstp->rq_res.head[0].iov_len = 0;
  412. svc_reserve(rqstp, 0);
  413. rqstp->rq_xprt = NULL;
  414. svc_xprt_put(xprt);
  415. }
  416. /*
  417. * External function to wake up a server waiting for data
  418. * This really only makes sense for services like lockd
  419. * which have exactly one thread anyway.
  420. */
  421. void svc_wake_up(struct svc_serv *serv)
  422. {
  423. struct svc_rqst *rqstp;
  424. unsigned int i;
  425. struct svc_pool *pool;
  426. for (i = 0; i < serv->sv_nrpools; i++) {
  427. pool = &serv->sv_pools[i];
  428. spin_lock_bh(&pool->sp_lock);
  429. if (!list_empty(&pool->sp_threads)) {
  430. rqstp = list_entry(pool->sp_threads.next,
  431. struct svc_rqst,
  432. rq_list);
  433. dprintk("svc: daemon %p woken up.\n", rqstp);
  434. /*
  435. svc_thread_dequeue(pool, rqstp);
  436. rqstp->rq_xprt = NULL;
  437. */
  438. wake_up(&rqstp->rq_wait);
  439. }
  440. spin_unlock_bh(&pool->sp_lock);
  441. }
  442. }
  443. EXPORT_SYMBOL_GPL(svc_wake_up);
  444. int svc_port_is_privileged(struct sockaddr *sin)
  445. {
  446. switch (sin->sa_family) {
  447. case AF_INET:
  448. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  449. < PROT_SOCK;
  450. case AF_INET6:
  451. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  452. < PROT_SOCK;
  453. default:
  454. return 0;
  455. }
  456. }
  457. /*
  458. * Make sure that we don't have too many active connections. If we have,
  459. * something must be dropped. It's not clear what will happen if we allow
  460. * "too many" connections, but when dealing with network-facing software,
  461. * we have to code defensively. Here we do that by imposing hard limits.
  462. *
  463. * There's no point in trying to do random drop here for DoS
  464. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  465. * attacker can easily beat that.
  466. *
  467. * The only somewhat efficient mechanism would be if drop old
  468. * connections from the same IP first. But right now we don't even
  469. * record the client IP in svc_sock.
  470. *
  471. * single-threaded services that expect a lot of clients will probably
  472. * need to set sv_maxconn to override the default value which is based
  473. * on the number of threads
  474. */
  475. static void svc_check_conn_limits(struct svc_serv *serv)
  476. {
  477. unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
  478. (serv->sv_nrthreads+3) * 20;
  479. if (serv->sv_tmpcnt > limit) {
  480. struct svc_xprt *xprt = NULL;
  481. spin_lock_bh(&serv->sv_lock);
  482. if (!list_empty(&serv->sv_tempsocks)) {
  483. /* Try to help the admin */
  484. net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n",
  485. serv->sv_name, serv->sv_maxconn ?
  486. "max number of connections" :
  487. "number of threads");
  488. /*
  489. * Always select the oldest connection. It's not fair,
  490. * but so is life
  491. */
  492. xprt = list_entry(serv->sv_tempsocks.prev,
  493. struct svc_xprt,
  494. xpt_list);
  495. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  496. svc_xprt_get(xprt);
  497. }
  498. spin_unlock_bh(&serv->sv_lock);
  499. if (xprt) {
  500. svc_xprt_enqueue(xprt);
  501. svc_xprt_put(xprt);
  502. }
  503. }
  504. }
  505. int svc_alloc_arg(struct svc_rqst *rqstp)
  506. {
  507. struct svc_serv *serv = rqstp->rq_server;
  508. struct xdr_buf *arg;
  509. int pages;
  510. int i;
  511. /* now allocate needed pages. If we get a failure, sleep briefly */
  512. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  513. BUG_ON(pages >= RPCSVC_MAXPAGES);
  514. for (i = 0; i < pages ; i++)
  515. while (rqstp->rq_pages[i] == NULL) {
  516. struct page *p = alloc_page(GFP_KERNEL);
  517. if (!p) {
  518. set_current_state(TASK_INTERRUPTIBLE);
  519. if (signalled() || kthread_should_stop()) {
  520. set_current_state(TASK_RUNNING);
  521. return -EINTR;
  522. }
  523. schedule_timeout(msecs_to_jiffies(500));
  524. }
  525. rqstp->rq_pages[i] = p;
  526. }
  527. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  528. /* Make arg->head point to first page and arg->pages point to rest */
  529. arg = &rqstp->rq_arg;
  530. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  531. arg->head[0].iov_len = PAGE_SIZE;
  532. arg->pages = rqstp->rq_pages + 1;
  533. arg->page_base = 0;
  534. /* save at least one page for response */
  535. arg->page_len = (pages-2)*PAGE_SIZE;
  536. arg->len = (pages-1)*PAGE_SIZE;
  537. arg->tail[0].iov_len = 0;
  538. return 0;
  539. }
  540. struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout)
  541. {
  542. struct svc_xprt *xprt;
  543. struct svc_pool *pool = rqstp->rq_pool;
  544. DECLARE_WAITQUEUE(wait, current);
  545. long time_left;
  546. /* Normally we will wait up to 5 seconds for any required
  547. * cache information to be provided.
  548. */
  549. rqstp->rq_chandle.thread_wait = 5*HZ;
  550. spin_lock_bh(&pool->sp_lock);
  551. xprt = svc_xprt_dequeue(pool);
  552. if (xprt) {
  553. rqstp->rq_xprt = xprt;
  554. svc_xprt_get(xprt);
  555. /* As there is a shortage of threads and this request
  556. * had to be queued, don't allow the thread to wait so
  557. * long for cache updates.
  558. */
  559. rqstp->rq_chandle.thread_wait = 1*HZ;
  560. } else {
  561. /* No data pending. Go to sleep */
  562. svc_thread_enqueue(pool, rqstp);
  563. /*
  564. * We have to be able to interrupt this wait
  565. * to bring down the daemons ...
  566. */
  567. set_current_state(TASK_INTERRUPTIBLE);
  568. /*
  569. * checking kthread_should_stop() here allows us to avoid
  570. * locking and signalling when stopping kthreads that call
  571. * svc_recv. If the thread has already been woken up, then
  572. * we can exit here without sleeping. If not, then it
  573. * it'll be woken up quickly during the schedule_timeout
  574. */
  575. if (kthread_should_stop()) {
  576. set_current_state(TASK_RUNNING);
  577. spin_unlock_bh(&pool->sp_lock);
  578. return ERR_PTR(-EINTR);
  579. }
  580. add_wait_queue(&rqstp->rq_wait, &wait);
  581. spin_unlock_bh(&pool->sp_lock);
  582. time_left = schedule_timeout(timeout);
  583. try_to_freeze();
  584. spin_lock_bh(&pool->sp_lock);
  585. remove_wait_queue(&rqstp->rq_wait, &wait);
  586. if (!time_left)
  587. pool->sp_stats.threads_timedout++;
  588. xprt = rqstp->rq_xprt;
  589. if (!xprt) {
  590. svc_thread_dequeue(pool, rqstp);
  591. spin_unlock_bh(&pool->sp_lock);
  592. dprintk("svc: server %p, no data yet\n", rqstp);
  593. if (signalled() || kthread_should_stop())
  594. return ERR_PTR(-EINTR);
  595. else
  596. return ERR_PTR(-EAGAIN);
  597. }
  598. }
  599. spin_unlock_bh(&pool->sp_lock);
  600. return xprt;
  601. }
  602. static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  603. {
  604. struct svc_serv *serv = rqstp->rq_server;
  605. int len = 0;
  606. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  607. dprintk("svc_recv: found XPT_CLOSE\n");
  608. svc_delete_xprt(xprt);
  609. /* Leave XPT_BUSY set on the dead xprt: */
  610. return 0;
  611. }
  612. if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  613. struct svc_xprt *newxpt;
  614. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  615. if (newxpt) {
  616. /*
  617. * We know this module_get will succeed because the
  618. * listener holds a reference too
  619. */
  620. __module_get(newxpt->xpt_class->xcl_owner);
  621. svc_check_conn_limits(xprt->xpt_server);
  622. spin_lock_bh(&serv->sv_lock);
  623. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  624. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  625. serv->sv_tmpcnt++;
  626. if (serv->sv_temptimer.function == NULL) {
  627. /* setup timer to age temp transports */
  628. setup_timer(&serv->sv_temptimer,
  629. svc_age_temp_xprts,
  630. (unsigned long)serv);
  631. mod_timer(&serv->sv_temptimer,
  632. jiffies + svc_conn_age_period * HZ);
  633. }
  634. spin_unlock_bh(&serv->sv_lock);
  635. svc_xprt_received(newxpt);
  636. }
  637. } else if (xprt->xpt_ops->xpo_has_wspace(xprt)) {
  638. /* XPT_DATA|XPT_DEFERRED case: */
  639. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  640. rqstp, rqstp->rq_pool->sp_id, xprt,
  641. atomic_read(&xprt->xpt_ref.refcount));
  642. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  643. if (rqstp->rq_deferred)
  644. len = svc_deferred_recv(rqstp);
  645. else
  646. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  647. dprintk("svc: got len=%d\n", len);
  648. rqstp->rq_reserved = serv->sv_max_mesg;
  649. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  650. }
  651. /* clear XPT_BUSY: */
  652. svc_xprt_received(xprt);
  653. return len;
  654. }
  655. /*
  656. * Receive the next request on any transport. This code is carefully
  657. * organised not to touch any cachelines in the shared svc_serv
  658. * structure, only cachelines in the local svc_pool.
  659. */
  660. int svc_recv(struct svc_rqst *rqstp, long timeout)
  661. {
  662. struct svc_xprt *xprt = NULL;
  663. struct svc_serv *serv = rqstp->rq_server;
  664. int len, err;
  665. dprintk("svc: server %p waiting for data (to = %ld)\n",
  666. rqstp, timeout);
  667. if (rqstp->rq_xprt)
  668. printk(KERN_ERR
  669. "svc_recv: service %p, transport not NULL!\n",
  670. rqstp);
  671. if (waitqueue_active(&rqstp->rq_wait))
  672. printk(KERN_ERR
  673. "svc_recv: service %p, wait queue active!\n",
  674. rqstp);
  675. err = svc_alloc_arg(rqstp);
  676. if (err)
  677. return err;
  678. try_to_freeze();
  679. cond_resched();
  680. if (signalled() || kthread_should_stop())
  681. return -EINTR;
  682. xprt = svc_get_next_xprt(rqstp, timeout);
  683. if (IS_ERR(xprt))
  684. return PTR_ERR(xprt);
  685. len = svc_handle_xprt(rqstp, xprt);
  686. /* No data, incomplete (TCP) read, or accept() */
  687. if (len <= 0)
  688. goto out;
  689. clear_bit(XPT_OLD, &xprt->xpt_flags);
  690. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  691. rqstp->rq_chandle.defer = svc_defer;
  692. if (serv->sv_stats)
  693. serv->sv_stats->netcnt++;
  694. return len;
  695. out:
  696. rqstp->rq_res.len = 0;
  697. svc_xprt_release(rqstp);
  698. return -EAGAIN;
  699. }
  700. EXPORT_SYMBOL_GPL(svc_recv);
  701. /*
  702. * Drop request
  703. */
  704. void svc_drop(struct svc_rqst *rqstp)
  705. {
  706. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  707. svc_xprt_release(rqstp);
  708. }
  709. EXPORT_SYMBOL_GPL(svc_drop);
  710. /*
  711. * Return reply to client.
  712. */
  713. int svc_send(struct svc_rqst *rqstp)
  714. {
  715. struct svc_xprt *xprt;
  716. int len;
  717. struct xdr_buf *xb;
  718. xprt = rqstp->rq_xprt;
  719. if (!xprt)
  720. return -EFAULT;
  721. /* release the receive skb before sending the reply */
  722. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  723. /* calculate over-all length */
  724. xb = &rqstp->rq_res;
  725. xb->len = xb->head[0].iov_len +
  726. xb->page_len +
  727. xb->tail[0].iov_len;
  728. /* Grab mutex to serialize outgoing data. */
  729. mutex_lock(&xprt->xpt_mutex);
  730. if (test_bit(XPT_DEAD, &xprt->xpt_flags)
  731. || test_bit(XPT_CLOSE, &xprt->xpt_flags))
  732. len = -ENOTCONN;
  733. else
  734. len = xprt->xpt_ops->xpo_sendto(rqstp);
  735. mutex_unlock(&xprt->xpt_mutex);
  736. rpc_wake_up(&xprt->xpt_bc_pending);
  737. svc_xprt_release(rqstp);
  738. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  739. return 0;
  740. return len;
  741. }
  742. /*
  743. * Timer function to close old temporary transports, using
  744. * a mark-and-sweep algorithm.
  745. */
  746. static void svc_age_temp_xprts(unsigned long closure)
  747. {
  748. struct svc_serv *serv = (struct svc_serv *)closure;
  749. struct svc_xprt *xprt;
  750. struct list_head *le, *next;
  751. LIST_HEAD(to_be_aged);
  752. dprintk("svc_age_temp_xprts\n");
  753. if (!spin_trylock_bh(&serv->sv_lock)) {
  754. /* busy, try again 1 sec later */
  755. dprintk("svc_age_temp_xprts: busy\n");
  756. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  757. return;
  758. }
  759. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  760. xprt = list_entry(le, struct svc_xprt, xpt_list);
  761. /* First time through, just mark it OLD. Second time
  762. * through, close it. */
  763. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  764. continue;
  765. if (atomic_read(&xprt->xpt_ref.refcount) > 1 ||
  766. test_bit(XPT_BUSY, &xprt->xpt_flags))
  767. continue;
  768. svc_xprt_get(xprt);
  769. list_move(le, &to_be_aged);
  770. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  771. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  772. }
  773. spin_unlock_bh(&serv->sv_lock);
  774. while (!list_empty(&to_be_aged)) {
  775. le = to_be_aged.next;
  776. /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
  777. list_del_init(le);
  778. xprt = list_entry(le, struct svc_xprt, xpt_list);
  779. dprintk("queuing xprt %p for closing\n", xprt);
  780. /* a thread will dequeue and close it soon */
  781. svc_xprt_enqueue(xprt);
  782. svc_xprt_put(xprt);
  783. }
  784. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  785. }
  786. static void call_xpt_users(struct svc_xprt *xprt)
  787. {
  788. struct svc_xpt_user *u;
  789. spin_lock(&xprt->xpt_lock);
  790. while (!list_empty(&xprt->xpt_users)) {
  791. u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
  792. list_del(&u->list);
  793. u->callback(u);
  794. }
  795. spin_unlock(&xprt->xpt_lock);
  796. }
  797. /*
  798. * Remove a dead transport
  799. */
  800. static void svc_delete_xprt(struct svc_xprt *xprt)
  801. {
  802. struct svc_serv *serv = xprt->xpt_server;
  803. struct svc_deferred_req *dr;
  804. /* Only do this once */
  805. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  806. BUG();
  807. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  808. xprt->xpt_ops->xpo_detach(xprt);
  809. spin_lock_bh(&serv->sv_lock);
  810. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  811. list_del_init(&xprt->xpt_list);
  812. BUG_ON(!list_empty(&xprt->xpt_ready));
  813. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  814. serv->sv_tmpcnt--;
  815. spin_unlock_bh(&serv->sv_lock);
  816. while ((dr = svc_deferred_dequeue(xprt)) != NULL)
  817. kfree(dr);
  818. call_xpt_users(xprt);
  819. svc_xprt_put(xprt);
  820. }
  821. void svc_close_xprt(struct svc_xprt *xprt)
  822. {
  823. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  824. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  825. /* someone else will have to effect the close */
  826. return;
  827. /*
  828. * We expect svc_close_xprt() to work even when no threads are
  829. * running (e.g., while configuring the server before starting
  830. * any threads), so if the transport isn't busy, we delete
  831. * it ourself:
  832. */
  833. svc_delete_xprt(xprt);
  834. }
  835. EXPORT_SYMBOL_GPL(svc_close_xprt);
  836. static void svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net)
  837. {
  838. struct svc_xprt *xprt;
  839. spin_lock(&serv->sv_lock);
  840. list_for_each_entry(xprt, xprt_list, xpt_list) {
  841. if (xprt->xpt_net != net)
  842. continue;
  843. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  844. set_bit(XPT_BUSY, &xprt->xpt_flags);
  845. }
  846. spin_unlock(&serv->sv_lock);
  847. }
  848. static void svc_clear_pools(struct svc_serv *serv, struct net *net)
  849. {
  850. struct svc_pool *pool;
  851. struct svc_xprt *xprt;
  852. struct svc_xprt *tmp;
  853. int i;
  854. for (i = 0; i < serv->sv_nrpools; i++) {
  855. pool = &serv->sv_pools[i];
  856. spin_lock_bh(&pool->sp_lock);
  857. list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) {
  858. if (xprt->xpt_net != net)
  859. continue;
  860. list_del_init(&xprt->xpt_ready);
  861. }
  862. spin_unlock_bh(&pool->sp_lock);
  863. }
  864. }
  865. static void svc_clear_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net)
  866. {
  867. struct svc_xprt *xprt;
  868. struct svc_xprt *tmp;
  869. LIST_HEAD(victims);
  870. spin_lock(&serv->sv_lock);
  871. list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
  872. if (xprt->xpt_net != net)
  873. continue;
  874. list_move(&xprt->xpt_list, &victims);
  875. }
  876. spin_unlock(&serv->sv_lock);
  877. list_for_each_entry_safe(xprt, tmp, &victims, xpt_list)
  878. svc_delete_xprt(xprt);
  879. }
  880. void svc_close_net(struct svc_serv *serv, struct net *net)
  881. {
  882. svc_close_list(serv, &serv->sv_tempsocks, net);
  883. svc_close_list(serv, &serv->sv_permsocks, net);
  884. svc_clear_pools(serv, net);
  885. /*
  886. * At this point the sp_sockets lists will stay empty, since
  887. * svc_xprt_enqueue will not add new entries without taking the
  888. * sp_lock and checking XPT_BUSY.
  889. */
  890. svc_clear_list(serv, &serv->sv_tempsocks, net);
  891. svc_clear_list(serv, &serv->sv_permsocks, net);
  892. }
  893. /*
  894. * Handle defer and revisit of requests
  895. */
  896. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  897. {
  898. struct svc_deferred_req *dr =
  899. container_of(dreq, struct svc_deferred_req, handle);
  900. struct svc_xprt *xprt = dr->xprt;
  901. spin_lock(&xprt->xpt_lock);
  902. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  903. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  904. spin_unlock(&xprt->xpt_lock);
  905. dprintk("revisit canceled\n");
  906. svc_xprt_put(xprt);
  907. kfree(dr);
  908. return;
  909. }
  910. dprintk("revisit queued\n");
  911. dr->xprt = NULL;
  912. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  913. spin_unlock(&xprt->xpt_lock);
  914. svc_xprt_enqueue(xprt);
  915. svc_xprt_put(xprt);
  916. }
  917. /*
  918. * Save the request off for later processing. The request buffer looks
  919. * like this:
  920. *
  921. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  922. *
  923. * This code can only handle requests that consist of an xprt-header
  924. * and rpc-header.
  925. */
  926. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  927. {
  928. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  929. struct svc_deferred_req *dr;
  930. if (rqstp->rq_arg.page_len || !rqstp->rq_usedeferral)
  931. return NULL; /* if more than a page, give up FIXME */
  932. if (rqstp->rq_deferred) {
  933. dr = rqstp->rq_deferred;
  934. rqstp->rq_deferred = NULL;
  935. } else {
  936. size_t skip;
  937. size_t size;
  938. /* FIXME maybe discard if size too large */
  939. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  940. dr = kmalloc(size, GFP_KERNEL);
  941. if (dr == NULL)
  942. return NULL;
  943. dr->handle.owner = rqstp->rq_server;
  944. dr->prot = rqstp->rq_prot;
  945. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  946. dr->addrlen = rqstp->rq_addrlen;
  947. dr->daddr = rqstp->rq_daddr;
  948. dr->argslen = rqstp->rq_arg.len >> 2;
  949. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  950. /* back up head to the start of the buffer and copy */
  951. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  952. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  953. dr->argslen << 2);
  954. }
  955. svc_xprt_get(rqstp->rq_xprt);
  956. dr->xprt = rqstp->rq_xprt;
  957. rqstp->rq_dropme = true;
  958. dr->handle.revisit = svc_revisit;
  959. return &dr->handle;
  960. }
  961. /*
  962. * recv data from a deferred request into an active one
  963. */
  964. static int svc_deferred_recv(struct svc_rqst *rqstp)
  965. {
  966. struct svc_deferred_req *dr = rqstp->rq_deferred;
  967. /* setup iov_base past transport header */
  968. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  969. /* The iov_len does not include the transport header bytes */
  970. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  971. rqstp->rq_arg.page_len = 0;
  972. /* The rq_arg.len includes the transport header bytes */
  973. rqstp->rq_arg.len = dr->argslen<<2;
  974. rqstp->rq_prot = dr->prot;
  975. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  976. rqstp->rq_addrlen = dr->addrlen;
  977. /* Save off transport header len in case we get deferred again */
  978. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  979. rqstp->rq_daddr = dr->daddr;
  980. rqstp->rq_respages = rqstp->rq_pages;
  981. return (dr->argslen<<2) - dr->xprt_hlen;
  982. }
  983. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  984. {
  985. struct svc_deferred_req *dr = NULL;
  986. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  987. return NULL;
  988. spin_lock(&xprt->xpt_lock);
  989. if (!list_empty(&xprt->xpt_deferred)) {
  990. dr = list_entry(xprt->xpt_deferred.next,
  991. struct svc_deferred_req,
  992. handle.recent);
  993. list_del_init(&dr->handle.recent);
  994. } else
  995. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  996. spin_unlock(&xprt->xpt_lock);
  997. return dr;
  998. }
  999. /**
  1000. * svc_find_xprt - find an RPC transport instance
  1001. * @serv: pointer to svc_serv to search
  1002. * @xcl_name: C string containing transport's class name
  1003. * @net: owner net pointer
  1004. * @af: Address family of transport's local address
  1005. * @port: transport's IP port number
  1006. *
  1007. * Return the transport instance pointer for the endpoint accepting
  1008. * connections/peer traffic from the specified transport class,
  1009. * address family and port.
  1010. *
  1011. * Specifying 0 for the address family or port is effectively a
  1012. * wild-card, and will result in matching the first transport in the
  1013. * service's list that has a matching class name.
  1014. */
  1015. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  1016. struct net *net, const sa_family_t af,
  1017. const unsigned short port)
  1018. {
  1019. struct svc_xprt *xprt;
  1020. struct svc_xprt *found = NULL;
  1021. /* Sanity check the args */
  1022. if (serv == NULL || xcl_name == NULL)
  1023. return found;
  1024. spin_lock_bh(&serv->sv_lock);
  1025. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1026. if (xprt->xpt_net != net)
  1027. continue;
  1028. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  1029. continue;
  1030. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  1031. continue;
  1032. if (port != 0 && port != svc_xprt_local_port(xprt))
  1033. continue;
  1034. found = xprt;
  1035. svc_xprt_get(xprt);
  1036. break;
  1037. }
  1038. spin_unlock_bh(&serv->sv_lock);
  1039. return found;
  1040. }
  1041. EXPORT_SYMBOL_GPL(svc_find_xprt);
  1042. static int svc_one_xprt_name(const struct svc_xprt *xprt,
  1043. char *pos, int remaining)
  1044. {
  1045. int len;
  1046. len = snprintf(pos, remaining, "%s %u\n",
  1047. xprt->xpt_class->xcl_name,
  1048. svc_xprt_local_port(xprt));
  1049. if (len >= remaining)
  1050. return -ENAMETOOLONG;
  1051. return len;
  1052. }
  1053. /**
  1054. * svc_xprt_names - format a buffer with a list of transport names
  1055. * @serv: pointer to an RPC service
  1056. * @buf: pointer to a buffer to be filled in
  1057. * @buflen: length of buffer to be filled in
  1058. *
  1059. * Fills in @buf with a string containing a list of transport names,
  1060. * each name terminated with '\n'.
  1061. *
  1062. * Returns positive length of the filled-in string on success; otherwise
  1063. * a negative errno value is returned if an error occurs.
  1064. */
  1065. int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
  1066. {
  1067. struct svc_xprt *xprt;
  1068. int len, totlen;
  1069. char *pos;
  1070. /* Sanity check args */
  1071. if (!serv)
  1072. return 0;
  1073. spin_lock_bh(&serv->sv_lock);
  1074. pos = buf;
  1075. totlen = 0;
  1076. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1077. len = svc_one_xprt_name(xprt, pos, buflen - totlen);
  1078. if (len < 0) {
  1079. *buf = '\0';
  1080. totlen = len;
  1081. }
  1082. if (len <= 0)
  1083. break;
  1084. pos += len;
  1085. totlen += len;
  1086. }
  1087. spin_unlock_bh(&serv->sv_lock);
  1088. return totlen;
  1089. }
  1090. EXPORT_SYMBOL_GPL(svc_xprt_names);
  1091. /*----------------------------------------------------------------------------*/
  1092. static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
  1093. {
  1094. unsigned int pidx = (unsigned int)*pos;
  1095. struct svc_serv *serv = m->private;
  1096. dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
  1097. if (!pidx)
  1098. return SEQ_START_TOKEN;
  1099. return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
  1100. }
  1101. static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
  1102. {
  1103. struct svc_pool *pool = p;
  1104. struct svc_serv *serv = m->private;
  1105. dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
  1106. if (p == SEQ_START_TOKEN) {
  1107. pool = &serv->sv_pools[0];
  1108. } else {
  1109. unsigned int pidx = (pool - &serv->sv_pools[0]);
  1110. if (pidx < serv->sv_nrpools-1)
  1111. pool = &serv->sv_pools[pidx+1];
  1112. else
  1113. pool = NULL;
  1114. }
  1115. ++*pos;
  1116. return pool;
  1117. }
  1118. static void svc_pool_stats_stop(struct seq_file *m, void *p)
  1119. {
  1120. }
  1121. static int svc_pool_stats_show(struct seq_file *m, void *p)
  1122. {
  1123. struct svc_pool *pool = p;
  1124. if (p == SEQ_START_TOKEN) {
  1125. seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
  1126. return 0;
  1127. }
  1128. seq_printf(m, "%u %lu %lu %lu %lu\n",
  1129. pool->sp_id,
  1130. pool->sp_stats.packets,
  1131. pool->sp_stats.sockets_queued,
  1132. pool->sp_stats.threads_woken,
  1133. pool->sp_stats.threads_timedout);
  1134. return 0;
  1135. }
  1136. static const struct seq_operations svc_pool_stats_seq_ops = {
  1137. .start = svc_pool_stats_start,
  1138. .next = svc_pool_stats_next,
  1139. .stop = svc_pool_stats_stop,
  1140. .show = svc_pool_stats_show,
  1141. };
  1142. int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
  1143. {
  1144. int err;
  1145. err = seq_open(file, &svc_pool_stats_seq_ops);
  1146. if (!err)
  1147. ((struct seq_file *) file->private_data)->private = serv;
  1148. return err;
  1149. }
  1150. EXPORT_SYMBOL(svc_pool_stats_open);
  1151. /*----------------------------------------------------------------------------*/