svc_xprt.c 35 KB

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