svcsock.c 54 KB

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