svc_xprt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. /* Try to help the admin */
  481. net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n",
  482. serv->sv_name, serv->sv_maxconn ?
  483. "max number of connections" :
  484. "number of threads");
  485. /*
  486. * Always select the oldest connection. It's not fair,
  487. * but so is life
  488. */
  489. xprt = list_entry(serv->sv_tempsocks.prev,
  490. struct svc_xprt,
  491. xpt_list);
  492. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  493. svc_xprt_get(xprt);
  494. }
  495. spin_unlock_bh(&serv->sv_lock);
  496. if (xprt) {
  497. svc_xprt_enqueue(xprt);
  498. svc_xprt_put(xprt);
  499. }
  500. }
  501. }
  502. /*
  503. * Receive the next request on any transport. This code is carefully
  504. * organised not to touch any cachelines in the shared svc_serv
  505. * structure, only cachelines in the local svc_pool.
  506. */
  507. int svc_recv(struct svc_rqst *rqstp, long timeout)
  508. {
  509. struct svc_xprt *xprt = NULL;
  510. struct svc_serv *serv = rqstp->rq_server;
  511. struct svc_pool *pool = rqstp->rq_pool;
  512. int len, i;
  513. int pages;
  514. struct xdr_buf *arg;
  515. DECLARE_WAITQUEUE(wait, current);
  516. long time_left;
  517. dprintk("svc: server %p waiting for data (to = %ld)\n",
  518. rqstp, timeout);
  519. if (rqstp->rq_xprt)
  520. printk(KERN_ERR
  521. "svc_recv: service %p, transport not NULL!\n",
  522. rqstp);
  523. if (waitqueue_active(&rqstp->rq_wait))
  524. printk(KERN_ERR
  525. "svc_recv: service %p, wait queue active!\n",
  526. rqstp);
  527. /* now allocate needed pages. If we get a failure, sleep briefly */
  528. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  529. for (i = 0; i < pages ; i++)
  530. while (rqstp->rq_pages[i] == NULL) {
  531. struct page *p = alloc_page(GFP_KERNEL);
  532. if (!p) {
  533. set_current_state(TASK_INTERRUPTIBLE);
  534. if (signalled() || kthread_should_stop()) {
  535. set_current_state(TASK_RUNNING);
  536. return -EINTR;
  537. }
  538. schedule_timeout(msecs_to_jiffies(500));
  539. }
  540. rqstp->rq_pages[i] = p;
  541. }
  542. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  543. BUG_ON(pages >= RPCSVC_MAXPAGES);
  544. /* Make arg->head point to first page and arg->pages point to rest */
  545. arg = &rqstp->rq_arg;
  546. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  547. arg->head[0].iov_len = PAGE_SIZE;
  548. arg->pages = rqstp->rq_pages + 1;
  549. arg->page_base = 0;
  550. /* save at least one page for response */
  551. arg->page_len = (pages-2)*PAGE_SIZE;
  552. arg->len = (pages-1)*PAGE_SIZE;
  553. arg->tail[0].iov_len = 0;
  554. try_to_freeze();
  555. cond_resched();
  556. if (signalled() || kthread_should_stop())
  557. return -EINTR;
  558. /* Normally we will wait up to 5 seconds for any required
  559. * cache information to be provided.
  560. */
  561. rqstp->rq_chandle.thread_wait = 5*HZ;
  562. spin_lock_bh(&pool->sp_lock);
  563. xprt = svc_xprt_dequeue(pool);
  564. if (xprt) {
  565. rqstp->rq_xprt = xprt;
  566. svc_xprt_get(xprt);
  567. /* As there is a shortage of threads and this request
  568. * had to be queued, don't allow the thread to wait so
  569. * long for cache updates.
  570. */
  571. rqstp->rq_chandle.thread_wait = 1*HZ;
  572. } else {
  573. /* No data pending. Go to sleep */
  574. svc_thread_enqueue(pool, rqstp);
  575. /*
  576. * We have to be able to interrupt this wait
  577. * to bring down the daemons ...
  578. */
  579. set_current_state(TASK_INTERRUPTIBLE);
  580. /*
  581. * checking kthread_should_stop() here allows us to avoid
  582. * locking and signalling when stopping kthreads that call
  583. * svc_recv. If the thread has already been woken up, then
  584. * we can exit here without sleeping. If not, then it
  585. * it'll be woken up quickly during the schedule_timeout
  586. */
  587. if (kthread_should_stop()) {
  588. set_current_state(TASK_RUNNING);
  589. spin_unlock_bh(&pool->sp_lock);
  590. return -EINTR;
  591. }
  592. add_wait_queue(&rqstp->rq_wait, &wait);
  593. spin_unlock_bh(&pool->sp_lock);
  594. time_left = schedule_timeout(timeout);
  595. try_to_freeze();
  596. spin_lock_bh(&pool->sp_lock);
  597. remove_wait_queue(&rqstp->rq_wait, &wait);
  598. if (!time_left)
  599. pool->sp_stats.threads_timedout++;
  600. xprt = rqstp->rq_xprt;
  601. if (!xprt) {
  602. svc_thread_dequeue(pool, rqstp);
  603. spin_unlock_bh(&pool->sp_lock);
  604. dprintk("svc: server %p, no data yet\n", rqstp);
  605. if (signalled() || kthread_should_stop())
  606. return -EINTR;
  607. else
  608. return -EAGAIN;
  609. }
  610. }
  611. spin_unlock_bh(&pool->sp_lock);
  612. len = 0;
  613. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  614. dprintk("svc_recv: found XPT_CLOSE\n");
  615. svc_delete_xprt(xprt);
  616. /* Leave XPT_BUSY set on the dead xprt: */
  617. goto out;
  618. }
  619. if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  620. struct svc_xprt *newxpt;
  621. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  622. if (newxpt) {
  623. /*
  624. * We know this module_get will succeed because the
  625. * listener holds a reference too
  626. */
  627. __module_get(newxpt->xpt_class->xcl_owner);
  628. svc_check_conn_limits(xprt->xpt_server);
  629. spin_lock_bh(&serv->sv_lock);
  630. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  631. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  632. serv->sv_tmpcnt++;
  633. if (serv->sv_temptimer.function == NULL) {
  634. /* setup timer to age temp transports */
  635. setup_timer(&serv->sv_temptimer,
  636. svc_age_temp_xprts,
  637. (unsigned long)serv);
  638. mod_timer(&serv->sv_temptimer,
  639. jiffies + svc_conn_age_period * HZ);
  640. }
  641. spin_unlock_bh(&serv->sv_lock);
  642. svc_xprt_received(newxpt);
  643. }
  644. } else if (xprt->xpt_ops->xpo_has_wspace(xprt)) {
  645. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  646. rqstp, pool->sp_id, xprt,
  647. atomic_read(&xprt->xpt_ref.refcount));
  648. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  649. if (rqstp->rq_deferred)
  650. len = svc_deferred_recv(rqstp);
  651. else
  652. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  653. dprintk("svc: got len=%d\n", len);
  654. rqstp->rq_reserved = serv->sv_max_mesg;
  655. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  656. }
  657. svc_xprt_received(xprt);
  658. /* No data, incomplete (TCP) read, or accept() */
  659. if (len == 0 || len == -EAGAIN)
  660. goto out;
  661. clear_bit(XPT_OLD, &xprt->xpt_flags);
  662. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  663. rqstp->rq_chandle.defer = svc_defer;
  664. if (serv->sv_stats)
  665. serv->sv_stats->netcnt++;
  666. return len;
  667. out:
  668. rqstp->rq_res.len = 0;
  669. svc_xprt_release(rqstp);
  670. return -EAGAIN;
  671. }
  672. EXPORT_SYMBOL_GPL(svc_recv);
  673. /*
  674. * Drop request
  675. */
  676. void svc_drop(struct svc_rqst *rqstp)
  677. {
  678. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  679. svc_xprt_release(rqstp);
  680. }
  681. EXPORT_SYMBOL_GPL(svc_drop);
  682. /*
  683. * Return reply to client.
  684. */
  685. int svc_send(struct svc_rqst *rqstp)
  686. {
  687. struct svc_xprt *xprt;
  688. int len;
  689. struct xdr_buf *xb;
  690. xprt = rqstp->rq_xprt;
  691. if (!xprt)
  692. return -EFAULT;
  693. /* release the receive skb before sending the reply */
  694. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  695. /* calculate over-all length */
  696. xb = &rqstp->rq_res;
  697. xb->len = xb->head[0].iov_len +
  698. xb->page_len +
  699. xb->tail[0].iov_len;
  700. /* Grab mutex to serialize outgoing data. */
  701. mutex_lock(&xprt->xpt_mutex);
  702. if (test_bit(XPT_DEAD, &xprt->xpt_flags)
  703. || test_bit(XPT_CLOSE, &xprt->xpt_flags))
  704. len = -ENOTCONN;
  705. else
  706. len = xprt->xpt_ops->xpo_sendto(rqstp);
  707. mutex_unlock(&xprt->xpt_mutex);
  708. rpc_wake_up(&xprt->xpt_bc_pending);
  709. svc_xprt_release(rqstp);
  710. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  711. return 0;
  712. return len;
  713. }
  714. /*
  715. * Timer function to close old temporary transports, using
  716. * a mark-and-sweep algorithm.
  717. */
  718. static void svc_age_temp_xprts(unsigned long closure)
  719. {
  720. struct svc_serv *serv = (struct svc_serv *)closure;
  721. struct svc_xprt *xprt;
  722. struct list_head *le, *next;
  723. dprintk("svc_age_temp_xprts\n");
  724. if (!spin_trylock_bh(&serv->sv_lock)) {
  725. /* busy, try again 1 sec later */
  726. dprintk("svc_age_temp_xprts: busy\n");
  727. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  728. return;
  729. }
  730. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  731. xprt = list_entry(le, struct svc_xprt, xpt_list);
  732. /* First time through, just mark it OLD. Second time
  733. * through, close it. */
  734. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  735. continue;
  736. if (atomic_read(&xprt->xpt_ref.refcount) > 1 ||
  737. test_bit(XPT_BUSY, &xprt->xpt_flags))
  738. continue;
  739. list_del_init(le);
  740. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  741. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  742. dprintk("queuing xprt %p for closing\n", xprt);
  743. /* a thread will dequeue and close it soon */
  744. svc_xprt_enqueue(xprt);
  745. }
  746. spin_unlock_bh(&serv->sv_lock);
  747. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  748. }
  749. static void call_xpt_users(struct svc_xprt *xprt)
  750. {
  751. struct svc_xpt_user *u;
  752. spin_lock(&xprt->xpt_lock);
  753. while (!list_empty(&xprt->xpt_users)) {
  754. u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
  755. list_del(&u->list);
  756. u->callback(u);
  757. }
  758. spin_unlock(&xprt->xpt_lock);
  759. }
  760. /*
  761. * Remove a dead transport
  762. */
  763. static void svc_delete_xprt(struct svc_xprt *xprt)
  764. {
  765. struct svc_serv *serv = xprt->xpt_server;
  766. struct svc_deferred_req *dr;
  767. /* Only do this once */
  768. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  769. BUG();
  770. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  771. xprt->xpt_ops->xpo_detach(xprt);
  772. spin_lock_bh(&serv->sv_lock);
  773. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  774. list_del_init(&xprt->xpt_list);
  775. BUG_ON(!list_empty(&xprt->xpt_ready));
  776. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  777. serv->sv_tmpcnt--;
  778. spin_unlock_bh(&serv->sv_lock);
  779. while ((dr = svc_deferred_dequeue(xprt)) != NULL)
  780. kfree(dr);
  781. call_xpt_users(xprt);
  782. svc_xprt_put(xprt);
  783. }
  784. void svc_close_xprt(struct svc_xprt *xprt)
  785. {
  786. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  787. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  788. /* someone else will have to effect the close */
  789. return;
  790. /*
  791. * We expect svc_close_xprt() to work even when no threads are
  792. * running (e.g., while configuring the server before starting
  793. * any threads), so if the transport isn't busy, we delete
  794. * it ourself:
  795. */
  796. svc_delete_xprt(xprt);
  797. }
  798. EXPORT_SYMBOL_GPL(svc_close_xprt);
  799. static void svc_close_list(struct list_head *xprt_list, struct net *net)
  800. {
  801. struct svc_xprt *xprt;
  802. list_for_each_entry(xprt, xprt_list, xpt_list) {
  803. if (xprt->xpt_net != net)
  804. continue;
  805. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  806. set_bit(XPT_BUSY, &xprt->xpt_flags);
  807. }
  808. }
  809. static void svc_clear_pools(struct svc_serv *serv, struct net *net)
  810. {
  811. struct svc_pool *pool;
  812. struct svc_xprt *xprt;
  813. struct svc_xprt *tmp;
  814. int i;
  815. for (i = 0; i < serv->sv_nrpools; i++) {
  816. pool = &serv->sv_pools[i];
  817. spin_lock_bh(&pool->sp_lock);
  818. list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) {
  819. if (xprt->xpt_net != net)
  820. continue;
  821. list_del_init(&xprt->xpt_ready);
  822. }
  823. spin_unlock_bh(&pool->sp_lock);
  824. }
  825. }
  826. static void svc_clear_list(struct list_head *xprt_list, struct net *net)
  827. {
  828. struct svc_xprt *xprt;
  829. struct svc_xprt *tmp;
  830. list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
  831. if (xprt->xpt_net != net)
  832. continue;
  833. svc_delete_xprt(xprt);
  834. }
  835. list_for_each_entry(xprt, xprt_list, xpt_list)
  836. BUG_ON(xprt->xpt_net == net);
  837. }
  838. void svc_close_net(struct svc_serv *serv, struct net *net)
  839. {
  840. svc_close_list(&serv->sv_tempsocks, net);
  841. svc_close_list(&serv->sv_permsocks, net);
  842. svc_clear_pools(serv, net);
  843. /*
  844. * At this point the sp_sockets lists will stay empty, since
  845. * svc_enqueue will not add new entries without taking the
  846. * sp_lock and checking XPT_BUSY.
  847. */
  848. svc_clear_list(&serv->sv_tempsocks, net);
  849. svc_clear_list(&serv->sv_permsocks, net);
  850. }
  851. /*
  852. * Handle defer and revisit of requests
  853. */
  854. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  855. {
  856. struct svc_deferred_req *dr =
  857. container_of(dreq, struct svc_deferred_req, handle);
  858. struct svc_xprt *xprt = dr->xprt;
  859. spin_lock(&xprt->xpt_lock);
  860. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  861. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  862. spin_unlock(&xprt->xpt_lock);
  863. dprintk("revisit canceled\n");
  864. svc_xprt_put(xprt);
  865. kfree(dr);
  866. return;
  867. }
  868. dprintk("revisit queued\n");
  869. dr->xprt = NULL;
  870. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  871. spin_unlock(&xprt->xpt_lock);
  872. svc_xprt_enqueue(xprt);
  873. svc_xprt_put(xprt);
  874. }
  875. /*
  876. * Save the request off for later processing. The request buffer looks
  877. * like this:
  878. *
  879. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  880. *
  881. * This code can only handle requests that consist of an xprt-header
  882. * and rpc-header.
  883. */
  884. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  885. {
  886. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  887. struct svc_deferred_req *dr;
  888. if (rqstp->rq_arg.page_len || !rqstp->rq_usedeferral)
  889. return NULL; /* if more than a page, give up FIXME */
  890. if (rqstp->rq_deferred) {
  891. dr = rqstp->rq_deferred;
  892. rqstp->rq_deferred = NULL;
  893. } else {
  894. size_t skip;
  895. size_t size;
  896. /* FIXME maybe discard if size too large */
  897. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  898. dr = kmalloc(size, GFP_KERNEL);
  899. if (dr == NULL)
  900. return NULL;
  901. dr->handle.owner = rqstp->rq_server;
  902. dr->prot = rqstp->rq_prot;
  903. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  904. dr->addrlen = rqstp->rq_addrlen;
  905. dr->daddr = rqstp->rq_daddr;
  906. dr->argslen = rqstp->rq_arg.len >> 2;
  907. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  908. /* back up head to the start of the buffer and copy */
  909. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  910. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  911. dr->argslen << 2);
  912. }
  913. svc_xprt_get(rqstp->rq_xprt);
  914. dr->xprt = rqstp->rq_xprt;
  915. rqstp->rq_dropme = true;
  916. dr->handle.revisit = svc_revisit;
  917. return &dr->handle;
  918. }
  919. /*
  920. * recv data from a deferred request into an active one
  921. */
  922. static int svc_deferred_recv(struct svc_rqst *rqstp)
  923. {
  924. struct svc_deferred_req *dr = rqstp->rq_deferred;
  925. /* setup iov_base past transport header */
  926. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  927. /* The iov_len does not include the transport header bytes */
  928. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  929. rqstp->rq_arg.page_len = 0;
  930. /* The rq_arg.len includes the transport header bytes */
  931. rqstp->rq_arg.len = dr->argslen<<2;
  932. rqstp->rq_prot = dr->prot;
  933. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  934. rqstp->rq_addrlen = dr->addrlen;
  935. /* Save off transport header len in case we get deferred again */
  936. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  937. rqstp->rq_daddr = dr->daddr;
  938. rqstp->rq_respages = rqstp->rq_pages;
  939. return (dr->argslen<<2) - dr->xprt_hlen;
  940. }
  941. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  942. {
  943. struct svc_deferred_req *dr = NULL;
  944. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  945. return NULL;
  946. spin_lock(&xprt->xpt_lock);
  947. if (!list_empty(&xprt->xpt_deferred)) {
  948. dr = list_entry(xprt->xpt_deferred.next,
  949. struct svc_deferred_req,
  950. handle.recent);
  951. list_del_init(&dr->handle.recent);
  952. } else
  953. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  954. spin_unlock(&xprt->xpt_lock);
  955. return dr;
  956. }
  957. /**
  958. * svc_find_xprt - find an RPC transport instance
  959. * @serv: pointer to svc_serv to search
  960. * @xcl_name: C string containing transport's class name
  961. * @net: owner net pointer
  962. * @af: Address family of transport's local address
  963. * @port: transport's IP port number
  964. *
  965. * Return the transport instance pointer for the endpoint accepting
  966. * connections/peer traffic from the specified transport class,
  967. * address family and port.
  968. *
  969. * Specifying 0 for the address family or port is effectively a
  970. * wild-card, and will result in matching the first transport in the
  971. * service's list that has a matching class name.
  972. */
  973. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  974. struct net *net, const sa_family_t af,
  975. const unsigned short port)
  976. {
  977. struct svc_xprt *xprt;
  978. struct svc_xprt *found = NULL;
  979. /* Sanity check the args */
  980. if (serv == NULL || xcl_name == NULL)
  981. return found;
  982. spin_lock_bh(&serv->sv_lock);
  983. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  984. if (xprt->xpt_net != net)
  985. continue;
  986. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  987. continue;
  988. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  989. continue;
  990. if (port != 0 && port != svc_xprt_local_port(xprt))
  991. continue;
  992. found = xprt;
  993. svc_xprt_get(xprt);
  994. break;
  995. }
  996. spin_unlock_bh(&serv->sv_lock);
  997. return found;
  998. }
  999. EXPORT_SYMBOL_GPL(svc_find_xprt);
  1000. static int svc_one_xprt_name(const struct svc_xprt *xprt,
  1001. char *pos, int remaining)
  1002. {
  1003. int len;
  1004. len = snprintf(pos, remaining, "%s %u\n",
  1005. xprt->xpt_class->xcl_name,
  1006. svc_xprt_local_port(xprt));
  1007. if (len >= remaining)
  1008. return -ENAMETOOLONG;
  1009. return len;
  1010. }
  1011. /**
  1012. * svc_xprt_names - format a buffer with a list of transport names
  1013. * @serv: pointer to an RPC service
  1014. * @buf: pointer to a buffer to be filled in
  1015. * @buflen: length of buffer to be filled in
  1016. *
  1017. * Fills in @buf with a string containing a list of transport names,
  1018. * each name terminated with '\n'.
  1019. *
  1020. * Returns positive length of the filled-in string on success; otherwise
  1021. * a negative errno value is returned if an error occurs.
  1022. */
  1023. int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
  1024. {
  1025. struct svc_xprt *xprt;
  1026. int len, totlen;
  1027. char *pos;
  1028. /* Sanity check args */
  1029. if (!serv)
  1030. return 0;
  1031. spin_lock_bh(&serv->sv_lock);
  1032. pos = buf;
  1033. totlen = 0;
  1034. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1035. len = svc_one_xprt_name(xprt, pos, buflen - totlen);
  1036. if (len < 0) {
  1037. *buf = '\0';
  1038. totlen = len;
  1039. }
  1040. if (len <= 0)
  1041. break;
  1042. pos += len;
  1043. totlen += len;
  1044. }
  1045. spin_unlock_bh(&serv->sv_lock);
  1046. return totlen;
  1047. }
  1048. EXPORT_SYMBOL_GPL(svc_xprt_names);
  1049. /*----------------------------------------------------------------------------*/
  1050. static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
  1051. {
  1052. unsigned int pidx = (unsigned int)*pos;
  1053. struct svc_serv *serv = m->private;
  1054. dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
  1055. if (!pidx)
  1056. return SEQ_START_TOKEN;
  1057. return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
  1058. }
  1059. static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
  1060. {
  1061. struct svc_pool *pool = p;
  1062. struct svc_serv *serv = m->private;
  1063. dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
  1064. if (p == SEQ_START_TOKEN) {
  1065. pool = &serv->sv_pools[0];
  1066. } else {
  1067. unsigned int pidx = (pool - &serv->sv_pools[0]);
  1068. if (pidx < serv->sv_nrpools-1)
  1069. pool = &serv->sv_pools[pidx+1];
  1070. else
  1071. pool = NULL;
  1072. }
  1073. ++*pos;
  1074. return pool;
  1075. }
  1076. static void svc_pool_stats_stop(struct seq_file *m, void *p)
  1077. {
  1078. }
  1079. static int svc_pool_stats_show(struct seq_file *m, void *p)
  1080. {
  1081. struct svc_pool *pool = p;
  1082. if (p == SEQ_START_TOKEN) {
  1083. seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
  1084. return 0;
  1085. }
  1086. seq_printf(m, "%u %lu %lu %lu %lu\n",
  1087. pool->sp_id,
  1088. pool->sp_stats.packets,
  1089. pool->sp_stats.sockets_queued,
  1090. pool->sp_stats.threads_woken,
  1091. pool->sp_stats.threads_timedout);
  1092. return 0;
  1093. }
  1094. static const struct seq_operations svc_pool_stats_seq_ops = {
  1095. .start = svc_pool_stats_start,
  1096. .next = svc_pool_stats_next,
  1097. .stop = svc_pool_stats_stop,
  1098. .show = svc_pool_stats_show,
  1099. };
  1100. int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
  1101. {
  1102. int err;
  1103. err = seq_open(file, &svc_pool_stats_seq_ops);
  1104. if (!err)
  1105. ((struct seq_file *) file->private_data)->private = serv;
  1106. return err;
  1107. }
  1108. EXPORT_SYMBOL(svc_pool_stats_open);
  1109. /*----------------------------------------------------------------------------*/