blob: 5b0fe1b66a23d5fbed578179268bedc15eae52c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/fcntl.h>
25#include <linux/net.h>
26#include <linux/in.h>
27#include <linux/inet.h>
28#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070029#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/unistd.h>
31#include <linux/slab.h>
32#include <linux/netdevice.h>
33#include <linux/skbuff.h>
34#include <net/sock.h>
35#include <net/checksum.h>
36#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070037#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include <asm/ioctls.h>
40
41#include <linux/sunrpc/types.h>
42#include <linux/sunrpc/xdr.h>
43#include <linux/sunrpc/svcsock.h>
44#include <linux/sunrpc/stats.h>
45
46/* SMP locking strategy:
47 *
48 * svc_serv->sv_lock protects most stuff for that service.
49 *
50 * Some flags can be set to certain values at any time
51 * providing that certain rules are followed:
52 *
53 * SK_BUSY can be set to 0 at any time.
54 * svc_sock_enqueue must be called afterwards
55 * SK_CONN, SK_DATA, can be set or cleared at any time.
56 * after a set, svc_sock_enqueue must be called.
57 * after a clear, the socket must be read/accepted
58 * if this succeeds, it must be set again.
59 * SK_CLOSE can set at any time. It is never cleared.
60 *
61 */
62
63#define RPCDBG_FACILITY RPCDBG_SVCSOCK
64
65
66static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
67 int *errp, int pmap_reg);
68static void svc_udp_data_ready(struct sock *, int);
69static int svc_udp_recvfrom(struct svc_rqst *);
70static int svc_udp_sendto(struct svc_rqst *);
71
72static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
73static int svc_deferred_recv(struct svc_rqst *rqstp);
74static struct cache_deferred_req *svc_defer(struct cache_req *req);
75
76/*
77 * Queue up an idle server thread. Must have serv->sv_lock held.
78 * Note: this is really a stack rather than a queue, so that we only
79 * use as many different threads as we need, and the rest don't polute
80 * the cache.
81 */
82static inline void
83svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
84{
85 list_add(&rqstp->rq_list, &serv->sv_threads);
86}
87
88/*
89 * Dequeue an nfsd thread. Must have serv->sv_lock held.
90 */
91static inline void
92svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
93{
94 list_del(&rqstp->rq_list);
95}
96
97/*
98 * Release an skbuff after use
99 */
100static inline void
101svc_release_skb(struct svc_rqst *rqstp)
102{
103 struct sk_buff *skb = rqstp->rq_skbuff;
104 struct svc_deferred_req *dr = rqstp->rq_deferred;
105
106 if (skb) {
107 rqstp->rq_skbuff = NULL;
108
109 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
110 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
111 }
112 if (dr) {
113 rqstp->rq_deferred = NULL;
114 kfree(dr);
115 }
116}
117
118/*
119 * Any space to write?
120 */
121static inline unsigned long
122svc_sock_wspace(struct svc_sock *svsk)
123{
124 int wspace;
125
126 if (svsk->sk_sock->type == SOCK_STREAM)
127 wspace = sk_stream_wspace(svsk->sk_sk);
128 else
129 wspace = sock_wspace(svsk->sk_sk);
130
131 return wspace;
132}
133
134/*
135 * Queue up a socket with data pending. If there are idle nfsd
136 * processes, wake 'em up.
137 *
138 */
139static void
140svc_sock_enqueue(struct svc_sock *svsk)
141{
142 struct svc_serv *serv = svsk->sk_server;
143 struct svc_rqst *rqstp;
144
145 if (!(svsk->sk_flags &
146 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
147 return;
148 if (test_bit(SK_DEAD, &svsk->sk_flags))
149 return;
150
151 spin_lock_bh(&serv->sv_lock);
152
153 if (!list_empty(&serv->sv_threads) &&
154 !list_empty(&serv->sv_sockets))
155 printk(KERN_ERR
156 "svc_sock_enqueue: threads and sockets both waiting??\n");
157
158 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
159 /* Don't enqueue dead sockets */
160 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
161 goto out_unlock;
162 }
163
164 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
165 /* Don't enqueue socket while daemon is receiving */
166 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
167 goto out_unlock;
168 }
169
170 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
171 if (((svsk->sk_reserved + serv->sv_bufsz)*2
172 > svc_sock_wspace(svsk))
173 && !test_bit(SK_CLOSE, &svsk->sk_flags)
174 && !test_bit(SK_CONN, &svsk->sk_flags)) {
175 /* Don't enqueue while not enough space for reply */
176 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
177 svsk->sk_sk, svsk->sk_reserved+serv->sv_bufsz,
178 svc_sock_wspace(svsk));
179 goto out_unlock;
180 }
181 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
182
183 /* Mark socket as busy. It will remain in this state until the
184 * server has processed all pending data and put the socket back
185 * on the idle list.
186 */
187 set_bit(SK_BUSY, &svsk->sk_flags);
188
189 if (!list_empty(&serv->sv_threads)) {
190 rqstp = list_entry(serv->sv_threads.next,
191 struct svc_rqst,
192 rq_list);
193 dprintk("svc: socket %p served by daemon %p\n",
194 svsk->sk_sk, rqstp);
195 svc_serv_dequeue(serv, rqstp);
196 if (rqstp->rq_sock)
197 printk(KERN_ERR
198 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
199 rqstp, rqstp->rq_sock);
200 rqstp->rq_sock = svsk;
201 svsk->sk_inuse++;
202 rqstp->rq_reserved = serv->sv_bufsz;
203 svsk->sk_reserved += rqstp->rq_reserved;
204 wake_up(&rqstp->rq_wait);
205 } else {
206 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
207 list_add_tail(&svsk->sk_ready, &serv->sv_sockets);
208 }
209
210out_unlock:
211 spin_unlock_bh(&serv->sv_lock);
212}
213
214/*
215 * Dequeue the first socket. Must be called with the serv->sv_lock held.
216 */
217static inline struct svc_sock *
218svc_sock_dequeue(struct svc_serv *serv)
219{
220 struct svc_sock *svsk;
221
222 if (list_empty(&serv->sv_sockets))
223 return NULL;
224
225 svsk = list_entry(serv->sv_sockets.next,
226 struct svc_sock, sk_ready);
227 list_del_init(&svsk->sk_ready);
228
229 dprintk("svc: socket %p dequeued, inuse=%d\n",
230 svsk->sk_sk, svsk->sk_inuse);
231
232 return svsk;
233}
234
235/*
236 * Having read something from a socket, check whether it
237 * needs to be re-enqueued.
238 * Note: SK_DATA only gets cleared when a read-attempt finds
239 * no (or insufficient) data.
240 */
241static inline void
242svc_sock_received(struct svc_sock *svsk)
243{
244 clear_bit(SK_BUSY, &svsk->sk_flags);
245 svc_sock_enqueue(svsk);
246}
247
248
249/**
250 * svc_reserve - change the space reserved for the reply to a request.
251 * @rqstp: The request in question
252 * @space: new max space to reserve
253 *
254 * Each request reserves some space on the output queue of the socket
255 * to make sure the reply fits. This function reduces that reserved
256 * space to be the amount of space used already, plus @space.
257 *
258 */
259void svc_reserve(struct svc_rqst *rqstp, int space)
260{
261 space += rqstp->rq_res.head[0].iov_len;
262
263 if (space < rqstp->rq_reserved) {
264 struct svc_sock *svsk = rqstp->rq_sock;
265 spin_lock_bh(&svsk->sk_server->sv_lock);
266 svsk->sk_reserved -= (rqstp->rq_reserved - space);
267 rqstp->rq_reserved = space;
268 spin_unlock_bh(&svsk->sk_server->sv_lock);
269
270 svc_sock_enqueue(svsk);
271 }
272}
273
274/*
275 * Release a socket after use.
276 */
277static inline void
278svc_sock_put(struct svc_sock *svsk)
279{
280 struct svc_serv *serv = svsk->sk_server;
281
282 spin_lock_bh(&serv->sv_lock);
283 if (!--(svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
284 spin_unlock_bh(&serv->sv_lock);
285 dprintk("svc: releasing dead socket\n");
286 sock_release(svsk->sk_sock);
287 kfree(svsk);
288 }
289 else
290 spin_unlock_bh(&serv->sv_lock);
291}
292
293static void
294svc_sock_release(struct svc_rqst *rqstp)
295{
296 struct svc_sock *svsk = rqstp->rq_sock;
297
298 svc_release_skb(rqstp);
299
300 svc_free_allpages(rqstp);
301 rqstp->rq_res.page_len = 0;
302 rqstp->rq_res.page_base = 0;
303
304
305 /* Reset response buffer and release
306 * the reservation.
307 * But first, check that enough space was reserved
308 * for the reply, otherwise we have a bug!
309 */
310 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
311 printk(KERN_ERR "RPC request reserved %d but used %d\n",
312 rqstp->rq_reserved,
313 rqstp->rq_res.len);
314
315 rqstp->rq_res.head[0].iov_len = 0;
316 svc_reserve(rqstp, 0);
317 rqstp->rq_sock = NULL;
318
319 svc_sock_put(svsk);
320}
321
322/*
323 * External function to wake up a server waiting for data
324 */
325void
326svc_wake_up(struct svc_serv *serv)
327{
328 struct svc_rqst *rqstp;
329
330 spin_lock_bh(&serv->sv_lock);
331 if (!list_empty(&serv->sv_threads)) {
332 rqstp = list_entry(serv->sv_threads.next,
333 struct svc_rqst,
334 rq_list);
335 dprintk("svc: daemon %p woken up.\n", rqstp);
336 /*
337 svc_serv_dequeue(serv, rqstp);
338 rqstp->rq_sock = NULL;
339 */
340 wake_up(&rqstp->rq_wait);
341 }
342 spin_unlock_bh(&serv->sv_lock);
343}
344
345/*
346 * Generic sendto routine
347 */
348static int
349svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
350{
351 struct svc_sock *svsk = rqstp->rq_sock;
352 struct socket *sock = svsk->sk_sock;
353 int slen;
354 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
355 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
356 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
357 int len = 0;
358 int result;
359 int size;
360 struct page **ppage = xdr->pages;
361 size_t base = xdr->page_base;
362 unsigned int pglen = xdr->page_len;
363 unsigned int flags = MSG_MORE;
364
365 slen = xdr->len;
366
367 if (rqstp->rq_prot == IPPROTO_UDP) {
368 /* set the source and destination */
369 struct msghdr msg;
370 msg.msg_name = &rqstp->rq_addr;
371 msg.msg_namelen = sizeof(rqstp->rq_addr);
372 msg.msg_iov = NULL;
373 msg.msg_iovlen = 0;
374 msg.msg_flags = MSG_MORE;
375
376 msg.msg_control = cmh;
377 msg.msg_controllen = sizeof(buffer);
378 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
379 cmh->cmsg_level = SOL_IP;
380 cmh->cmsg_type = IP_PKTINFO;
381 pki->ipi_ifindex = 0;
382 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
383
384 if (sock_sendmsg(sock, &msg, 0) < 0)
385 goto out;
386 }
387
388 /* send head */
389 if (slen == xdr->head[0].iov_len)
390 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700391 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (len != xdr->head[0].iov_len)
393 goto out;
394 slen -= xdr->head[0].iov_len;
395 if (slen == 0)
396 goto out;
397
398 /* send page data */
399 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
400 while (pglen > 0) {
401 if (slen == size)
402 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700403 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (result > 0)
405 len += result;
406 if (result != size)
407 goto out;
408 slen -= size;
409 pglen -= size;
410 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
411 base = 0;
412 ppage++;
413 }
414 /* send tail */
415 if (xdr->tail[0].iov_len) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700416 result = kernel_sendpage(sock, rqstp->rq_respages[rqstp->rq_restailpage],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ((unsigned long)xdr->tail[0].iov_base)& (PAGE_SIZE-1),
418 xdr->tail[0].iov_len, 0);
419
420 if (result > 0)
421 len += result;
422 }
423out:
424 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
425 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
426 rqstp->rq_addr.sin_addr.s_addr);
427
428 return len;
429}
430
431/*
432 * Check input queue length
433 */
434static int
435svc_recv_available(struct svc_sock *svsk)
436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct socket *sock = svsk->sk_sock;
438 int avail, err;
439
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700440 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return (err >= 0)? avail : err;
443}
444
445/*
446 * Generic recvfrom routine.
447 */
448static int
449svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
450{
451 struct msghdr msg;
452 struct socket *sock;
453 int len, alen;
454
455 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
456 sock = rqstp->rq_sock->sk_sock;
457
458 msg.msg_name = &rqstp->rq_addr;
459 msg.msg_namelen = sizeof(rqstp->rq_addr);
460 msg.msg_control = NULL;
461 msg.msg_controllen = 0;
462
463 msg.msg_flags = MSG_DONTWAIT;
464
465 len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
466
467 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
468 * possibly we should cache this in the svc_sock structure
469 * at accept time. FIXME
470 */
471 alen = sizeof(rqstp->rq_addr);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700472 kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
475 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
476
477 return len;
478}
479
480/*
481 * Set socket snd and rcv buffer lengths
482 */
483static inline void
484svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
485{
486#if 0
487 mm_segment_t oldfs;
488 oldfs = get_fs(); set_fs(KERNEL_DS);
489 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
490 (char*)&snd, sizeof(snd));
491 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
492 (char*)&rcv, sizeof(rcv));
493#else
494 /* sock_setsockopt limits use to sysctl_?mem_max,
495 * which isn't acceptable. Until that is made conditional
496 * on not having CAP_SYS_RESOURCE or similar, we go direct...
497 * DaveM said I could!
498 */
499 lock_sock(sock->sk);
500 sock->sk->sk_sndbuf = snd * 2;
501 sock->sk->sk_rcvbuf = rcv * 2;
502 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
503 release_sock(sock->sk);
504#endif
505}
506/*
507 * INET callback when data has been received on the socket.
508 */
509static void
510svc_udp_data_ready(struct sock *sk, int count)
511{
Neil Brown939bb7e2005-09-13 01:25:39 -0700512 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Neil Brown939bb7e2005-09-13 01:25:39 -0700514 if (svsk) {
515 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
516 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
517 set_bit(SK_DATA, &svsk->sk_flags);
518 svc_sock_enqueue(svsk);
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
521 wake_up_interruptible(sk->sk_sleep);
522}
523
524/*
525 * INET callback when space is newly available on the socket.
526 */
527static void
528svc_write_space(struct sock *sk)
529{
530 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
531
532 if (svsk) {
533 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
534 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
535 svc_sock_enqueue(svsk);
536 }
537
538 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700539 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 svsk);
541 wake_up_interruptible(sk->sk_sleep);
542 }
543}
544
545/*
546 * Receive a datagram from a UDP socket.
547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int
549svc_udp_recvfrom(struct svc_rqst *rqstp)
550{
551 struct svc_sock *svsk = rqstp->rq_sock;
552 struct svc_serv *serv = svsk->sk_server;
553 struct sk_buff *skb;
554 int err, len;
555
556 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
557 /* udp sockets need large rcvbuf as all pending
558 * requests are still in that buffer. sndbuf must
559 * also be large enough that there is enough space
560 * for one reply per thread.
561 */
562 svc_sock_setbufsize(svsk->sk_sock,
563 (serv->sv_nrthreads+3) * serv->sv_bufsz,
564 (serv->sv_nrthreads+3) * serv->sv_bufsz);
565
566 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
567 svc_sock_received(svsk);
568 return svc_deferred_recv(rqstp);
569 }
570
571 clear_bit(SK_DATA, &svsk->sk_flags);
572 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
573 if (err == -EAGAIN) {
574 svc_sock_received(svsk);
575 return err;
576 }
577 /* possibly an icmp error */
578 dprintk("svc: recvfrom returned error %d\n", -err);
579 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700580 if (skb->tstamp.off_sec == 0) {
581 struct timeval tv;
582
583 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700584 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700585 skb_set_timestamp(skb, &tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Don't enable netstamp, sunrpc doesn't
587 need that much accuracy */
588 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700589 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
591
592 /*
593 * Maybe more packets - kick another thread ASAP.
594 */
595 svc_sock_received(svsk);
596
597 len = skb->len - sizeof(struct udphdr);
598 rqstp->rq_arg.len = len;
599
600 rqstp->rq_prot = IPPROTO_UDP;
601
602 /* Get sender address */
603 rqstp->rq_addr.sin_family = AF_INET;
604 rqstp->rq_addr.sin_port = skb->h.uh->source;
605 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
606 rqstp->rq_daddr = skb->nh.iph->daddr;
607
608 if (skb_is_nonlinear(skb)) {
609 /* we have to copy */
610 local_bh_disable();
611 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
612 local_bh_enable();
613 /* checksum error */
614 skb_free_datagram(svsk->sk_sk, skb);
615 return 0;
616 }
617 local_bh_enable();
618 skb_free_datagram(svsk->sk_sk, skb);
619 } else {
620 /* we can use it in-place */
621 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
622 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800623 if (skb_checksum_complete(skb)) {
624 skb_free_datagram(svsk->sk_sk, skb);
625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 rqstp->rq_skbuff = skb;
628 }
629
630 rqstp->rq_arg.page_base = 0;
631 if (len <= rqstp->rq_arg.head[0].iov_len) {
632 rqstp->rq_arg.head[0].iov_len = len;
633 rqstp->rq_arg.page_len = 0;
634 } else {
635 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
636 rqstp->rq_argused += (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
637 }
638
639 if (serv->sv_stats)
640 serv->sv_stats->netudpcnt++;
641
642 return len;
643}
644
645static int
646svc_udp_sendto(struct svc_rqst *rqstp)
647{
648 int error;
649
650 error = svc_sendto(rqstp, &rqstp->rq_res);
651 if (error == -ECONNREFUSED)
652 /* ICMP error on earlier request. */
653 error = svc_sendto(rqstp, &rqstp->rq_res);
654
655 return error;
656}
657
658static void
659svc_udp_init(struct svc_sock *svsk)
660{
661 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
662 svsk->sk_sk->sk_write_space = svc_write_space;
663 svsk->sk_recvfrom = svc_udp_recvfrom;
664 svsk->sk_sendto = svc_udp_sendto;
665
666 /* initialise setting must have enough space to
667 * receive and respond to one request.
668 * svc_udp_recvfrom will re-adjust if necessary
669 */
670 svc_sock_setbufsize(svsk->sk_sock,
671 3 * svsk->sk_server->sv_bufsz,
672 3 * svsk->sk_server->sv_bufsz);
673
674 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
675 set_bit(SK_CHNGBUF, &svsk->sk_flags);
676}
677
678/*
679 * A data_ready event on a listening socket means there's a connection
680 * pending. Do not use state_change as a substitute for it.
681 */
682static void
683svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
684{
Neil Brown939bb7e2005-09-13 01:25:39 -0700685 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700688 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Neil Brown939bb7e2005-09-13 01:25:39 -0700690 /*
691 * This callback may called twice when a new connection
692 * is established as a child socket inherits everything
693 * from a parent LISTEN socket.
694 * 1) data_ready method of the parent socket will be called
695 * when one of child sockets become ESTABLISHED.
696 * 2) data_ready method of the child socket may be called
697 * when it receives data before the socket is accepted.
698 * In case of 2, we should ignore it silently.
699 */
700 if (sk->sk_state == TCP_LISTEN) {
701 if (svsk) {
702 set_bit(SK_CONN, &svsk->sk_flags);
703 svc_sock_enqueue(svsk);
704 } else
705 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
709 wake_up_interruptible_all(sk->sk_sleep);
710}
711
712/*
713 * A state change on a connected socket means it's dying or dead.
714 */
715static void
716svc_tcp_state_change(struct sock *sk)
717{
Neil Brown939bb7e2005-09-13 01:25:39 -0700718 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700721 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Neil Brown939bb7e2005-09-13 01:25:39 -0700723 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700725 else {
726 set_bit(SK_CLOSE, &svsk->sk_flags);
727 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
730 wake_up_interruptible_all(sk->sk_sleep);
731}
732
733static void
734svc_tcp_data_ready(struct sock *sk, int count)
735{
Neil Brown939bb7e2005-09-13 01:25:39 -0700736 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700739 sk, sk->sk_user_data);
740 if (svsk) {
741 set_bit(SK_DATA, &svsk->sk_flags);
742 svc_sock_enqueue(svsk);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
745 wake_up_interruptible(sk->sk_sleep);
746}
747
748/*
749 * Accept a TCP connection
750 */
751static void
752svc_tcp_accept(struct svc_sock *svsk)
753{
754 struct sockaddr_in sin;
755 struct svc_serv *serv = svsk->sk_server;
756 struct socket *sock = svsk->sk_sock;
757 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct svc_sock *newsvsk;
759 int err, slen;
760
761 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
762 if (!sock)
763 return;
764
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700765 clear_bit(SK_CONN, &svsk->sk_flags);
766 err = kernel_accept(sock, &newsock, O_NONBLOCK);
767 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (err == -ENOMEM)
769 printk(KERN_WARNING "%s: no more sockets!\n",
770 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700771 else if (err != -EAGAIN && net_ratelimit())
772 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
773 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return;
775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 set_bit(SK_CONN, &svsk->sk_flags);
778 svc_sock_enqueue(svsk);
779
780 slen = sizeof(sin);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700781 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (err < 0) {
783 if (net_ratelimit())
784 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
785 serv->sv_name, -err);
786 goto failed; /* aborted connection or whatever */
787 }
788
789 /* Ideally, we would want to reject connections from unauthorized
790 * hosts here, but when we get encription, the IP of the host won't
791 * tell us anything. For now just warn about unpriv connections.
792 */
793 if (ntohs(sin.sin_port) >= 1024) {
794 dprintk(KERN_WARNING
795 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
796 serv->sv_name,
797 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
798 }
799
800 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
801 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
802
803 /* make sure that a write doesn't block forever when
804 * low on memory
805 */
806 newsock->sk->sk_sndtimeo = HZ*30;
807
808 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
809 goto failed;
810
811
812 /* make sure that we don't have too many active connections.
813 * If we have, something must be dropped.
814 *
815 * There's no point in trying to do random drop here for
816 * DoS prevention. The NFS clients does 1 reconnect in 15
817 * seconds. An attacker can easily beat that.
818 *
819 * The only somewhat efficient mechanism would be if drop
820 * old connections from the same IP first. But right now
821 * we don't even record the client IP in svc_sock.
822 */
823 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
824 struct svc_sock *svsk = NULL;
825 spin_lock_bh(&serv->sv_lock);
826 if (!list_empty(&serv->sv_tempsocks)) {
827 if (net_ratelimit()) {
828 /* Try to help the admin */
829 printk(KERN_NOTICE "%s: too many open TCP "
830 "sockets, consider increasing the "
831 "number of nfsd threads\n",
832 serv->sv_name);
833 printk(KERN_NOTICE "%s: last TCP connect from "
834 "%u.%u.%u.%u:%d\n",
835 serv->sv_name,
836 NIPQUAD(sin.sin_addr.s_addr),
837 ntohs(sin.sin_port));
838 }
839 /*
840 * Always select the oldest socket. It's not fair,
841 * but so is life
842 */
843 svsk = list_entry(serv->sv_tempsocks.prev,
844 struct svc_sock,
845 sk_list);
846 set_bit(SK_CLOSE, &svsk->sk_flags);
847 svsk->sk_inuse ++;
848 }
849 spin_unlock_bh(&serv->sv_lock);
850
851 if (svsk) {
852 svc_sock_enqueue(svsk);
853 svc_sock_put(svsk);
854 }
855
856 }
857
858 if (serv->sv_stats)
859 serv->sv_stats->nettcpconn++;
860
861 return;
862
863failed:
864 sock_release(newsock);
865 return;
866}
867
868/*
869 * Receive data from a TCP socket.
870 */
871static int
872svc_tcp_recvfrom(struct svc_rqst *rqstp)
873{
874 struct svc_sock *svsk = rqstp->rq_sock;
875 struct svc_serv *serv = svsk->sk_server;
876 int len;
877 struct kvec vec[RPCSVC_MAXPAGES];
878 int pnum, vlen;
879
880 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
881 svsk, test_bit(SK_DATA, &svsk->sk_flags),
882 test_bit(SK_CONN, &svsk->sk_flags),
883 test_bit(SK_CLOSE, &svsk->sk_flags));
884
885 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
886 svc_sock_received(svsk);
887 return svc_deferred_recv(rqstp);
888 }
889
890 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
891 svc_delete_socket(svsk);
892 return 0;
893 }
894
895 if (test_bit(SK_CONN, &svsk->sk_flags)) {
896 svc_tcp_accept(svsk);
897 svc_sock_received(svsk);
898 return 0;
899 }
900
901 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
902 /* sndbuf needs to have room for one request
903 * per thread, otherwise we can stall even when the
904 * network isn't a bottleneck.
905 * rcvbuf just needs to be able to hold a few requests.
906 * Normally they will be removed from the queue
907 * as soon a a complete request arrives.
908 */
909 svc_sock_setbufsize(svsk->sk_sock,
910 (serv->sv_nrthreads+3) * serv->sv_bufsz,
911 3 * serv->sv_bufsz);
912
913 clear_bit(SK_DATA, &svsk->sk_flags);
914
915 /* Receive data. If we haven't got the record length yet, get
916 * the next four bytes. Otherwise try to gobble up as much as
917 * possible up to the complete record length.
918 */
919 if (svsk->sk_tcplen < 4) {
920 unsigned long want = 4 - svsk->sk_tcplen;
921 struct kvec iov;
922
923 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
924 iov.iov_len = want;
925 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
926 goto error;
927 svsk->sk_tcplen += len;
928
929 if (len < want) {
930 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
931 len, want);
932 svc_sock_received(svsk);
933 return -EAGAIN; /* record header not complete */
934 }
935
936 svsk->sk_reclen = ntohl(svsk->sk_reclen);
937 if (!(svsk->sk_reclen & 0x80000000)) {
938 /* FIXME: technically, a record can be fragmented,
939 * and non-terminal fragments will not have the top
940 * bit set in the fragment length header.
941 * But apparently no known nfs clients send fragmented
942 * records. */
943 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
944 (unsigned long) svsk->sk_reclen);
945 goto err_delete;
946 }
947 svsk->sk_reclen &= 0x7fffffff;
948 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
949 if (svsk->sk_reclen > serv->sv_bufsz) {
950 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
951 (unsigned long) svsk->sk_reclen);
952 goto err_delete;
953 }
954 }
955
956 /* Check whether enough data is available */
957 len = svc_recv_available(svsk);
958 if (len < 0)
959 goto error;
960
961 if (len < svsk->sk_reclen) {
962 dprintk("svc: incomplete TCP record (%d of %d)\n",
963 len, svsk->sk_reclen);
964 svc_sock_received(svsk);
965 return -EAGAIN; /* record not complete */
966 }
967 len = svsk->sk_reclen;
968 set_bit(SK_DATA, &svsk->sk_flags);
969
970 vec[0] = rqstp->rq_arg.head[0];
971 vlen = PAGE_SIZE;
972 pnum = 1;
973 while (vlen < len) {
974 vec[pnum].iov_base = page_address(rqstp->rq_argpages[rqstp->rq_argused++]);
975 vec[pnum].iov_len = PAGE_SIZE;
976 pnum++;
977 vlen += PAGE_SIZE;
978 }
979
980 /* Now receive data */
981 len = svc_recvfrom(rqstp, vec, pnum, len);
982 if (len < 0)
983 goto error;
984
985 dprintk("svc: TCP complete record (%d bytes)\n", len);
986 rqstp->rq_arg.len = len;
987 rqstp->rq_arg.page_base = 0;
988 if (len <= rqstp->rq_arg.head[0].iov_len) {
989 rqstp->rq_arg.head[0].iov_len = len;
990 rqstp->rq_arg.page_len = 0;
991 } else {
992 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
993 }
994
995 rqstp->rq_skbuff = NULL;
996 rqstp->rq_prot = IPPROTO_TCP;
997
998 /* Reset TCP read info */
999 svsk->sk_reclen = 0;
1000 svsk->sk_tcplen = 0;
1001
1002 svc_sock_received(svsk);
1003 if (serv->sv_stats)
1004 serv->sv_stats->nettcpcnt++;
1005
1006 return len;
1007
1008 err_delete:
1009 svc_delete_socket(svsk);
1010 return -EAGAIN;
1011
1012 error:
1013 if (len == -EAGAIN) {
1014 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1015 svc_sock_received(svsk);
1016 } else {
1017 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1018 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001019 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
1022 return len;
1023}
1024
1025/*
1026 * Send out data on TCP socket.
1027 */
1028static int
1029svc_tcp_sendto(struct svc_rqst *rqstp)
1030{
1031 struct xdr_buf *xbufp = &rqstp->rq_res;
1032 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001033 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* Set up the first element of the reply kvec.
1036 * Any other kvecs that may be in use have been taken
1037 * care of by the server implementation itself.
1038 */
1039 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1040 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1041
1042 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1043 return -ENOTCONN;
1044
1045 sent = svc_sendto(rqstp, &rqstp->rq_res);
1046 if (sent != xbufp->len) {
1047 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1048 rqstp->rq_sock->sk_server->sv_name,
1049 (sent<0)?"got error":"sent only",
1050 sent, xbufp->len);
1051 svc_delete_socket(rqstp->rq_sock);
1052 sent = -EAGAIN;
1053 }
1054 return sent;
1055}
1056
1057static void
1058svc_tcp_init(struct svc_sock *svsk)
1059{
1060 struct sock *sk = svsk->sk_sk;
1061 struct tcp_sock *tp = tcp_sk(sk);
1062
1063 svsk->sk_recvfrom = svc_tcp_recvfrom;
1064 svsk->sk_sendto = svc_tcp_sendto;
1065
1066 if (sk->sk_state == TCP_LISTEN) {
1067 dprintk("setting up TCP socket for listening\n");
1068 sk->sk_data_ready = svc_tcp_listen_data_ready;
1069 set_bit(SK_CONN, &svsk->sk_flags);
1070 } else {
1071 dprintk("setting up TCP socket for reading\n");
1072 sk->sk_state_change = svc_tcp_state_change;
1073 sk->sk_data_ready = svc_tcp_data_ready;
1074 sk->sk_write_space = svc_write_space;
1075
1076 svsk->sk_reclen = 0;
1077 svsk->sk_tcplen = 0;
1078
1079 tp->nonagle = 1; /* disable Nagle's algorithm */
1080
1081 /* initialise setting must have enough space to
1082 * receive and respond to one request.
1083 * svc_tcp_recvfrom will re-adjust if necessary
1084 */
1085 svc_sock_setbufsize(svsk->sk_sock,
1086 3 * svsk->sk_server->sv_bufsz,
1087 3 * svsk->sk_server->sv_bufsz);
1088
1089 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1090 set_bit(SK_DATA, &svsk->sk_flags);
1091 if (sk->sk_state != TCP_ESTABLISHED)
1092 set_bit(SK_CLOSE, &svsk->sk_flags);
1093 }
1094}
1095
1096void
1097svc_sock_update_bufs(struct svc_serv *serv)
1098{
1099 /*
1100 * The number of server threads has changed. Update
1101 * rcvbuf and sndbuf accordingly on all sockets
1102 */
1103 struct list_head *le;
1104
1105 spin_lock_bh(&serv->sv_lock);
1106 list_for_each(le, &serv->sv_permsocks) {
1107 struct svc_sock *svsk =
1108 list_entry(le, struct svc_sock, sk_list);
1109 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1110 }
1111 list_for_each(le, &serv->sv_tempsocks) {
1112 struct svc_sock *svsk =
1113 list_entry(le, struct svc_sock, sk_list);
1114 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1115 }
1116 spin_unlock_bh(&serv->sv_lock);
1117}
1118
1119/*
1120 * Receive the next request on any socket.
1121 */
1122int
1123svc_recv(struct svc_serv *serv, struct svc_rqst *rqstp, long timeout)
1124{
1125 struct svc_sock *svsk =NULL;
1126 int len;
1127 int pages;
1128 struct xdr_buf *arg;
1129 DECLARE_WAITQUEUE(wait, current);
1130
1131 dprintk("svc: server %p waiting for data (to = %ld)\n",
1132 rqstp, timeout);
1133
1134 if (rqstp->rq_sock)
1135 printk(KERN_ERR
1136 "svc_recv: service %p, socket not NULL!\n",
1137 rqstp);
1138 if (waitqueue_active(&rqstp->rq_wait))
1139 printk(KERN_ERR
1140 "svc_recv: service %p, wait queue active!\n",
1141 rqstp);
1142
1143 /* Initialize the buffers */
1144 /* first reclaim pages that were moved to response list */
1145 svc_pushback_allpages(rqstp);
1146
1147 /* now allocate needed pages. If we get a failure, sleep briefly */
1148 pages = 2 + (serv->sv_bufsz + PAGE_SIZE -1) / PAGE_SIZE;
1149 while (rqstp->rq_arghi < pages) {
1150 struct page *p = alloc_page(GFP_KERNEL);
1151 if (!p) {
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001152 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 continue;
1154 }
1155 rqstp->rq_argpages[rqstp->rq_arghi++] = p;
1156 }
1157
1158 /* Make arg->head point to first page and arg->pages point to rest */
1159 arg = &rqstp->rq_arg;
1160 arg->head[0].iov_base = page_address(rqstp->rq_argpages[0]);
1161 arg->head[0].iov_len = PAGE_SIZE;
1162 rqstp->rq_argused = 1;
1163 arg->pages = rqstp->rq_argpages + 1;
1164 arg->page_base = 0;
1165 /* save at least one page for response */
1166 arg->page_len = (pages-2)*PAGE_SIZE;
1167 arg->len = (pages-1)*PAGE_SIZE;
1168 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001169
1170 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001171 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (signalled())
1173 return -EINTR;
1174
1175 spin_lock_bh(&serv->sv_lock);
1176 if (!list_empty(&serv->sv_tempsocks)) {
1177 svsk = list_entry(serv->sv_tempsocks.next,
1178 struct svc_sock, sk_list);
1179 /* apparently the "standard" is that clients close
1180 * idle connections after 5 minutes, servers after
1181 * 6 minutes
1182 * http://www.connectathon.org/talks96/nfstcp.pdf
1183 */
1184 if (get_seconds() - svsk->sk_lastrecv < 6*60
1185 || test_bit(SK_BUSY, &svsk->sk_flags))
1186 svsk = NULL;
1187 }
1188 if (svsk) {
1189 set_bit(SK_BUSY, &svsk->sk_flags);
1190 set_bit(SK_CLOSE, &svsk->sk_flags);
1191 rqstp->rq_sock = svsk;
1192 svsk->sk_inuse++;
1193 } else if ((svsk = svc_sock_dequeue(serv)) != NULL) {
1194 rqstp->rq_sock = svsk;
1195 svsk->sk_inuse++;
1196 rqstp->rq_reserved = serv->sv_bufsz;
1197 svsk->sk_reserved += rqstp->rq_reserved;
1198 } else {
1199 /* No data pending. Go to sleep */
1200 svc_serv_enqueue(serv, rqstp);
1201
1202 /*
1203 * We have to be able to interrupt this wait
1204 * to bring down the daemons ...
1205 */
1206 set_current_state(TASK_INTERRUPTIBLE);
1207 add_wait_queue(&rqstp->rq_wait, &wait);
1208 spin_unlock_bh(&serv->sv_lock);
1209
1210 schedule_timeout(timeout);
1211
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001212 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 spin_lock_bh(&serv->sv_lock);
1215 remove_wait_queue(&rqstp->rq_wait, &wait);
1216
1217 if (!(svsk = rqstp->rq_sock)) {
1218 svc_serv_dequeue(serv, rqstp);
1219 spin_unlock_bh(&serv->sv_lock);
1220 dprintk("svc: server %p, no data yet\n", rqstp);
1221 return signalled()? -EINTR : -EAGAIN;
1222 }
1223 }
1224 spin_unlock_bh(&serv->sv_lock);
1225
1226 dprintk("svc: server %p, socket %p, inuse=%d\n",
1227 rqstp, svsk, svsk->sk_inuse);
1228 len = svsk->sk_recvfrom(rqstp);
1229 dprintk("svc: got len=%d\n", len);
1230
1231 /* No data, incomplete (TCP) read, or accept() */
1232 if (len == 0 || len == -EAGAIN) {
1233 rqstp->rq_res.len = 0;
1234 svc_sock_release(rqstp);
1235 return -EAGAIN;
1236 }
1237 svsk->sk_lastrecv = get_seconds();
1238 if (test_bit(SK_TEMP, &svsk->sk_flags)) {
1239 /* push active sockets to end of list */
1240 spin_lock_bh(&serv->sv_lock);
1241 if (!list_empty(&svsk->sk_list))
1242 list_move_tail(&svsk->sk_list, &serv->sv_tempsocks);
1243 spin_unlock_bh(&serv->sv_lock);
1244 }
1245
1246 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1247 rqstp->rq_chandle.defer = svc_defer;
1248
1249 if (serv->sv_stats)
1250 serv->sv_stats->netcnt++;
1251 return len;
1252}
1253
1254/*
1255 * Drop request
1256 */
1257void
1258svc_drop(struct svc_rqst *rqstp)
1259{
1260 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1261 svc_sock_release(rqstp);
1262}
1263
1264/*
1265 * Return reply to client.
1266 */
1267int
1268svc_send(struct svc_rqst *rqstp)
1269{
1270 struct svc_sock *svsk;
1271 int len;
1272 struct xdr_buf *xb;
1273
1274 if ((svsk = rqstp->rq_sock) == NULL) {
1275 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1276 __FILE__, __LINE__);
1277 return -EFAULT;
1278 }
1279
1280 /* release the receive skb before sending the reply */
1281 svc_release_skb(rqstp);
1282
1283 /* calculate over-all length */
1284 xb = & rqstp->rq_res;
1285 xb->len = xb->head[0].iov_len +
1286 xb->page_len +
1287 xb->tail[0].iov_len;
1288
Ingo Molnar57b47a52006-03-20 22:35:41 -08001289 /* Grab svsk->sk_mutex to serialize outgoing data. */
1290 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (test_bit(SK_DEAD, &svsk->sk_flags))
1292 len = -ENOTCONN;
1293 else
1294 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001295 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 svc_sock_release(rqstp);
1297
1298 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1299 return 0;
1300 return len;
1301}
1302
1303/*
1304 * Initialize socket for RPC use and create svc_sock struct
1305 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1306 */
1307static struct svc_sock *
1308svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1309 int *errp, int pmap_register)
1310{
1311 struct svc_sock *svsk;
1312 struct sock *inet;
1313
1314 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001315 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 *errp = -ENOMEM;
1317 return NULL;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 inet = sock->sk;
1321
1322 /* Register socket with portmapper */
1323 if (*errp >= 0 && pmap_register)
1324 *errp = svc_register(serv, inet->sk_protocol,
1325 ntohs(inet_sk(inet)->sport));
1326
1327 if (*errp < 0) {
1328 kfree(svsk);
1329 return NULL;
1330 }
1331
1332 set_bit(SK_BUSY, &svsk->sk_flags);
1333 inet->sk_user_data = svsk;
1334 svsk->sk_sock = sock;
1335 svsk->sk_sk = inet;
1336 svsk->sk_ostate = inet->sk_state_change;
1337 svsk->sk_odata = inet->sk_data_ready;
1338 svsk->sk_owspace = inet->sk_write_space;
1339 svsk->sk_server = serv;
1340 svsk->sk_lastrecv = get_seconds();
1341 INIT_LIST_HEAD(&svsk->sk_deferred);
1342 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001343 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /* Initialize the socket */
1346 if (sock->type == SOCK_DGRAM)
1347 svc_udp_init(svsk);
1348 else
1349 svc_tcp_init(svsk);
1350
1351 spin_lock_bh(&serv->sv_lock);
1352 if (!pmap_register) {
1353 set_bit(SK_TEMP, &svsk->sk_flags);
1354 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1355 serv->sv_tmpcnt++;
1356 } else {
1357 clear_bit(SK_TEMP, &svsk->sk_flags);
1358 list_add(&svsk->sk_list, &serv->sv_permsocks);
1359 }
1360 spin_unlock_bh(&serv->sv_lock);
1361
1362 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1363 svsk, svsk->sk_sk);
1364
1365 clear_bit(SK_BUSY, &svsk->sk_flags);
1366 svc_sock_enqueue(svsk);
1367 return svsk;
1368}
1369
1370/*
1371 * Create socket for RPC service.
1372 */
1373static int
1374svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1375{
1376 struct svc_sock *svsk;
1377 struct socket *sock;
1378 int error;
1379 int type;
1380
1381 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1382 serv->sv_program->pg_name, protocol,
1383 NIPQUAD(sin->sin_addr.s_addr),
1384 ntohs(sin->sin_port));
1385
1386 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1387 printk(KERN_WARNING "svc: only UDP and TCP "
1388 "sockets supported\n");
1389 return -EINVAL;
1390 }
1391 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1392
1393 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1394 return error;
1395
Eric Sesterhenn18114742006-09-28 14:37:07 -07001396 if (type == SOCK_STREAM)
1397 sock->sk->sk_reuse = 1; /* allow address reuse */
1398 error = kernel_bind(sock, (struct sockaddr *) sin,
1399 sizeof(*sin));
1400 if (error < 0)
1401 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001404 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 goto bummer;
1406 }
1407
1408 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1409 return 0;
1410
1411bummer:
1412 dprintk("svc: svc_create_socket error = %d\n", -error);
1413 sock_release(sock);
1414 return error;
1415}
1416
1417/*
1418 * Remove a dead socket
1419 */
1420void
1421svc_delete_socket(struct svc_sock *svsk)
1422{
1423 struct svc_serv *serv;
1424 struct sock *sk;
1425
1426 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1427
1428 serv = svsk->sk_server;
1429 sk = svsk->sk_sk;
1430
1431 sk->sk_state_change = svsk->sk_ostate;
1432 sk->sk_data_ready = svsk->sk_odata;
1433 sk->sk_write_space = svsk->sk_owspace;
1434
1435 spin_lock_bh(&serv->sv_lock);
1436
1437 list_del_init(&svsk->sk_list);
1438 list_del_init(&svsk->sk_ready);
1439 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags))
1440 if (test_bit(SK_TEMP, &svsk->sk_flags))
1441 serv->sv_tmpcnt--;
1442
1443 if (!svsk->sk_inuse) {
1444 spin_unlock_bh(&serv->sv_lock);
1445 sock_release(svsk->sk_sock);
1446 kfree(svsk);
1447 } else {
1448 spin_unlock_bh(&serv->sv_lock);
1449 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1450 /* svsk->sk_server = NULL; */
1451 }
1452}
1453
1454/*
1455 * Make a socket for nfsd and lockd
1456 */
1457int
1458svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1459{
1460 struct sockaddr_in sin;
1461
1462 dprintk("svc: creating socket proto = %d\n", protocol);
1463 sin.sin_family = AF_INET;
1464 sin.sin_addr.s_addr = INADDR_ANY;
1465 sin.sin_port = htons(port);
1466 return svc_create_socket(serv, protocol, &sin);
1467}
1468
1469/*
1470 * Handle defer and revisit of requests
1471 */
1472
1473static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1474{
1475 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
1476 struct svc_serv *serv = dreq->owner;
1477 struct svc_sock *svsk;
1478
1479 if (too_many) {
1480 svc_sock_put(dr->svsk);
1481 kfree(dr);
1482 return;
1483 }
1484 dprintk("revisit queued\n");
1485 svsk = dr->svsk;
1486 dr->svsk = NULL;
1487 spin_lock_bh(&serv->sv_lock);
1488 list_add(&dr->handle.recent, &svsk->sk_deferred);
1489 spin_unlock_bh(&serv->sv_lock);
1490 set_bit(SK_DEFERRED, &svsk->sk_flags);
1491 svc_sock_enqueue(svsk);
1492 svc_sock_put(svsk);
1493}
1494
1495static struct cache_deferred_req *
1496svc_defer(struct cache_req *req)
1497{
1498 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1499 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1500 struct svc_deferred_req *dr;
1501
1502 if (rqstp->rq_arg.page_len)
1503 return NULL; /* if more than a page, give up FIXME */
1504 if (rqstp->rq_deferred) {
1505 dr = rqstp->rq_deferred;
1506 rqstp->rq_deferred = NULL;
1507 } else {
1508 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1509 /* FIXME maybe discard if size too large */
1510 dr = kmalloc(size, GFP_KERNEL);
1511 if (dr == NULL)
1512 return NULL;
1513
1514 dr->handle.owner = rqstp->rq_server;
1515 dr->prot = rqstp->rq_prot;
1516 dr->addr = rqstp->rq_addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001517 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 dr->argslen = rqstp->rq_arg.len >> 2;
1519 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1520 }
1521 spin_lock_bh(&rqstp->rq_server->sv_lock);
1522 rqstp->rq_sock->sk_inuse++;
1523 dr->svsk = rqstp->rq_sock;
1524 spin_unlock_bh(&rqstp->rq_server->sv_lock);
1525
1526 dr->handle.revisit = svc_revisit;
1527 return &dr->handle;
1528}
1529
1530/*
1531 * recv data from a deferred request into an active one
1532 */
1533static int svc_deferred_recv(struct svc_rqst *rqstp)
1534{
1535 struct svc_deferred_req *dr = rqstp->rq_deferred;
1536
1537 rqstp->rq_arg.head[0].iov_base = dr->args;
1538 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1539 rqstp->rq_arg.page_len = 0;
1540 rqstp->rq_arg.len = dr->argslen<<2;
1541 rqstp->rq_prot = dr->prot;
1542 rqstp->rq_addr = dr->addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001543 rqstp->rq_daddr = dr->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return dr->argslen<<2;
1545}
1546
1547
1548static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1549{
1550 struct svc_deferred_req *dr = NULL;
1551 struct svc_serv *serv = svsk->sk_server;
1552
1553 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1554 return NULL;
1555 spin_lock_bh(&serv->sv_lock);
1556 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1557 if (!list_empty(&svsk->sk_deferred)) {
1558 dr = list_entry(svsk->sk_deferred.next,
1559 struct svc_deferred_req,
1560 handle.recent);
1561 list_del_init(&dr->handle.recent);
1562 set_bit(SK_DEFERRED, &svsk->sk_flags);
1563 }
1564 spin_unlock_bh(&serv->sv_lock);
1565 return dr;
1566}