svc_xprt.c 29 KB

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