svc_xprt.c 33 KB

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