svc_xprt.c 33 KB

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