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