svc_xprt.c 29 KB

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