svc_xprt.c 37 KB

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