svcsock.c 50 KB

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