svc_xprt.c 30 KB

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