svc_xprt.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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 <net/sock.h>
  11. #include <linux/sunrpc/stats.h>
  12. #include <linux/sunrpc/svc_xprt.h>
  13. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  14. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
  15. static int svc_deferred_recv(struct svc_rqst *rqstp);
  16. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  17. static void svc_age_temp_xprts(unsigned long closure);
  18. /* apparently the "standard" is that clients close
  19. * idle connections after 5 minutes, servers after
  20. * 6 minutes
  21. * http://www.connectathon.org/talks96/nfstcp.pdf
  22. */
  23. static int svc_conn_age_period = 6*60;
  24. /* List of registered transport classes */
  25. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  26. static LIST_HEAD(svc_xprt_class_list);
  27. /* SMP locking strategy:
  28. *
  29. * svc_pool->sp_lock protects most of the fields of that pool.
  30. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  31. * when both need to be taken (rare), svc_serv->sv_lock is first.
  32. * BKL protects svc_serv->sv_nrthread.
  33. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  34. * and the ->sk_info_authunix cache.
  35. *
  36. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  37. * enqueued multiply. During normal transport processing this bit
  38. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  39. * Providers should not manipulate this bit directly.
  40. *
  41. * Some flags can be set to certain values at any time
  42. * providing that certain rules are followed:
  43. *
  44. * XPT_CONN, XPT_DATA:
  45. * - Can be set or cleared at any time.
  46. * - After a set, svc_xprt_enqueue must be called to enqueue
  47. * the transport for processing.
  48. * - After a clear, the transport must be read/accepted.
  49. * If this succeeds, it must be set again.
  50. * XPT_CLOSE:
  51. * - Can set at any time. It is never cleared.
  52. * XPT_DEAD:
  53. * - Can only be set while XPT_BUSY is held which ensures
  54. * that no other thread will be using the transport or will
  55. * try to set XPT_DEAD.
  56. */
  57. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  58. {
  59. struct svc_xprt_class *cl;
  60. int res = -EEXIST;
  61. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  62. INIT_LIST_HEAD(&xcl->xcl_list);
  63. spin_lock(&svc_xprt_class_lock);
  64. /* Make sure there isn't already a class with the same name */
  65. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  66. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  67. goto out;
  68. }
  69. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  70. res = 0;
  71. out:
  72. spin_unlock(&svc_xprt_class_lock);
  73. return res;
  74. }
  75. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  76. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  77. {
  78. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  79. spin_lock(&svc_xprt_class_lock);
  80. list_del_init(&xcl->xcl_list);
  81. spin_unlock(&svc_xprt_class_lock);
  82. }
  83. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  84. /*
  85. * Format the transport list for printing
  86. */
  87. int svc_print_xprts(char *buf, int maxlen)
  88. {
  89. struct list_head *le;
  90. char tmpstr[80];
  91. int len = 0;
  92. buf[0] = '\0';
  93. spin_lock(&svc_xprt_class_lock);
  94. list_for_each(le, &svc_xprt_class_list) {
  95. int slen;
  96. struct svc_xprt_class *xcl =
  97. list_entry(le, struct svc_xprt_class, xcl_list);
  98. sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
  99. slen = strlen(tmpstr);
  100. if (len + slen > maxlen)
  101. break;
  102. len += slen;
  103. strcat(buf, tmpstr);
  104. }
  105. spin_unlock(&svc_xprt_class_lock);
  106. return len;
  107. }
  108. static void svc_xprt_free(struct kref *kref)
  109. {
  110. struct svc_xprt *xprt =
  111. container_of(kref, struct svc_xprt, xpt_ref);
  112. struct module *owner = xprt->xpt_class->xcl_owner;
  113. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
  114. && xprt->xpt_auth_cache != NULL)
  115. svcauth_unix_info_release(xprt->xpt_auth_cache);
  116. xprt->xpt_ops->xpo_free(xprt);
  117. module_put(owner);
  118. }
  119. void svc_xprt_put(struct svc_xprt *xprt)
  120. {
  121. kref_put(&xprt->xpt_ref, svc_xprt_free);
  122. }
  123. EXPORT_SYMBOL_GPL(svc_xprt_put);
  124. /*
  125. * Called by transport drivers to initialize the transport independent
  126. * portion of the transport instance.
  127. */
  128. void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
  129. struct svc_serv *serv)
  130. {
  131. memset(xprt, 0, sizeof(*xprt));
  132. xprt->xpt_class = xcl;
  133. xprt->xpt_ops = xcl->xcl_ops;
  134. kref_init(&xprt->xpt_ref);
  135. xprt->xpt_server = serv;
  136. INIT_LIST_HEAD(&xprt->xpt_list);
  137. INIT_LIST_HEAD(&xprt->xpt_ready);
  138. INIT_LIST_HEAD(&xprt->xpt_deferred);
  139. mutex_init(&xprt->xpt_mutex);
  140. spin_lock_init(&xprt->xpt_lock);
  141. set_bit(XPT_BUSY, &xprt->xpt_flags);
  142. }
  143. EXPORT_SYMBOL_GPL(svc_xprt_init);
  144. static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
  145. struct svc_serv *serv,
  146. const int family,
  147. const unsigned short port,
  148. int flags)
  149. {
  150. struct sockaddr_in sin = {
  151. .sin_family = AF_INET,
  152. .sin_addr.s_addr = htonl(INADDR_ANY),
  153. .sin_port = htons(port),
  154. };
  155. struct sockaddr_in6 sin6 = {
  156. .sin6_family = AF_INET6,
  157. .sin6_addr = IN6ADDR_ANY_INIT,
  158. .sin6_port = htons(port),
  159. };
  160. struct sockaddr *sap;
  161. size_t len;
  162. switch (family) {
  163. case PF_INET:
  164. sap = (struct sockaddr *)&sin;
  165. len = sizeof(sin);
  166. break;
  167. case PF_INET6:
  168. sap = (struct sockaddr *)&sin6;
  169. len = sizeof(sin6);
  170. break;
  171. default:
  172. return ERR_PTR(-EAFNOSUPPORT);
  173. }
  174. return xcl->xcl_ops->xpo_create(serv, sap, len, flags);
  175. }
  176. int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
  177. const int family, const unsigned short port,
  178. int flags)
  179. {
  180. struct svc_xprt_class *xcl;
  181. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  182. spin_lock(&svc_xprt_class_lock);
  183. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  184. struct svc_xprt *newxprt;
  185. if (strcmp(xprt_name, xcl->xcl_name))
  186. continue;
  187. if (!try_module_get(xcl->xcl_owner))
  188. goto err;
  189. spin_unlock(&svc_xprt_class_lock);
  190. newxprt = __svc_xpo_create(xcl, serv, family, port, flags);
  191. if (IS_ERR(newxprt)) {
  192. module_put(xcl->xcl_owner);
  193. return PTR_ERR(newxprt);
  194. }
  195. clear_bit(XPT_TEMP, &newxprt->xpt_flags);
  196. spin_lock_bh(&serv->sv_lock);
  197. list_add(&newxprt->xpt_list, &serv->sv_permsocks);
  198. spin_unlock_bh(&serv->sv_lock);
  199. clear_bit(XPT_BUSY, &newxprt->xpt_flags);
  200. return svc_xprt_local_port(newxprt);
  201. }
  202. err:
  203. spin_unlock(&svc_xprt_class_lock);
  204. dprintk("svc: transport %s not found\n", xprt_name);
  205. return -ENOENT;
  206. }
  207. EXPORT_SYMBOL_GPL(svc_create_xprt);
  208. /*
  209. * Copy the local and remote xprt addresses to the rqstp structure
  210. */
  211. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  212. {
  213. struct sockaddr *sin;
  214. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  215. rqstp->rq_addrlen = xprt->xpt_remotelen;
  216. /*
  217. * Destination address in request is needed for binding the
  218. * source address in RPC replies/callbacks later.
  219. */
  220. sin = (struct sockaddr *)&xprt->xpt_local;
  221. switch (sin->sa_family) {
  222. case AF_INET:
  223. rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
  224. break;
  225. case AF_INET6:
  226. rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
  227. break;
  228. }
  229. }
  230. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  231. /**
  232. * svc_print_addr - Format rq_addr field for printing
  233. * @rqstp: svc_rqst struct containing address to print
  234. * @buf: target buffer for formatted address
  235. * @len: length of target buffer
  236. *
  237. */
  238. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  239. {
  240. return __svc_print_addr(svc_addr(rqstp), buf, len);
  241. }
  242. EXPORT_SYMBOL_GPL(svc_print_addr);
  243. /*
  244. * Queue up an idle server thread. Must have pool->sp_lock held.
  245. * Note: this is really a stack rather than a queue, so that we only
  246. * use as many different threads as we need, and the rest don't pollute
  247. * the cache.
  248. */
  249. static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  250. {
  251. list_add(&rqstp->rq_list, &pool->sp_threads);
  252. }
  253. /*
  254. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  255. */
  256. static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  257. {
  258. list_del(&rqstp->rq_list);
  259. }
  260. /*
  261. * Queue up a transport with data pending. If there are idle nfsd
  262. * processes, wake 'em up.
  263. *
  264. */
  265. void svc_xprt_enqueue(struct svc_xprt *xprt)
  266. {
  267. struct svc_serv *serv = xprt->xpt_server;
  268. struct svc_pool *pool;
  269. struct svc_rqst *rqstp;
  270. int cpu;
  271. if (!(xprt->xpt_flags &
  272. ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
  273. return;
  274. cpu = get_cpu();
  275. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  276. put_cpu();
  277. spin_lock_bh(&pool->sp_lock);
  278. if (!list_empty(&pool->sp_threads) &&
  279. !list_empty(&pool->sp_sockets))
  280. printk(KERN_ERR
  281. "svc_xprt_enqueue: "
  282. "threads and transports both waiting??\n");
  283. if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  284. /* Don't enqueue dead transports */
  285. dprintk("svc: transport %p is dead, not enqueued\n", xprt);
  286. goto out_unlock;
  287. }
  288. /* Mark transport as busy. It will remain in this state until
  289. * the provider calls svc_xprt_received. We update XPT_BUSY
  290. * atomically because it also guards against trying to enqueue
  291. * the transport twice.
  292. */
  293. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
  294. /* Don't enqueue transport while already enqueued */
  295. dprintk("svc: transport %p busy, not enqueued\n", xprt);
  296. goto out_unlock;
  297. }
  298. BUG_ON(xprt->xpt_pool != NULL);
  299. xprt->xpt_pool = pool;
  300. /* Handle pending connection */
  301. if (test_bit(XPT_CONN, &xprt->xpt_flags))
  302. goto process;
  303. /* Handle close in-progress */
  304. if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
  305. goto process;
  306. /* Check if we have space to reply to a request */
  307. if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
  308. /* Don't enqueue while not enough space for reply */
  309. dprintk("svc: no write space, transport %p not enqueued\n",
  310. xprt);
  311. xprt->xpt_pool = NULL;
  312. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  313. goto out_unlock;
  314. }
  315. process:
  316. if (!list_empty(&pool->sp_threads)) {
  317. rqstp = list_entry(pool->sp_threads.next,
  318. struct svc_rqst,
  319. rq_list);
  320. dprintk("svc: transport %p served by daemon %p\n",
  321. xprt, rqstp);
  322. svc_thread_dequeue(pool, rqstp);
  323. if (rqstp->rq_xprt)
  324. printk(KERN_ERR
  325. "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
  326. rqstp, rqstp->rq_xprt);
  327. rqstp->rq_xprt = xprt;
  328. svc_xprt_get(xprt);
  329. rqstp->rq_reserved = serv->sv_max_mesg;
  330. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  331. BUG_ON(xprt->xpt_pool != pool);
  332. wake_up(&rqstp->rq_wait);
  333. } else {
  334. dprintk("svc: transport %p put into queue\n", xprt);
  335. list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
  336. BUG_ON(xprt->xpt_pool != pool);
  337. }
  338. out_unlock:
  339. spin_unlock_bh(&pool->sp_lock);
  340. }
  341. EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
  342. /*
  343. * Dequeue the first transport. Must be called with the pool->sp_lock held.
  344. */
  345. static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
  346. {
  347. struct svc_xprt *xprt;
  348. if (list_empty(&pool->sp_sockets))
  349. return NULL;
  350. xprt = list_entry(pool->sp_sockets.next,
  351. struct svc_xprt, xpt_ready);
  352. list_del_init(&xprt->xpt_ready);
  353. dprintk("svc: transport %p dequeued, inuse=%d\n",
  354. xprt, atomic_read(&xprt->xpt_ref.refcount));
  355. return xprt;
  356. }
  357. /*
  358. * svc_xprt_received conditionally queues the transport for processing
  359. * by another thread. The caller must hold the XPT_BUSY bit and must
  360. * not thereafter touch transport data.
  361. *
  362. * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
  363. * insufficient) data.
  364. */
  365. void svc_xprt_received(struct svc_xprt *xprt)
  366. {
  367. BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
  368. xprt->xpt_pool = NULL;
  369. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  370. svc_xprt_enqueue(xprt);
  371. }
  372. EXPORT_SYMBOL_GPL(svc_xprt_received);
  373. /**
  374. * svc_reserve - change the space reserved for the reply to a request.
  375. * @rqstp: The request in question
  376. * @space: new max space to reserve
  377. *
  378. * Each request reserves some space on the output queue of the transport
  379. * to make sure the reply fits. This function reduces that reserved
  380. * space to be the amount of space used already, plus @space.
  381. *
  382. */
  383. void svc_reserve(struct svc_rqst *rqstp, int space)
  384. {
  385. space += rqstp->rq_res.head[0].iov_len;
  386. if (space < rqstp->rq_reserved) {
  387. struct svc_xprt *xprt = rqstp->rq_xprt;
  388. atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
  389. rqstp->rq_reserved = space;
  390. svc_xprt_enqueue(xprt);
  391. }
  392. }
  393. EXPORT_SYMBOL_GPL(svc_reserve);
  394. static void svc_xprt_release(struct svc_rqst *rqstp)
  395. {
  396. struct svc_xprt *xprt = rqstp->rq_xprt;
  397. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  398. kfree(rqstp->rq_deferred);
  399. rqstp->rq_deferred = NULL;
  400. svc_free_res_pages(rqstp);
  401. rqstp->rq_res.page_len = 0;
  402. rqstp->rq_res.page_base = 0;
  403. /* Reset response buffer and release
  404. * the reservation.
  405. * But first, check that enough space was reserved
  406. * for the reply, otherwise we have a bug!
  407. */
  408. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  409. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  410. rqstp->rq_reserved,
  411. rqstp->rq_res.len);
  412. rqstp->rq_res.head[0].iov_len = 0;
  413. svc_reserve(rqstp, 0);
  414. rqstp->rq_xprt = NULL;
  415. svc_xprt_put(xprt);
  416. }
  417. /*
  418. * External function to wake up a server waiting for data
  419. * This really only makes sense for services like lockd
  420. * which have exactly one thread anyway.
  421. */
  422. void svc_wake_up(struct svc_serv *serv)
  423. {
  424. struct svc_rqst *rqstp;
  425. unsigned int i;
  426. struct svc_pool *pool;
  427. for (i = 0; i < serv->sv_nrpools; i++) {
  428. pool = &serv->sv_pools[i];
  429. spin_lock_bh(&pool->sp_lock);
  430. if (!list_empty(&pool->sp_threads)) {
  431. rqstp = list_entry(pool->sp_threads.next,
  432. struct svc_rqst,
  433. rq_list);
  434. dprintk("svc: daemon %p woken up.\n", rqstp);
  435. /*
  436. svc_thread_dequeue(pool, rqstp);
  437. rqstp->rq_xprt = NULL;
  438. */
  439. wake_up(&rqstp->rq_wait);
  440. }
  441. spin_unlock_bh(&pool->sp_lock);
  442. }
  443. }
  444. EXPORT_SYMBOL_GPL(svc_wake_up);
  445. int svc_port_is_privileged(struct sockaddr *sin)
  446. {
  447. switch (sin->sa_family) {
  448. case AF_INET:
  449. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  450. < PROT_SOCK;
  451. case AF_INET6:
  452. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  453. < PROT_SOCK;
  454. default:
  455. return 0;
  456. }
  457. }
  458. /*
  459. * Make sure that we don't have too many active connections. If we have,
  460. * something must be dropped. It's not clear what will happen if we allow
  461. * "too many" connections, but when dealing with network-facing software,
  462. * we have to code defensively. Here we do that by imposing hard limits.
  463. *
  464. * There's no point in trying to do random drop here for DoS
  465. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  466. * attacker can easily beat that.
  467. *
  468. * The only somewhat efficient mechanism would be if drop old
  469. * connections from the same IP first. But right now we don't even
  470. * record the client IP in svc_sock.
  471. *
  472. * single-threaded services that expect a lot of clients will probably
  473. * need to set sv_maxconn to override the default value which is based
  474. * on the number of threads
  475. */
  476. static void svc_check_conn_limits(struct svc_serv *serv)
  477. {
  478. unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
  479. (serv->sv_nrthreads+3) * 20;
  480. if (serv->sv_tmpcnt > limit) {
  481. struct svc_xprt *xprt = NULL;
  482. spin_lock_bh(&serv->sv_lock);
  483. if (!list_empty(&serv->sv_tempsocks)) {
  484. if (net_ratelimit()) {
  485. /* Try to help the admin */
  486. printk(KERN_NOTICE "%s: too many open "
  487. "connections, consider increasing %s\n",
  488. serv->sv_name, serv->sv_maxconn ?
  489. "the max number of connections." :
  490. "the number of threads.");
  491. }
  492. /*
  493. * Always select the oldest connection. It's not fair,
  494. * but so is life
  495. */
  496. xprt = list_entry(serv->sv_tempsocks.prev,
  497. struct svc_xprt,
  498. xpt_list);
  499. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  500. svc_xprt_get(xprt);
  501. }
  502. spin_unlock_bh(&serv->sv_lock);
  503. if (xprt) {
  504. svc_xprt_enqueue(xprt);
  505. svc_xprt_put(xprt);
  506. }
  507. }
  508. }
  509. /*
  510. * Receive the next request on any transport. This code is carefully
  511. * organised not to touch any cachelines in the shared svc_serv
  512. * structure, only cachelines in the local svc_pool.
  513. */
  514. int svc_recv(struct svc_rqst *rqstp, long timeout)
  515. {
  516. struct svc_xprt *xprt = NULL;
  517. struct svc_serv *serv = rqstp->rq_server;
  518. struct svc_pool *pool = rqstp->rq_pool;
  519. int len, i;
  520. int pages;
  521. struct xdr_buf *arg;
  522. DECLARE_WAITQUEUE(wait, current);
  523. dprintk("svc: server %p waiting for data (to = %ld)\n",
  524. rqstp, timeout);
  525. if (rqstp->rq_xprt)
  526. printk(KERN_ERR
  527. "svc_recv: service %p, transport not NULL!\n",
  528. rqstp);
  529. if (waitqueue_active(&rqstp->rq_wait))
  530. printk(KERN_ERR
  531. "svc_recv: service %p, wait queue active!\n",
  532. rqstp);
  533. /* now allocate needed pages. If we get a failure, sleep briefly */
  534. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  535. for (i = 0; i < pages ; i++)
  536. while (rqstp->rq_pages[i] == NULL) {
  537. struct page *p = alloc_page(GFP_KERNEL);
  538. if (!p) {
  539. set_current_state(TASK_INTERRUPTIBLE);
  540. if (signalled() || kthread_should_stop()) {
  541. set_current_state(TASK_RUNNING);
  542. return -EINTR;
  543. }
  544. schedule_timeout(msecs_to_jiffies(500));
  545. }
  546. rqstp->rq_pages[i] = p;
  547. }
  548. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  549. BUG_ON(pages >= RPCSVC_MAXPAGES);
  550. /* Make arg->head point to first page and arg->pages point to rest */
  551. arg = &rqstp->rq_arg;
  552. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  553. arg->head[0].iov_len = PAGE_SIZE;
  554. arg->pages = rqstp->rq_pages + 1;
  555. arg->page_base = 0;
  556. /* save at least one page for response */
  557. arg->page_len = (pages-2)*PAGE_SIZE;
  558. arg->len = (pages-1)*PAGE_SIZE;
  559. arg->tail[0].iov_len = 0;
  560. try_to_freeze();
  561. cond_resched();
  562. if (signalled() || kthread_should_stop())
  563. return -EINTR;
  564. spin_lock_bh(&pool->sp_lock);
  565. xprt = svc_xprt_dequeue(pool);
  566. if (xprt) {
  567. rqstp->rq_xprt = xprt;
  568. svc_xprt_get(xprt);
  569. rqstp->rq_reserved = serv->sv_max_mesg;
  570. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  571. } else {
  572. /* No data pending. Go to sleep */
  573. svc_thread_enqueue(pool, rqstp);
  574. /*
  575. * We have to be able to interrupt this wait
  576. * to bring down the daemons ...
  577. */
  578. set_current_state(TASK_INTERRUPTIBLE);
  579. /*
  580. * checking kthread_should_stop() here allows us to avoid
  581. * locking and signalling when stopping kthreads that call
  582. * svc_recv. If the thread has already been woken up, then
  583. * we can exit here without sleeping. If not, then it
  584. * it'll be woken up quickly during the schedule_timeout
  585. */
  586. if (kthread_should_stop()) {
  587. set_current_state(TASK_RUNNING);
  588. spin_unlock_bh(&pool->sp_lock);
  589. return -EINTR;
  590. }
  591. add_wait_queue(&rqstp->rq_wait, &wait);
  592. spin_unlock_bh(&pool->sp_lock);
  593. schedule_timeout(timeout);
  594. try_to_freeze();
  595. spin_lock_bh(&pool->sp_lock);
  596. remove_wait_queue(&rqstp->rq_wait, &wait);
  597. xprt = rqstp->rq_xprt;
  598. if (!xprt) {
  599. svc_thread_dequeue(pool, rqstp);
  600. spin_unlock_bh(&pool->sp_lock);
  601. dprintk("svc: server %p, no data yet\n", rqstp);
  602. if (signalled() || kthread_should_stop())
  603. return -EINTR;
  604. else
  605. return -EAGAIN;
  606. }
  607. }
  608. spin_unlock_bh(&pool->sp_lock);
  609. len = 0;
  610. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  611. dprintk("svc_recv: found XPT_CLOSE\n");
  612. svc_delete_xprt(xprt);
  613. } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  614. struct svc_xprt *newxpt;
  615. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  616. if (newxpt) {
  617. /*
  618. * We know this module_get will succeed because the
  619. * listener holds a reference too
  620. */
  621. __module_get(newxpt->xpt_class->xcl_owner);
  622. svc_check_conn_limits(xprt->xpt_server);
  623. spin_lock_bh(&serv->sv_lock);
  624. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  625. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  626. serv->sv_tmpcnt++;
  627. if (serv->sv_temptimer.function == NULL) {
  628. /* setup timer to age temp transports */
  629. setup_timer(&serv->sv_temptimer,
  630. svc_age_temp_xprts,
  631. (unsigned long)serv);
  632. mod_timer(&serv->sv_temptimer,
  633. jiffies + svc_conn_age_period * HZ);
  634. }
  635. spin_unlock_bh(&serv->sv_lock);
  636. svc_xprt_received(newxpt);
  637. }
  638. svc_xprt_received(xprt);
  639. } else {
  640. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  641. rqstp, pool->sp_id, xprt,
  642. atomic_read(&xprt->xpt_ref.refcount));
  643. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  644. if (rqstp->rq_deferred) {
  645. svc_xprt_received(xprt);
  646. len = svc_deferred_recv(rqstp);
  647. } else
  648. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  649. dprintk("svc: got len=%d\n", len);
  650. }
  651. /* No data, incomplete (TCP) read, or accept() */
  652. if (len == 0 || len == -EAGAIN) {
  653. rqstp->rq_res.len = 0;
  654. svc_xprt_release(rqstp);
  655. return -EAGAIN;
  656. }
  657. clear_bit(XPT_OLD, &xprt->xpt_flags);
  658. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  659. rqstp->rq_chandle.defer = svc_defer;
  660. if (serv->sv_stats)
  661. serv->sv_stats->netcnt++;
  662. return len;
  663. }
  664. EXPORT_SYMBOL_GPL(svc_recv);
  665. /*
  666. * Drop request
  667. */
  668. void svc_drop(struct svc_rqst *rqstp)
  669. {
  670. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  671. svc_xprt_release(rqstp);
  672. }
  673. EXPORT_SYMBOL_GPL(svc_drop);
  674. /*
  675. * Return reply to client.
  676. */
  677. int svc_send(struct svc_rqst *rqstp)
  678. {
  679. struct svc_xprt *xprt;
  680. int len;
  681. struct xdr_buf *xb;
  682. xprt = rqstp->rq_xprt;
  683. if (!xprt)
  684. return -EFAULT;
  685. /* release the receive skb before sending the reply */
  686. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  687. /* calculate over-all length */
  688. xb = &rqstp->rq_res;
  689. xb->len = xb->head[0].iov_len +
  690. xb->page_len +
  691. xb->tail[0].iov_len;
  692. /* Grab mutex to serialize outgoing data. */
  693. mutex_lock(&xprt->xpt_mutex);
  694. if (test_bit(XPT_DEAD, &xprt->xpt_flags))
  695. len = -ENOTCONN;
  696. else
  697. len = xprt->xpt_ops->xpo_sendto(rqstp);
  698. mutex_unlock(&xprt->xpt_mutex);
  699. svc_xprt_release(rqstp);
  700. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  701. return 0;
  702. return len;
  703. }
  704. /*
  705. * Timer function to close old temporary transports, using
  706. * a mark-and-sweep algorithm.
  707. */
  708. static void svc_age_temp_xprts(unsigned long closure)
  709. {
  710. struct svc_serv *serv = (struct svc_serv *)closure;
  711. struct svc_xprt *xprt;
  712. struct list_head *le, *next;
  713. LIST_HEAD(to_be_aged);
  714. dprintk("svc_age_temp_xprts\n");
  715. if (!spin_trylock_bh(&serv->sv_lock)) {
  716. /* busy, try again 1 sec later */
  717. dprintk("svc_age_temp_xprts: busy\n");
  718. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  719. return;
  720. }
  721. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  722. xprt = list_entry(le, struct svc_xprt, xpt_list);
  723. /* First time through, just mark it OLD. Second time
  724. * through, close it. */
  725. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  726. continue;
  727. if (atomic_read(&xprt->xpt_ref.refcount) > 1
  728. || test_bit(XPT_BUSY, &xprt->xpt_flags))
  729. continue;
  730. svc_xprt_get(xprt);
  731. list_move(le, &to_be_aged);
  732. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  733. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  734. }
  735. spin_unlock_bh(&serv->sv_lock);
  736. while (!list_empty(&to_be_aged)) {
  737. le = to_be_aged.next;
  738. /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
  739. list_del_init(le);
  740. xprt = list_entry(le, struct svc_xprt, xpt_list);
  741. dprintk("queuing xprt %p for closing\n", xprt);
  742. /* a thread will dequeue and close it soon */
  743. svc_xprt_enqueue(xprt);
  744. svc_xprt_put(xprt);
  745. }
  746. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  747. }
  748. /*
  749. * Remove a dead transport
  750. */
  751. void svc_delete_xprt(struct svc_xprt *xprt)
  752. {
  753. struct svc_serv *serv = xprt->xpt_server;
  754. struct svc_deferred_req *dr;
  755. /* Only do this once */
  756. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  757. return;
  758. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  759. xprt->xpt_ops->xpo_detach(xprt);
  760. spin_lock_bh(&serv->sv_lock);
  761. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  762. list_del_init(&xprt->xpt_list);
  763. /*
  764. * We used to delete the transport from whichever list
  765. * it's sk_xprt.xpt_ready node was on, but we don't actually
  766. * need to. This is because the only time we're called
  767. * while still attached to a queue, the queue itself
  768. * is about to be destroyed (in svc_destroy).
  769. */
  770. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  771. serv->sv_tmpcnt--;
  772. for (dr = svc_deferred_dequeue(xprt); dr;
  773. dr = svc_deferred_dequeue(xprt)) {
  774. svc_xprt_put(xprt);
  775. kfree(dr);
  776. }
  777. svc_xprt_put(xprt);
  778. spin_unlock_bh(&serv->sv_lock);
  779. }
  780. void svc_close_xprt(struct svc_xprt *xprt)
  781. {
  782. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  783. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  784. /* someone else will have to effect the close */
  785. return;
  786. svc_xprt_get(xprt);
  787. svc_delete_xprt(xprt);
  788. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  789. svc_xprt_put(xprt);
  790. }
  791. EXPORT_SYMBOL_GPL(svc_close_xprt);
  792. void svc_close_all(struct list_head *xprt_list)
  793. {
  794. struct svc_xprt *xprt;
  795. struct svc_xprt *tmp;
  796. list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
  797. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  798. if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
  799. /* Waiting to be processed, but no threads left,
  800. * So just remove it from the waiting list
  801. */
  802. list_del_init(&xprt->xpt_ready);
  803. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  804. }
  805. svc_close_xprt(xprt);
  806. }
  807. }
  808. /*
  809. * Handle defer and revisit of requests
  810. */
  811. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  812. {
  813. struct svc_deferred_req *dr =
  814. container_of(dreq, struct svc_deferred_req, handle);
  815. struct svc_xprt *xprt = dr->xprt;
  816. spin_lock(&xprt->xpt_lock);
  817. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  818. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  819. spin_unlock(&xprt->xpt_lock);
  820. dprintk("revisit canceled\n");
  821. svc_xprt_put(xprt);
  822. kfree(dr);
  823. return;
  824. }
  825. dprintk("revisit queued\n");
  826. dr->xprt = NULL;
  827. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  828. spin_unlock(&xprt->xpt_lock);
  829. svc_xprt_enqueue(xprt);
  830. svc_xprt_put(xprt);
  831. }
  832. /*
  833. * Save the request off for later processing. The request buffer looks
  834. * like this:
  835. *
  836. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  837. *
  838. * This code can only handle requests that consist of an xprt-header
  839. * and rpc-header.
  840. */
  841. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  842. {
  843. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  844. struct svc_deferred_req *dr;
  845. if (rqstp->rq_arg.page_len)
  846. return NULL; /* if more than a page, give up FIXME */
  847. if (rqstp->rq_deferred) {
  848. dr = rqstp->rq_deferred;
  849. rqstp->rq_deferred = NULL;
  850. } else {
  851. size_t skip;
  852. size_t size;
  853. /* FIXME maybe discard if size too large */
  854. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  855. dr = kmalloc(size, GFP_KERNEL);
  856. if (dr == NULL)
  857. return NULL;
  858. dr->handle.owner = rqstp->rq_server;
  859. dr->prot = rqstp->rq_prot;
  860. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  861. dr->addrlen = rqstp->rq_addrlen;
  862. dr->daddr = rqstp->rq_daddr;
  863. dr->argslen = rqstp->rq_arg.len >> 2;
  864. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  865. /* back up head to the start of the buffer and copy */
  866. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  867. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  868. dr->argslen << 2);
  869. }
  870. svc_xprt_get(rqstp->rq_xprt);
  871. dr->xprt = rqstp->rq_xprt;
  872. dr->handle.revisit = svc_revisit;
  873. return &dr->handle;
  874. }
  875. /*
  876. * recv data from a deferred request into an active one
  877. */
  878. static int svc_deferred_recv(struct svc_rqst *rqstp)
  879. {
  880. struct svc_deferred_req *dr = rqstp->rq_deferred;
  881. /* setup iov_base past transport header */
  882. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  883. /* The iov_len does not include the transport header bytes */
  884. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  885. rqstp->rq_arg.page_len = 0;
  886. /* The rq_arg.len includes the transport header bytes */
  887. rqstp->rq_arg.len = dr->argslen<<2;
  888. rqstp->rq_prot = dr->prot;
  889. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  890. rqstp->rq_addrlen = dr->addrlen;
  891. /* Save off transport header len in case we get deferred again */
  892. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  893. rqstp->rq_daddr = dr->daddr;
  894. rqstp->rq_respages = rqstp->rq_pages;
  895. return (dr->argslen<<2) - dr->xprt_hlen;
  896. }
  897. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  898. {
  899. struct svc_deferred_req *dr = NULL;
  900. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  901. return NULL;
  902. spin_lock(&xprt->xpt_lock);
  903. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  904. if (!list_empty(&xprt->xpt_deferred)) {
  905. dr = list_entry(xprt->xpt_deferred.next,
  906. struct svc_deferred_req,
  907. handle.recent);
  908. list_del_init(&dr->handle.recent);
  909. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  910. }
  911. spin_unlock(&xprt->xpt_lock);
  912. return dr;
  913. }
  914. /**
  915. * svc_find_xprt - find an RPC transport instance
  916. * @serv: pointer to svc_serv to search
  917. * @xcl_name: C string containing transport's class name
  918. * @af: Address family of transport's local address
  919. * @port: transport's IP port number
  920. *
  921. * Return the transport instance pointer for the endpoint accepting
  922. * connections/peer traffic from the specified transport class,
  923. * address family and port.
  924. *
  925. * Specifying 0 for the address family or port is effectively a
  926. * wild-card, and will result in matching the first transport in the
  927. * service's list that has a matching class name.
  928. */
  929. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  930. const sa_family_t af, const unsigned short port)
  931. {
  932. struct svc_xprt *xprt;
  933. struct svc_xprt *found = NULL;
  934. /* Sanity check the args */
  935. if (serv == NULL || xcl_name == NULL)
  936. return found;
  937. spin_lock_bh(&serv->sv_lock);
  938. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  939. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  940. continue;
  941. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  942. continue;
  943. if (port != 0 && port != svc_xprt_local_port(xprt))
  944. continue;
  945. found = xprt;
  946. svc_xprt_get(xprt);
  947. break;
  948. }
  949. spin_unlock_bh(&serv->sv_lock);
  950. return found;
  951. }
  952. EXPORT_SYMBOL_GPL(svc_find_xprt);
  953. /*
  954. * Format a buffer with a list of the active transports. A zero for
  955. * the buflen parameter disables target buffer overflow checking.
  956. */
  957. int svc_xprt_names(struct svc_serv *serv, char *buf, int buflen)
  958. {
  959. struct svc_xprt *xprt;
  960. char xprt_str[64];
  961. int totlen = 0;
  962. int len;
  963. /* Sanity check args */
  964. if (!serv)
  965. return 0;
  966. spin_lock_bh(&serv->sv_lock);
  967. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  968. len = snprintf(xprt_str, sizeof(xprt_str),
  969. "%s %d\n", xprt->xpt_class->xcl_name,
  970. svc_xprt_local_port(xprt));
  971. /* If the string was truncated, replace with error string */
  972. if (len >= sizeof(xprt_str))
  973. strcpy(xprt_str, "name-too-long\n");
  974. /* Don't overflow buffer */
  975. len = strlen(xprt_str);
  976. if (buflen && (len + totlen >= buflen))
  977. break;
  978. strcpy(buf+totlen, xprt_str);
  979. totlen += len;
  980. }
  981. spin_unlock_bh(&serv->sv_lock);
  982. return totlen;
  983. }
  984. EXPORT_SYMBOL_GPL(svc_xprt_names);