svc_xprt.c 34 KB

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