blob: cba85d195222e9603be9c29c68628b113fd5582a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
8 * svc_sock_enqueue procedure...
9 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/fcntl.h>
25#include <linux/net.h>
26#include <linux/in.h>
27#include <linux/inet.h>
28#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070029#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/unistd.h>
31#include <linux/slab.h>
32#include <linux/netdevice.h>
33#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070034#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/sock.h>
36#include <net/checksum.h>
37#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070038#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/ioctls.h>
41
42#include <linux/sunrpc/types.h>
43#include <linux/sunrpc/xdr.h>
44#include <linux/sunrpc/svcsock.h>
45#include <linux/sunrpc/stats.h>
46
47/* SMP locking strategy:
48 *
Greg Banks3262c812006-10-02 02:17:58 -070049 * svc_pool->sp_lock protects most of the fields of that pool.
50 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
51 * when both need to be taken (rare), svc_serv->sv_lock is first.
52 * BKL protects svc_serv->sv_nrthread.
Greg Banks1a68d952006-10-02 02:17:55 -070053 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
Greg Banksc081a0c2006-10-02 02:17:57 -070054 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Some flags can be set to certain values at any time
57 * providing that certain rules are followed:
58 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * SK_CONN, SK_DATA, can be set or cleared at any time.
60 * after a set, svc_sock_enqueue must be called.
61 * after a clear, the socket must be read/accepted
62 * if this succeeds, it must be set again.
63 * SK_CLOSE can set at any time. It is never cleared.
64 *
65 */
66
67#define RPCDBG_FACILITY RPCDBG_SVCSOCK
68
69
70static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
71 int *errp, int pmap_reg);
72static void svc_udp_data_ready(struct sock *, int);
73static int svc_udp_recvfrom(struct svc_rqst *);
74static int svc_udp_sendto(struct svc_rqst *);
75
76static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
77static int svc_deferred_recv(struct svc_rqst *rqstp);
78static struct cache_deferred_req *svc_defer(struct cache_req *req);
79
Greg Banks36bdfc82006-10-02 02:17:54 -070080/* apparently the "standard" is that clients close
81 * idle connections after 5 minutes, servers after
82 * 6 minutes
83 * http://www.connectathon.org/talks96/nfstcp.pdf
84 */
85static int svc_conn_age_period = 6*60;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Greg Banks3262c812006-10-02 02:17:58 -070088 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -070090 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * the cache.
92 */
93static inline void
Greg Banks3262c812006-10-02 02:17:58 -070094svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Greg Banks3262c812006-10-02 02:17:58 -070096 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
Greg Banks3262c812006-10-02 02:17:58 -0700100 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700103svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 list_del(&rqstp->rq_list);
106}
107
108/*
109 * Release an skbuff after use
110 */
111static inline void
112svc_release_skb(struct svc_rqst *rqstp)
113{
114 struct sk_buff *skb = rqstp->rq_skbuff;
115 struct svc_deferred_req *dr = rqstp->rq_deferred;
116
117 if (skb) {
118 rqstp->rq_skbuff = NULL;
119
120 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
121 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
122 }
123 if (dr) {
124 rqstp->rq_deferred = NULL;
125 kfree(dr);
126 }
127}
128
129/*
130 * Any space to write?
131 */
132static inline unsigned long
133svc_sock_wspace(struct svc_sock *svsk)
134{
135 int wspace;
136
137 if (svsk->sk_sock->type == SOCK_STREAM)
138 wspace = sk_stream_wspace(svsk->sk_sk);
139 else
140 wspace = sock_wspace(svsk->sk_sk);
141
142 return wspace;
143}
144
145/*
146 * Queue up a socket with data pending. If there are idle nfsd
147 * processes, wake 'em up.
148 *
149 */
150static void
151svc_sock_enqueue(struct svc_sock *svsk)
152{
153 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700154 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700156 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (!(svsk->sk_flags &
159 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
160 return;
161 if (test_bit(SK_DEAD, &svsk->sk_flags))
162 return;
163
Greg Banksbfd24162006-10-02 02:18:01 -0700164 cpu = get_cpu();
165 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
166 put_cpu();
167
Greg Banks3262c812006-10-02 02:17:58 -0700168 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Greg Banks3262c812006-10-02 02:17:58 -0700170 if (!list_empty(&pool->sp_threads) &&
171 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 printk(KERN_ERR
173 "svc_sock_enqueue: threads and sockets both waiting??\n");
174
175 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
176 /* Don't enqueue dead sockets */
177 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
178 goto out_unlock;
179 }
180
Greg Banksc081a0c2006-10-02 02:17:57 -0700181 /* Mark socket as busy. It will remain in this state until the
182 * server has processed all pending data and put the socket back
183 * on the idle list. We update SK_BUSY atomically because
184 * it also guards against trying to enqueue the svc_sock twice.
185 */
186 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
187 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
189 goto out_unlock;
190 }
Greg Banks3262c812006-10-02 02:17:58 -0700191 BUG_ON(svsk->sk_pool != NULL);
192 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Greg Banks5685f0f2006-10-02 02:17:56 -0700195 if (((atomic_read(&svsk->sk_reserved) + serv->sv_bufsz)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 > svc_sock_wspace(svsk))
197 && !test_bit(SK_CLOSE, &svsk->sk_flags)
198 && !test_bit(SK_CONN, &svsk->sk_flags)) {
199 /* Don't enqueue while not enough space for reply */
200 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
Greg Banks5685f0f2006-10-02 02:17:56 -0700201 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_bufsz,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 svc_sock_wspace(svsk));
Greg Banks3262c812006-10-02 02:17:58 -0700203 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700204 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out_unlock;
206 }
207 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Greg Banks3262c812006-10-02 02:17:58 -0700210 if (!list_empty(&pool->sp_threads)) {
211 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct svc_rqst,
213 rq_list);
214 dprintk("svc: socket %p served by daemon %p\n",
215 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700216 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (rqstp->rq_sock)
218 printk(KERN_ERR
219 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
220 rqstp, rqstp->rq_sock);
221 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700222 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 rqstp->rq_reserved = serv->sv_bufsz;
Greg Banks5685f0f2006-10-02 02:17:56 -0700224 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700225 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 wake_up(&rqstp->rq_wait);
227 } else {
228 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700229 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
230 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700234 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/*
Greg Banks3262c812006-10-02 02:17:58 -0700238 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700241svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct svc_sock *svsk;
244
Greg Banks3262c812006-10-02 02:17:58 -0700245 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return NULL;
247
Greg Banks3262c812006-10-02 02:17:58 -0700248 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct svc_sock, sk_ready);
250 list_del_init(&svsk->sk_ready);
251
252 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700253 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return svsk;
256}
257
258/*
259 * Having read something from a socket, check whether it
260 * needs to be re-enqueued.
261 * Note: SK_DATA only gets cleared when a read-attempt finds
262 * no (or insufficient) data.
263 */
264static inline void
265svc_sock_received(struct svc_sock *svsk)
266{
Greg Banks3262c812006-10-02 02:17:58 -0700267 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 clear_bit(SK_BUSY, &svsk->sk_flags);
269 svc_sock_enqueue(svsk);
270}
271
272
273/**
274 * svc_reserve - change the space reserved for the reply to a request.
275 * @rqstp: The request in question
276 * @space: new max space to reserve
277 *
278 * Each request reserves some space on the output queue of the socket
279 * to make sure the reply fits. This function reduces that reserved
280 * space to be the amount of space used already, plus @space.
281 *
282 */
283void svc_reserve(struct svc_rqst *rqstp, int space)
284{
285 space += rqstp->rq_res.head[0].iov_len;
286
287 if (space < rqstp->rq_reserved) {
288 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700289 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 svc_sock_enqueue(svsk);
293 }
294}
295
296/*
297 * Release a socket after use.
298 */
299static inline void
300svc_sock_put(struct svc_sock *svsk)
301{
Greg Banksc45c3572006-10-02 02:17:54 -0700302 if (atomic_dec_and_test(&svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 dprintk("svc: releasing dead socket\n");
304 sock_release(svsk->sk_sock);
305 kfree(svsk);
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static void
310svc_sock_release(struct svc_rqst *rqstp)
311{
312 struct svc_sock *svsk = rqstp->rq_sock;
313
314 svc_release_skb(rqstp);
315
316 svc_free_allpages(rqstp);
317 rqstp->rq_res.page_len = 0;
318 rqstp->rq_res.page_base = 0;
319
320
321 /* Reset response buffer and release
322 * the reservation.
323 * But first, check that enough space was reserved
324 * for the reply, otherwise we have a bug!
325 */
326 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
327 printk(KERN_ERR "RPC request reserved %d but used %d\n",
328 rqstp->rq_reserved,
329 rqstp->rq_res.len);
330
331 rqstp->rq_res.head[0].iov_len = 0;
332 svc_reserve(rqstp, 0);
333 rqstp->rq_sock = NULL;
334
335 svc_sock_put(svsk);
336}
337
338/*
339 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700340 * This really only makes sense for services like lockd
341 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
343void
344svc_wake_up(struct svc_serv *serv)
345{
346 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700347 unsigned int i;
348 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Greg Banks3262c812006-10-02 02:17:58 -0700350 for (i = 0; i < serv->sv_nrpools; i++) {
351 pool = &serv->sv_pools[i];
352
353 spin_lock_bh(&pool->sp_lock);
354 if (!list_empty(&pool->sp_threads)) {
355 rqstp = list_entry(pool->sp_threads.next,
356 struct svc_rqst,
357 rq_list);
358 dprintk("svc: daemon %p woken up.\n", rqstp);
359 /*
360 svc_thread_dequeue(pool, rqstp);
361 rqstp->rq_sock = NULL;
362 */
363 wake_up(&rqstp->rq_wait);
364 }
365 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
370 * Generic sendto routine
371 */
372static int
373svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
374{
375 struct svc_sock *svsk = rqstp->rq_sock;
376 struct socket *sock = svsk->sk_sock;
377 int slen;
378 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
379 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
380 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
381 int len = 0;
382 int result;
383 int size;
384 struct page **ppage = xdr->pages;
385 size_t base = xdr->page_base;
386 unsigned int pglen = xdr->page_len;
387 unsigned int flags = MSG_MORE;
388
389 slen = xdr->len;
390
391 if (rqstp->rq_prot == IPPROTO_UDP) {
392 /* set the source and destination */
393 struct msghdr msg;
394 msg.msg_name = &rqstp->rq_addr;
395 msg.msg_namelen = sizeof(rqstp->rq_addr);
396 msg.msg_iov = NULL;
397 msg.msg_iovlen = 0;
398 msg.msg_flags = MSG_MORE;
399
400 msg.msg_control = cmh;
401 msg.msg_controllen = sizeof(buffer);
402 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
403 cmh->cmsg_level = SOL_IP;
404 cmh->cmsg_type = IP_PKTINFO;
405 pki->ipi_ifindex = 0;
406 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
407
408 if (sock_sendmsg(sock, &msg, 0) < 0)
409 goto out;
410 }
411
412 /* send head */
413 if (slen == xdr->head[0].iov_len)
414 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700415 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (len != xdr->head[0].iov_len)
417 goto out;
418 slen -= xdr->head[0].iov_len;
419 if (slen == 0)
420 goto out;
421
422 /* send page data */
423 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
424 while (pglen > 0) {
425 if (slen == size)
426 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700427 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (result > 0)
429 len += result;
430 if (result != size)
431 goto out;
432 slen -= size;
433 pglen -= size;
434 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
435 base = 0;
436 ppage++;
437 }
438 /* send tail */
439 if (xdr->tail[0].iov_len) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700440 result = kernel_sendpage(sock, rqstp->rq_respages[rqstp->rq_restailpage],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ((unsigned long)xdr->tail[0].iov_base)& (PAGE_SIZE-1),
442 xdr->tail[0].iov_len, 0);
443
444 if (result > 0)
445 len += result;
446 }
447out:
448 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
449 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
450 rqstp->rq_addr.sin_addr.s_addr);
451
452 return len;
453}
454
455/*
NeilBrown80212d52006-10-02 02:17:47 -0700456 * Report socket names for nfsdfs
457 */
458static int one_sock_name(char *buf, struct svc_sock *svsk)
459{
460 int len;
461
462 switch(svsk->sk_sk->sk_family) {
463 case AF_INET:
464 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
465 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
466 "udp" : "tcp",
467 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
468 inet_sk(svsk->sk_sk)->num);
469 break;
470 default:
471 len = sprintf(buf, "*unknown-%d*\n",
472 svsk->sk_sk->sk_family);
473 }
474 return len;
475}
476
477int
NeilBrownb41b66d2006-10-02 02:17:48 -0700478svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700479{
NeilBrownb41b66d2006-10-02 02:17:48 -0700480 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700481 int len = 0;
482
483 if (!serv)
484 return 0;
485 spin_lock(&serv->sv_lock);
486 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
487 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700488 if (toclose && strcmp(toclose, buf+len) == 0)
489 closesk = svsk;
490 else
491 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700492 }
493 spin_unlock(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700494 if (closesk)
495 svc_delete_socket(closesk);
NeilBrown80212d52006-10-02 02:17:47 -0700496 return len;
497}
498EXPORT_SYMBOL(svc_sock_names);
499
500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Check input queue length
502 */
503static int
504svc_recv_available(struct svc_sock *svsk)
505{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct socket *sock = svsk->sk_sock;
507 int avail, err;
508
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700509 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 return (err >= 0)? avail : err;
512}
513
514/*
515 * Generic recvfrom routine.
516 */
517static int
518svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
519{
520 struct msghdr msg;
521 struct socket *sock;
522 int len, alen;
523
524 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
525 sock = rqstp->rq_sock->sk_sock;
526
527 msg.msg_name = &rqstp->rq_addr;
528 msg.msg_namelen = sizeof(rqstp->rq_addr);
529 msg.msg_control = NULL;
530 msg.msg_controllen = 0;
531
532 msg.msg_flags = MSG_DONTWAIT;
533
534 len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
535
536 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
537 * possibly we should cache this in the svc_sock structure
538 * at accept time. FIXME
539 */
540 alen = sizeof(rqstp->rq_addr);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700541 kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
544 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
545
546 return len;
547}
548
549/*
550 * Set socket snd and rcv buffer lengths
551 */
552static inline void
553svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
554{
555#if 0
556 mm_segment_t oldfs;
557 oldfs = get_fs(); set_fs(KERNEL_DS);
558 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
559 (char*)&snd, sizeof(snd));
560 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
561 (char*)&rcv, sizeof(rcv));
562#else
563 /* sock_setsockopt limits use to sysctl_?mem_max,
564 * which isn't acceptable. Until that is made conditional
565 * on not having CAP_SYS_RESOURCE or similar, we go direct...
566 * DaveM said I could!
567 */
568 lock_sock(sock->sk);
569 sock->sk->sk_sndbuf = snd * 2;
570 sock->sk->sk_rcvbuf = rcv * 2;
571 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
572 release_sock(sock->sk);
573#endif
574}
575/*
576 * INET callback when data has been received on the socket.
577 */
578static void
579svc_udp_data_ready(struct sock *sk, int count)
580{
Neil Brown939bb7e2005-09-13 01:25:39 -0700581 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Neil Brown939bb7e2005-09-13 01:25:39 -0700583 if (svsk) {
584 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
585 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
586 set_bit(SK_DATA, &svsk->sk_flags);
587 svc_sock_enqueue(svsk);
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
590 wake_up_interruptible(sk->sk_sleep);
591}
592
593/*
594 * INET callback when space is newly available on the socket.
595 */
596static void
597svc_write_space(struct sock *sk)
598{
599 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
600
601 if (svsk) {
602 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
603 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
604 svc_sock_enqueue(svsk);
605 }
606
607 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700608 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 svsk);
610 wake_up_interruptible(sk->sk_sleep);
611 }
612}
613
614/*
615 * Receive a datagram from a UDP socket.
616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static int
618svc_udp_recvfrom(struct svc_rqst *rqstp)
619{
620 struct svc_sock *svsk = rqstp->rq_sock;
621 struct svc_serv *serv = svsk->sk_server;
622 struct sk_buff *skb;
623 int err, len;
624
625 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
626 /* udp sockets need large rcvbuf as all pending
627 * requests are still in that buffer. sndbuf must
628 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700629 * for one reply per thread. We count all threads
630 * rather than threads in a particular pool, which
631 * provides an upper bound on the number of threads
632 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634 svc_sock_setbufsize(svsk->sk_sock,
635 (serv->sv_nrthreads+3) * serv->sv_bufsz,
636 (serv->sv_nrthreads+3) * serv->sv_bufsz);
637
638 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
639 svc_sock_received(svsk);
640 return svc_deferred_recv(rqstp);
641 }
642
643 clear_bit(SK_DATA, &svsk->sk_flags);
644 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
645 if (err == -EAGAIN) {
646 svc_sock_received(svsk);
647 return err;
648 }
649 /* possibly an icmp error */
650 dprintk("svc: recvfrom returned error %d\n", -err);
651 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700652 if (skb->tstamp.off_sec == 0) {
653 struct timeval tv;
654
655 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700656 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700657 skb_set_timestamp(skb, &tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* Don't enable netstamp, sunrpc doesn't
659 need that much accuracy */
660 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700661 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
663
664 /*
665 * Maybe more packets - kick another thread ASAP.
666 */
667 svc_sock_received(svsk);
668
669 len = skb->len - sizeof(struct udphdr);
670 rqstp->rq_arg.len = len;
671
672 rqstp->rq_prot = IPPROTO_UDP;
673
674 /* Get sender address */
675 rqstp->rq_addr.sin_family = AF_INET;
676 rqstp->rq_addr.sin_port = skb->h.uh->source;
677 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
678 rqstp->rq_daddr = skb->nh.iph->daddr;
679
680 if (skb_is_nonlinear(skb)) {
681 /* we have to copy */
682 local_bh_disable();
683 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
684 local_bh_enable();
685 /* checksum error */
686 skb_free_datagram(svsk->sk_sk, skb);
687 return 0;
688 }
689 local_bh_enable();
690 skb_free_datagram(svsk->sk_sk, skb);
691 } else {
692 /* we can use it in-place */
693 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
694 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800695 if (skb_checksum_complete(skb)) {
696 skb_free_datagram(svsk->sk_sk, skb);
697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 rqstp->rq_skbuff = skb;
700 }
701
702 rqstp->rq_arg.page_base = 0;
703 if (len <= rqstp->rq_arg.head[0].iov_len) {
704 rqstp->rq_arg.head[0].iov_len = len;
705 rqstp->rq_arg.page_len = 0;
706 } else {
707 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
708 rqstp->rq_argused += (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
709 }
710
711 if (serv->sv_stats)
712 serv->sv_stats->netudpcnt++;
713
714 return len;
715}
716
717static int
718svc_udp_sendto(struct svc_rqst *rqstp)
719{
720 int error;
721
722 error = svc_sendto(rqstp, &rqstp->rq_res);
723 if (error == -ECONNREFUSED)
724 /* ICMP error on earlier request. */
725 error = svc_sendto(rqstp, &rqstp->rq_res);
726
727 return error;
728}
729
730static void
731svc_udp_init(struct svc_sock *svsk)
732{
733 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
734 svsk->sk_sk->sk_write_space = svc_write_space;
735 svsk->sk_recvfrom = svc_udp_recvfrom;
736 svsk->sk_sendto = svc_udp_sendto;
737
738 /* initialise setting must have enough space to
739 * receive and respond to one request.
740 * svc_udp_recvfrom will re-adjust if necessary
741 */
742 svc_sock_setbufsize(svsk->sk_sock,
743 3 * svsk->sk_server->sv_bufsz,
744 3 * svsk->sk_server->sv_bufsz);
745
746 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
747 set_bit(SK_CHNGBUF, &svsk->sk_flags);
748}
749
750/*
751 * A data_ready event on a listening socket means there's a connection
752 * pending. Do not use state_change as a substitute for it.
753 */
754static void
755svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
756{
Neil Brown939bb7e2005-09-13 01:25:39 -0700757 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700760 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Neil Brown939bb7e2005-09-13 01:25:39 -0700762 /*
763 * This callback may called twice when a new connection
764 * is established as a child socket inherits everything
765 * from a parent LISTEN socket.
766 * 1) data_ready method of the parent socket will be called
767 * when one of child sockets become ESTABLISHED.
768 * 2) data_ready method of the child socket may be called
769 * when it receives data before the socket is accepted.
770 * In case of 2, we should ignore it silently.
771 */
772 if (sk->sk_state == TCP_LISTEN) {
773 if (svsk) {
774 set_bit(SK_CONN, &svsk->sk_flags);
775 svc_sock_enqueue(svsk);
776 } else
777 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
781 wake_up_interruptible_all(sk->sk_sleep);
782}
783
784/*
785 * A state change on a connected socket means it's dying or dead.
786 */
787static void
788svc_tcp_state_change(struct sock *sk)
789{
Neil Brown939bb7e2005-09-13 01:25:39 -0700790 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700793 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Neil Brown939bb7e2005-09-13 01:25:39 -0700795 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700797 else {
798 set_bit(SK_CLOSE, &svsk->sk_flags);
799 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
802 wake_up_interruptible_all(sk->sk_sleep);
803}
804
805static void
806svc_tcp_data_ready(struct sock *sk, int count)
807{
Neil Brown939bb7e2005-09-13 01:25:39 -0700808 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700811 sk, sk->sk_user_data);
812 if (svsk) {
813 set_bit(SK_DATA, &svsk->sk_flags);
814 svc_sock_enqueue(svsk);
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
817 wake_up_interruptible(sk->sk_sleep);
818}
819
820/*
821 * Accept a TCP connection
822 */
823static void
824svc_tcp_accept(struct svc_sock *svsk)
825{
826 struct sockaddr_in sin;
827 struct svc_serv *serv = svsk->sk_server;
828 struct socket *sock = svsk->sk_sock;
829 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct svc_sock *newsvsk;
831 int err, slen;
832
833 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
834 if (!sock)
835 return;
836
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700837 clear_bit(SK_CONN, &svsk->sk_flags);
838 err = kernel_accept(sock, &newsock, O_NONBLOCK);
839 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (err == -ENOMEM)
841 printk(KERN_WARNING "%s: no more sockets!\n",
842 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700843 else if (err != -EAGAIN && net_ratelimit())
844 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
845 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return;
847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 set_bit(SK_CONN, &svsk->sk_flags);
850 svc_sock_enqueue(svsk);
851
852 slen = sizeof(sin);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700853 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (err < 0) {
855 if (net_ratelimit())
856 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
857 serv->sv_name, -err);
858 goto failed; /* aborted connection or whatever */
859 }
860
861 /* Ideally, we would want to reject connections from unauthorized
862 * hosts here, but when we get encription, the IP of the host won't
863 * tell us anything. For now just warn about unpriv connections.
864 */
865 if (ntohs(sin.sin_port) >= 1024) {
866 dprintk(KERN_WARNING
867 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
868 serv->sv_name,
869 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
870 }
871
872 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
873 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
874
875 /* make sure that a write doesn't block forever when
876 * low on memory
877 */
878 newsock->sk->sk_sndtimeo = HZ*30;
879
880 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
881 goto failed;
882
883
884 /* make sure that we don't have too many active connections.
885 * If we have, something must be dropped.
886 *
887 * There's no point in trying to do random drop here for
888 * DoS prevention. The NFS clients does 1 reconnect in 15
889 * seconds. An attacker can easily beat that.
890 *
891 * The only somewhat efficient mechanism would be if drop
892 * old connections from the same IP first. But right now
893 * we don't even record the client IP in svc_sock.
894 */
895 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
896 struct svc_sock *svsk = NULL;
897 spin_lock_bh(&serv->sv_lock);
898 if (!list_empty(&serv->sv_tempsocks)) {
899 if (net_ratelimit()) {
900 /* Try to help the admin */
901 printk(KERN_NOTICE "%s: too many open TCP "
902 "sockets, consider increasing the "
903 "number of nfsd threads\n",
904 serv->sv_name);
905 printk(KERN_NOTICE "%s: last TCP connect from "
906 "%u.%u.%u.%u:%d\n",
907 serv->sv_name,
908 NIPQUAD(sin.sin_addr.s_addr),
909 ntohs(sin.sin_port));
910 }
911 /*
912 * Always select the oldest socket. It's not fair,
913 * but so is life
914 */
915 svsk = list_entry(serv->sv_tempsocks.prev,
916 struct svc_sock,
917 sk_list);
918 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -0700919 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 spin_unlock_bh(&serv->sv_lock);
922
923 if (svsk) {
924 svc_sock_enqueue(svsk);
925 svc_sock_put(svsk);
926 }
927
928 }
929
930 if (serv->sv_stats)
931 serv->sv_stats->nettcpconn++;
932
933 return;
934
935failed:
936 sock_release(newsock);
937 return;
938}
939
940/*
941 * Receive data from a TCP socket.
942 */
943static int
944svc_tcp_recvfrom(struct svc_rqst *rqstp)
945{
946 struct svc_sock *svsk = rqstp->rq_sock;
947 struct svc_serv *serv = svsk->sk_server;
948 int len;
949 struct kvec vec[RPCSVC_MAXPAGES];
950 int pnum, vlen;
951
952 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
953 svsk, test_bit(SK_DATA, &svsk->sk_flags),
954 test_bit(SK_CONN, &svsk->sk_flags),
955 test_bit(SK_CLOSE, &svsk->sk_flags));
956
957 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
958 svc_sock_received(svsk);
959 return svc_deferred_recv(rqstp);
960 }
961
962 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
963 svc_delete_socket(svsk);
964 return 0;
965 }
966
967 if (test_bit(SK_CONN, &svsk->sk_flags)) {
968 svc_tcp_accept(svsk);
969 svc_sock_received(svsk);
970 return 0;
971 }
972
973 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
974 /* sndbuf needs to have room for one request
975 * per thread, otherwise we can stall even when the
976 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -0700977 *
978 * We count all threads rather than threads in a
979 * particular pool, which provides an upper bound
980 * on the number of threads which will access the socket.
981 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * rcvbuf just needs to be able to hold a few requests.
983 * Normally they will be removed from the queue
984 * as soon a a complete request arrives.
985 */
986 svc_sock_setbufsize(svsk->sk_sock,
987 (serv->sv_nrthreads+3) * serv->sv_bufsz,
988 3 * serv->sv_bufsz);
989
990 clear_bit(SK_DATA, &svsk->sk_flags);
991
992 /* Receive data. If we haven't got the record length yet, get
993 * the next four bytes. Otherwise try to gobble up as much as
994 * possible up to the complete record length.
995 */
996 if (svsk->sk_tcplen < 4) {
997 unsigned long want = 4 - svsk->sk_tcplen;
998 struct kvec iov;
999
1000 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1001 iov.iov_len = want;
1002 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1003 goto error;
1004 svsk->sk_tcplen += len;
1005
1006 if (len < want) {
1007 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
1008 len, want);
1009 svc_sock_received(svsk);
1010 return -EAGAIN; /* record header not complete */
1011 }
1012
1013 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1014 if (!(svsk->sk_reclen & 0x80000000)) {
1015 /* FIXME: technically, a record can be fragmented,
1016 * and non-terminal fragments will not have the top
1017 * bit set in the fragment length header.
1018 * But apparently no known nfs clients send fragmented
1019 * records. */
1020 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
1021 (unsigned long) svsk->sk_reclen);
1022 goto err_delete;
1023 }
1024 svsk->sk_reclen &= 0x7fffffff;
1025 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
1026 if (svsk->sk_reclen > serv->sv_bufsz) {
1027 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
1028 (unsigned long) svsk->sk_reclen);
1029 goto err_delete;
1030 }
1031 }
1032
1033 /* Check whether enough data is available */
1034 len = svc_recv_available(svsk);
1035 if (len < 0)
1036 goto error;
1037
1038 if (len < svsk->sk_reclen) {
1039 dprintk("svc: incomplete TCP record (%d of %d)\n",
1040 len, svsk->sk_reclen);
1041 svc_sock_received(svsk);
1042 return -EAGAIN; /* record not complete */
1043 }
1044 len = svsk->sk_reclen;
1045 set_bit(SK_DATA, &svsk->sk_flags);
1046
1047 vec[0] = rqstp->rq_arg.head[0];
1048 vlen = PAGE_SIZE;
1049 pnum = 1;
1050 while (vlen < len) {
1051 vec[pnum].iov_base = page_address(rqstp->rq_argpages[rqstp->rq_argused++]);
1052 vec[pnum].iov_len = PAGE_SIZE;
1053 pnum++;
1054 vlen += PAGE_SIZE;
1055 }
1056
1057 /* Now receive data */
1058 len = svc_recvfrom(rqstp, vec, pnum, len);
1059 if (len < 0)
1060 goto error;
1061
1062 dprintk("svc: TCP complete record (%d bytes)\n", len);
1063 rqstp->rq_arg.len = len;
1064 rqstp->rq_arg.page_base = 0;
1065 if (len <= rqstp->rq_arg.head[0].iov_len) {
1066 rqstp->rq_arg.head[0].iov_len = len;
1067 rqstp->rq_arg.page_len = 0;
1068 } else {
1069 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1070 }
1071
1072 rqstp->rq_skbuff = NULL;
1073 rqstp->rq_prot = IPPROTO_TCP;
1074
1075 /* Reset TCP read info */
1076 svsk->sk_reclen = 0;
1077 svsk->sk_tcplen = 0;
1078
1079 svc_sock_received(svsk);
1080 if (serv->sv_stats)
1081 serv->sv_stats->nettcpcnt++;
1082
1083 return len;
1084
1085 err_delete:
1086 svc_delete_socket(svsk);
1087 return -EAGAIN;
1088
1089 error:
1090 if (len == -EAGAIN) {
1091 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1092 svc_sock_received(svsk);
1093 } else {
1094 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1095 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001096 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 return len;
1100}
1101
1102/*
1103 * Send out data on TCP socket.
1104 */
1105static int
1106svc_tcp_sendto(struct svc_rqst *rqstp)
1107{
1108 struct xdr_buf *xbufp = &rqstp->rq_res;
1109 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001110 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Set up the first element of the reply kvec.
1113 * Any other kvecs that may be in use have been taken
1114 * care of by the server implementation itself.
1115 */
1116 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1117 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1118
1119 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1120 return -ENOTCONN;
1121
1122 sent = svc_sendto(rqstp, &rqstp->rq_res);
1123 if (sent != xbufp->len) {
1124 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1125 rqstp->rq_sock->sk_server->sv_name,
1126 (sent<0)?"got error":"sent only",
1127 sent, xbufp->len);
1128 svc_delete_socket(rqstp->rq_sock);
1129 sent = -EAGAIN;
1130 }
1131 return sent;
1132}
1133
1134static void
1135svc_tcp_init(struct svc_sock *svsk)
1136{
1137 struct sock *sk = svsk->sk_sk;
1138 struct tcp_sock *tp = tcp_sk(sk);
1139
1140 svsk->sk_recvfrom = svc_tcp_recvfrom;
1141 svsk->sk_sendto = svc_tcp_sendto;
1142
1143 if (sk->sk_state == TCP_LISTEN) {
1144 dprintk("setting up TCP socket for listening\n");
1145 sk->sk_data_ready = svc_tcp_listen_data_ready;
1146 set_bit(SK_CONN, &svsk->sk_flags);
1147 } else {
1148 dprintk("setting up TCP socket for reading\n");
1149 sk->sk_state_change = svc_tcp_state_change;
1150 sk->sk_data_ready = svc_tcp_data_ready;
1151 sk->sk_write_space = svc_write_space;
1152
1153 svsk->sk_reclen = 0;
1154 svsk->sk_tcplen = 0;
1155
1156 tp->nonagle = 1; /* disable Nagle's algorithm */
1157
1158 /* initialise setting must have enough space to
1159 * receive and respond to one request.
1160 * svc_tcp_recvfrom will re-adjust if necessary
1161 */
1162 svc_sock_setbufsize(svsk->sk_sock,
1163 3 * svsk->sk_server->sv_bufsz,
1164 3 * svsk->sk_server->sv_bufsz);
1165
1166 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1167 set_bit(SK_DATA, &svsk->sk_flags);
1168 if (sk->sk_state != TCP_ESTABLISHED)
1169 set_bit(SK_CLOSE, &svsk->sk_flags);
1170 }
1171}
1172
1173void
1174svc_sock_update_bufs(struct svc_serv *serv)
1175{
1176 /*
1177 * The number of server threads has changed. Update
1178 * rcvbuf and sndbuf accordingly on all sockets
1179 */
1180 struct list_head *le;
1181
1182 spin_lock_bh(&serv->sv_lock);
1183 list_for_each(le, &serv->sv_permsocks) {
1184 struct svc_sock *svsk =
1185 list_entry(le, struct svc_sock, sk_list);
1186 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1187 }
1188 list_for_each(le, &serv->sv_tempsocks) {
1189 struct svc_sock *svsk =
1190 list_entry(le, struct svc_sock, sk_list);
1191 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1192 }
1193 spin_unlock_bh(&serv->sv_lock);
1194}
1195
1196/*
Greg Banks3262c812006-10-02 02:17:58 -07001197 * Receive the next request on any socket. This code is carefully
1198 * organised not to touch any cachelines in the shared svc_serv
1199 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
1201int
NeilBrown6fb2b472006-10-02 02:17:50 -07001202svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct svc_sock *svsk =NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001205 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001206 struct svc_pool *pool = rqstp->rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int len;
1208 int pages;
1209 struct xdr_buf *arg;
1210 DECLARE_WAITQUEUE(wait, current);
1211
1212 dprintk("svc: server %p waiting for data (to = %ld)\n",
1213 rqstp, timeout);
1214
1215 if (rqstp->rq_sock)
1216 printk(KERN_ERR
1217 "svc_recv: service %p, socket not NULL!\n",
1218 rqstp);
1219 if (waitqueue_active(&rqstp->rq_wait))
1220 printk(KERN_ERR
1221 "svc_recv: service %p, wait queue active!\n",
1222 rqstp);
1223
1224 /* Initialize the buffers */
1225 /* first reclaim pages that were moved to response list */
1226 svc_pushback_allpages(rqstp);
1227
1228 /* now allocate needed pages. If we get a failure, sleep briefly */
1229 pages = 2 + (serv->sv_bufsz + PAGE_SIZE -1) / PAGE_SIZE;
1230 while (rqstp->rq_arghi < pages) {
1231 struct page *p = alloc_page(GFP_KERNEL);
1232 if (!p) {
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001233 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 continue;
1235 }
1236 rqstp->rq_argpages[rqstp->rq_arghi++] = p;
1237 }
1238
1239 /* Make arg->head point to first page and arg->pages point to rest */
1240 arg = &rqstp->rq_arg;
1241 arg->head[0].iov_base = page_address(rqstp->rq_argpages[0]);
1242 arg->head[0].iov_len = PAGE_SIZE;
1243 rqstp->rq_argused = 1;
1244 arg->pages = rqstp->rq_argpages + 1;
1245 arg->page_base = 0;
1246 /* save at least one page for response */
1247 arg->page_len = (pages-2)*PAGE_SIZE;
1248 arg->len = (pages-1)*PAGE_SIZE;
1249 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001250
1251 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001252 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (signalled())
1254 return -EINTR;
1255
Greg Banks3262c812006-10-02 02:17:58 -07001256 spin_lock_bh(&pool->sp_lock);
1257 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001259 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 rqstp->rq_reserved = serv->sv_bufsz;
Greg Banks5685f0f2006-10-02 02:17:56 -07001261 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001264 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
1267 * We have to be able to interrupt this wait
1268 * to bring down the daemons ...
1269 */
1270 set_current_state(TASK_INTERRUPTIBLE);
1271 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001272 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 schedule_timeout(timeout);
1275
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001276 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Greg Banks3262c812006-10-02 02:17:58 -07001278 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 remove_wait_queue(&rqstp->rq_wait, &wait);
1280
1281 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001282 svc_thread_dequeue(pool, rqstp);
1283 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 dprintk("svc: server %p, no data yet\n", rqstp);
1285 return signalled()? -EINTR : -EAGAIN;
1286 }
1287 }
Greg Banks3262c812006-10-02 02:17:58 -07001288 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Greg Banks3262c812006-10-02 02:17:58 -07001290 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1291 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 len = svsk->sk_recvfrom(rqstp);
1293 dprintk("svc: got len=%d\n", len);
1294
1295 /* No data, incomplete (TCP) read, or accept() */
1296 if (len == 0 || len == -EAGAIN) {
1297 rqstp->rq_res.len = 0;
1298 svc_sock_release(rqstp);
1299 return -EAGAIN;
1300 }
1301 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001302 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1305 rqstp->rq_chandle.defer = svc_defer;
1306
1307 if (serv->sv_stats)
1308 serv->sv_stats->netcnt++;
1309 return len;
1310}
1311
1312/*
1313 * Drop request
1314 */
1315void
1316svc_drop(struct svc_rqst *rqstp)
1317{
1318 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1319 svc_sock_release(rqstp);
1320}
1321
1322/*
1323 * Return reply to client.
1324 */
1325int
1326svc_send(struct svc_rqst *rqstp)
1327{
1328 struct svc_sock *svsk;
1329 int len;
1330 struct xdr_buf *xb;
1331
1332 if ((svsk = rqstp->rq_sock) == NULL) {
1333 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1334 __FILE__, __LINE__);
1335 return -EFAULT;
1336 }
1337
1338 /* release the receive skb before sending the reply */
1339 svc_release_skb(rqstp);
1340
1341 /* calculate over-all length */
1342 xb = & rqstp->rq_res;
1343 xb->len = xb->head[0].iov_len +
1344 xb->page_len +
1345 xb->tail[0].iov_len;
1346
Ingo Molnar57b47a52006-03-20 22:35:41 -08001347 /* Grab svsk->sk_mutex to serialize outgoing data. */
1348 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (test_bit(SK_DEAD, &svsk->sk_flags))
1350 len = -ENOTCONN;
1351 else
1352 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001353 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 svc_sock_release(rqstp);
1355
1356 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1357 return 0;
1358 return len;
1359}
1360
1361/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001362 * Timer function to close old temporary sockets, using
1363 * a mark-and-sweep algorithm.
1364 */
1365static void
1366svc_age_temp_sockets(unsigned long closure)
1367{
1368 struct svc_serv *serv = (struct svc_serv *)closure;
1369 struct svc_sock *svsk;
1370 struct list_head *le, *next;
1371 LIST_HEAD(to_be_aged);
1372
1373 dprintk("svc_age_temp_sockets\n");
1374
1375 if (!spin_trylock_bh(&serv->sv_lock)) {
1376 /* busy, try again 1 sec later */
1377 dprintk("svc_age_temp_sockets: busy\n");
1378 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1379 return;
1380 }
1381
1382 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1383 svsk = list_entry(le, struct svc_sock, sk_list);
1384
1385 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1386 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001387 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001388 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001389 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001390 list_move(le, &to_be_aged);
1391 set_bit(SK_CLOSE, &svsk->sk_flags);
1392 set_bit(SK_DETACHED, &svsk->sk_flags);
1393 }
1394 spin_unlock_bh(&serv->sv_lock);
1395
1396 while (!list_empty(&to_be_aged)) {
1397 le = to_be_aged.next;
1398 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1399 list_del_init(le);
1400 svsk = list_entry(le, struct svc_sock, sk_list);
1401
1402 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1403 svsk, get_seconds() - svsk->sk_lastrecv);
1404
1405 /* a thread will dequeue and close it soon */
1406 svc_sock_enqueue(svsk);
1407 svc_sock_put(svsk);
1408 }
1409
1410 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1411}
1412
1413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * Initialize socket for RPC use and create svc_sock struct
1415 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1416 */
1417static struct svc_sock *
1418svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1419 int *errp, int pmap_register)
1420{
1421 struct svc_sock *svsk;
1422 struct sock *inet;
1423
1424 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001425 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *errp = -ENOMEM;
1427 return NULL;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 inet = sock->sk;
1431
1432 /* Register socket with portmapper */
1433 if (*errp >= 0 && pmap_register)
1434 *errp = svc_register(serv, inet->sk_protocol,
1435 ntohs(inet_sk(inet)->sport));
1436
1437 if (*errp < 0) {
1438 kfree(svsk);
1439 return NULL;
1440 }
1441
1442 set_bit(SK_BUSY, &svsk->sk_flags);
1443 inet->sk_user_data = svsk;
1444 svsk->sk_sock = sock;
1445 svsk->sk_sk = inet;
1446 svsk->sk_ostate = inet->sk_state_change;
1447 svsk->sk_odata = inet->sk_data_ready;
1448 svsk->sk_owspace = inet->sk_write_space;
1449 svsk->sk_server = serv;
Greg Banksc45c3572006-10-02 02:17:54 -07001450 atomic_set(&svsk->sk_inuse, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 svsk->sk_lastrecv = get_seconds();
Greg Banks1a68d952006-10-02 02:17:55 -07001452 spin_lock_init(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 INIT_LIST_HEAD(&svsk->sk_deferred);
1454 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001455 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* Initialize the socket */
1458 if (sock->type == SOCK_DGRAM)
1459 svc_udp_init(svsk);
1460 else
1461 svc_tcp_init(svsk);
1462
1463 spin_lock_bh(&serv->sv_lock);
1464 if (!pmap_register) {
1465 set_bit(SK_TEMP, &svsk->sk_flags);
1466 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1467 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001468 if (serv->sv_temptimer.function == NULL) {
1469 /* setup timer to age temp sockets */
1470 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1471 (unsigned long)serv);
1472 mod_timer(&serv->sv_temptimer,
1473 jiffies + svc_conn_age_period * HZ);
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 } else {
1476 clear_bit(SK_TEMP, &svsk->sk_flags);
1477 list_add(&svsk->sk_list, &serv->sv_permsocks);
1478 }
1479 spin_unlock_bh(&serv->sv_lock);
1480
1481 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1482 svsk, svsk->sk_sk);
1483
1484 clear_bit(SK_BUSY, &svsk->sk_flags);
1485 svc_sock_enqueue(svsk);
1486 return svsk;
1487}
1488
NeilBrownb41b66d2006-10-02 02:17:48 -07001489int svc_addsock(struct svc_serv *serv,
1490 int fd,
1491 char *name_return,
1492 int *proto)
1493{
1494 int err = 0;
1495 struct socket *so = sockfd_lookup(fd, &err);
1496 struct svc_sock *svsk = NULL;
1497
1498 if (!so)
1499 return err;
1500 if (so->sk->sk_family != AF_INET)
1501 err = -EAFNOSUPPORT;
1502 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1503 so->sk->sk_protocol != IPPROTO_UDP)
1504 err = -EPROTONOSUPPORT;
1505 else if (so->state > SS_UNCONNECTED)
1506 err = -EISCONN;
1507 else {
1508 svsk = svc_setup_socket(serv, so, &err, 1);
1509 if (svsk)
1510 err = 0;
1511 }
1512 if (err) {
1513 sockfd_put(so);
1514 return err;
1515 }
1516 if (proto) *proto = so->sk->sk_protocol;
1517 return one_sock_name(name_return, svsk);
1518}
1519EXPORT_SYMBOL_GPL(svc_addsock);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/*
1522 * Create socket for RPC service.
1523 */
1524static int
1525svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1526{
1527 struct svc_sock *svsk;
1528 struct socket *sock;
1529 int error;
1530 int type;
1531
1532 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1533 serv->sv_program->pg_name, protocol,
1534 NIPQUAD(sin->sin_addr.s_addr),
1535 ntohs(sin->sin_port));
1536
1537 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1538 printk(KERN_WARNING "svc: only UDP and TCP "
1539 "sockets supported\n");
1540 return -EINVAL;
1541 }
1542 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1543
1544 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1545 return error;
1546
Eric Sesterhenn18114742006-09-28 14:37:07 -07001547 if (type == SOCK_STREAM)
1548 sock->sk->sk_reuse = 1; /* allow address reuse */
1549 error = kernel_bind(sock, (struct sockaddr *) sin,
1550 sizeof(*sin));
1551 if (error < 0)
1552 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001555 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto bummer;
1557 }
1558
1559 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1560 return 0;
1561
1562bummer:
1563 dprintk("svc: svc_create_socket error = %d\n", -error);
1564 sock_release(sock);
1565 return error;
1566}
1567
1568/*
1569 * Remove a dead socket
1570 */
1571void
1572svc_delete_socket(struct svc_sock *svsk)
1573{
1574 struct svc_serv *serv;
1575 struct sock *sk;
1576
1577 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1578
1579 serv = svsk->sk_server;
1580 sk = svsk->sk_sk;
1581
1582 sk->sk_state_change = svsk->sk_ostate;
1583 sk->sk_data_ready = svsk->sk_odata;
1584 sk->sk_write_space = svsk->sk_owspace;
1585
1586 spin_lock_bh(&serv->sv_lock);
1587
Greg Banks36bdfc82006-10-02 02:17:54 -07001588 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1589 list_del_init(&svsk->sk_list);
Greg Banks3262c812006-10-02 02:17:58 -07001590 /*
1591 * We used to delete the svc_sock from whichever list
1592 * it's sk_ready node was on, but we don't actually
1593 * need to. This is because the only time we're called
1594 * while still attached to a queue, the queue itself
1595 * is about to be destroyed (in svc_destroy).
1596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags))
1598 if (test_bit(SK_TEMP, &svsk->sk_flags))
1599 serv->sv_tmpcnt--;
1600
Greg Banksc45c3572006-10-02 02:17:54 -07001601 if (!atomic_read(&svsk->sk_inuse)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -07001603 if (svsk->sk_sock->file)
1604 sockfd_put(svsk->sk_sock);
1605 else
1606 sock_release(svsk->sk_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 kfree(svsk);
1608 } else {
1609 spin_unlock_bh(&serv->sv_lock);
1610 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1611 /* svsk->sk_server = NULL; */
1612 }
1613}
1614
1615/*
1616 * Make a socket for nfsd and lockd
1617 */
1618int
1619svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1620{
1621 struct sockaddr_in sin;
1622
1623 dprintk("svc: creating socket proto = %d\n", protocol);
1624 sin.sin_family = AF_INET;
1625 sin.sin_addr.s_addr = INADDR_ANY;
1626 sin.sin_port = htons(port);
1627 return svc_create_socket(serv, protocol, &sin);
1628}
1629
1630/*
1631 * Handle defer and revisit of requests
1632 */
1633
1634static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1635{
1636 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct svc_sock *svsk;
1638
1639 if (too_many) {
1640 svc_sock_put(dr->svsk);
1641 kfree(dr);
1642 return;
1643 }
1644 dprintk("revisit queued\n");
1645 svsk = dr->svsk;
1646 dr->svsk = NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001647 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 list_add(&dr->handle.recent, &svsk->sk_deferred);
Greg Banks1a68d952006-10-02 02:17:55 -07001649 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 set_bit(SK_DEFERRED, &svsk->sk_flags);
1651 svc_sock_enqueue(svsk);
1652 svc_sock_put(svsk);
1653}
1654
1655static struct cache_deferred_req *
1656svc_defer(struct cache_req *req)
1657{
1658 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1659 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1660 struct svc_deferred_req *dr;
1661
1662 if (rqstp->rq_arg.page_len)
1663 return NULL; /* if more than a page, give up FIXME */
1664 if (rqstp->rq_deferred) {
1665 dr = rqstp->rq_deferred;
1666 rqstp->rq_deferred = NULL;
1667 } else {
1668 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1669 /* FIXME maybe discard if size too large */
1670 dr = kmalloc(size, GFP_KERNEL);
1671 if (dr == NULL)
1672 return NULL;
1673
1674 dr->handle.owner = rqstp->rq_server;
1675 dr->prot = rqstp->rq_prot;
1676 dr->addr = rqstp->rq_addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001677 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dr->argslen = rqstp->rq_arg.len >> 2;
1679 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1680 }
Greg Banksc45c3572006-10-02 02:17:54 -07001681 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 dr->handle.revisit = svc_revisit;
1685 return &dr->handle;
1686}
1687
1688/*
1689 * recv data from a deferred request into an active one
1690 */
1691static int svc_deferred_recv(struct svc_rqst *rqstp)
1692{
1693 struct svc_deferred_req *dr = rqstp->rq_deferred;
1694
1695 rqstp->rq_arg.head[0].iov_base = dr->args;
1696 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1697 rqstp->rq_arg.page_len = 0;
1698 rqstp->rq_arg.len = dr->argslen<<2;
1699 rqstp->rq_prot = dr->prot;
1700 rqstp->rq_addr = dr->addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001701 rqstp->rq_daddr = dr->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return dr->argslen<<2;
1703}
1704
1705
1706static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1707{
1708 struct svc_deferred_req *dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1711 return NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001712 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1714 if (!list_empty(&svsk->sk_deferred)) {
1715 dr = list_entry(svsk->sk_deferred.next,
1716 struct svc_deferred_req,
1717 handle.recent);
1718 list_del_init(&dr->handle.recent);
1719 set_bit(SK_DEFERRED, &svsk->sk_flags);
1720 }
Greg Banks1a68d952006-10-02 02:17:55 -07001721 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return dr;
1723}