svc_xprt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * linux/net/sunrpc/svc_xprt.c
  3. *
  4. * Author: Tom Tucker <tom@opengridcomputing.com>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/smp_lock.h>
  8. #include <linux/errno.h>
  9. #include <linux/freezer.h>
  10. #include <linux/kthread.h>
  11. #include <net/sock.h>
  12. #include <linux/sunrpc/stats.h>
  13. #include <linux/sunrpc/svc_xprt.h>
  14. #include <linux/sunrpc/svcsock.h>
  15. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  16. #define SVC_MAX_WAKING 5
  17. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
  18. static int svc_deferred_recv(struct svc_rqst *rqstp);
  19. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  20. static void svc_age_temp_xprts(unsigned long closure);
  21. /* apparently the "standard" is that clients close
  22. * idle connections after 5 minutes, servers after
  23. * 6 minutes
  24. * http://www.connectathon.org/talks96/nfstcp.pdf
  25. */
  26. static int svc_conn_age_period = 6*60;
  27. /* List of registered transport classes */
  28. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  29. static LIST_HEAD(svc_xprt_class_list);
  30. /* SMP locking strategy:
  31. *
  32. * svc_pool->sp_lock protects most of the fields of that pool.
  33. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  34. * when both need to be taken (rare), svc_serv->sv_lock is first.
  35. * BKL protects svc_serv->sv_nrthread.
  36. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  37. * and the ->sk_info_authunix cache.
  38. *
  39. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  40. * enqueued multiply. During normal transport processing this bit
  41. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  42. * Providers should not manipulate this bit directly.
  43. *
  44. * Some flags can be set to certain values at any time
  45. * providing that certain rules are followed:
  46. *
  47. * XPT_CONN, XPT_DATA:
  48. * - Can be set or cleared at any time.
  49. * - After a set, svc_xprt_enqueue must be called to enqueue
  50. * the transport for processing.
  51. * - After a clear, the transport must be read/accepted.
  52. * If this succeeds, it must be set again.
  53. * XPT_CLOSE:
  54. * - Can set at any time. It is never cleared.
  55. * XPT_DEAD:
  56. * - Can only be set while XPT_BUSY is held which ensures
  57. * that no other thread will be using the transport or will
  58. * try to set XPT_DEAD.
  59. */
  60. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  61. {
  62. struct svc_xprt_class *cl;
  63. int res = -EEXIST;
  64. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  65. INIT_LIST_HEAD(&xcl->xcl_list);
  66. spin_lock(&svc_xprt_class_lock);
  67. /* Make sure there isn't already a class with the same name */
  68. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  69. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  70. goto out;
  71. }
  72. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  73. res = 0;
  74. out:
  75. spin_unlock(&svc_xprt_class_lock);
  76. return res;
  77. }
  78. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  79. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  80. {
  81. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  82. spin_lock(&svc_xprt_class_lock);
  83. list_del_init(&xcl->xcl_list);
  84. spin_unlock(&svc_xprt_class_lock);
  85. }
  86. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  87. /*
  88. * Format the transport list for printing
  89. */
  90. int svc_print_xprts(char *buf, int maxlen)
  91. {
  92. struct list_head *le;
  93. char tmpstr[80];
  94. int len = 0;
  95. buf[0] = '\0';
  96. spin_lock(&svc_xprt_class_lock);
  97. list_for_each(le, &svc_xprt_class_list) {
  98. int slen;
  99. struct svc_xprt_class *xcl =
  100. list_entry(le, struct svc_xprt_class, xcl_list);
  101. sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
  102. slen = strlen(tmpstr);
  103. if (len + slen > maxlen)
  104. break;
  105. len += slen;
  106. strcat(buf, tmpstr);
  107. }
  108. spin_unlock(&svc_xprt_class_lock);
  109. return len;
  110. }
  111. static void svc_xprt_free(struct kref *kref)
  112. {
  113. struct svc_xprt *xprt =
  114. container_of(kref, struct svc_xprt, xpt_ref);
  115. struct module *owner = xprt->xpt_class->xcl_owner;
  116. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
  117. && xprt->xpt_auth_cache != NULL)
  118. svcauth_unix_info_release(xprt->xpt_auth_cache);
  119. xprt->xpt_ops->xpo_free(xprt);
  120. module_put(owner);
  121. }
  122. void svc_xprt_put(struct svc_xprt *xprt)
  123. {
  124. kref_put(&xprt->xpt_ref, svc_xprt_free);
  125. }
  126. EXPORT_SYMBOL_GPL(svc_xprt_put);
  127. /*
  128. * Called by transport drivers to initialize the transport independent
  129. * portion of the transport instance.
  130. */
  131. void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
  132. struct svc_serv *serv)
  133. {
  134. memset(xprt, 0, sizeof(*xprt));
  135. xprt->xpt_class = xcl;
  136. xprt->xpt_ops = xcl->xcl_ops;
  137. kref_init(&xprt->xpt_ref);
  138. xprt->xpt_server = serv;
  139. INIT_LIST_HEAD(&xprt->xpt_list);
  140. INIT_LIST_HEAD(&xprt->xpt_ready);
  141. INIT_LIST_HEAD(&xprt->xpt_deferred);
  142. mutex_init(&xprt->xpt_mutex);
  143. spin_lock_init(&xprt->xpt_lock);
  144. set_bit(XPT_BUSY, &xprt->xpt_flags);
  145. rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending");
  146. }
  147. EXPORT_SYMBOL_GPL(svc_xprt_init);
  148. static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
  149. struct svc_serv *serv,
  150. const int family,
  151. const unsigned short port,
  152. int flags)
  153. {
  154. struct sockaddr_in sin = {
  155. .sin_family = AF_INET,
  156. .sin_addr.s_addr = htonl(INADDR_ANY),
  157. .sin_port = htons(port),
  158. };
  159. struct sockaddr_in6 sin6 = {
  160. .sin6_family = AF_INET6,
  161. .sin6_addr = IN6ADDR_ANY_INIT,
  162. .sin6_port = htons(port),
  163. };
  164. struct sockaddr *sap;
  165. size_t len;
  166. switch (family) {
  167. case PF_INET:
  168. sap = (struct sockaddr *)&sin;
  169. len = sizeof(sin);
  170. break;
  171. case PF_INET6:
  172. sap = (struct sockaddr *)&sin6;
  173. len = sizeof(sin6);
  174. break;
  175. default:
  176. return ERR_PTR(-EAFNOSUPPORT);
  177. }
  178. return xcl->xcl_ops->xpo_create(serv, sap, len, flags);
  179. }
  180. int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
  181. const int family, const unsigned short port,
  182. int flags)
  183. {
  184. struct svc_xprt_class *xcl;
  185. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  186. spin_lock(&svc_xprt_class_lock);
  187. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  188. struct svc_xprt *newxprt;
  189. if (strcmp(xprt_name, xcl->xcl_name))
  190. continue;
  191. if (!try_module_get(xcl->xcl_owner))
  192. goto err;
  193. spin_unlock(&svc_xprt_class_lock);
  194. newxprt = __svc_xpo_create(xcl, serv, family, port, flags);
  195. if (IS_ERR(newxprt)) {
  196. module_put(xcl->xcl_owner);
  197. return PTR_ERR(newxprt);
  198. }
  199. clear_bit(XPT_TEMP, &newxprt->xpt_flags);
  200. spin_lock_bh(&serv->sv_lock);
  201. list_add(&newxprt->xpt_list, &serv->sv_permsocks);
  202. spin_unlock_bh(&serv->sv_lock);
  203. clear_bit(XPT_BUSY, &newxprt->xpt_flags);
  204. return svc_xprt_local_port(newxprt);
  205. }
  206. err:
  207. spin_unlock(&svc_xprt_class_lock);
  208. dprintk("svc: transport %s not found\n", xprt_name);
  209. return -ENOENT;
  210. }
  211. EXPORT_SYMBOL_GPL(svc_create_xprt);
  212. /*
  213. * Copy the local and remote xprt addresses to the rqstp structure
  214. */
  215. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  216. {
  217. struct sockaddr *sin;
  218. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  219. rqstp->rq_addrlen = xprt->xpt_remotelen;
  220. /*
  221. * Destination address in request is needed for binding the
  222. * source address in RPC replies/callbacks later.
  223. */
  224. sin = (struct sockaddr *)&xprt->xpt_local;
  225. switch (sin->sa_family) {
  226. case AF_INET:
  227. rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
  228. break;
  229. case AF_INET6:
  230. rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
  231. break;
  232. }
  233. }
  234. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  235. /**
  236. * svc_print_addr - Format rq_addr field for printing
  237. * @rqstp: svc_rqst struct containing address to print
  238. * @buf: target buffer for formatted address
  239. * @len: length of target buffer
  240. *
  241. */
  242. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  243. {
  244. return __svc_print_addr(svc_addr(rqstp), buf, len);
  245. }
  246. EXPORT_SYMBOL_GPL(svc_print_addr);
  247. /*
  248. * Queue up an idle server thread. Must have pool->sp_lock held.
  249. * Note: this is really a stack rather than a queue, so that we only
  250. * use as many different threads as we need, and the rest don't pollute
  251. * the cache.
  252. */
  253. static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  254. {
  255. list_add(&rqstp->rq_list, &pool->sp_threads);
  256. }
  257. /*
  258. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  259. */
  260. static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  261. {
  262. list_del(&rqstp->rq_list);
  263. }
  264. /*
  265. * Queue up a transport with data pending. If there are idle nfsd
  266. * processes, wake 'em up.
  267. *
  268. */
  269. void svc_xprt_enqueue(struct svc_xprt *xprt)
  270. {
  271. struct svc_serv *serv = xprt->xpt_server;
  272. struct svc_pool *pool;
  273. struct svc_rqst *rqstp;
  274. int cpu;
  275. int thread_avail;
  276. if (!(xprt->xpt_flags &
  277. ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
  278. return;
  279. cpu = get_cpu();
  280. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  281. put_cpu();
  282. spin_lock_bh(&pool->sp_lock);
  283. if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  284. /* Don't enqueue dead transports */
  285. dprintk("svc: transport %p is dead, not enqueued\n", xprt);
  286. goto out_unlock;
  287. }
  288. pool->sp_stats.packets++;
  289. /* Mark transport as busy. It will remain in this state until
  290. * the provider calls svc_xprt_received. We update XPT_BUSY
  291. * atomically because it also guards against trying to enqueue
  292. * the transport twice.
  293. */
  294. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
  295. /* Don't enqueue transport while already enqueued */
  296. dprintk("svc: transport %p busy, not enqueued\n", xprt);
  297. goto out_unlock;
  298. }
  299. BUG_ON(xprt->xpt_pool != NULL);
  300. xprt->xpt_pool = pool;
  301. /* Handle pending connection */
  302. if (test_bit(XPT_CONN, &xprt->xpt_flags))
  303. goto process;
  304. /* Handle close in-progress */
  305. if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
  306. goto process;
  307. /* Check if we have space to reply to a request */
  308. if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
  309. /* Don't enqueue while not enough space for reply */
  310. dprintk("svc: no write space, transport %p not enqueued\n",
  311. xprt);
  312. xprt->xpt_pool = NULL;
  313. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  314. goto out_unlock;
  315. }
  316. process:
  317. /* Work out whether threads are available */
  318. thread_avail = !list_empty(&pool->sp_threads); /* threads are asleep */
  319. if (pool->sp_nwaking >= SVC_MAX_WAKING) {
  320. /* too many threads are runnable and trying to wake up */
  321. thread_avail = 0;
  322. pool->sp_stats.overloads_avoided++;
  323. }
  324. if (thread_avail) {
  325. rqstp = list_entry(pool->sp_threads.next,
  326. struct svc_rqst,
  327. rq_list);
  328. dprintk("svc: transport %p served by daemon %p\n",
  329. xprt, rqstp);
  330. svc_thread_dequeue(pool, rqstp);
  331. if (rqstp->rq_xprt)
  332. printk(KERN_ERR
  333. "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
  334. rqstp, rqstp->rq_xprt);
  335. rqstp->rq_xprt = xprt;
  336. svc_xprt_get(xprt);
  337. rqstp->rq_reserved = serv->sv_max_mesg;
  338. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  339. rqstp->rq_waking = 1;
  340. pool->sp_nwaking++;
  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. if (rqstp->rq_waking) {
  579. rqstp->rq_waking = 0;
  580. pool->sp_nwaking--;
  581. BUG_ON(pool->sp_nwaking < 0);
  582. }
  583. xprt = svc_xprt_dequeue(pool);
  584. if (xprt) {
  585. rqstp->rq_xprt = xprt;
  586. svc_xprt_get(xprt);
  587. rqstp->rq_reserved = serv->sv_max_mesg;
  588. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  589. } else {
  590. /* No data pending. Go to sleep */
  591. svc_thread_enqueue(pool, rqstp);
  592. /*
  593. * We have to be able to interrupt this wait
  594. * to bring down the daemons ...
  595. */
  596. set_current_state(TASK_INTERRUPTIBLE);
  597. /*
  598. * checking kthread_should_stop() here allows us to avoid
  599. * locking and signalling when stopping kthreads that call
  600. * svc_recv. If the thread has already been woken up, then
  601. * we can exit here without sleeping. If not, then it
  602. * it'll be woken up quickly during the schedule_timeout
  603. */
  604. if (kthread_should_stop()) {
  605. set_current_state(TASK_RUNNING);
  606. spin_unlock_bh(&pool->sp_lock);
  607. return -EINTR;
  608. }
  609. add_wait_queue(&rqstp->rq_wait, &wait);
  610. spin_unlock_bh(&pool->sp_lock);
  611. time_left = schedule_timeout(timeout);
  612. try_to_freeze();
  613. spin_lock_bh(&pool->sp_lock);
  614. remove_wait_queue(&rqstp->rq_wait, &wait);
  615. if (!time_left)
  616. pool->sp_stats.threads_timedout++;
  617. xprt = rqstp->rq_xprt;
  618. if (!xprt) {
  619. svc_thread_dequeue(pool, rqstp);
  620. spin_unlock_bh(&pool->sp_lock);
  621. dprintk("svc: server %p, no data yet\n", rqstp);
  622. if (signalled() || kthread_should_stop())
  623. return -EINTR;
  624. else
  625. return -EAGAIN;
  626. }
  627. }
  628. spin_unlock_bh(&pool->sp_lock);
  629. len = 0;
  630. if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  631. struct svc_xprt *newxpt;
  632. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  633. if (newxpt) {
  634. /*
  635. * We know this module_get will succeed because the
  636. * listener holds a reference too
  637. */
  638. __module_get(newxpt->xpt_class->xcl_owner);
  639. svc_check_conn_limits(xprt->xpt_server);
  640. spin_lock_bh(&serv->sv_lock);
  641. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  642. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  643. serv->sv_tmpcnt++;
  644. if (serv->sv_temptimer.function == NULL) {
  645. /* setup timer to age temp transports */
  646. setup_timer(&serv->sv_temptimer,
  647. svc_age_temp_xprts,
  648. (unsigned long)serv);
  649. mod_timer(&serv->sv_temptimer,
  650. jiffies + svc_conn_age_period * HZ);
  651. }
  652. spin_unlock_bh(&serv->sv_lock);
  653. svc_xprt_received(newxpt);
  654. }
  655. svc_xprt_received(xprt);
  656. } else if (!test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  657. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  658. rqstp, pool->sp_id, xprt,
  659. atomic_read(&xprt->xpt_ref.refcount));
  660. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  661. if (rqstp->rq_deferred) {
  662. svc_xprt_received(xprt);
  663. len = svc_deferred_recv(rqstp);
  664. } else
  665. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  666. dprintk("svc: got len=%d\n", len);
  667. }
  668. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  669. dprintk("svc_recv: found XPT_CLOSE\n");
  670. svc_delete_xprt(xprt);
  671. }
  672. /* No data, incomplete (TCP) read, or accept() */
  673. if (len == 0 || len == -EAGAIN) {
  674. rqstp->rq_res.len = 0;
  675. svc_xprt_release(rqstp);
  676. return -EAGAIN;
  677. }
  678. clear_bit(XPT_OLD, &xprt->xpt_flags);
  679. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  680. rqstp->rq_chandle.defer = svc_defer;
  681. if (serv->sv_stats)
  682. serv->sv_stats->netcnt++;
  683. return len;
  684. }
  685. EXPORT_SYMBOL_GPL(svc_recv);
  686. /*
  687. * Drop request
  688. */
  689. void svc_drop(struct svc_rqst *rqstp)
  690. {
  691. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  692. svc_xprt_release(rqstp);
  693. }
  694. EXPORT_SYMBOL_GPL(svc_drop);
  695. /*
  696. * Return reply to client.
  697. */
  698. int svc_send(struct svc_rqst *rqstp)
  699. {
  700. struct svc_xprt *xprt;
  701. int len;
  702. struct xdr_buf *xb;
  703. xprt = rqstp->rq_xprt;
  704. if (!xprt)
  705. return -EFAULT;
  706. /* release the receive skb before sending the reply */
  707. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  708. /* calculate over-all length */
  709. xb = &rqstp->rq_res;
  710. xb->len = xb->head[0].iov_len +
  711. xb->page_len +
  712. xb->tail[0].iov_len;
  713. /* Grab mutex to serialize outgoing data. */
  714. mutex_lock(&xprt->xpt_mutex);
  715. if (test_bit(XPT_DEAD, &xprt->xpt_flags))
  716. len = -ENOTCONN;
  717. else
  718. len = xprt->xpt_ops->xpo_sendto(rqstp);
  719. mutex_unlock(&xprt->xpt_mutex);
  720. rpc_wake_up(&xprt->xpt_bc_pending);
  721. svc_xprt_release(rqstp);
  722. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  723. return 0;
  724. return len;
  725. }
  726. /*
  727. * Timer function to close old temporary transports, using
  728. * a mark-and-sweep algorithm.
  729. */
  730. static void svc_age_temp_xprts(unsigned long closure)
  731. {
  732. struct svc_serv *serv = (struct svc_serv *)closure;
  733. struct svc_xprt *xprt;
  734. struct list_head *le, *next;
  735. LIST_HEAD(to_be_aged);
  736. dprintk("svc_age_temp_xprts\n");
  737. if (!spin_trylock_bh(&serv->sv_lock)) {
  738. /* busy, try again 1 sec later */
  739. dprintk("svc_age_temp_xprts: busy\n");
  740. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  741. return;
  742. }
  743. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  744. xprt = list_entry(le, struct svc_xprt, xpt_list);
  745. /* First time through, just mark it OLD. Second time
  746. * through, close it. */
  747. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  748. continue;
  749. if (atomic_read(&xprt->xpt_ref.refcount) > 1
  750. || test_bit(XPT_BUSY, &xprt->xpt_flags))
  751. continue;
  752. svc_xprt_get(xprt);
  753. list_move(le, &to_be_aged);
  754. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  755. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  756. }
  757. spin_unlock_bh(&serv->sv_lock);
  758. while (!list_empty(&to_be_aged)) {
  759. le = to_be_aged.next;
  760. /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
  761. list_del_init(le);
  762. xprt = list_entry(le, struct svc_xprt, xpt_list);
  763. dprintk("queuing xprt %p for closing\n", xprt);
  764. /* a thread will dequeue and close it soon */
  765. svc_xprt_enqueue(xprt);
  766. svc_xprt_put(xprt);
  767. }
  768. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  769. }
  770. /*
  771. * Remove a dead transport
  772. */
  773. void svc_delete_xprt(struct svc_xprt *xprt)
  774. {
  775. struct svc_serv *serv = xprt->xpt_server;
  776. struct svc_deferred_req *dr;
  777. /* Only do this once */
  778. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  779. return;
  780. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  781. xprt->xpt_ops->xpo_detach(xprt);
  782. spin_lock_bh(&serv->sv_lock);
  783. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  784. list_del_init(&xprt->xpt_list);
  785. /*
  786. * We used to delete the transport from whichever list
  787. * it's sk_xprt.xpt_ready node was on, but we don't actually
  788. * need to. This is because the only time we're called
  789. * while still attached to a queue, the queue itself
  790. * is about to be destroyed (in svc_destroy).
  791. */
  792. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  793. serv->sv_tmpcnt--;
  794. for (dr = svc_deferred_dequeue(xprt); dr;
  795. dr = svc_deferred_dequeue(xprt)) {
  796. svc_xprt_put(xprt);
  797. kfree(dr);
  798. }
  799. svc_xprt_put(xprt);
  800. spin_unlock_bh(&serv->sv_lock);
  801. }
  802. void svc_close_xprt(struct svc_xprt *xprt)
  803. {
  804. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  805. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  806. /* someone else will have to effect the close */
  807. return;
  808. svc_xprt_get(xprt);
  809. svc_delete_xprt(xprt);
  810. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  811. svc_xprt_put(xprt);
  812. }
  813. EXPORT_SYMBOL_GPL(svc_close_xprt);
  814. void svc_close_all(struct list_head *xprt_list)
  815. {
  816. struct svc_xprt *xprt;
  817. struct svc_xprt *tmp;
  818. list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
  819. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  820. if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
  821. /* Waiting to be processed, but no threads left,
  822. * So just remove it from the waiting list
  823. */
  824. list_del_init(&xprt->xpt_ready);
  825. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  826. }
  827. svc_close_xprt(xprt);
  828. }
  829. }
  830. /*
  831. * Handle defer and revisit of requests
  832. */
  833. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  834. {
  835. struct svc_deferred_req *dr =
  836. container_of(dreq, struct svc_deferred_req, handle);
  837. struct svc_xprt *xprt = dr->xprt;
  838. spin_lock(&xprt->xpt_lock);
  839. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  840. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  841. spin_unlock(&xprt->xpt_lock);
  842. dprintk("revisit canceled\n");
  843. svc_xprt_put(xprt);
  844. kfree(dr);
  845. return;
  846. }
  847. dprintk("revisit queued\n");
  848. dr->xprt = NULL;
  849. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  850. spin_unlock(&xprt->xpt_lock);
  851. svc_xprt_enqueue(xprt);
  852. svc_xprt_put(xprt);
  853. }
  854. /*
  855. * Save the request off for later processing. The request buffer looks
  856. * like this:
  857. *
  858. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  859. *
  860. * This code can only handle requests that consist of an xprt-header
  861. * and rpc-header.
  862. */
  863. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  864. {
  865. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  866. struct svc_deferred_req *dr;
  867. if (rqstp->rq_arg.page_len || !rqstp->rq_usedeferral)
  868. return NULL; /* if more than a page, give up FIXME */
  869. if (rqstp->rq_deferred) {
  870. dr = rqstp->rq_deferred;
  871. rqstp->rq_deferred = NULL;
  872. } else {
  873. size_t skip;
  874. size_t size;
  875. /* FIXME maybe discard if size too large */
  876. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  877. dr = kmalloc(size, GFP_KERNEL);
  878. if (dr == NULL)
  879. return NULL;
  880. dr->handle.owner = rqstp->rq_server;
  881. dr->prot = rqstp->rq_prot;
  882. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  883. dr->addrlen = rqstp->rq_addrlen;
  884. dr->daddr = rqstp->rq_daddr;
  885. dr->argslen = rqstp->rq_arg.len >> 2;
  886. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  887. /* back up head to the start of the buffer and copy */
  888. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  889. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  890. dr->argslen << 2);
  891. }
  892. svc_xprt_get(rqstp->rq_xprt);
  893. dr->xprt = rqstp->rq_xprt;
  894. dr->handle.revisit = svc_revisit;
  895. return &dr->handle;
  896. }
  897. /*
  898. * recv data from a deferred request into an active one
  899. */
  900. static int svc_deferred_recv(struct svc_rqst *rqstp)
  901. {
  902. struct svc_deferred_req *dr = rqstp->rq_deferred;
  903. /* setup iov_base past transport header */
  904. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  905. /* The iov_len does not include the transport header bytes */
  906. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  907. rqstp->rq_arg.page_len = 0;
  908. /* The rq_arg.len includes the transport header bytes */
  909. rqstp->rq_arg.len = dr->argslen<<2;
  910. rqstp->rq_prot = dr->prot;
  911. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  912. rqstp->rq_addrlen = dr->addrlen;
  913. /* Save off transport header len in case we get deferred again */
  914. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  915. rqstp->rq_daddr = dr->daddr;
  916. rqstp->rq_respages = rqstp->rq_pages;
  917. return (dr->argslen<<2) - dr->xprt_hlen;
  918. }
  919. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  920. {
  921. struct svc_deferred_req *dr = NULL;
  922. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  923. return NULL;
  924. spin_lock(&xprt->xpt_lock);
  925. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  926. if (!list_empty(&xprt->xpt_deferred)) {
  927. dr = list_entry(xprt->xpt_deferred.next,
  928. struct svc_deferred_req,
  929. handle.recent);
  930. list_del_init(&dr->handle.recent);
  931. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  932. }
  933. spin_unlock(&xprt->xpt_lock);
  934. return dr;
  935. }
  936. /**
  937. * svc_find_xprt - find an RPC transport instance
  938. * @serv: pointer to svc_serv to search
  939. * @xcl_name: C string containing transport's class name
  940. * @af: Address family of transport's local address
  941. * @port: transport's IP port number
  942. *
  943. * Return the transport instance pointer for the endpoint accepting
  944. * connections/peer traffic from the specified transport class,
  945. * address family and port.
  946. *
  947. * Specifying 0 for the address family or port is effectively a
  948. * wild-card, and will result in matching the first transport in the
  949. * service's list that has a matching class name.
  950. */
  951. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  952. const sa_family_t af, const unsigned short port)
  953. {
  954. struct svc_xprt *xprt;
  955. struct svc_xprt *found = NULL;
  956. /* Sanity check the args */
  957. if (serv == NULL || xcl_name == NULL)
  958. return found;
  959. spin_lock_bh(&serv->sv_lock);
  960. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  961. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  962. continue;
  963. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  964. continue;
  965. if (port != 0 && port != svc_xprt_local_port(xprt))
  966. continue;
  967. found = xprt;
  968. svc_xprt_get(xprt);
  969. break;
  970. }
  971. spin_unlock_bh(&serv->sv_lock);
  972. return found;
  973. }
  974. EXPORT_SYMBOL_GPL(svc_find_xprt);
  975. static int svc_one_xprt_name(const struct svc_xprt *xprt,
  976. char *pos, int remaining)
  977. {
  978. int len;
  979. len = snprintf(pos, remaining, "%s %u\n",
  980. xprt->xpt_class->xcl_name,
  981. svc_xprt_local_port(xprt));
  982. if (len >= remaining)
  983. return -ENAMETOOLONG;
  984. return len;
  985. }
  986. /**
  987. * svc_xprt_names - format a buffer with a list of transport names
  988. * @serv: pointer to an RPC service
  989. * @buf: pointer to a buffer to be filled in
  990. * @buflen: length of buffer to be filled in
  991. *
  992. * Fills in @buf with a string containing a list of transport names,
  993. * each name terminated with '\n'.
  994. *
  995. * Returns positive length of the filled-in string on success; otherwise
  996. * a negative errno value is returned if an error occurs.
  997. */
  998. int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
  999. {
  1000. struct svc_xprt *xprt;
  1001. int len, totlen;
  1002. char *pos;
  1003. /* Sanity check args */
  1004. if (!serv)
  1005. return 0;
  1006. spin_lock_bh(&serv->sv_lock);
  1007. pos = buf;
  1008. totlen = 0;
  1009. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1010. len = svc_one_xprt_name(xprt, pos, buflen - totlen);
  1011. if (len < 0) {
  1012. *buf = '\0';
  1013. totlen = len;
  1014. }
  1015. if (len <= 0)
  1016. break;
  1017. pos += len;
  1018. totlen += len;
  1019. }
  1020. spin_unlock_bh(&serv->sv_lock);
  1021. return totlen;
  1022. }
  1023. EXPORT_SYMBOL_GPL(svc_xprt_names);
  1024. /*----------------------------------------------------------------------------*/
  1025. static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
  1026. {
  1027. unsigned int pidx = (unsigned int)*pos;
  1028. struct svc_serv *serv = m->private;
  1029. dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
  1030. if (!pidx)
  1031. return SEQ_START_TOKEN;
  1032. return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
  1033. }
  1034. static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
  1035. {
  1036. struct svc_pool *pool = p;
  1037. struct svc_serv *serv = m->private;
  1038. dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
  1039. if (p == SEQ_START_TOKEN) {
  1040. pool = &serv->sv_pools[0];
  1041. } else {
  1042. unsigned int pidx = (pool - &serv->sv_pools[0]);
  1043. if (pidx < serv->sv_nrpools-1)
  1044. pool = &serv->sv_pools[pidx+1];
  1045. else
  1046. pool = NULL;
  1047. }
  1048. ++*pos;
  1049. return pool;
  1050. }
  1051. static void svc_pool_stats_stop(struct seq_file *m, void *p)
  1052. {
  1053. }
  1054. static int svc_pool_stats_show(struct seq_file *m, void *p)
  1055. {
  1056. struct svc_pool *pool = p;
  1057. if (p == SEQ_START_TOKEN) {
  1058. seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken overloads-avoided threads-timedout\n");
  1059. return 0;
  1060. }
  1061. seq_printf(m, "%u %lu %lu %lu %lu %lu\n",
  1062. pool->sp_id,
  1063. pool->sp_stats.packets,
  1064. pool->sp_stats.sockets_queued,
  1065. pool->sp_stats.threads_woken,
  1066. pool->sp_stats.overloads_avoided,
  1067. pool->sp_stats.threads_timedout);
  1068. return 0;
  1069. }
  1070. static const struct seq_operations svc_pool_stats_seq_ops = {
  1071. .start = svc_pool_stats_start,
  1072. .next = svc_pool_stats_next,
  1073. .stop = svc_pool_stats_stop,
  1074. .show = svc_pool_stats_show,
  1075. };
  1076. int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
  1077. {
  1078. int err;
  1079. err = seq_open(file, &svc_pool_stats_seq_ops);
  1080. if (!err)
  1081. ((struct seq_file *) file->private_data)->private = serv;
  1082. return err;
  1083. }
  1084. EXPORT_SYMBOL(svc_pool_stats_open);
  1085. /*----------------------------------------------------------------------------*/