svc_xprt.c 39 KB

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