svc_xprt.c 28 KB

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