svc_xprt.c 36 KB

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