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