svcsock.c 49 KB

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