svc_xprt.c 33 KB

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