svcsock.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /*
  2. * linux/net/sunrpc/svcsock.c
  3. *
  4. * These are the RPC server socket internals.
  5. *
  6. * The server scheduling algorithm does not always distribute the load
  7. * evenly when servicing a single client. May need to modify the
  8. * svc_sock_enqueue procedure...
  9. *
  10. * TCP support is largely untested and may be a little slow. The problem
  11. * is that we currently do two separate recvfrom's, one for the 4-byte
  12. * record length, and the second for the actual record. This could possibly
  13. * be improved by always reading a minimum size of around 100 bytes and
  14. * tucking any superfluous bytes away in a temporary store. Still, that
  15. * leaves write requests out in the rain. An alternative may be to peek at
  16. * the first skb in the queue, and if it matches the next TCP sequence
  17. * number, to extract the record marker. Yuck.
  18. *
  19. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/errno.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/net.h>
  26. #include <linux/in.h>
  27. #include <linux/inet.h>
  28. #include <linux/udp.h>
  29. #include <linux/tcp.h>
  30. #include <linux/unistd.h>
  31. #include <linux/slab.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/file.h>
  35. #include <linux/freezer.h>
  36. #include <net/sock.h>
  37. #include <net/checksum.h>
  38. #include <net/ip.h>
  39. #include <net/ipv6.h>
  40. #include <net/tcp_states.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/ioctls.h>
  43. #include <linux/sunrpc/types.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/sunrpc/xdr.h>
  46. #include <linux/sunrpc/svcsock.h>
  47. #include <linux/sunrpc/stats.h>
  48. /* SMP locking strategy:
  49. *
  50. * svc_pool->sp_lock protects most of the fields of that pool.
  51. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  52. * when both need to be taken (rare), svc_serv->sv_lock is first.
  53. * BKL protects svc_serv->sv_nrthread.
  54. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  55. * and the ->sk_info_authunix cache.
  56. * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being
  57. * enqueued multiply.
  58. *
  59. * Some flags can be set to certain values at any time
  60. * providing that certain rules are followed:
  61. *
  62. * XPT_CONN, XPT_DATA, can be set or cleared at any time.
  63. * after a set, svc_sock_enqueue must be called.
  64. * after a clear, the socket must be read/accepted
  65. * if this succeeds, it must be set again.
  66. * XPT_CLOSE can set at any time. It is never cleared.
  67. * xpt_ref contains a bias of '1' until XPT_DEAD is set.
  68. * so when xprt_ref hits zero, we know the transport is dead
  69. * and no-one is using it.
  70. * XPT_DEAD can only be set while XPT_BUSY is held which ensures
  71. * no other thread will be using the socket or will try to
  72. * set XPT_DEAD.
  73. *
  74. */
  75. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  76. static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
  77. int *errp, int flags);
  78. static void svc_delete_socket(struct svc_sock *svsk);
  79. static void svc_udp_data_ready(struct sock *, int);
  80. static int svc_udp_recvfrom(struct svc_rqst *);
  81. static int svc_udp_sendto(struct svc_rqst *);
  82. static void svc_close_socket(struct svc_sock *svsk);
  83. static void svc_sock_detach(struct svc_xprt *);
  84. static void svc_sock_free(struct svc_xprt *);
  85. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
  86. static int svc_deferred_recv(struct svc_rqst *rqstp);
  87. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  88. static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
  89. struct sockaddr *, int, int);
  90. /* apparently the "standard" is that clients close
  91. * idle connections after 5 minutes, servers after
  92. * 6 minutes
  93. * http://www.connectathon.org/talks96/nfstcp.pdf
  94. */
  95. static int svc_conn_age_period = 6*60;
  96. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  97. static struct lock_class_key svc_key[2];
  98. static struct lock_class_key svc_slock_key[2];
  99. static inline void svc_reclassify_socket(struct socket *sock)
  100. {
  101. struct sock *sk = sock->sk;
  102. BUG_ON(sock_owned_by_user(sk));
  103. switch (sk->sk_family) {
  104. case AF_INET:
  105. sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
  106. &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
  107. break;
  108. case AF_INET6:
  109. sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
  110. &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
  111. break;
  112. default:
  113. BUG();
  114. }
  115. }
  116. #else
  117. static inline void svc_reclassify_socket(struct socket *sock)
  118. {
  119. }
  120. #endif
  121. static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
  122. {
  123. switch (addr->sa_family) {
  124. case AF_INET:
  125. snprintf(buf, len, "%u.%u.%u.%u, port=%u",
  126. NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
  127. ntohs(((struct sockaddr_in *) addr)->sin_port));
  128. break;
  129. case AF_INET6:
  130. snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
  131. NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
  132. ntohs(((struct sockaddr_in6 *) addr)->sin6_port));
  133. break;
  134. default:
  135. snprintf(buf, len, "unknown address type: %d", addr->sa_family);
  136. break;
  137. }
  138. return buf;
  139. }
  140. /**
  141. * svc_print_addr - Format rq_addr field for printing
  142. * @rqstp: svc_rqst struct containing address to print
  143. * @buf: target buffer for formatted address
  144. * @len: length of target buffer
  145. *
  146. */
  147. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  148. {
  149. return __svc_print_addr(svc_addr(rqstp), buf, len);
  150. }
  151. EXPORT_SYMBOL_GPL(svc_print_addr);
  152. /*
  153. * Queue up an idle server thread. Must have pool->sp_lock held.
  154. * Note: this is really a stack rather than a queue, so that we only
  155. * use as many different threads as we need, and the rest don't pollute
  156. * the cache.
  157. */
  158. static inline void
  159. svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  160. {
  161. list_add(&rqstp->rq_list, &pool->sp_threads);
  162. }
  163. /*
  164. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  165. */
  166. static inline void
  167. svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  168. {
  169. list_del(&rqstp->rq_list);
  170. }
  171. /*
  172. * Release an skbuff after use
  173. */
  174. static void svc_release_skb(struct svc_rqst *rqstp)
  175. {
  176. struct sk_buff *skb = rqstp->rq_xprt_ctxt;
  177. struct svc_deferred_req *dr = rqstp->rq_deferred;
  178. if (skb) {
  179. rqstp->rq_xprt_ctxt = NULL;
  180. dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
  181. skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
  182. }
  183. if (dr) {
  184. rqstp->rq_deferred = NULL;
  185. kfree(dr);
  186. }
  187. }
  188. /*
  189. * Queue up a socket with data pending. If there are idle nfsd
  190. * processes, wake 'em up.
  191. *
  192. */
  193. static void
  194. svc_sock_enqueue(struct svc_sock *svsk)
  195. {
  196. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  197. struct svc_pool *pool;
  198. struct svc_rqst *rqstp;
  199. int cpu;
  200. if (!(svsk->sk_xprt.xpt_flags &
  201. ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
  202. return;
  203. if (test_bit(XPT_DEAD, &svsk->sk_xprt.xpt_flags))
  204. return;
  205. cpu = get_cpu();
  206. pool = svc_pool_for_cpu(svsk->sk_xprt.xpt_server, cpu);
  207. put_cpu();
  208. spin_lock_bh(&pool->sp_lock);
  209. if (!list_empty(&pool->sp_threads) &&
  210. !list_empty(&pool->sp_sockets))
  211. printk(KERN_ERR
  212. "svc_sock_enqueue: threads and sockets both waiting??\n");
  213. if (test_bit(XPT_DEAD, &svsk->sk_xprt.xpt_flags)) {
  214. /* Don't enqueue dead sockets */
  215. dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
  216. goto out_unlock;
  217. }
  218. /* Mark socket as busy. It will remain in this state until the
  219. * server has processed all pending data and put the socket back
  220. * on the idle list. We update XPT_BUSY atomically because
  221. * it also guards against trying to enqueue the svc_sock twice.
  222. */
  223. if (test_and_set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)) {
  224. /* Don't enqueue socket while already enqueued */
  225. dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
  226. goto out_unlock;
  227. }
  228. BUG_ON(svsk->sk_xprt.xpt_pool != NULL);
  229. svsk->sk_xprt.xpt_pool = pool;
  230. /* Handle pending connection */
  231. if (test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags))
  232. goto process;
  233. /* Handle close in-progress */
  234. if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags))
  235. goto process;
  236. /* Check if we have space to reply to a request */
  237. if (!svsk->sk_xprt.xpt_ops->xpo_has_wspace(&svsk->sk_xprt)) {
  238. /* Don't enqueue while not enough space for reply */
  239. dprintk("svc: no write space, socket %p not enqueued\n", svsk);
  240. svsk->sk_xprt.xpt_pool = NULL;
  241. clear_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
  242. goto out_unlock;
  243. }
  244. process:
  245. if (!list_empty(&pool->sp_threads)) {
  246. rqstp = list_entry(pool->sp_threads.next,
  247. struct svc_rqst,
  248. rq_list);
  249. dprintk("svc: socket %p served by daemon %p\n",
  250. svsk->sk_sk, rqstp);
  251. svc_thread_dequeue(pool, rqstp);
  252. if (rqstp->rq_sock)
  253. printk(KERN_ERR
  254. "svc_sock_enqueue: server %p, rq_sock=%p!\n",
  255. rqstp, rqstp->rq_sock);
  256. rqstp->rq_sock = svsk;
  257. svc_xprt_get(&svsk->sk_xprt);
  258. rqstp->rq_reserved = serv->sv_max_mesg;
  259. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  260. BUG_ON(svsk->sk_xprt.xpt_pool != pool);
  261. wake_up(&rqstp->rq_wait);
  262. } else {
  263. dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
  264. list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
  265. BUG_ON(svsk->sk_xprt.xpt_pool != pool);
  266. }
  267. out_unlock:
  268. spin_unlock_bh(&pool->sp_lock);
  269. }
  270. /*
  271. * Dequeue the first socket. Must be called with the pool->sp_lock held.
  272. */
  273. static inline struct svc_sock *
  274. svc_sock_dequeue(struct svc_pool *pool)
  275. {
  276. struct svc_sock *svsk;
  277. if (list_empty(&pool->sp_sockets))
  278. return NULL;
  279. svsk = list_entry(pool->sp_sockets.next,
  280. struct svc_sock, sk_ready);
  281. list_del_init(&svsk->sk_ready);
  282. dprintk("svc: socket %p dequeued, inuse=%d\n",
  283. svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
  284. return svsk;
  285. }
  286. /*
  287. * Having read something from a socket, check whether it
  288. * needs to be re-enqueued.
  289. * Note: XPT_DATA only gets cleared when a read-attempt finds
  290. * no (or insufficient) data.
  291. */
  292. static inline void
  293. svc_sock_received(struct svc_sock *svsk)
  294. {
  295. svsk->sk_xprt.xpt_pool = NULL;
  296. clear_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
  297. svc_sock_enqueue(svsk);
  298. }
  299. /**
  300. * svc_reserve - change the space reserved for the reply to a request.
  301. * @rqstp: The request in question
  302. * @space: new max space to reserve
  303. *
  304. * Each request reserves some space on the output queue of the socket
  305. * to make sure the reply fits. This function reduces that reserved
  306. * space to be the amount of space used already, plus @space.
  307. *
  308. */
  309. void svc_reserve(struct svc_rqst *rqstp, int space)
  310. {
  311. space += rqstp->rq_res.head[0].iov_len;
  312. if (space < rqstp->rq_reserved) {
  313. struct svc_sock *svsk = rqstp->rq_sock;
  314. atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
  315. rqstp->rq_reserved = space;
  316. svc_sock_enqueue(svsk);
  317. }
  318. }
  319. static void
  320. svc_sock_release(struct svc_rqst *rqstp)
  321. {
  322. struct svc_sock *svsk = rqstp->rq_sock;
  323. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  324. svc_free_res_pages(rqstp);
  325. rqstp->rq_res.page_len = 0;
  326. rqstp->rq_res.page_base = 0;
  327. /* Reset response buffer and release
  328. * the reservation.
  329. * But first, check that enough space was reserved
  330. * for the reply, otherwise we have a bug!
  331. */
  332. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  333. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  334. rqstp->rq_reserved,
  335. rqstp->rq_res.len);
  336. rqstp->rq_res.head[0].iov_len = 0;
  337. svc_reserve(rqstp, 0);
  338. rqstp->rq_sock = NULL;
  339. svc_xprt_put(&svsk->sk_xprt);
  340. }
  341. /*
  342. * External function to wake up a server waiting for data
  343. * This really only makes sense for services like lockd
  344. * which have exactly one thread anyway.
  345. */
  346. void
  347. svc_wake_up(struct svc_serv *serv)
  348. {
  349. struct svc_rqst *rqstp;
  350. unsigned int i;
  351. struct svc_pool *pool;
  352. for (i = 0; i < serv->sv_nrpools; i++) {
  353. pool = &serv->sv_pools[i];
  354. spin_lock_bh(&pool->sp_lock);
  355. if (!list_empty(&pool->sp_threads)) {
  356. rqstp = list_entry(pool->sp_threads.next,
  357. struct svc_rqst,
  358. rq_list);
  359. dprintk("svc: daemon %p woken up.\n", rqstp);
  360. /*
  361. svc_thread_dequeue(pool, rqstp);
  362. rqstp->rq_sock = NULL;
  363. */
  364. wake_up(&rqstp->rq_wait);
  365. }
  366. spin_unlock_bh(&pool->sp_lock);
  367. }
  368. }
  369. union svc_pktinfo_u {
  370. struct in_pktinfo pkti;
  371. struct in6_pktinfo pkti6;
  372. };
  373. #define SVC_PKTINFO_SPACE \
  374. CMSG_SPACE(sizeof(union svc_pktinfo_u))
  375. static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
  376. {
  377. switch (rqstp->rq_sock->sk_sk->sk_family) {
  378. case AF_INET: {
  379. struct in_pktinfo *pki = CMSG_DATA(cmh);
  380. cmh->cmsg_level = SOL_IP;
  381. cmh->cmsg_type = IP_PKTINFO;
  382. pki->ipi_ifindex = 0;
  383. pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
  384. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  385. }
  386. break;
  387. case AF_INET6: {
  388. struct in6_pktinfo *pki = CMSG_DATA(cmh);
  389. cmh->cmsg_level = SOL_IPV6;
  390. cmh->cmsg_type = IPV6_PKTINFO;
  391. pki->ipi6_ifindex = 0;
  392. ipv6_addr_copy(&pki->ipi6_addr,
  393. &rqstp->rq_daddr.addr6);
  394. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  395. }
  396. break;
  397. }
  398. return;
  399. }
  400. /*
  401. * Generic sendto routine
  402. */
  403. static int
  404. svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
  405. {
  406. struct svc_sock *svsk = rqstp->rq_sock;
  407. struct socket *sock = svsk->sk_sock;
  408. int slen;
  409. union {
  410. struct cmsghdr hdr;
  411. long all[SVC_PKTINFO_SPACE / sizeof(long)];
  412. } buffer;
  413. struct cmsghdr *cmh = &buffer.hdr;
  414. int len = 0;
  415. int result;
  416. int size;
  417. struct page **ppage = xdr->pages;
  418. size_t base = xdr->page_base;
  419. unsigned int pglen = xdr->page_len;
  420. unsigned int flags = MSG_MORE;
  421. char buf[RPC_MAX_ADDRBUFLEN];
  422. slen = xdr->len;
  423. if (rqstp->rq_prot == IPPROTO_UDP) {
  424. struct msghdr msg = {
  425. .msg_name = &rqstp->rq_addr,
  426. .msg_namelen = rqstp->rq_addrlen,
  427. .msg_control = cmh,
  428. .msg_controllen = sizeof(buffer),
  429. .msg_flags = MSG_MORE,
  430. };
  431. svc_set_cmsg_data(rqstp, cmh);
  432. if (sock_sendmsg(sock, &msg, 0) < 0)
  433. goto out;
  434. }
  435. /* send head */
  436. if (slen == xdr->head[0].iov_len)
  437. flags = 0;
  438. len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
  439. xdr->head[0].iov_len, flags);
  440. if (len != xdr->head[0].iov_len)
  441. goto out;
  442. slen -= xdr->head[0].iov_len;
  443. if (slen == 0)
  444. goto out;
  445. /* send page data */
  446. size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
  447. while (pglen > 0) {
  448. if (slen == size)
  449. flags = 0;
  450. result = kernel_sendpage(sock, *ppage, base, size, flags);
  451. if (result > 0)
  452. len += result;
  453. if (result != size)
  454. goto out;
  455. slen -= size;
  456. pglen -= size;
  457. size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
  458. base = 0;
  459. ppage++;
  460. }
  461. /* send tail */
  462. if (xdr->tail[0].iov_len) {
  463. result = kernel_sendpage(sock, rqstp->rq_respages[0],
  464. ((unsigned long)xdr->tail[0].iov_base)
  465. & (PAGE_SIZE-1),
  466. xdr->tail[0].iov_len, 0);
  467. if (result > 0)
  468. len += result;
  469. }
  470. out:
  471. dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
  472. rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
  473. xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
  474. return len;
  475. }
  476. /*
  477. * Report socket names for nfsdfs
  478. */
  479. static int one_sock_name(char *buf, struct svc_sock *svsk)
  480. {
  481. int len;
  482. switch(svsk->sk_sk->sk_family) {
  483. case AF_INET:
  484. len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
  485. svsk->sk_sk->sk_protocol==IPPROTO_UDP?
  486. "udp" : "tcp",
  487. NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
  488. inet_sk(svsk->sk_sk)->num);
  489. break;
  490. default:
  491. len = sprintf(buf, "*unknown-%d*\n",
  492. svsk->sk_sk->sk_family);
  493. }
  494. return len;
  495. }
  496. int
  497. svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
  498. {
  499. struct svc_sock *svsk, *closesk = NULL;
  500. int len = 0;
  501. if (!serv)
  502. return 0;
  503. spin_lock_bh(&serv->sv_lock);
  504. list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
  505. int onelen = one_sock_name(buf+len, svsk);
  506. if (toclose && strcmp(toclose, buf+len) == 0)
  507. closesk = svsk;
  508. else
  509. len += onelen;
  510. }
  511. spin_unlock_bh(&serv->sv_lock);
  512. if (closesk)
  513. /* Should unregister with portmap, but you cannot
  514. * unregister just one protocol...
  515. */
  516. svc_close_socket(closesk);
  517. else if (toclose)
  518. return -ENOENT;
  519. return len;
  520. }
  521. EXPORT_SYMBOL(svc_sock_names);
  522. /*
  523. * Check input queue length
  524. */
  525. static int
  526. svc_recv_available(struct svc_sock *svsk)
  527. {
  528. struct socket *sock = svsk->sk_sock;
  529. int avail, err;
  530. err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
  531. return (err >= 0)? avail : err;
  532. }
  533. /*
  534. * Generic recvfrom routine.
  535. */
  536. static int
  537. svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
  538. {
  539. struct svc_sock *svsk = rqstp->rq_sock;
  540. struct msghdr msg = {
  541. .msg_flags = MSG_DONTWAIT,
  542. };
  543. struct sockaddr *sin;
  544. int len;
  545. len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
  546. msg.msg_flags);
  547. /* sock_recvmsg doesn't fill in the name/namelen, so we must..
  548. */
  549. memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
  550. rqstp->rq_addrlen = svsk->sk_remotelen;
  551. /* Destination address in request is needed for binding the
  552. * source address in RPC callbacks later.
  553. */
  554. sin = (struct sockaddr *)&svsk->sk_local;
  555. switch (sin->sa_family) {
  556. case AF_INET:
  557. rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
  558. break;
  559. case AF_INET6:
  560. rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
  561. break;
  562. }
  563. dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
  564. svsk, iov[0].iov_base, iov[0].iov_len, len);
  565. return len;
  566. }
  567. /*
  568. * Set socket snd and rcv buffer lengths
  569. */
  570. static inline void
  571. svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
  572. {
  573. #if 0
  574. mm_segment_t oldfs;
  575. oldfs = get_fs(); set_fs(KERNEL_DS);
  576. sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
  577. (char*)&snd, sizeof(snd));
  578. sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  579. (char*)&rcv, sizeof(rcv));
  580. #else
  581. /* sock_setsockopt limits use to sysctl_?mem_max,
  582. * which isn't acceptable. Until that is made conditional
  583. * on not having CAP_SYS_RESOURCE or similar, we go direct...
  584. * DaveM said I could!
  585. */
  586. lock_sock(sock->sk);
  587. sock->sk->sk_sndbuf = snd * 2;
  588. sock->sk->sk_rcvbuf = rcv * 2;
  589. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
  590. release_sock(sock->sk);
  591. #endif
  592. }
  593. /*
  594. * INET callback when data has been received on the socket.
  595. */
  596. static void
  597. svc_udp_data_ready(struct sock *sk, int count)
  598. {
  599. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  600. if (svsk) {
  601. dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
  602. svsk, sk, count,
  603. test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
  604. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  605. svc_sock_enqueue(svsk);
  606. }
  607. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  608. wake_up_interruptible(sk->sk_sleep);
  609. }
  610. /*
  611. * INET callback when space is newly available on the socket.
  612. */
  613. static void
  614. svc_write_space(struct sock *sk)
  615. {
  616. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  617. if (svsk) {
  618. dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
  619. svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
  620. svc_sock_enqueue(svsk);
  621. }
  622. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
  623. dprintk("RPC svc_write_space: someone sleeping on %p\n",
  624. svsk);
  625. wake_up_interruptible(sk->sk_sleep);
  626. }
  627. }
  628. static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
  629. struct cmsghdr *cmh)
  630. {
  631. switch (rqstp->rq_sock->sk_sk->sk_family) {
  632. case AF_INET: {
  633. struct in_pktinfo *pki = CMSG_DATA(cmh);
  634. rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
  635. break;
  636. }
  637. case AF_INET6: {
  638. struct in6_pktinfo *pki = CMSG_DATA(cmh);
  639. ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
  640. break;
  641. }
  642. }
  643. }
  644. /*
  645. * Receive a datagram from a UDP socket.
  646. */
  647. static int
  648. svc_udp_recvfrom(struct svc_rqst *rqstp)
  649. {
  650. struct svc_sock *svsk = rqstp->rq_sock;
  651. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  652. struct sk_buff *skb;
  653. union {
  654. struct cmsghdr hdr;
  655. long all[SVC_PKTINFO_SPACE / sizeof(long)];
  656. } buffer;
  657. struct cmsghdr *cmh = &buffer.hdr;
  658. int err, len;
  659. struct msghdr msg = {
  660. .msg_name = svc_addr(rqstp),
  661. .msg_control = cmh,
  662. .msg_controllen = sizeof(buffer),
  663. .msg_flags = MSG_DONTWAIT,
  664. };
  665. if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
  666. /* udp sockets need large rcvbuf as all pending
  667. * requests are still in that buffer. sndbuf must
  668. * also be large enough that there is enough space
  669. * for one reply per thread. We count all threads
  670. * rather than threads in a particular pool, which
  671. * provides an upper bound on the number of threads
  672. * which will access the socket.
  673. */
  674. svc_sock_setbufsize(svsk->sk_sock,
  675. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  676. (serv->sv_nrthreads+3) * serv->sv_max_mesg);
  677. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  678. svc_sock_received(svsk);
  679. return svc_deferred_recv(rqstp);
  680. }
  681. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  682. skb = NULL;
  683. err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
  684. 0, 0, MSG_PEEK | MSG_DONTWAIT);
  685. if (err >= 0)
  686. skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
  687. if (skb == NULL) {
  688. if (err != -EAGAIN) {
  689. /* possibly an icmp error */
  690. dprintk("svc: recvfrom returned error %d\n", -err);
  691. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  692. }
  693. svc_sock_received(svsk);
  694. return -EAGAIN;
  695. }
  696. rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
  697. if (skb->tstamp.tv64 == 0) {
  698. skb->tstamp = ktime_get_real();
  699. /* Don't enable netstamp, sunrpc doesn't
  700. need that much accuracy */
  701. }
  702. svsk->sk_sk->sk_stamp = skb->tstamp;
  703. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
  704. /*
  705. * Maybe more packets - kick another thread ASAP.
  706. */
  707. svc_sock_received(svsk);
  708. len = skb->len - sizeof(struct udphdr);
  709. rqstp->rq_arg.len = len;
  710. rqstp->rq_prot = IPPROTO_UDP;
  711. if (cmh->cmsg_level != IPPROTO_IP ||
  712. cmh->cmsg_type != IP_PKTINFO) {
  713. if (net_ratelimit())
  714. printk("rpcsvc: received unknown control message:"
  715. "%d/%d\n",
  716. cmh->cmsg_level, cmh->cmsg_type);
  717. skb_free_datagram(svsk->sk_sk, skb);
  718. return 0;
  719. }
  720. svc_udp_get_dest_address(rqstp, cmh);
  721. if (skb_is_nonlinear(skb)) {
  722. /* we have to copy */
  723. local_bh_disable();
  724. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
  725. local_bh_enable();
  726. /* checksum error */
  727. skb_free_datagram(svsk->sk_sk, skb);
  728. return 0;
  729. }
  730. local_bh_enable();
  731. skb_free_datagram(svsk->sk_sk, skb);
  732. } else {
  733. /* we can use it in-place */
  734. rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
  735. rqstp->rq_arg.head[0].iov_len = len;
  736. if (skb_checksum_complete(skb)) {
  737. skb_free_datagram(svsk->sk_sk, skb);
  738. return 0;
  739. }
  740. rqstp->rq_xprt_ctxt = skb;
  741. }
  742. rqstp->rq_arg.page_base = 0;
  743. if (len <= rqstp->rq_arg.head[0].iov_len) {
  744. rqstp->rq_arg.head[0].iov_len = len;
  745. rqstp->rq_arg.page_len = 0;
  746. rqstp->rq_respages = rqstp->rq_pages+1;
  747. } else {
  748. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  749. rqstp->rq_respages = rqstp->rq_pages + 1 +
  750. DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
  751. }
  752. if (serv->sv_stats)
  753. serv->sv_stats->netudpcnt++;
  754. return len;
  755. }
  756. static int
  757. svc_udp_sendto(struct svc_rqst *rqstp)
  758. {
  759. int error;
  760. error = svc_sendto(rqstp, &rqstp->rq_res);
  761. if (error == -ECONNREFUSED)
  762. /* ICMP error on earlier request. */
  763. error = svc_sendto(rqstp, &rqstp->rq_res);
  764. return error;
  765. }
  766. static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
  767. {
  768. }
  769. static int svc_udp_has_wspace(struct svc_xprt *xprt)
  770. {
  771. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  772. struct svc_serv *serv = xprt->xpt_server;
  773. unsigned long required;
  774. /*
  775. * Set the SOCK_NOSPACE flag before checking the available
  776. * sock space.
  777. */
  778. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  779. required = atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg;
  780. if (required*2 > sock_wspace(svsk->sk_sk))
  781. return 0;
  782. clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  783. return 1;
  784. }
  785. static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
  786. {
  787. BUG();
  788. return NULL;
  789. }
  790. static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
  791. struct sockaddr *sa, int salen,
  792. int flags)
  793. {
  794. return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags);
  795. }
  796. static struct svc_xprt_ops svc_udp_ops = {
  797. .xpo_create = svc_udp_create,
  798. .xpo_recvfrom = svc_udp_recvfrom,
  799. .xpo_sendto = svc_udp_sendto,
  800. .xpo_release_rqst = svc_release_skb,
  801. .xpo_detach = svc_sock_detach,
  802. .xpo_free = svc_sock_free,
  803. .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
  804. .xpo_has_wspace = svc_udp_has_wspace,
  805. .xpo_accept = svc_udp_accept,
  806. };
  807. static struct svc_xprt_class svc_udp_class = {
  808. .xcl_name = "udp",
  809. .xcl_owner = THIS_MODULE,
  810. .xcl_ops = &svc_udp_ops,
  811. .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
  812. };
  813. static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
  814. {
  815. int one = 1;
  816. mm_segment_t oldfs;
  817. svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
  818. svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
  819. svsk->sk_sk->sk_write_space = svc_write_space;
  820. /* initialise setting must have enough space to
  821. * receive and respond to one request.
  822. * svc_udp_recvfrom will re-adjust if necessary
  823. */
  824. svc_sock_setbufsize(svsk->sk_sock,
  825. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
  826. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
  827. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */
  828. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  829. oldfs = get_fs();
  830. set_fs(KERNEL_DS);
  831. /* make sure we get destination address info */
  832. svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
  833. (char __user *)&one, sizeof(one));
  834. set_fs(oldfs);
  835. }
  836. /*
  837. * A data_ready event on a listening socket means there's a connection
  838. * pending. Do not use state_change as a substitute for it.
  839. */
  840. static void
  841. svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
  842. {
  843. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  844. dprintk("svc: socket %p TCP (listen) state change %d\n",
  845. sk, sk->sk_state);
  846. /*
  847. * This callback may called twice when a new connection
  848. * is established as a child socket inherits everything
  849. * from a parent LISTEN socket.
  850. * 1) data_ready method of the parent socket will be called
  851. * when one of child sockets become ESTABLISHED.
  852. * 2) data_ready method of the child socket may be called
  853. * when it receives data before the socket is accepted.
  854. * In case of 2, we should ignore it silently.
  855. */
  856. if (sk->sk_state == TCP_LISTEN) {
  857. if (svsk) {
  858. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  859. svc_sock_enqueue(svsk);
  860. } else
  861. printk("svc: socket %p: no user data\n", sk);
  862. }
  863. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  864. wake_up_interruptible_all(sk->sk_sleep);
  865. }
  866. /*
  867. * A state change on a connected socket means it's dying or dead.
  868. */
  869. static void
  870. svc_tcp_state_change(struct sock *sk)
  871. {
  872. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  873. dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
  874. sk, sk->sk_state, sk->sk_user_data);
  875. if (!svsk)
  876. printk("svc: socket %p: no user data\n", sk);
  877. else {
  878. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  879. svc_sock_enqueue(svsk);
  880. }
  881. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  882. wake_up_interruptible_all(sk->sk_sleep);
  883. }
  884. static void
  885. svc_tcp_data_ready(struct sock *sk, int count)
  886. {
  887. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  888. dprintk("svc: socket %p TCP data ready (svsk %p)\n",
  889. sk, sk->sk_user_data);
  890. if (svsk) {
  891. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  892. svc_sock_enqueue(svsk);
  893. }
  894. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  895. wake_up_interruptible(sk->sk_sleep);
  896. }
  897. static inline int svc_port_is_privileged(struct sockaddr *sin)
  898. {
  899. switch (sin->sa_family) {
  900. case AF_INET:
  901. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  902. < PROT_SOCK;
  903. case AF_INET6:
  904. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  905. < PROT_SOCK;
  906. default:
  907. return 0;
  908. }
  909. }
  910. /*
  911. * Accept a TCP connection
  912. */
  913. static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
  914. {
  915. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  916. struct sockaddr_storage addr;
  917. struct sockaddr *sin = (struct sockaddr *) &addr;
  918. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  919. struct socket *sock = svsk->sk_sock;
  920. struct socket *newsock;
  921. struct svc_sock *newsvsk;
  922. int err, slen;
  923. char buf[RPC_MAX_ADDRBUFLEN];
  924. dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
  925. if (!sock)
  926. return NULL;
  927. clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  928. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  929. if (err < 0) {
  930. if (err == -ENOMEM)
  931. printk(KERN_WARNING "%s: no more sockets!\n",
  932. serv->sv_name);
  933. else if (err != -EAGAIN && net_ratelimit())
  934. printk(KERN_WARNING "%s: accept failed (err %d)!\n",
  935. serv->sv_name, -err);
  936. return NULL;
  937. }
  938. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  939. err = kernel_getpeername(newsock, sin, &slen);
  940. if (err < 0) {
  941. if (net_ratelimit())
  942. printk(KERN_WARNING "%s: peername failed (err %d)!\n",
  943. serv->sv_name, -err);
  944. goto failed; /* aborted connection or whatever */
  945. }
  946. /* Ideally, we would want to reject connections from unauthorized
  947. * hosts here, but when we get encryption, the IP of the host won't
  948. * tell us anything. For now just warn about unpriv connections.
  949. */
  950. if (!svc_port_is_privileged(sin)) {
  951. dprintk(KERN_WARNING
  952. "%s: connect from unprivileged port: %s\n",
  953. serv->sv_name,
  954. __svc_print_addr(sin, buf, sizeof(buf)));
  955. }
  956. dprintk("%s: connect from %s\n", serv->sv_name,
  957. __svc_print_addr(sin, buf, sizeof(buf)));
  958. /* make sure that a write doesn't block forever when
  959. * low on memory
  960. */
  961. newsock->sk->sk_sndtimeo = HZ*30;
  962. if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
  963. (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
  964. goto failed;
  965. memcpy(&newsvsk->sk_remote, sin, slen);
  966. newsvsk->sk_remotelen = slen;
  967. err = kernel_getsockname(newsock, sin, &slen);
  968. if (unlikely(err < 0)) {
  969. dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
  970. slen = offsetof(struct sockaddr, sa_data);
  971. }
  972. memcpy(&newsvsk->sk_local, sin, slen);
  973. svc_sock_received(newsvsk);
  974. if (serv->sv_stats)
  975. serv->sv_stats->nettcpconn++;
  976. return &newsvsk->sk_xprt;
  977. failed:
  978. sock_release(newsock);
  979. return NULL;
  980. }
  981. /*
  982. * Receive data from a TCP socket.
  983. */
  984. static int
  985. svc_tcp_recvfrom(struct svc_rqst *rqstp)
  986. {
  987. struct svc_sock *svsk = rqstp->rq_sock;
  988. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  989. int len;
  990. struct kvec *vec;
  991. int pnum, vlen;
  992. dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
  993. svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
  994. test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
  995. test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
  996. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  997. svc_sock_received(svsk);
  998. return svc_deferred_recv(rqstp);
  999. }
  1000. if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
  1001. /* sndbuf needs to have room for one request
  1002. * per thread, otherwise we can stall even when the
  1003. * network isn't a bottleneck.
  1004. *
  1005. * We count all threads rather than threads in a
  1006. * particular pool, which provides an upper bound
  1007. * on the number of threads which will access the socket.
  1008. *
  1009. * rcvbuf just needs to be able to hold a few requests.
  1010. * Normally they will be removed from the queue
  1011. * as soon a a complete request arrives.
  1012. */
  1013. svc_sock_setbufsize(svsk->sk_sock,
  1014. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  1015. 3 * serv->sv_max_mesg);
  1016. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1017. /* Receive data. If we haven't got the record length yet, get
  1018. * the next four bytes. Otherwise try to gobble up as much as
  1019. * possible up to the complete record length.
  1020. */
  1021. if (svsk->sk_tcplen < 4) {
  1022. unsigned long want = 4 - svsk->sk_tcplen;
  1023. struct kvec iov;
  1024. iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
  1025. iov.iov_len = want;
  1026. if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
  1027. goto error;
  1028. svsk->sk_tcplen += len;
  1029. if (len < want) {
  1030. dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
  1031. len, want);
  1032. svc_sock_received(svsk);
  1033. return -EAGAIN; /* record header not complete */
  1034. }
  1035. svsk->sk_reclen = ntohl(svsk->sk_reclen);
  1036. if (!(svsk->sk_reclen & 0x80000000)) {
  1037. /* FIXME: technically, a record can be fragmented,
  1038. * and non-terminal fragments will not have the top
  1039. * bit set in the fragment length header.
  1040. * But apparently no known nfs clients send fragmented
  1041. * records. */
  1042. if (net_ratelimit())
  1043. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  1044. " (non-terminal)\n",
  1045. (unsigned long) svsk->sk_reclen);
  1046. goto err_delete;
  1047. }
  1048. svsk->sk_reclen &= 0x7fffffff;
  1049. dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
  1050. if (svsk->sk_reclen > serv->sv_max_mesg) {
  1051. if (net_ratelimit())
  1052. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  1053. " (large)\n",
  1054. (unsigned long) svsk->sk_reclen);
  1055. goto err_delete;
  1056. }
  1057. }
  1058. /* Check whether enough data is available */
  1059. len = svc_recv_available(svsk);
  1060. if (len < 0)
  1061. goto error;
  1062. if (len < svsk->sk_reclen) {
  1063. dprintk("svc: incomplete TCP record (%d of %d)\n",
  1064. len, svsk->sk_reclen);
  1065. svc_sock_received(svsk);
  1066. return -EAGAIN; /* record not complete */
  1067. }
  1068. len = svsk->sk_reclen;
  1069. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1070. vec = rqstp->rq_vec;
  1071. vec[0] = rqstp->rq_arg.head[0];
  1072. vlen = PAGE_SIZE;
  1073. pnum = 1;
  1074. while (vlen < len) {
  1075. vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
  1076. vec[pnum].iov_len = PAGE_SIZE;
  1077. pnum++;
  1078. vlen += PAGE_SIZE;
  1079. }
  1080. rqstp->rq_respages = &rqstp->rq_pages[pnum];
  1081. /* Now receive data */
  1082. len = svc_recvfrom(rqstp, vec, pnum, len);
  1083. if (len < 0)
  1084. goto error;
  1085. dprintk("svc: TCP complete record (%d bytes)\n", len);
  1086. rqstp->rq_arg.len = len;
  1087. rqstp->rq_arg.page_base = 0;
  1088. if (len <= rqstp->rq_arg.head[0].iov_len) {
  1089. rqstp->rq_arg.head[0].iov_len = len;
  1090. rqstp->rq_arg.page_len = 0;
  1091. } else {
  1092. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  1093. }
  1094. rqstp->rq_xprt_ctxt = NULL;
  1095. rqstp->rq_prot = IPPROTO_TCP;
  1096. /* Reset TCP read info */
  1097. svsk->sk_reclen = 0;
  1098. svsk->sk_tcplen = 0;
  1099. svc_sock_received(svsk);
  1100. if (serv->sv_stats)
  1101. serv->sv_stats->nettcpcnt++;
  1102. return len;
  1103. err_delete:
  1104. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1105. return -EAGAIN;
  1106. error:
  1107. if (len == -EAGAIN) {
  1108. dprintk("RPC: TCP recvfrom got EAGAIN\n");
  1109. svc_sock_received(svsk);
  1110. } else {
  1111. printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
  1112. svsk->sk_xprt.xpt_server->sv_name, -len);
  1113. goto err_delete;
  1114. }
  1115. return len;
  1116. }
  1117. /*
  1118. * Send out data on TCP socket.
  1119. */
  1120. static int
  1121. svc_tcp_sendto(struct svc_rqst *rqstp)
  1122. {
  1123. struct xdr_buf *xbufp = &rqstp->rq_res;
  1124. int sent;
  1125. __be32 reclen;
  1126. /* Set up the first element of the reply kvec.
  1127. * Any other kvecs that may be in use have been taken
  1128. * care of by the server implementation itself.
  1129. */
  1130. reclen = htonl(0x80000000|((xbufp->len ) - 4));
  1131. memcpy(xbufp->head[0].iov_base, &reclen, 4);
  1132. if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags))
  1133. return -ENOTCONN;
  1134. sent = svc_sendto(rqstp, &rqstp->rq_res);
  1135. if (sent != xbufp->len) {
  1136. printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
  1137. rqstp->rq_sock->sk_xprt.xpt_server->sv_name,
  1138. (sent<0)?"got error":"sent only",
  1139. sent, xbufp->len);
  1140. set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags);
  1141. svc_sock_enqueue(rqstp->rq_sock);
  1142. sent = -EAGAIN;
  1143. }
  1144. return sent;
  1145. }
  1146. /*
  1147. * Setup response header. TCP has a 4B record length field.
  1148. */
  1149. static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
  1150. {
  1151. struct kvec *resv = &rqstp->rq_res.head[0];
  1152. /* tcp needs a space for the record length... */
  1153. svc_putnl(resv, 0);
  1154. }
  1155. static int svc_tcp_has_wspace(struct svc_xprt *xprt)
  1156. {
  1157. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1158. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  1159. int required;
  1160. int wspace;
  1161. /*
  1162. * Set the SOCK_NOSPACE flag before checking the available
  1163. * sock space.
  1164. */
  1165. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  1166. required = atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg;
  1167. wspace = sk_stream_wspace(svsk->sk_sk);
  1168. if (wspace < sk_stream_min_wspace(svsk->sk_sk))
  1169. return 0;
  1170. if (required * 2 > wspace)
  1171. return 0;
  1172. clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  1173. return 1;
  1174. }
  1175. static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
  1176. struct sockaddr *sa, int salen,
  1177. int flags)
  1178. {
  1179. return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags);
  1180. }
  1181. static struct svc_xprt_ops svc_tcp_ops = {
  1182. .xpo_create = svc_tcp_create,
  1183. .xpo_recvfrom = svc_tcp_recvfrom,
  1184. .xpo_sendto = svc_tcp_sendto,
  1185. .xpo_release_rqst = svc_release_skb,
  1186. .xpo_detach = svc_sock_detach,
  1187. .xpo_free = svc_sock_free,
  1188. .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
  1189. .xpo_has_wspace = svc_tcp_has_wspace,
  1190. .xpo_accept = svc_tcp_accept,
  1191. };
  1192. static struct svc_xprt_class svc_tcp_class = {
  1193. .xcl_name = "tcp",
  1194. .xcl_owner = THIS_MODULE,
  1195. .xcl_ops = &svc_tcp_ops,
  1196. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  1197. };
  1198. void svc_init_xprt_sock(void)
  1199. {
  1200. svc_reg_xprt_class(&svc_tcp_class);
  1201. svc_reg_xprt_class(&svc_udp_class);
  1202. }
  1203. void svc_cleanup_xprt_sock(void)
  1204. {
  1205. svc_unreg_xprt_class(&svc_tcp_class);
  1206. svc_unreg_xprt_class(&svc_udp_class);
  1207. }
  1208. static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
  1209. {
  1210. struct sock *sk = svsk->sk_sk;
  1211. struct tcp_sock *tp = tcp_sk(sk);
  1212. svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
  1213. if (sk->sk_state == TCP_LISTEN) {
  1214. dprintk("setting up TCP socket for listening\n");
  1215. set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
  1216. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1217. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  1218. } else {
  1219. dprintk("setting up TCP socket for reading\n");
  1220. sk->sk_state_change = svc_tcp_state_change;
  1221. sk->sk_data_ready = svc_tcp_data_ready;
  1222. sk->sk_write_space = svc_write_space;
  1223. svsk->sk_reclen = 0;
  1224. svsk->sk_tcplen = 0;
  1225. tp->nonagle = 1; /* disable Nagle's algorithm */
  1226. /* initialise setting must have enough space to
  1227. * receive and respond to one request.
  1228. * svc_tcp_recvfrom will re-adjust if necessary
  1229. */
  1230. svc_sock_setbufsize(svsk->sk_sock,
  1231. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
  1232. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
  1233. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1234. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1235. if (sk->sk_state != TCP_ESTABLISHED)
  1236. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1237. }
  1238. }
  1239. void
  1240. svc_sock_update_bufs(struct svc_serv *serv)
  1241. {
  1242. /*
  1243. * The number of server threads has changed. Update
  1244. * rcvbuf and sndbuf accordingly on all sockets
  1245. */
  1246. struct list_head *le;
  1247. spin_lock_bh(&serv->sv_lock);
  1248. list_for_each(le, &serv->sv_permsocks) {
  1249. struct svc_sock *svsk =
  1250. list_entry(le, struct svc_sock, sk_list);
  1251. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1252. }
  1253. list_for_each(le, &serv->sv_tempsocks) {
  1254. struct svc_sock *svsk =
  1255. list_entry(le, struct svc_sock, sk_list);
  1256. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1257. }
  1258. spin_unlock_bh(&serv->sv_lock);
  1259. }
  1260. /*
  1261. * Make sure that we don't have too many active connections. If we
  1262. * have, something must be dropped.
  1263. *
  1264. * There's no point in trying to do random drop here for DoS
  1265. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  1266. * attacker can easily beat that.
  1267. *
  1268. * The only somewhat efficient mechanism would be if drop old
  1269. * connections from the same IP first. But right now we don't even
  1270. * record the client IP in svc_sock.
  1271. */
  1272. static void svc_check_conn_limits(struct svc_serv *serv)
  1273. {
  1274. if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
  1275. struct svc_sock *svsk = NULL;
  1276. spin_lock_bh(&serv->sv_lock);
  1277. if (!list_empty(&serv->sv_tempsocks)) {
  1278. if (net_ratelimit()) {
  1279. /* Try to help the admin */
  1280. printk(KERN_NOTICE "%s: too many open TCP "
  1281. "sockets, consider increasing the "
  1282. "number of nfsd threads\n",
  1283. serv->sv_name);
  1284. }
  1285. /*
  1286. * Always select the oldest socket. It's not fair,
  1287. * but so is life
  1288. */
  1289. svsk = list_entry(serv->sv_tempsocks.prev,
  1290. struct svc_sock,
  1291. sk_list);
  1292. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1293. svc_xprt_get(&svsk->sk_xprt);
  1294. }
  1295. spin_unlock_bh(&serv->sv_lock);
  1296. if (svsk) {
  1297. svc_sock_enqueue(svsk);
  1298. svc_xprt_put(&svsk->sk_xprt);
  1299. }
  1300. }
  1301. }
  1302. /*
  1303. * Receive the next request on any socket. This code is carefully
  1304. * organised not to touch any cachelines in the shared svc_serv
  1305. * structure, only cachelines in the local svc_pool.
  1306. */
  1307. int
  1308. svc_recv(struct svc_rqst *rqstp, long timeout)
  1309. {
  1310. struct svc_sock *svsk = NULL;
  1311. struct svc_serv *serv = rqstp->rq_server;
  1312. struct svc_pool *pool = rqstp->rq_pool;
  1313. int len, i;
  1314. int pages;
  1315. struct xdr_buf *arg;
  1316. DECLARE_WAITQUEUE(wait, current);
  1317. dprintk("svc: server %p waiting for data (to = %ld)\n",
  1318. rqstp, timeout);
  1319. if (rqstp->rq_sock)
  1320. printk(KERN_ERR
  1321. "svc_recv: service %p, socket not NULL!\n",
  1322. rqstp);
  1323. if (waitqueue_active(&rqstp->rq_wait))
  1324. printk(KERN_ERR
  1325. "svc_recv: service %p, wait queue active!\n",
  1326. rqstp);
  1327. /* now allocate needed pages. If we get a failure, sleep briefly */
  1328. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  1329. for (i=0; i < pages ; i++)
  1330. while (rqstp->rq_pages[i] == NULL) {
  1331. struct page *p = alloc_page(GFP_KERNEL);
  1332. if (!p)
  1333. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1334. rqstp->rq_pages[i] = p;
  1335. }
  1336. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  1337. BUG_ON(pages >= RPCSVC_MAXPAGES);
  1338. /* Make arg->head point to first page and arg->pages point to rest */
  1339. arg = &rqstp->rq_arg;
  1340. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  1341. arg->head[0].iov_len = PAGE_SIZE;
  1342. arg->pages = rqstp->rq_pages + 1;
  1343. arg->page_base = 0;
  1344. /* save at least one page for response */
  1345. arg->page_len = (pages-2)*PAGE_SIZE;
  1346. arg->len = (pages-1)*PAGE_SIZE;
  1347. arg->tail[0].iov_len = 0;
  1348. try_to_freeze();
  1349. cond_resched();
  1350. if (signalled())
  1351. return -EINTR;
  1352. spin_lock_bh(&pool->sp_lock);
  1353. if ((svsk = svc_sock_dequeue(pool)) != NULL) {
  1354. rqstp->rq_sock = svsk;
  1355. svc_xprt_get(&svsk->sk_xprt);
  1356. rqstp->rq_reserved = serv->sv_max_mesg;
  1357. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  1358. } else {
  1359. /* No data pending. Go to sleep */
  1360. svc_thread_enqueue(pool, rqstp);
  1361. /*
  1362. * We have to be able to interrupt this wait
  1363. * to bring down the daemons ...
  1364. */
  1365. set_current_state(TASK_INTERRUPTIBLE);
  1366. add_wait_queue(&rqstp->rq_wait, &wait);
  1367. spin_unlock_bh(&pool->sp_lock);
  1368. schedule_timeout(timeout);
  1369. try_to_freeze();
  1370. spin_lock_bh(&pool->sp_lock);
  1371. remove_wait_queue(&rqstp->rq_wait, &wait);
  1372. if (!(svsk = rqstp->rq_sock)) {
  1373. svc_thread_dequeue(pool, rqstp);
  1374. spin_unlock_bh(&pool->sp_lock);
  1375. dprintk("svc: server %p, no data yet\n", rqstp);
  1376. return signalled()? -EINTR : -EAGAIN;
  1377. }
  1378. }
  1379. spin_unlock_bh(&pool->sp_lock);
  1380. len = 0;
  1381. if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) {
  1382. dprintk("svc_recv: found XPT_CLOSE\n");
  1383. svc_delete_socket(svsk);
  1384. } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) {
  1385. struct svc_xprt *newxpt;
  1386. newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt);
  1387. if (newxpt) {
  1388. /*
  1389. * We know this module_get will succeed because the
  1390. * listener holds a reference too
  1391. */
  1392. __module_get(newxpt->xpt_class->xcl_owner);
  1393. svc_check_conn_limits(svsk->sk_xprt.xpt_server);
  1394. }
  1395. svc_sock_received(svsk);
  1396. } else {
  1397. dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
  1398. rqstp, pool->sp_id, svsk,
  1399. atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
  1400. len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp);
  1401. dprintk("svc: got len=%d\n", len);
  1402. }
  1403. /* No data, incomplete (TCP) read, or accept() */
  1404. if (len == 0 || len == -EAGAIN) {
  1405. rqstp->rq_res.len = 0;
  1406. svc_sock_release(rqstp);
  1407. return -EAGAIN;
  1408. }
  1409. svsk->sk_lastrecv = get_seconds();
  1410. clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags);
  1411. rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
  1412. rqstp->rq_chandle.defer = svc_defer;
  1413. if (serv->sv_stats)
  1414. serv->sv_stats->netcnt++;
  1415. return len;
  1416. }
  1417. /*
  1418. * Drop request
  1419. */
  1420. void
  1421. svc_drop(struct svc_rqst *rqstp)
  1422. {
  1423. dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
  1424. svc_sock_release(rqstp);
  1425. }
  1426. /*
  1427. * Return reply to client.
  1428. */
  1429. int
  1430. svc_send(struct svc_rqst *rqstp)
  1431. {
  1432. struct svc_sock *svsk;
  1433. int len;
  1434. struct xdr_buf *xb;
  1435. if ((svsk = rqstp->rq_sock) == NULL) {
  1436. printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
  1437. __FILE__, __LINE__);
  1438. return -EFAULT;
  1439. }
  1440. /* release the receive skb before sending the reply */
  1441. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  1442. /* calculate over-all length */
  1443. xb = & rqstp->rq_res;
  1444. xb->len = xb->head[0].iov_len +
  1445. xb->page_len +
  1446. xb->tail[0].iov_len;
  1447. /* Grab svsk->sk_mutex to serialize outgoing data. */
  1448. mutex_lock(&svsk->sk_mutex);
  1449. if (test_bit(XPT_DEAD, &svsk->sk_xprt.xpt_flags))
  1450. len = -ENOTCONN;
  1451. else
  1452. len = svsk->sk_xprt.xpt_ops->xpo_sendto(rqstp);
  1453. mutex_unlock(&svsk->sk_mutex);
  1454. svc_sock_release(rqstp);
  1455. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  1456. return 0;
  1457. return len;
  1458. }
  1459. /*
  1460. * Timer function to close old temporary sockets, using
  1461. * a mark-and-sweep algorithm.
  1462. */
  1463. static void
  1464. svc_age_temp_sockets(unsigned long closure)
  1465. {
  1466. struct svc_serv *serv = (struct svc_serv *)closure;
  1467. struct svc_sock *svsk;
  1468. struct list_head *le, *next;
  1469. LIST_HEAD(to_be_aged);
  1470. dprintk("svc_age_temp_sockets\n");
  1471. if (!spin_trylock_bh(&serv->sv_lock)) {
  1472. /* busy, try again 1 sec later */
  1473. dprintk("svc_age_temp_sockets: busy\n");
  1474. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  1475. return;
  1476. }
  1477. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  1478. svsk = list_entry(le, struct svc_sock, sk_list);
  1479. if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags))
  1480. continue;
  1481. if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1
  1482. || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
  1483. continue;
  1484. svc_xprt_get(&svsk->sk_xprt);
  1485. list_move(le, &to_be_aged);
  1486. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1487. set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags);
  1488. }
  1489. spin_unlock_bh(&serv->sv_lock);
  1490. while (!list_empty(&to_be_aged)) {
  1491. le = to_be_aged.next;
  1492. /* fiddling the sk_list node is safe 'cos we're XPT_DETACHED */
  1493. list_del_init(le);
  1494. svsk = list_entry(le, struct svc_sock, sk_list);
  1495. dprintk("queuing svsk %p for closing, %lu seconds old\n",
  1496. svsk, get_seconds() - svsk->sk_lastrecv);
  1497. /* a thread will dequeue and close it soon */
  1498. svc_sock_enqueue(svsk);
  1499. svc_xprt_put(&svsk->sk_xprt);
  1500. }
  1501. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  1502. }
  1503. /*
  1504. * Initialize socket for RPC use and create svc_sock struct
  1505. * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
  1506. */
  1507. static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
  1508. struct socket *sock,
  1509. int *errp, int flags)
  1510. {
  1511. struct svc_sock *svsk;
  1512. struct sock *inet;
  1513. int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
  1514. int is_temporary = flags & SVC_SOCK_TEMPORARY;
  1515. dprintk("svc: svc_setup_socket %p\n", sock);
  1516. if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
  1517. *errp = -ENOMEM;
  1518. return NULL;
  1519. }
  1520. inet = sock->sk;
  1521. /* Register socket with portmapper */
  1522. if (*errp >= 0 && pmap_register)
  1523. *errp = svc_register(serv, inet->sk_protocol,
  1524. ntohs(inet_sk(inet)->sport));
  1525. if (*errp < 0) {
  1526. kfree(svsk);
  1527. return NULL;
  1528. }
  1529. set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
  1530. inet->sk_user_data = svsk;
  1531. svsk->sk_sock = sock;
  1532. svsk->sk_sk = inet;
  1533. svsk->sk_ostate = inet->sk_state_change;
  1534. svsk->sk_odata = inet->sk_data_ready;
  1535. svsk->sk_owspace = inet->sk_write_space;
  1536. svsk->sk_lastrecv = get_seconds();
  1537. spin_lock_init(&svsk->sk_lock);
  1538. INIT_LIST_HEAD(&svsk->sk_deferred);
  1539. INIT_LIST_HEAD(&svsk->sk_ready);
  1540. mutex_init(&svsk->sk_mutex);
  1541. /* Initialize the socket */
  1542. if (sock->type == SOCK_DGRAM)
  1543. svc_udp_init(svsk, serv);
  1544. else
  1545. svc_tcp_init(svsk, serv);
  1546. spin_lock_bh(&serv->sv_lock);
  1547. if (is_temporary) {
  1548. set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
  1549. list_add(&svsk->sk_list, &serv->sv_tempsocks);
  1550. serv->sv_tmpcnt++;
  1551. if (serv->sv_temptimer.function == NULL) {
  1552. /* setup timer to age temp sockets */
  1553. setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
  1554. (unsigned long)serv);
  1555. mod_timer(&serv->sv_temptimer,
  1556. jiffies + svc_conn_age_period * HZ);
  1557. }
  1558. } else {
  1559. clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
  1560. list_add(&svsk->sk_list, &serv->sv_permsocks);
  1561. }
  1562. spin_unlock_bh(&serv->sv_lock);
  1563. dprintk("svc: svc_setup_socket created %p (inet %p)\n",
  1564. svsk, svsk->sk_sk);
  1565. return svsk;
  1566. }
  1567. int svc_addsock(struct svc_serv *serv,
  1568. int fd,
  1569. char *name_return,
  1570. int *proto)
  1571. {
  1572. int err = 0;
  1573. struct socket *so = sockfd_lookup(fd, &err);
  1574. struct svc_sock *svsk = NULL;
  1575. if (!so)
  1576. return err;
  1577. if (so->sk->sk_family != AF_INET)
  1578. err = -EAFNOSUPPORT;
  1579. else if (so->sk->sk_protocol != IPPROTO_TCP &&
  1580. so->sk->sk_protocol != IPPROTO_UDP)
  1581. err = -EPROTONOSUPPORT;
  1582. else if (so->state > SS_UNCONNECTED)
  1583. err = -EISCONN;
  1584. else {
  1585. svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
  1586. if (svsk) {
  1587. svc_sock_received(svsk);
  1588. err = 0;
  1589. }
  1590. }
  1591. if (err) {
  1592. sockfd_put(so);
  1593. return err;
  1594. }
  1595. if (proto) *proto = so->sk->sk_protocol;
  1596. return one_sock_name(name_return, svsk);
  1597. }
  1598. EXPORT_SYMBOL_GPL(svc_addsock);
  1599. /*
  1600. * Create socket for RPC service.
  1601. */
  1602. static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
  1603. int protocol,
  1604. struct sockaddr *sin, int len,
  1605. int flags)
  1606. {
  1607. struct svc_sock *svsk;
  1608. struct socket *sock;
  1609. int error;
  1610. int type;
  1611. char buf[RPC_MAX_ADDRBUFLEN];
  1612. dprintk("svc: svc_create_socket(%s, %d, %s)\n",
  1613. serv->sv_program->pg_name, protocol,
  1614. __svc_print_addr(sin, buf, sizeof(buf)));
  1615. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1616. printk(KERN_WARNING "svc: only UDP and TCP "
  1617. "sockets supported\n");
  1618. return ERR_PTR(-EINVAL);
  1619. }
  1620. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1621. error = sock_create_kern(sin->sa_family, type, protocol, &sock);
  1622. if (error < 0)
  1623. return ERR_PTR(error);
  1624. svc_reclassify_socket(sock);
  1625. if (type == SOCK_STREAM)
  1626. sock->sk->sk_reuse = 1; /* allow address reuse */
  1627. error = kernel_bind(sock, sin, len);
  1628. if (error < 0)
  1629. goto bummer;
  1630. if (protocol == IPPROTO_TCP) {
  1631. if ((error = kernel_listen(sock, 64)) < 0)
  1632. goto bummer;
  1633. }
  1634. if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
  1635. svc_sock_received(svsk);
  1636. return (struct svc_xprt *)svsk;
  1637. }
  1638. bummer:
  1639. dprintk("svc: svc_create_socket error = %d\n", -error);
  1640. sock_release(sock);
  1641. return ERR_PTR(error);
  1642. }
  1643. /*
  1644. * Detach the svc_sock from the socket so that no
  1645. * more callbacks occur.
  1646. */
  1647. static void svc_sock_detach(struct svc_xprt *xprt)
  1648. {
  1649. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1650. struct sock *sk = svsk->sk_sk;
  1651. dprintk("svc: svc_sock_detach(%p)\n", svsk);
  1652. /* put back the old socket callbacks */
  1653. sk->sk_state_change = svsk->sk_ostate;
  1654. sk->sk_data_ready = svsk->sk_odata;
  1655. sk->sk_write_space = svsk->sk_owspace;
  1656. }
  1657. /*
  1658. * Free the svc_sock's socket resources and the svc_sock itself.
  1659. */
  1660. static void svc_sock_free(struct svc_xprt *xprt)
  1661. {
  1662. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1663. dprintk("svc: svc_sock_free(%p)\n", svsk);
  1664. if (svsk->sk_info_authunix != NULL)
  1665. svcauth_unix_info_release(svsk->sk_info_authunix);
  1666. if (svsk->sk_sock->file)
  1667. sockfd_put(svsk->sk_sock);
  1668. else
  1669. sock_release(svsk->sk_sock);
  1670. kfree(svsk);
  1671. }
  1672. /*
  1673. * Remove a dead socket
  1674. */
  1675. static void
  1676. svc_delete_socket(struct svc_sock *svsk)
  1677. {
  1678. struct svc_serv *serv;
  1679. struct sock *sk;
  1680. dprintk("svc: svc_delete_socket(%p)\n", svsk);
  1681. serv = svsk->sk_xprt.xpt_server;
  1682. sk = svsk->sk_sk;
  1683. svsk->sk_xprt.xpt_ops->xpo_detach(&svsk->sk_xprt);
  1684. spin_lock_bh(&serv->sv_lock);
  1685. if (!test_and_set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags))
  1686. list_del_init(&svsk->sk_list);
  1687. /*
  1688. * We used to delete the svc_sock from whichever list
  1689. * it's sk_ready node was on, but we don't actually
  1690. * need to. This is because the only time we're called
  1691. * while still attached to a queue, the queue itself
  1692. * is about to be destroyed (in svc_destroy).
  1693. */
  1694. if (!test_and_set_bit(XPT_DEAD, &svsk->sk_xprt.xpt_flags)) {
  1695. BUG_ON(atomic_read(&svsk->sk_xprt.xpt_ref.refcount) < 2);
  1696. if (test_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags))
  1697. serv->sv_tmpcnt--;
  1698. svc_xprt_put(&svsk->sk_xprt);
  1699. }
  1700. spin_unlock_bh(&serv->sv_lock);
  1701. }
  1702. static void svc_close_socket(struct svc_sock *svsk)
  1703. {
  1704. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1705. if (test_and_set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
  1706. /* someone else will have to effect the close */
  1707. return;
  1708. svc_xprt_get(&svsk->sk_xprt);
  1709. svc_delete_socket(svsk);
  1710. clear_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
  1711. svc_xprt_put(&svsk->sk_xprt);
  1712. }
  1713. void svc_force_close_socket(struct svc_sock *svsk)
  1714. {
  1715. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1716. if (test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)) {
  1717. /* Waiting to be processed, but no threads left,
  1718. * So just remove it from the waiting list
  1719. */
  1720. list_del_init(&svsk->sk_ready);
  1721. clear_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
  1722. }
  1723. svc_close_socket(svsk);
  1724. }
  1725. /*
  1726. * Handle defer and revisit of requests
  1727. */
  1728. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  1729. {
  1730. struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
  1731. struct svc_sock *svsk;
  1732. if (too_many) {
  1733. svc_xprt_put(&dr->svsk->sk_xprt);
  1734. kfree(dr);
  1735. return;
  1736. }
  1737. dprintk("revisit queued\n");
  1738. svsk = dr->svsk;
  1739. dr->svsk = NULL;
  1740. spin_lock(&svsk->sk_lock);
  1741. list_add(&dr->handle.recent, &svsk->sk_deferred);
  1742. spin_unlock(&svsk->sk_lock);
  1743. set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
  1744. svc_sock_enqueue(svsk);
  1745. svc_xprt_put(&svsk->sk_xprt);
  1746. }
  1747. static struct cache_deferred_req *
  1748. svc_defer(struct cache_req *req)
  1749. {
  1750. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  1751. int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
  1752. struct svc_deferred_req *dr;
  1753. if (rqstp->rq_arg.page_len)
  1754. return NULL; /* if more than a page, give up FIXME */
  1755. if (rqstp->rq_deferred) {
  1756. dr = rqstp->rq_deferred;
  1757. rqstp->rq_deferred = NULL;
  1758. } else {
  1759. int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1760. /* FIXME maybe discard if size too large */
  1761. dr = kmalloc(size, GFP_KERNEL);
  1762. if (dr == NULL)
  1763. return NULL;
  1764. dr->handle.owner = rqstp->rq_server;
  1765. dr->prot = rqstp->rq_prot;
  1766. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  1767. dr->addrlen = rqstp->rq_addrlen;
  1768. dr->daddr = rqstp->rq_daddr;
  1769. dr->argslen = rqstp->rq_arg.len >> 2;
  1770. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
  1771. }
  1772. svc_xprt_get(rqstp->rq_xprt);
  1773. dr->svsk = rqstp->rq_sock;
  1774. dr->handle.revisit = svc_revisit;
  1775. return &dr->handle;
  1776. }
  1777. /*
  1778. * recv data from a deferred request into an active one
  1779. */
  1780. static int svc_deferred_recv(struct svc_rqst *rqstp)
  1781. {
  1782. struct svc_deferred_req *dr = rqstp->rq_deferred;
  1783. rqstp->rq_arg.head[0].iov_base = dr->args;
  1784. rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
  1785. rqstp->rq_arg.page_len = 0;
  1786. rqstp->rq_arg.len = dr->argslen<<2;
  1787. rqstp->rq_prot = dr->prot;
  1788. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  1789. rqstp->rq_addrlen = dr->addrlen;
  1790. rqstp->rq_daddr = dr->daddr;
  1791. rqstp->rq_respages = rqstp->rq_pages;
  1792. return dr->argslen<<2;
  1793. }
  1794. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
  1795. {
  1796. struct svc_deferred_req *dr = NULL;
  1797. if (!test_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags))
  1798. return NULL;
  1799. spin_lock(&svsk->sk_lock);
  1800. clear_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
  1801. if (!list_empty(&svsk->sk_deferred)) {
  1802. dr = list_entry(svsk->sk_deferred.next,
  1803. struct svc_deferred_req,
  1804. handle.recent);
  1805. list_del_init(&dr->handle.recent);
  1806. set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
  1807. }
  1808. spin_unlock(&svsk->sk_lock);
  1809. return dr;
  1810. }