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