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