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. static void svc_xprt_do_enqueue(struct svc_xprt *xprt);
  25. /* apparently the "standard" is that clients close
  26. * idle connections after 5 minutes, servers after
  27. * 6 minutes
  28. * http://www.connectathon.org/talks96/nfstcp.pdf
  29. */
  30. static int svc_conn_age_period = 6*60;
  31. /* List of registered transport classes */
  32. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  33. static LIST_HEAD(svc_xprt_class_list);
  34. /* SMP locking strategy:
  35. *
  36. * svc_pool->sp_lock protects most of the fields of that pool.
  37. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  38. * when both need to be taken (rare), svc_serv->sv_lock is first.
  39. * The "service mutex" protects svc_serv->sv_nrthread.
  40. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  41. * and the ->sk_info_authunix cache.
  42. *
  43. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  44. * enqueued multiply. During normal transport processing this bit
  45. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  46. * Providers should not manipulate this bit directly.
  47. *
  48. * Some flags can be set to certain values at any time
  49. * providing that certain rules are followed:
  50. *
  51. * XPT_CONN, XPT_DATA:
  52. * - Can be set or cleared at any time.
  53. * - After a set, svc_xprt_enqueue must be called to enqueue
  54. * the transport for processing.
  55. * - After a clear, the transport must be read/accepted.
  56. * If this succeeds, it must be set again.
  57. * XPT_CLOSE:
  58. * - Can set at any time. It is never cleared.
  59. * XPT_DEAD:
  60. * - Can only be set while XPT_BUSY is held which ensures
  61. * that no other thread will be using the transport or will
  62. * try to set XPT_DEAD.
  63. */
  64. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  65. {
  66. struct svc_xprt_class *cl;
  67. int res = -EEXIST;
  68. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  69. INIT_LIST_HEAD(&xcl->xcl_list);
  70. spin_lock(&svc_xprt_class_lock);
  71. /* Make sure there isn't already a class with the same name */
  72. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  73. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  74. goto out;
  75. }
  76. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  77. res = 0;
  78. out:
  79. spin_unlock(&svc_xprt_class_lock);
  80. return res;
  81. }
  82. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  83. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  84. {
  85. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  86. spin_lock(&svc_xprt_class_lock);
  87. list_del_init(&xcl->xcl_list);
  88. spin_unlock(&svc_xprt_class_lock);
  89. }
  90. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  91. /*
  92. * Format the transport list for printing
  93. */
  94. int svc_print_xprts(char *buf, int maxlen)
  95. {
  96. struct svc_xprt_class *xcl;
  97. char tmpstr[80];
  98. int len = 0;
  99. buf[0] = '\0';
  100. spin_lock(&svc_xprt_class_lock);
  101. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  102. int slen;
  103. sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
  104. slen = strlen(tmpstr);
  105. if (len + slen > maxlen)
  106. break;
  107. len += slen;
  108. strcat(buf, tmpstr);
  109. }
  110. spin_unlock(&svc_xprt_class_lock);
  111. return len;
  112. }
  113. static void svc_xprt_free(struct kref *kref)
  114. {
  115. struct svc_xprt *xprt =
  116. container_of(kref, struct svc_xprt, xpt_ref);
  117. struct module *owner = xprt->xpt_class->xcl_owner;
  118. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
  119. svcauth_unix_info_release(xprt);
  120. put_net(xprt->xpt_net);
  121. /* See comment on corresponding get in xs_setup_bc_tcp(): */
  122. if (xprt->xpt_bc_xprt)
  123. xprt_put(xprt->xpt_bc_xprt);
  124. xprt->xpt_ops->xpo_free(xprt);
  125. module_put(owner);
  126. }
  127. void svc_xprt_put(struct svc_xprt *xprt)
  128. {
  129. kref_put(&xprt->xpt_ref, svc_xprt_free);
  130. }
  131. EXPORT_SYMBOL_GPL(svc_xprt_put);
  132. /*
  133. * Called by transport drivers to initialize the transport independent
  134. * portion of the transport instance.
  135. */
  136. void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl,
  137. struct svc_xprt *xprt, struct svc_serv *serv)
  138. {
  139. memset(xprt, 0, sizeof(*xprt));
  140. xprt->xpt_class = xcl;
  141. xprt->xpt_ops = xcl->xcl_ops;
  142. kref_init(&xprt->xpt_ref);
  143. xprt->xpt_server = serv;
  144. INIT_LIST_HEAD(&xprt->xpt_list);
  145. INIT_LIST_HEAD(&xprt->xpt_ready);
  146. INIT_LIST_HEAD(&xprt->xpt_deferred);
  147. INIT_LIST_HEAD(&xprt->xpt_users);
  148. mutex_init(&xprt->xpt_mutex);
  149. spin_lock_init(&xprt->xpt_lock);
  150. set_bit(XPT_BUSY, &xprt->xpt_flags);
  151. rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending");
  152. xprt->xpt_net = get_net(net);
  153. }
  154. EXPORT_SYMBOL_GPL(svc_xprt_init);
  155. static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
  156. struct svc_serv *serv,
  157. struct net *net,
  158. const int family,
  159. const unsigned short port,
  160. int flags)
  161. {
  162. struct sockaddr_in sin = {
  163. .sin_family = AF_INET,
  164. .sin_addr.s_addr = htonl(INADDR_ANY),
  165. .sin_port = htons(port),
  166. };
  167. #if IS_ENABLED(CONFIG_IPV6)
  168. struct sockaddr_in6 sin6 = {
  169. .sin6_family = AF_INET6,
  170. .sin6_addr = IN6ADDR_ANY_INIT,
  171. .sin6_port = htons(port),
  172. };
  173. #endif
  174. struct sockaddr *sap;
  175. size_t len;
  176. switch (family) {
  177. case PF_INET:
  178. sap = (struct sockaddr *)&sin;
  179. len = sizeof(sin);
  180. break;
  181. #if IS_ENABLED(CONFIG_IPV6)
  182. case PF_INET6:
  183. sap = (struct sockaddr *)&sin6;
  184. len = sizeof(sin6);
  185. break;
  186. #endif
  187. default:
  188. return ERR_PTR(-EAFNOSUPPORT);
  189. }
  190. return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
  191. }
  192. /*
  193. * svc_xprt_received conditionally queues the transport for processing
  194. * by another thread. The caller must hold the XPT_BUSY bit and must
  195. * not thereafter touch transport data.
  196. *
  197. * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
  198. * insufficient) data.
  199. */
  200. static void svc_xprt_received(struct svc_xprt *xprt)
  201. {
  202. if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) {
  203. WARN_ONCE(1, "xprt=0x%p already busy!", xprt);
  204. return;
  205. }
  206. /* As soon as we clear busy, the xprt could be closed and
  207. * 'put', so we need a reference to call svc_xprt_do_enqueue with:
  208. */
  209. svc_xprt_get(xprt);
  210. smp_mb__before_atomic();
  211. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  212. svc_xprt_do_enqueue(xprt);
  213. svc_xprt_put(xprt);
  214. }
  215. void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new)
  216. {
  217. clear_bit(XPT_TEMP, &new->xpt_flags);
  218. spin_lock_bh(&serv->sv_lock);
  219. list_add(&new->xpt_list, &serv->sv_permsocks);
  220. spin_unlock_bh(&serv->sv_lock);
  221. svc_xprt_received(new);
  222. }
  223. int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
  224. struct net *net, const int family,
  225. const unsigned short port, int flags)
  226. {
  227. struct svc_xprt_class *xcl;
  228. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  229. spin_lock(&svc_xprt_class_lock);
  230. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  231. struct svc_xprt *newxprt;
  232. unsigned short newport;
  233. if (strcmp(xprt_name, xcl->xcl_name))
  234. continue;
  235. if (!try_module_get(xcl->xcl_owner))
  236. goto err;
  237. spin_unlock(&svc_xprt_class_lock);
  238. newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags);
  239. if (IS_ERR(newxprt)) {
  240. module_put(xcl->xcl_owner);
  241. return PTR_ERR(newxprt);
  242. }
  243. svc_add_new_perm_xprt(serv, newxprt);
  244. newport = svc_xprt_local_port(newxprt);
  245. return newport;
  246. }
  247. err:
  248. spin_unlock(&svc_xprt_class_lock);
  249. dprintk("svc: transport %s not found\n", xprt_name);
  250. /* This errno is exposed to user space. Provide a reasonable
  251. * perror msg for a bad transport. */
  252. return -EPROTONOSUPPORT;
  253. }
  254. EXPORT_SYMBOL_GPL(svc_create_xprt);
  255. /*
  256. * Copy the local and remote xprt addresses to the rqstp structure
  257. */
  258. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  259. {
  260. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  261. rqstp->rq_addrlen = xprt->xpt_remotelen;
  262. /*
  263. * Destination address in request is needed for binding the
  264. * source address in RPC replies/callbacks later.
  265. */
  266. memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen);
  267. rqstp->rq_daddrlen = xprt->xpt_locallen;
  268. }
  269. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  270. /**
  271. * svc_print_addr - Format rq_addr field for printing
  272. * @rqstp: svc_rqst struct containing address to print
  273. * @buf: target buffer for formatted address
  274. * @len: length of target buffer
  275. *
  276. */
  277. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  278. {
  279. return __svc_print_addr(svc_addr(rqstp), buf, len);
  280. }
  281. EXPORT_SYMBOL_GPL(svc_print_addr);
  282. static bool svc_xprt_has_something_to_do(struct svc_xprt *xprt)
  283. {
  284. if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE)))
  285. return true;
  286. if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED)))
  287. return xprt->xpt_ops->xpo_has_wspace(xprt);
  288. return false;
  289. }
  290. static void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  291. {
  292. struct svc_pool *pool;
  293. struct svc_rqst *rqstp = NULL;
  294. int cpu;
  295. bool queued = false;
  296. if (!svc_xprt_has_something_to_do(xprt))
  297. goto out;
  298. /* Mark transport as busy. It will remain in this state until
  299. * the provider calls svc_xprt_received. We update XPT_BUSY
  300. * atomically because it also guards against trying to enqueue
  301. * the transport twice.
  302. */
  303. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
  304. /* Don't enqueue transport while already enqueued */
  305. dprintk("svc: transport %p busy, not enqueued\n", xprt);
  306. goto out;
  307. }
  308. cpu = get_cpu();
  309. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  310. atomic_long_inc(&pool->sp_stats.packets);
  311. redo_search:
  312. /* find a thread for this xprt */
  313. rcu_read_lock();
  314. list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) {
  315. /* Do a lockless check first */
  316. if (test_bit(RQ_BUSY, &rqstp->rq_flags))
  317. continue;
  318. /*
  319. * Once the xprt has been queued, it can only be dequeued by
  320. * the task that intends to service it. All we can do at that
  321. * point is to try to wake this thread back up so that it can
  322. * do so.
  323. */
  324. if (!queued) {
  325. spin_lock_bh(&rqstp->rq_lock);
  326. if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) {
  327. /* already busy, move on... */
  328. spin_unlock_bh(&rqstp->rq_lock);
  329. continue;
  330. }
  331. /* this one will do */
  332. rqstp->rq_xprt = xprt;
  333. svc_xprt_get(xprt);
  334. spin_unlock_bh(&rqstp->rq_lock);
  335. }
  336. rcu_read_unlock();
  337. atomic_long_inc(&pool->sp_stats.threads_woken);
  338. wake_up_process(rqstp->rq_task);
  339. put_cpu();
  340. goto out;
  341. }
  342. rcu_read_unlock();
  343. /*
  344. * We didn't find an idle thread to use, so we need to queue the xprt.
  345. * Do so and then search again. If we find one, we can't hook this one
  346. * up to it directly but we can wake the thread up in the hopes that it
  347. * will pick it up once it searches for a xprt to service.
  348. */
  349. if (!queued) {
  350. queued = true;
  351. dprintk("svc: transport %p put into queue\n", xprt);
  352. spin_lock_bh(&pool->sp_lock);
  353. list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
  354. pool->sp_stats.sockets_queued++;
  355. spin_unlock_bh(&pool->sp_lock);
  356. goto redo_search;
  357. }
  358. rqstp = NULL;
  359. put_cpu();
  360. out:
  361. trace_svc_xprt_do_enqueue(xprt, rqstp);
  362. }
  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. svc_xprt_do_enqueue(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. space += rqstp->rq_res.head[0].iov_len;
  410. if (space < rqstp->rq_reserved) {
  411. struct svc_xprt *xprt = rqstp->rq_xprt;
  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(&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. /*----------------------------------------------------------------------------*/