svc_xprt.c 33 KB

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