svc_xprt.c 39 KB

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