svc_xprt.c 33 KB

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