svc_xprt.c 34 KB

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