svc_xprt.c 33 KB

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