svc_xprt.c 33 KB

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