svc_xprt.c 29 KB

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