svc_xprt.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. 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. int svc_port_is_privileged(struct sockaddr *sin)
  437. {
  438. switch (sin->sa_family) {
  439. case AF_INET:
  440. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  441. < PROT_SOCK;
  442. case AF_INET6:
  443. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  444. < PROT_SOCK;
  445. default:
  446. return 0;
  447. }
  448. }
  449. /*
  450. * Make sure that we don't have too many active connections. If we
  451. * have, something must be dropped.
  452. *
  453. * There's no point in trying to do random drop here for DoS
  454. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  455. * attacker can easily beat that.
  456. *
  457. * The only somewhat efficient mechanism would be if drop old
  458. * connections from the same IP first. But right now we don't even
  459. * record the client IP in svc_sock.
  460. */
  461. static void svc_check_conn_limits(struct svc_serv *serv)
  462. {
  463. if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
  464. struct svc_xprt *xprt = NULL;
  465. spin_lock_bh(&serv->sv_lock);
  466. if (!list_empty(&serv->sv_tempsocks)) {
  467. if (net_ratelimit()) {
  468. /* Try to help the admin */
  469. printk(KERN_NOTICE "%s: too many open "
  470. "connections, consider increasing the "
  471. "number of nfsd threads\n",
  472. serv->sv_name);
  473. }
  474. /*
  475. * Always select the oldest connection. It's not fair,
  476. * but so is life
  477. */
  478. xprt = list_entry(serv->sv_tempsocks.prev,
  479. struct svc_xprt,
  480. xpt_list);
  481. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  482. svc_xprt_get(xprt);
  483. }
  484. spin_unlock_bh(&serv->sv_lock);
  485. if (xprt) {
  486. svc_xprt_enqueue(xprt);
  487. svc_xprt_put(xprt);
  488. }
  489. }
  490. }
  491. /*
  492. * Receive the next request on any transport. This code is carefully
  493. * organised not to touch any cachelines in the shared svc_serv
  494. * structure, only cachelines in the local svc_pool.
  495. */
  496. int svc_recv(struct svc_rqst *rqstp, long timeout)
  497. {
  498. struct svc_xprt *xprt = NULL;
  499. struct svc_serv *serv = rqstp->rq_server;
  500. struct svc_pool *pool = rqstp->rq_pool;
  501. int len, i;
  502. int pages;
  503. struct xdr_buf *arg;
  504. DECLARE_WAITQUEUE(wait, current);
  505. dprintk("svc: server %p waiting for data (to = %ld)\n",
  506. rqstp, timeout);
  507. if (rqstp->rq_xprt)
  508. printk(KERN_ERR
  509. "svc_recv: service %p, transport not NULL!\n",
  510. rqstp);
  511. if (waitqueue_active(&rqstp->rq_wait))
  512. printk(KERN_ERR
  513. "svc_recv: service %p, wait queue active!\n",
  514. rqstp);
  515. /* now allocate needed pages. If we get a failure, sleep briefly */
  516. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  517. for (i = 0; i < pages ; i++)
  518. while (rqstp->rq_pages[i] == NULL) {
  519. struct page *p = alloc_page(GFP_KERNEL);
  520. if (!p) {
  521. int j = msecs_to_jiffies(500);
  522. schedule_timeout_uninterruptible(j);
  523. }
  524. rqstp->rq_pages[i] = p;
  525. }
  526. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  527. BUG_ON(pages >= RPCSVC_MAXPAGES);
  528. /* Make arg->head point to first page and arg->pages point to rest */
  529. arg = &rqstp->rq_arg;
  530. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  531. arg->head[0].iov_len = PAGE_SIZE;
  532. arg->pages = rqstp->rq_pages + 1;
  533. arg->page_base = 0;
  534. /* save at least one page for response */
  535. arg->page_len = (pages-2)*PAGE_SIZE;
  536. arg->len = (pages-1)*PAGE_SIZE;
  537. arg->tail[0].iov_len = 0;
  538. try_to_freeze();
  539. cond_resched();
  540. if (signalled())
  541. return -EINTR;
  542. spin_lock_bh(&pool->sp_lock);
  543. xprt = svc_xprt_dequeue(pool);
  544. if (xprt) {
  545. rqstp->rq_xprt = xprt;
  546. svc_xprt_get(xprt);
  547. rqstp->rq_reserved = serv->sv_max_mesg;
  548. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  549. } else {
  550. /* No data pending. Go to sleep */
  551. svc_thread_enqueue(pool, rqstp);
  552. /*
  553. * We have to be able to interrupt this wait
  554. * to bring down the daemons ...
  555. */
  556. set_current_state(TASK_INTERRUPTIBLE);
  557. add_wait_queue(&rqstp->rq_wait, &wait);
  558. spin_unlock_bh(&pool->sp_lock);
  559. schedule_timeout(timeout);
  560. try_to_freeze();
  561. spin_lock_bh(&pool->sp_lock);
  562. remove_wait_queue(&rqstp->rq_wait, &wait);
  563. xprt = rqstp->rq_xprt;
  564. if (!xprt) {
  565. svc_thread_dequeue(pool, rqstp);
  566. spin_unlock_bh(&pool->sp_lock);
  567. dprintk("svc: server %p, no data yet\n", rqstp);
  568. return signalled()? -EINTR : -EAGAIN;
  569. }
  570. }
  571. spin_unlock_bh(&pool->sp_lock);
  572. len = 0;
  573. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  574. dprintk("svc_recv: found XPT_CLOSE\n");
  575. svc_delete_xprt(xprt);
  576. } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  577. struct svc_xprt *newxpt;
  578. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  579. if (newxpt) {
  580. /*
  581. * We know this module_get will succeed because the
  582. * listener holds a reference too
  583. */
  584. __module_get(newxpt->xpt_class->xcl_owner);
  585. svc_check_conn_limits(xprt->xpt_server);
  586. spin_lock_bh(&serv->sv_lock);
  587. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  588. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  589. serv->sv_tmpcnt++;
  590. if (serv->sv_temptimer.function == NULL) {
  591. /* setup timer to age temp transports */
  592. setup_timer(&serv->sv_temptimer,
  593. svc_age_temp_xprts,
  594. (unsigned long)serv);
  595. mod_timer(&serv->sv_temptimer,
  596. jiffies + svc_conn_age_period * HZ);
  597. }
  598. spin_unlock_bh(&serv->sv_lock);
  599. svc_xprt_received(newxpt);
  600. }
  601. svc_xprt_received(xprt);
  602. } else {
  603. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  604. rqstp, pool->sp_id, xprt,
  605. atomic_read(&xprt->xpt_ref.refcount));
  606. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  607. if (rqstp->rq_deferred) {
  608. svc_xprt_received(xprt);
  609. len = svc_deferred_recv(rqstp);
  610. } else
  611. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  612. dprintk("svc: got len=%d\n", len);
  613. }
  614. /* No data, incomplete (TCP) read, or accept() */
  615. if (len == 0 || len == -EAGAIN) {
  616. rqstp->rq_res.len = 0;
  617. svc_xprt_release(rqstp);
  618. return -EAGAIN;
  619. }
  620. clear_bit(XPT_OLD, &xprt->xpt_flags);
  621. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  622. rqstp->rq_chandle.defer = svc_defer;
  623. if (serv->sv_stats)
  624. serv->sv_stats->netcnt++;
  625. return len;
  626. }
  627. /*
  628. * Drop request
  629. */
  630. void svc_drop(struct svc_rqst *rqstp)
  631. {
  632. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  633. svc_xprt_release(rqstp);
  634. }
  635. /*
  636. * Return reply to client.
  637. */
  638. int svc_send(struct svc_rqst *rqstp)
  639. {
  640. struct svc_xprt *xprt;
  641. int len;
  642. struct xdr_buf *xb;
  643. xprt = rqstp->rq_xprt;
  644. if (!xprt)
  645. return -EFAULT;
  646. /* release the receive skb before sending the reply */
  647. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  648. /* calculate over-all length */
  649. xb = &rqstp->rq_res;
  650. xb->len = xb->head[0].iov_len +
  651. xb->page_len +
  652. xb->tail[0].iov_len;
  653. /* Grab mutex to serialize outgoing data. */
  654. mutex_lock(&xprt->xpt_mutex);
  655. if (test_bit(XPT_DEAD, &xprt->xpt_flags))
  656. len = -ENOTCONN;
  657. else
  658. len = xprt->xpt_ops->xpo_sendto(rqstp);
  659. mutex_unlock(&xprt->xpt_mutex);
  660. svc_xprt_release(rqstp);
  661. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  662. return 0;
  663. return len;
  664. }
  665. /*
  666. * Timer function to close old temporary transports, using
  667. * a mark-and-sweep algorithm.
  668. */
  669. static void svc_age_temp_xprts(unsigned long closure)
  670. {
  671. struct svc_serv *serv = (struct svc_serv *)closure;
  672. struct svc_xprt *xprt;
  673. struct list_head *le, *next;
  674. LIST_HEAD(to_be_aged);
  675. dprintk("svc_age_temp_xprts\n");
  676. if (!spin_trylock_bh(&serv->sv_lock)) {
  677. /* busy, try again 1 sec later */
  678. dprintk("svc_age_temp_xprts: busy\n");
  679. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  680. return;
  681. }
  682. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  683. xprt = list_entry(le, struct svc_xprt, xpt_list);
  684. /* First time through, just mark it OLD. Second time
  685. * through, close it. */
  686. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  687. continue;
  688. if (atomic_read(&xprt->xpt_ref.refcount) > 1
  689. || test_bit(XPT_BUSY, &xprt->xpt_flags))
  690. continue;
  691. svc_xprt_get(xprt);
  692. list_move(le, &to_be_aged);
  693. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  694. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  695. }
  696. spin_unlock_bh(&serv->sv_lock);
  697. while (!list_empty(&to_be_aged)) {
  698. le = to_be_aged.next;
  699. /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
  700. list_del_init(le);
  701. xprt = list_entry(le, struct svc_xprt, xpt_list);
  702. dprintk("queuing xprt %p for closing\n", xprt);
  703. /* a thread will dequeue and close it soon */
  704. svc_xprt_enqueue(xprt);
  705. svc_xprt_put(xprt);
  706. }
  707. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  708. }
  709. /*
  710. * Remove a dead transport
  711. */
  712. void svc_delete_xprt(struct svc_xprt *xprt)
  713. {
  714. struct svc_serv *serv = xprt->xpt_server;
  715. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  716. xprt->xpt_ops->xpo_detach(xprt);
  717. spin_lock_bh(&serv->sv_lock);
  718. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  719. list_del_init(&xprt->xpt_list);
  720. /*
  721. * We used to delete the transport from whichever list
  722. * it's sk_xprt.xpt_ready node was on, but we don't actually
  723. * need to. This is because the only time we're called
  724. * while still attached to a queue, the queue itself
  725. * is about to be destroyed (in svc_destroy).
  726. */
  727. if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
  728. BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
  729. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  730. serv->sv_tmpcnt--;
  731. svc_xprt_put(xprt);
  732. }
  733. spin_unlock_bh(&serv->sv_lock);
  734. }
  735. void svc_close_xprt(struct svc_xprt *xprt)
  736. {
  737. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  738. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  739. /* someone else will have to effect the close */
  740. return;
  741. svc_xprt_get(xprt);
  742. svc_delete_xprt(xprt);
  743. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  744. svc_xprt_put(xprt);
  745. }
  746. EXPORT_SYMBOL_GPL(svc_close_xprt);
  747. void svc_close_all(struct list_head *xprt_list)
  748. {
  749. struct svc_xprt *xprt;
  750. struct svc_xprt *tmp;
  751. list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
  752. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  753. if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
  754. /* Waiting to be processed, but no threads left,
  755. * So just remove it from the waiting list
  756. */
  757. list_del_init(&xprt->xpt_ready);
  758. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  759. }
  760. svc_close_xprt(xprt);
  761. }
  762. }
  763. /*
  764. * Handle defer and revisit of requests
  765. */
  766. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  767. {
  768. struct svc_deferred_req *dr =
  769. container_of(dreq, struct svc_deferred_req, handle);
  770. struct svc_xprt *xprt = dr->xprt;
  771. if (too_many) {
  772. svc_xprt_put(xprt);
  773. kfree(dr);
  774. return;
  775. }
  776. dprintk("revisit queued\n");
  777. dr->xprt = NULL;
  778. spin_lock(&xprt->xpt_lock);
  779. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  780. spin_unlock(&xprt->xpt_lock);
  781. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  782. svc_xprt_enqueue(xprt);
  783. svc_xprt_put(xprt);
  784. }
  785. /*
  786. * Save the request off for later processing. The request buffer looks
  787. * like this:
  788. *
  789. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  790. *
  791. * This code can only handle requests that consist of an xprt-header
  792. * and rpc-header.
  793. */
  794. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  795. {
  796. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  797. struct svc_deferred_req *dr;
  798. if (rqstp->rq_arg.page_len)
  799. return NULL; /* if more than a page, give up FIXME */
  800. if (rqstp->rq_deferred) {
  801. dr = rqstp->rq_deferred;
  802. rqstp->rq_deferred = NULL;
  803. } else {
  804. size_t skip;
  805. size_t size;
  806. /* FIXME maybe discard if size too large */
  807. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  808. dr = kmalloc(size, GFP_KERNEL);
  809. if (dr == NULL)
  810. return NULL;
  811. dr->handle.owner = rqstp->rq_server;
  812. dr->prot = rqstp->rq_prot;
  813. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  814. dr->addrlen = rqstp->rq_addrlen;
  815. dr->daddr = rqstp->rq_daddr;
  816. dr->argslen = rqstp->rq_arg.len >> 2;
  817. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  818. /* back up head to the start of the buffer and copy */
  819. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  820. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  821. dr->argslen << 2);
  822. }
  823. svc_xprt_get(rqstp->rq_xprt);
  824. dr->xprt = rqstp->rq_xprt;
  825. dr->handle.revisit = svc_revisit;
  826. return &dr->handle;
  827. }
  828. /*
  829. * recv data from a deferred request into an active one
  830. */
  831. static int svc_deferred_recv(struct svc_rqst *rqstp)
  832. {
  833. struct svc_deferred_req *dr = rqstp->rq_deferred;
  834. /* setup iov_base past transport header */
  835. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  836. /* The iov_len does not include the transport header bytes */
  837. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  838. rqstp->rq_arg.page_len = 0;
  839. /* The rq_arg.len includes the transport header bytes */
  840. rqstp->rq_arg.len = dr->argslen<<2;
  841. rqstp->rq_prot = dr->prot;
  842. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  843. rqstp->rq_addrlen = dr->addrlen;
  844. /* Save off transport header len in case we get deferred again */
  845. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  846. rqstp->rq_daddr = dr->daddr;
  847. rqstp->rq_respages = rqstp->rq_pages;
  848. return (dr->argslen<<2) - dr->xprt_hlen;
  849. }
  850. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  851. {
  852. struct svc_deferred_req *dr = NULL;
  853. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  854. return NULL;
  855. spin_lock(&xprt->xpt_lock);
  856. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  857. if (!list_empty(&xprt->xpt_deferred)) {
  858. dr = list_entry(xprt->xpt_deferred.next,
  859. struct svc_deferred_req,
  860. handle.recent);
  861. list_del_init(&dr->handle.recent);
  862. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  863. }
  864. spin_unlock(&xprt->xpt_lock);
  865. return dr;
  866. }
  867. /*
  868. * Return the transport instance pointer for the endpoint accepting
  869. * connections/peer traffic from the specified transport class,
  870. * address family and port.
  871. *
  872. * Specifying 0 for the address family or port is effectively a
  873. * wild-card, and will result in matching the first transport in the
  874. * service's list that has a matching class name.
  875. */
  876. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, char *xcl_name,
  877. int af, int port)
  878. {
  879. struct svc_xprt *xprt;
  880. struct svc_xprt *found = NULL;
  881. /* Sanity check the args */
  882. if (!serv || !xcl_name)
  883. return found;
  884. spin_lock_bh(&serv->sv_lock);
  885. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  886. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  887. continue;
  888. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  889. continue;
  890. if (port && port != svc_xprt_local_port(xprt))
  891. continue;
  892. found = xprt;
  893. svc_xprt_get(xprt);
  894. break;
  895. }
  896. spin_unlock_bh(&serv->sv_lock);
  897. return found;
  898. }
  899. EXPORT_SYMBOL_GPL(svc_find_xprt);