svc_xprt.c 33 KB

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