svc_xprt.c 32 KB

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