svc_xprt.c 33 KB

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