svc_xprt.c 29 KB

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