svc_xprt.c 28 KB

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