blob: 7292baf5a7419d7938670d586db545bc977cdffc [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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/sock.h>
37#include <net/checksum.h>
38#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070039#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41#include <asm/ioctls.h>
42
43#include <linux/sunrpc/types.h>
44#include <linux/sunrpc/xdr.h>
45#include <linux/sunrpc/svcsock.h>
46#include <linux/sunrpc/stats.h>
47
48/* SMP locking strategy:
49 *
Greg Banks3262c812006-10-02 02:17:58 -070050 * svc_pool->sp_lock protects most of the fields of that pool.
51 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
52 * when both need to be taken (rare), svc_serv->sv_lock is first.
53 * BKL protects svc_serv->sv_nrthread.
Greg Banks1a68d952006-10-02 02:17:55 -070054 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
Greg Banksc081a0c2006-10-02 02:17:57 -070055 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
57 * Some flags can be set to certain values at any time
58 * providing that certain rules are followed:
59 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * SK_CONN, SK_DATA, can be set or cleared at any time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080061 * after a set, svc_sock_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * after a clear, the socket must be read/accepted
63 * if this succeeds, it must be set again.
64 * SK_CLOSE can set at any time. It is never cleared.
NeilBrownaaf68cf2007-02-08 14:20:30 -080065 * sk_inuse contains a bias of '1' until SK_DEAD is set.
66 * so when sk_inuse hits zero, we know the socket is dead
67 * and no-one is using it.
68 * SK_DEAD can only be set while SK_BUSY is held which ensures
69 * no other thread will be using the socket or will try to
70 * set SK_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 */
73
74#define RPCDBG_FACILITY RPCDBG_SVCSOCK
75
76
77static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080078 int *errp, int flags);
NeilBrownaaf68cf2007-02-08 14:20:30 -080079static void svc_delete_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void svc_udp_data_ready(struct sock *, int);
81static int svc_udp_recvfrom(struct svc_rqst *);
82static int svc_udp_sendto(struct svc_rqst *);
83
84static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
85static int svc_deferred_recv(struct svc_rqst *rqstp);
86static struct cache_deferred_req *svc_defer(struct cache_req *req);
87
Greg Banks36bdfc82006-10-02 02:17:54 -070088/* apparently the "standard" is that clients close
89 * idle connections after 5 minutes, servers after
90 * 6 minutes
91 * http://www.connectathon.org/talks96/nfstcp.pdf
92 */
93static int svc_conn_age_period = 6*60;
94
Peter Zijlstraed075362006-12-06 20:35:24 -080095#ifdef CONFIG_DEBUG_LOCK_ALLOC
96static struct lock_class_key svc_key[2];
97static struct lock_class_key svc_slock_key[2];
98
99static inline void svc_reclassify_socket(struct socket *sock)
100{
101 struct sock *sk = sock->sk;
102 BUG_ON(sk->sk_lock.owner != NULL);
103 switch (sk->sk_family) {
104 case AF_INET:
105 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
106 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
107 break;
108
109 case AF_INET6:
110 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
111 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
112 break;
113
114 default:
115 BUG();
116 }
117}
118#else
119static inline void svc_reclassify_socket(struct socket *sock)
120{
121}
122#endif
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
Greg Banks3262c812006-10-02 02:17:58 -0700125 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700127 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * the cache.
129 */
130static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700131svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Greg Banks3262c812006-10-02 02:17:58 -0700133 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/*
Greg Banks3262c812006-10-02 02:17:58 -0700137 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700140svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 list_del(&rqstp->rq_list);
143}
144
145/*
146 * Release an skbuff after use
147 */
148static inline void
149svc_release_skb(struct svc_rqst *rqstp)
150{
151 struct sk_buff *skb = rqstp->rq_skbuff;
152 struct svc_deferred_req *dr = rqstp->rq_deferred;
153
154 if (skb) {
155 rqstp->rq_skbuff = NULL;
156
157 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
158 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
159 }
160 if (dr) {
161 rqstp->rq_deferred = NULL;
162 kfree(dr);
163 }
164}
165
166/*
167 * Any space to write?
168 */
169static inline unsigned long
170svc_sock_wspace(struct svc_sock *svsk)
171{
172 int wspace;
173
174 if (svsk->sk_sock->type == SOCK_STREAM)
175 wspace = sk_stream_wspace(svsk->sk_sk);
176 else
177 wspace = sock_wspace(svsk->sk_sk);
178
179 return wspace;
180}
181
182/*
183 * Queue up a socket with data pending. If there are idle nfsd
184 * processes, wake 'em up.
185 *
186 */
187static void
188svc_sock_enqueue(struct svc_sock *svsk)
189{
190 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700191 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700193 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!(svsk->sk_flags &
196 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
197 return;
198 if (test_bit(SK_DEAD, &svsk->sk_flags))
199 return;
200
Greg Banksbfd24162006-10-02 02:18:01 -0700201 cpu = get_cpu();
202 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
203 put_cpu();
204
Greg Banks3262c812006-10-02 02:17:58 -0700205 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Greg Banks3262c812006-10-02 02:17:58 -0700207 if (!list_empty(&pool->sp_threads) &&
208 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 printk(KERN_ERR
210 "svc_sock_enqueue: threads and sockets both waiting??\n");
211
212 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
213 /* Don't enqueue dead sockets */
214 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
215 goto out_unlock;
216 }
217
Greg Banksc081a0c2006-10-02 02:17:57 -0700218 /* Mark socket as busy. It will remain in this state until the
219 * server has processed all pending data and put the socket back
220 * on the idle list. We update SK_BUSY atomically because
221 * it also guards against trying to enqueue the svc_sock twice.
222 */
223 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
224 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
226 goto out_unlock;
227 }
Greg Banks3262c812006-10-02 02:17:58 -0700228 BUG_ON(svsk->sk_pool != NULL);
229 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700232 if (((atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 > svc_sock_wspace(svsk))
234 && !test_bit(SK_CLOSE, &svsk->sk_flags)
235 && !test_bit(SK_CONN, &svsk->sk_flags)) {
236 /* Don't enqueue while not enough space for reply */
237 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700238 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_max_mesg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 svc_sock_wspace(svsk));
Greg Banks3262c812006-10-02 02:17:58 -0700240 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700241 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out_unlock;
243 }
244 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Greg Banks3262c812006-10-02 02:17:58 -0700247 if (!list_empty(&pool->sp_threads)) {
248 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct svc_rqst,
250 rq_list);
251 dprintk("svc: socket %p served by daemon %p\n",
252 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700253 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800255 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
257 rqstp, rqstp->rq_sock);
258 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700259 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700260 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -0700261 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700262 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 wake_up(&rqstp->rq_wait);
264 } else {
265 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700266 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
267 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700271 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
Greg Banks3262c812006-10-02 02:17:58 -0700275 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700278svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct svc_sock *svsk;
281
Greg Banks3262c812006-10-02 02:17:58 -0700282 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return NULL;
284
Greg Banks3262c812006-10-02 02:17:58 -0700285 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct svc_sock, sk_ready);
287 list_del_init(&svsk->sk_ready);
288
289 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700290 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return svsk;
293}
294
295/*
296 * Having read something from a socket, check whether it
297 * needs to be re-enqueued.
298 * Note: SK_DATA only gets cleared when a read-attempt finds
299 * no (or insufficient) data.
300 */
301static inline void
302svc_sock_received(struct svc_sock *svsk)
303{
Greg Banks3262c812006-10-02 02:17:58 -0700304 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 clear_bit(SK_BUSY, &svsk->sk_flags);
306 svc_sock_enqueue(svsk);
307}
308
309
310/**
311 * svc_reserve - change the space reserved for the reply to a request.
312 * @rqstp: The request in question
313 * @space: new max space to reserve
314 *
315 * Each request reserves some space on the output queue of the socket
316 * to make sure the reply fits. This function reduces that reserved
317 * space to be the amount of space used already, plus @space.
318 *
319 */
320void svc_reserve(struct svc_rqst *rqstp, int space)
321{
322 space += rqstp->rq_res.head[0].iov_len;
323
324 if (space < rqstp->rq_reserved) {
325 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700326 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 svc_sock_enqueue(svsk);
330 }
331}
332
333/*
334 * Release a socket after use.
335 */
336static inline void
337svc_sock_put(struct svc_sock *svsk)
338{
NeilBrownaaf68cf2007-02-08 14:20:30 -0800339 if (atomic_dec_and_test(&svsk->sk_inuse)) {
340 BUG_ON(! test_bit(SK_DEAD, &svsk->sk_flags));
341
Andrew Morton202dd452006-10-29 22:57:57 -0800342 dprintk("svc: releasing dead socket\n");
Neil Brownd6740df2006-10-29 22:46:45 -0800343 if (svsk->sk_sock->file)
344 sockfd_put(svsk->sk_sock);
345 else
346 sock_release(svsk->sk_sock);
347 if (svsk->sk_info_authunix != NULL)
348 svcauth_unix_info_release(svsk->sk_info_authunix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(svsk);
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static void
354svc_sock_release(struct svc_rqst *rqstp)
355{
356 struct svc_sock *svsk = rqstp->rq_sock;
357
358 svc_release_skb(rqstp);
359
NeilBrown44524352006-10-04 02:15:46 -0700360 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rqstp->rq_res.page_len = 0;
362 rqstp->rq_res.page_base = 0;
363
364
365 /* Reset response buffer and release
366 * the reservation.
367 * But first, check that enough space was reserved
368 * for the reply, otherwise we have a bug!
369 */
370 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
371 printk(KERN_ERR "RPC request reserved %d but used %d\n",
372 rqstp->rq_reserved,
373 rqstp->rq_res.len);
374
375 rqstp->rq_res.head[0].iov_len = 0;
376 svc_reserve(rqstp, 0);
377 rqstp->rq_sock = NULL;
378
379 svc_sock_put(svsk);
380}
381
382/*
383 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700384 * This really only makes sense for services like lockd
385 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387void
388svc_wake_up(struct svc_serv *serv)
389{
390 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700391 unsigned int i;
392 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Greg Banks3262c812006-10-02 02:17:58 -0700394 for (i = 0; i < serv->sv_nrpools; i++) {
395 pool = &serv->sv_pools[i];
396
397 spin_lock_bh(&pool->sp_lock);
398 if (!list_empty(&pool->sp_threads)) {
399 rqstp = list_entry(pool->sp_threads.next,
400 struct svc_rqst,
401 rq_list);
402 dprintk("svc: daemon %p woken up.\n", rqstp);
403 /*
404 svc_thread_dequeue(pool, rqstp);
405 rqstp->rq_sock = NULL;
406 */
407 wake_up(&rqstp->rq_wait);
408 }
409 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/*
414 * Generic sendto routine
415 */
416static int
417svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
418{
419 struct svc_sock *svsk = rqstp->rq_sock;
420 struct socket *sock = svsk->sk_sock;
421 int slen;
422 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
423 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
424 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
425 int len = 0;
426 int result;
427 int size;
428 struct page **ppage = xdr->pages;
429 size_t base = xdr->page_base;
430 unsigned int pglen = xdr->page_len;
431 unsigned int flags = MSG_MORE;
432
433 slen = xdr->len;
434
435 if (rqstp->rq_prot == IPPROTO_UDP) {
436 /* set the source and destination */
437 struct msghdr msg;
438 msg.msg_name = &rqstp->rq_addr;
439 msg.msg_namelen = sizeof(rqstp->rq_addr);
440 msg.msg_iov = NULL;
441 msg.msg_iovlen = 0;
442 msg.msg_flags = MSG_MORE;
443
444 msg.msg_control = cmh;
445 msg.msg_controllen = sizeof(buffer);
446 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
447 cmh->cmsg_level = SOL_IP;
448 cmh->cmsg_type = IP_PKTINFO;
449 pki->ipi_ifindex = 0;
450 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
451
452 if (sock_sendmsg(sock, &msg, 0) < 0)
453 goto out;
454 }
455
456 /* send head */
457 if (slen == xdr->head[0].iov_len)
458 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700459 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
460 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (len != xdr->head[0].iov_len)
462 goto out;
463 slen -= xdr->head[0].iov_len;
464 if (slen == 0)
465 goto out;
466
467 /* send page data */
468 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
469 while (pglen > 0) {
470 if (slen == size)
471 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700472 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (result > 0)
474 len += result;
475 if (result != size)
476 goto out;
477 slen -= size;
478 pglen -= size;
479 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
480 base = 0;
481 ppage++;
482 }
483 /* send tail */
484 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700485 result = kernel_sendpage(sock, rqstp->rq_respages[0],
486 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800487 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 xdr->tail[0].iov_len, 0);
489
490 if (result > 0)
491 len += result;
492 }
493out:
494 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
495 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
496 rqstp->rq_addr.sin_addr.s_addr);
497
498 return len;
499}
500
501/*
NeilBrown80212d52006-10-02 02:17:47 -0700502 * Report socket names for nfsdfs
503 */
504static int one_sock_name(char *buf, struct svc_sock *svsk)
505{
506 int len;
507
508 switch(svsk->sk_sk->sk_family) {
509 case AF_INET:
510 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
511 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
512 "udp" : "tcp",
513 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
514 inet_sk(svsk->sk_sk)->num);
515 break;
516 default:
517 len = sprintf(buf, "*unknown-%d*\n",
518 svsk->sk_sk->sk_family);
519 }
520 return len;
521}
522
523int
NeilBrownb41b66d2006-10-02 02:17:48 -0700524svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700525{
NeilBrownb41b66d2006-10-02 02:17:48 -0700526 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700527 int len = 0;
528
529 if (!serv)
530 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800531 spin_lock_bh(&serv->sv_lock);
NeilBrown80212d52006-10-02 02:17:47 -0700532 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
533 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700534 if (toclose && strcmp(toclose, buf+len) == 0)
535 closesk = svsk;
536 else
537 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700538 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800539 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700540 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700541 /* Should unregister with portmap, but you cannot
542 * unregister just one protocol...
543 */
NeilBrownaaf68cf2007-02-08 14:20:30 -0800544 svc_close_socket(closesk);
NeilBrown37a03472006-10-04 02:15:44 -0700545 else if (toclose)
546 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700547 return len;
548}
549EXPORT_SYMBOL(svc_sock_names);
550
551/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * Check input queue length
553 */
554static int
555svc_recv_available(struct svc_sock *svsk)
556{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct socket *sock = svsk->sk_sock;
558 int avail, err;
559
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700560 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 return (err >= 0)? avail : err;
563}
564
565/*
566 * Generic recvfrom routine.
567 */
568static int
569svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
570{
571 struct msghdr msg;
572 struct socket *sock;
573 int len, alen;
574
575 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
576 sock = rqstp->rq_sock->sk_sock;
577
578 msg.msg_name = &rqstp->rq_addr;
579 msg.msg_namelen = sizeof(rqstp->rq_addr);
580 msg.msg_control = NULL;
581 msg.msg_controllen = 0;
582
583 msg.msg_flags = MSG_DONTWAIT;
584
585 len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
586
587 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
588 * possibly we should cache this in the svc_sock structure
589 * at accept time. FIXME
590 */
591 alen = sizeof(rqstp->rq_addr);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700592 kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
595 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
596
597 return len;
598}
599
600/*
601 * Set socket snd and rcv buffer lengths
602 */
603static inline void
604svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
605{
606#if 0
607 mm_segment_t oldfs;
608 oldfs = get_fs(); set_fs(KERNEL_DS);
609 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
610 (char*)&snd, sizeof(snd));
611 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
612 (char*)&rcv, sizeof(rcv));
613#else
614 /* sock_setsockopt limits use to sysctl_?mem_max,
615 * which isn't acceptable. Until that is made conditional
616 * on not having CAP_SYS_RESOURCE or similar, we go direct...
617 * DaveM said I could!
618 */
619 lock_sock(sock->sk);
620 sock->sk->sk_sndbuf = snd * 2;
621 sock->sk->sk_rcvbuf = rcv * 2;
622 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
623 release_sock(sock->sk);
624#endif
625}
626/*
627 * INET callback when data has been received on the socket.
628 */
629static void
630svc_udp_data_ready(struct sock *sk, int count)
631{
Neil Brown939bb7e2005-09-13 01:25:39 -0700632 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Neil Brown939bb7e2005-09-13 01:25:39 -0700634 if (svsk) {
635 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
636 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
637 set_bit(SK_DATA, &svsk->sk_flags);
638 svc_sock_enqueue(svsk);
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
641 wake_up_interruptible(sk->sk_sleep);
642}
643
644/*
645 * INET callback when space is newly available on the socket.
646 */
647static void
648svc_write_space(struct sock *sk)
649{
650 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
651
652 if (svsk) {
653 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
654 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
655 svc_sock_enqueue(svsk);
656 }
657
658 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700659 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 svsk);
661 wake_up_interruptible(sk->sk_sleep);
662 }
663}
664
665/*
666 * Receive a datagram from a UDP socket.
667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int
669svc_udp_recvfrom(struct svc_rqst *rqstp)
670{
671 struct svc_sock *svsk = rqstp->rq_sock;
672 struct svc_serv *serv = svsk->sk_server;
673 struct sk_buff *skb;
674 int err, len;
675
676 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
677 /* udp sockets need large rcvbuf as all pending
678 * requests are still in that buffer. sndbuf must
679 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700680 * for one reply per thread. We count all threads
681 * rather than threads in a particular pool, which
682 * provides an upper bound on the number of threads
683 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
685 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700686 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
687 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
690 svc_sock_received(svsk);
691 return svc_deferred_recv(rqstp);
692 }
693
NeilBrownaaf68cf2007-02-08 14:20:30 -0800694 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
695 svc_delete_socket(svsk);
696 return 0;
697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 clear_bit(SK_DATA, &svsk->sk_flags);
700 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
701 if (err == -EAGAIN) {
702 svc_sock_received(svsk);
703 return err;
704 }
705 /* possibly an icmp error */
706 dprintk("svc: recvfrom returned error %d\n", -err);
707 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700708 if (skb->tstamp.off_sec == 0) {
709 struct timeval tv;
710
711 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700712 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700713 skb_set_timestamp(skb, &tv);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800714 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 need that much accuracy */
716 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700717 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
719
720 /*
721 * Maybe more packets - kick another thread ASAP.
722 */
723 svc_sock_received(svsk);
724
725 len = skb->len - sizeof(struct udphdr);
726 rqstp->rq_arg.len = len;
727
728 rqstp->rq_prot = IPPROTO_UDP;
729
730 /* Get sender address */
731 rqstp->rq_addr.sin_family = AF_INET;
732 rqstp->rq_addr.sin_port = skb->h.uh->source;
733 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
734 rqstp->rq_daddr = skb->nh.iph->daddr;
735
736 if (skb_is_nonlinear(skb)) {
737 /* we have to copy */
738 local_bh_disable();
739 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
740 local_bh_enable();
741 /* checksum error */
742 skb_free_datagram(svsk->sk_sk, skb);
743 return 0;
744 }
745 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800746 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 } else {
748 /* we can use it in-place */
749 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
750 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800751 if (skb_checksum_complete(skb)) {
752 skb_free_datagram(svsk->sk_sk, skb);
753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 rqstp->rq_skbuff = skb;
756 }
757
758 rqstp->rq_arg.page_base = 0;
759 if (len <= rqstp->rq_arg.head[0].iov_len) {
760 rqstp->rq_arg.head[0].iov_len = len;
761 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700762 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else {
764 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700765 rqstp->rq_respages = rqstp->rq_pages + 1 +
766 (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 if (serv->sv_stats)
770 serv->sv_stats->netudpcnt++;
771
772 return len;
773}
774
775static int
776svc_udp_sendto(struct svc_rqst *rqstp)
777{
778 int error;
779
780 error = svc_sendto(rqstp, &rqstp->rq_res);
781 if (error == -ECONNREFUSED)
782 /* ICMP error on earlier request. */
783 error = svc_sendto(rqstp, &rqstp->rq_res);
784
785 return error;
786}
787
788static void
789svc_udp_init(struct svc_sock *svsk)
790{
791 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
792 svsk->sk_sk->sk_write_space = svc_write_space;
793 svsk->sk_recvfrom = svc_udp_recvfrom;
794 svsk->sk_sendto = svc_udp_sendto;
795
796 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800797 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * svc_udp_recvfrom will re-adjust if necessary
799 */
800 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700801 3 * svsk->sk_server->sv_max_mesg,
802 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
805 set_bit(SK_CHNGBUF, &svsk->sk_flags);
806}
807
808/*
809 * A data_ready event on a listening socket means there's a connection
810 * pending. Do not use state_change as a substitute for it.
811 */
812static void
813svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
814{
Neil Brown939bb7e2005-09-13 01:25:39 -0700815 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700818 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Neil Brown939bb7e2005-09-13 01:25:39 -0700820 /*
821 * This callback may called twice when a new connection
822 * is established as a child socket inherits everything
823 * from a parent LISTEN socket.
824 * 1) data_ready method of the parent socket will be called
825 * when one of child sockets become ESTABLISHED.
826 * 2) data_ready method of the child socket may be called
827 * when it receives data before the socket is accepted.
828 * In case of 2, we should ignore it silently.
829 */
830 if (sk->sk_state == TCP_LISTEN) {
831 if (svsk) {
832 set_bit(SK_CONN, &svsk->sk_flags);
833 svc_sock_enqueue(svsk);
834 } else
835 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
839 wake_up_interruptible_all(sk->sk_sleep);
840}
841
842/*
843 * A state change on a connected socket means it's dying or dead.
844 */
845static void
846svc_tcp_state_change(struct sock *sk)
847{
Neil Brown939bb7e2005-09-13 01:25:39 -0700848 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700851 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Neil Brown939bb7e2005-09-13 01:25:39 -0700853 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700855 else {
856 set_bit(SK_CLOSE, &svsk->sk_flags);
857 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
860 wake_up_interruptible_all(sk->sk_sleep);
861}
862
863static void
864svc_tcp_data_ready(struct sock *sk, int count)
865{
Neil Brown939bb7e2005-09-13 01:25:39 -0700866 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700869 sk, sk->sk_user_data);
870 if (svsk) {
871 set_bit(SK_DATA, &svsk->sk_flags);
872 svc_sock_enqueue(svsk);
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
875 wake_up_interruptible(sk->sk_sleep);
876}
877
878/*
879 * Accept a TCP connection
880 */
881static void
882svc_tcp_accept(struct svc_sock *svsk)
883{
884 struct sockaddr_in sin;
885 struct svc_serv *serv = svsk->sk_server;
886 struct socket *sock = svsk->sk_sock;
887 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct svc_sock *newsvsk;
889 int err, slen;
890
891 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
892 if (!sock)
893 return;
894
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700895 clear_bit(SK_CONN, &svsk->sk_flags);
896 err = kernel_accept(sock, &newsock, O_NONBLOCK);
897 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (err == -ENOMEM)
899 printk(KERN_WARNING "%s: no more sockets!\n",
900 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700901 else if (err != -EAGAIN && net_ratelimit())
902 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
903 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return;
905 }
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 set_bit(SK_CONN, &svsk->sk_flags);
908 svc_sock_enqueue(svsk);
909
910 slen = sizeof(sin);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700911 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (err < 0) {
913 if (net_ratelimit())
914 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
915 serv->sv_name, -err);
916 goto failed; /* aborted connection or whatever */
917 }
918
919 /* Ideally, we would want to reject connections from unauthorized
920 * hosts here, but when we get encription, the IP of the host won't
921 * tell us anything. For now just warn about unpriv connections.
922 */
923 if (ntohs(sin.sin_port) >= 1024) {
924 dprintk(KERN_WARNING
925 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800926 serv->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
928 }
929
930 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
931 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
932
933 /* make sure that a write doesn't block forever when
934 * low on memory
935 */
936 newsock->sk->sk_sndtimeo = HZ*30;
937
Chuck Lever6b174332007-02-12 00:53:28 -0800938 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
939 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto failed;
NeilBrowne79eff12007-02-12 00:53:30 -0800941 svc_sock_received(newsvsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* make sure that we don't have too many active connections.
944 * If we have, something must be dropped.
945 *
946 * There's no point in trying to do random drop here for
947 * DoS prevention. The NFS clients does 1 reconnect in 15
948 * seconds. An attacker can easily beat that.
949 *
950 * The only somewhat efficient mechanism would be if drop
951 * old connections from the same IP first. But right now
952 * we don't even record the client IP in svc_sock.
953 */
954 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
955 struct svc_sock *svsk = NULL;
956 spin_lock_bh(&serv->sv_lock);
957 if (!list_empty(&serv->sv_tempsocks)) {
958 if (net_ratelimit()) {
959 /* Try to help the admin */
960 printk(KERN_NOTICE "%s: too many open TCP "
961 "sockets, consider increasing the "
962 "number of nfsd threads\n",
963 serv->sv_name);
964 printk(KERN_NOTICE "%s: last TCP connect from "
965 "%u.%u.%u.%u:%d\n",
966 serv->sv_name,
967 NIPQUAD(sin.sin_addr.s_addr),
968 ntohs(sin.sin_port));
969 }
970 /*
971 * Always select the oldest socket. It's not fair,
972 * but so is life
973 */
974 svsk = list_entry(serv->sv_tempsocks.prev,
975 struct svc_sock,
976 sk_list);
977 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -0700978 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 spin_unlock_bh(&serv->sv_lock);
981
982 if (svsk) {
983 svc_sock_enqueue(svsk);
984 svc_sock_put(svsk);
985 }
986
987 }
988
989 if (serv->sv_stats)
990 serv->sv_stats->nettcpconn++;
991
992 return;
993
994failed:
995 sock_release(newsock);
996 return;
997}
998
999/*
1000 * Receive data from a TCP socket.
1001 */
1002static int
1003svc_tcp_recvfrom(struct svc_rqst *rqstp)
1004{
1005 struct svc_sock *svsk = rqstp->rq_sock;
1006 struct svc_serv *serv = svsk->sk_server;
1007 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001008 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int pnum, vlen;
1010
1011 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1012 svsk, test_bit(SK_DATA, &svsk->sk_flags),
1013 test_bit(SK_CONN, &svsk->sk_flags),
1014 test_bit(SK_CLOSE, &svsk->sk_flags));
1015
1016 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1017 svc_sock_received(svsk);
1018 return svc_deferred_recv(rqstp);
1019 }
1020
1021 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
1022 svc_delete_socket(svsk);
1023 return 0;
1024 }
1025
NeilBrown1a047062006-10-19 23:29:13 -07001026 if (svsk->sk_sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 svc_tcp_accept(svsk);
1028 svc_sock_received(svsk);
1029 return 0;
1030 }
1031
1032 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
1033 /* sndbuf needs to have room for one request
1034 * per thread, otherwise we can stall even when the
1035 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001036 *
1037 * We count all threads rather than threads in a
1038 * particular pool, which provides an upper bound
1039 * on the number of threads which will access the socket.
1040 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001042 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * as soon a a complete request arrives.
1044 */
1045 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001046 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1047 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 clear_bit(SK_DATA, &svsk->sk_flags);
1050
1051 /* Receive data. If we haven't got the record length yet, get
1052 * the next four bytes. Otherwise try to gobble up as much as
1053 * possible up to the complete record length.
1054 */
1055 if (svsk->sk_tcplen < 4) {
1056 unsigned long want = 4 - svsk->sk_tcplen;
1057 struct kvec iov;
1058
1059 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1060 iov.iov_len = want;
1061 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1062 goto error;
1063 svsk->sk_tcplen += len;
1064
1065 if (len < want) {
1066 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001067 len, want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 svc_sock_received(svsk);
1069 return -EAGAIN; /* record header not complete */
1070 }
1071
1072 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1073 if (!(svsk->sk_reclen & 0x80000000)) {
1074 /* FIXME: technically, a record can be fragmented,
1075 * and non-terminal fragments will not have the top
1076 * bit set in the fragment length header.
1077 * But apparently no known nfs clients send fragmented
1078 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001079 if (net_ratelimit())
1080 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1081 " (non-terminal)\n",
1082 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto err_delete;
1084 }
1085 svsk->sk_reclen &= 0x7fffffff;
1086 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001087 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001088 if (net_ratelimit())
1089 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1090 " (large)\n",
1091 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto err_delete;
1093 }
1094 }
1095
1096 /* Check whether enough data is available */
1097 len = svc_recv_available(svsk);
1098 if (len < 0)
1099 goto error;
1100
1101 if (len < svsk->sk_reclen) {
1102 dprintk("svc: incomplete TCP record (%d of %d)\n",
1103 len, svsk->sk_reclen);
1104 svc_sock_received(svsk);
1105 return -EAGAIN; /* record not complete */
1106 }
1107 len = svsk->sk_reclen;
1108 set_bit(SK_DATA, &svsk->sk_flags);
1109
NeilBrown3cc03b12006-10-04 02:15:47 -07001110 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 vec[0] = rqstp->rq_arg.head[0];
1112 vlen = PAGE_SIZE;
1113 pnum = 1;
1114 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001115 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 vec[pnum].iov_len = PAGE_SIZE;
1117 pnum++;
1118 vlen += PAGE_SIZE;
1119 }
NeilBrown44524352006-10-04 02:15:46 -07001120 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /* Now receive data */
1123 len = svc_recvfrom(rqstp, vec, pnum, len);
1124 if (len < 0)
1125 goto error;
1126
1127 dprintk("svc: TCP complete record (%d bytes)\n", len);
1128 rqstp->rq_arg.len = len;
1129 rqstp->rq_arg.page_base = 0;
1130 if (len <= rqstp->rq_arg.head[0].iov_len) {
1131 rqstp->rq_arg.head[0].iov_len = len;
1132 rqstp->rq_arg.page_len = 0;
1133 } else {
1134 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1135 }
1136
1137 rqstp->rq_skbuff = NULL;
1138 rqstp->rq_prot = IPPROTO_TCP;
1139
1140 /* Reset TCP read info */
1141 svsk->sk_reclen = 0;
1142 svsk->sk_tcplen = 0;
1143
1144 svc_sock_received(svsk);
1145 if (serv->sv_stats)
1146 serv->sv_stats->nettcpcnt++;
1147
1148 return len;
1149
1150 err_delete:
1151 svc_delete_socket(svsk);
1152 return -EAGAIN;
1153
1154 error:
1155 if (len == -EAGAIN) {
1156 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1157 svc_sock_received(svsk);
1158 } else {
1159 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1160 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001161 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 return len;
1165}
1166
1167/*
1168 * Send out data on TCP socket.
1169 */
1170static int
1171svc_tcp_sendto(struct svc_rqst *rqstp)
1172{
1173 struct xdr_buf *xbufp = &rqstp->rq_res;
1174 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001175 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* Set up the first element of the reply kvec.
1178 * Any other kvecs that may be in use have been taken
1179 * care of by the server implementation itself.
1180 */
1181 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1182 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1183
1184 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1185 return -ENOTCONN;
1186
1187 sent = svc_sendto(rqstp, &rqstp->rq_res);
1188 if (sent != xbufp->len) {
1189 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1190 rqstp->rq_sock->sk_server->sv_name,
1191 (sent<0)?"got error":"sent only",
1192 sent, xbufp->len);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001193 set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
1194 svc_sock_enqueue(rqstp->rq_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 sent = -EAGAIN;
1196 }
1197 return sent;
1198}
1199
1200static void
1201svc_tcp_init(struct svc_sock *svsk)
1202{
1203 struct sock *sk = svsk->sk_sk;
1204 struct tcp_sock *tp = tcp_sk(sk);
1205
1206 svsk->sk_recvfrom = svc_tcp_recvfrom;
1207 svsk->sk_sendto = svc_tcp_sendto;
1208
1209 if (sk->sk_state == TCP_LISTEN) {
1210 dprintk("setting up TCP socket for listening\n");
1211 sk->sk_data_ready = svc_tcp_listen_data_ready;
1212 set_bit(SK_CONN, &svsk->sk_flags);
1213 } else {
1214 dprintk("setting up TCP socket for reading\n");
1215 sk->sk_state_change = svc_tcp_state_change;
1216 sk->sk_data_ready = svc_tcp_data_ready;
1217 sk->sk_write_space = svc_write_space;
1218
1219 svsk->sk_reclen = 0;
1220 svsk->sk_tcplen = 0;
1221
1222 tp->nonagle = 1; /* disable Nagle's algorithm */
1223
1224 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001225 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * svc_tcp_recvfrom will re-adjust if necessary
1227 */
1228 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001229 3 * svsk->sk_server->sv_max_mesg,
1230 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1233 set_bit(SK_DATA, &svsk->sk_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001234 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 set_bit(SK_CLOSE, &svsk->sk_flags);
1236 }
1237}
1238
1239void
1240svc_sock_update_bufs(struct svc_serv *serv)
1241{
1242 /*
1243 * The number of server threads has changed. Update
1244 * rcvbuf and sndbuf accordingly on all sockets
1245 */
1246 struct list_head *le;
1247
1248 spin_lock_bh(&serv->sv_lock);
1249 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001250 struct svc_sock *svsk =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 list_entry(le, struct svc_sock, sk_list);
1252 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1253 }
1254 list_for_each(le, &serv->sv_tempsocks) {
1255 struct svc_sock *svsk =
1256 list_entry(le, struct svc_sock, sk_list);
1257 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1258 }
1259 spin_unlock_bh(&serv->sv_lock);
1260}
1261
1262/*
Greg Banks3262c812006-10-02 02:17:58 -07001263 * Receive the next request on any socket. This code is carefully
1264 * organised not to touch any cachelines in the shared svc_serv
1265 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
1267int
NeilBrown6fb2b472006-10-02 02:17:50 -07001268svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct svc_sock *svsk =NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001271 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001272 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001273 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int pages;
1275 struct xdr_buf *arg;
1276 DECLARE_WAITQUEUE(wait, current);
1277
1278 dprintk("svc: server %p waiting for data (to = %ld)\n",
1279 rqstp, timeout);
1280
1281 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001282 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 "svc_recv: service %p, socket not NULL!\n",
1284 rqstp);
1285 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001286 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 "svc_recv: service %p, wait queue active!\n",
1288 rqstp);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001292 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001293 for (i=0; i < pages ; i++)
1294 while (rqstp->rq_pages[i] == NULL) {
1295 struct page *p = alloc_page(GFP_KERNEL);
1296 if (!p)
1297 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1298 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
NeilBrown250f3912007-01-26 00:56:59 -08001300 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1301 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Make arg->head point to first page and arg->pages point to rest */
1304 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001305 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001307 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 arg->page_base = 0;
1309 /* save at least one page for response */
1310 arg->page_len = (pages-2)*PAGE_SIZE;
1311 arg->len = (pages-1)*PAGE_SIZE;
1312 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001313
1314 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001315 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (signalled())
1317 return -EINTR;
1318
Greg Banks3262c812006-10-02 02:17:58 -07001319 spin_lock_bh(&pool->sp_lock);
1320 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001322 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001323 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -07001324 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else {
1326 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001327 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /*
1330 * We have to be able to interrupt this wait
1331 * to bring down the daemons ...
1332 */
1333 set_current_state(TASK_INTERRUPTIBLE);
1334 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001335 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 schedule_timeout(timeout);
1338
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001339 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Greg Banks3262c812006-10-02 02:17:58 -07001341 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 remove_wait_queue(&rqstp->rq_wait, &wait);
1343
1344 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001345 svc_thread_dequeue(pool, rqstp);
1346 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dprintk("svc: server %p, no data yet\n", rqstp);
1348 return signalled()? -EINTR : -EAGAIN;
1349 }
1350 }
Greg Banks3262c812006-10-02 02:17:58 -07001351 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Greg Banks3262c812006-10-02 02:17:58 -07001353 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1354 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 len = svsk->sk_recvfrom(rqstp);
1356 dprintk("svc: got len=%d\n", len);
1357
1358 /* No data, incomplete (TCP) read, or accept() */
1359 if (len == 0 || len == -EAGAIN) {
1360 rqstp->rq_res.len = 0;
1361 svc_sock_release(rqstp);
1362 return -EAGAIN;
1363 }
1364 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001365 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1368 rqstp->rq_chandle.defer = svc_defer;
1369
1370 if (serv->sv_stats)
1371 serv->sv_stats->netcnt++;
1372 return len;
1373}
1374
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * Drop request
1377 */
1378void
1379svc_drop(struct svc_rqst *rqstp)
1380{
1381 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1382 svc_sock_release(rqstp);
1383}
1384
1385/*
1386 * Return reply to client.
1387 */
1388int
1389svc_send(struct svc_rqst *rqstp)
1390{
1391 struct svc_sock *svsk;
1392 int len;
1393 struct xdr_buf *xb;
1394
1395 if ((svsk = rqstp->rq_sock) == NULL) {
1396 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1397 __FILE__, __LINE__);
1398 return -EFAULT;
1399 }
1400
1401 /* release the receive skb before sending the reply */
1402 svc_release_skb(rqstp);
1403
1404 /* calculate over-all length */
1405 xb = & rqstp->rq_res;
1406 xb->len = xb->head[0].iov_len +
1407 xb->page_len +
1408 xb->tail[0].iov_len;
1409
Ingo Molnar57b47a52006-03-20 22:35:41 -08001410 /* Grab svsk->sk_mutex to serialize outgoing data. */
1411 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (test_bit(SK_DEAD, &svsk->sk_flags))
1413 len = -ENOTCONN;
1414 else
1415 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001416 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 svc_sock_release(rqstp);
1418
1419 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1420 return 0;
1421 return len;
1422}
1423
1424/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001425 * Timer function to close old temporary sockets, using
1426 * a mark-and-sweep algorithm.
1427 */
1428static void
1429svc_age_temp_sockets(unsigned long closure)
1430{
1431 struct svc_serv *serv = (struct svc_serv *)closure;
1432 struct svc_sock *svsk;
1433 struct list_head *le, *next;
1434 LIST_HEAD(to_be_aged);
1435
1436 dprintk("svc_age_temp_sockets\n");
1437
1438 if (!spin_trylock_bh(&serv->sv_lock)) {
1439 /* busy, try again 1 sec later */
1440 dprintk("svc_age_temp_sockets: busy\n");
1441 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1442 return;
1443 }
1444
1445 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1446 svsk = list_entry(le, struct svc_sock, sk_list);
1447
1448 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1449 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001450 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001451 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001452 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001453 list_move(le, &to_be_aged);
1454 set_bit(SK_CLOSE, &svsk->sk_flags);
1455 set_bit(SK_DETACHED, &svsk->sk_flags);
1456 }
1457 spin_unlock_bh(&serv->sv_lock);
1458
1459 while (!list_empty(&to_be_aged)) {
1460 le = to_be_aged.next;
1461 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1462 list_del_init(le);
1463 svsk = list_entry(le, struct svc_sock, sk_list);
1464
1465 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1466 svsk, get_seconds() - svsk->sk_lastrecv);
1467
1468 /* a thread will dequeue and close it soon */
1469 svc_sock_enqueue(svsk);
1470 svc_sock_put(svsk);
1471 }
1472
1473 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1474}
1475
1476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * Initialize socket for RPC use and create svc_sock struct
1478 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1479 */
Chuck Lever6b174332007-02-12 00:53:28 -08001480static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1481 struct socket *sock,
1482 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct svc_sock *svsk;
1485 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001486 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1487 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001490 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *errp = -ENOMEM;
1492 return NULL;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 inet = sock->sk;
1496
1497 /* Register socket with portmapper */
1498 if (*errp >= 0 && pmap_register)
1499 *errp = svc_register(serv, inet->sk_protocol,
1500 ntohs(inet_sk(inet)->sport));
1501
1502 if (*errp < 0) {
1503 kfree(svsk);
1504 return NULL;
1505 }
1506
1507 set_bit(SK_BUSY, &svsk->sk_flags);
1508 inet->sk_user_data = svsk;
1509 svsk->sk_sock = sock;
1510 svsk->sk_sk = inet;
1511 svsk->sk_ostate = inet->sk_state_change;
1512 svsk->sk_odata = inet->sk_data_ready;
1513 svsk->sk_owspace = inet->sk_write_space;
1514 svsk->sk_server = serv;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001515 atomic_set(&svsk->sk_inuse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 svsk->sk_lastrecv = get_seconds();
Greg Banks1a68d952006-10-02 02:17:55 -07001517 spin_lock_init(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 INIT_LIST_HEAD(&svsk->sk_deferred);
1519 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001520 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 /* Initialize the socket */
1523 if (sock->type == SOCK_DGRAM)
1524 svc_udp_init(svsk);
1525 else
1526 svc_tcp_init(svsk);
1527
1528 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001529 if (is_temporary) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 set_bit(SK_TEMP, &svsk->sk_flags);
1531 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1532 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001533 if (serv->sv_temptimer.function == NULL) {
1534 /* setup timer to age temp sockets */
1535 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1536 (unsigned long)serv);
1537 mod_timer(&serv->sv_temptimer,
1538 jiffies + svc_conn_age_period * HZ);
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 } else {
1541 clear_bit(SK_TEMP, &svsk->sk_flags);
1542 list_add(&svsk->sk_list, &serv->sv_permsocks);
1543 }
1544 spin_unlock_bh(&serv->sv_lock);
1545
1546 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1547 svsk, svsk->sk_sk);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return svsk;
1550}
1551
NeilBrownb41b66d2006-10-02 02:17:48 -07001552int svc_addsock(struct svc_serv *serv,
1553 int fd,
1554 char *name_return,
1555 int *proto)
1556{
1557 int err = 0;
1558 struct socket *so = sockfd_lookup(fd, &err);
1559 struct svc_sock *svsk = NULL;
1560
1561 if (!so)
1562 return err;
1563 if (so->sk->sk_family != AF_INET)
1564 err = -EAFNOSUPPORT;
1565 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1566 so->sk->sk_protocol != IPPROTO_UDP)
1567 err = -EPROTONOSUPPORT;
1568 else if (so->state > SS_UNCONNECTED)
1569 err = -EISCONN;
1570 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001571 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001572 if (svsk) {
1573 svc_sock_received(svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -07001574 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001575 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001576 }
1577 if (err) {
1578 sockfd_put(so);
1579 return err;
1580 }
1581 if (proto) *proto = so->sk->sk_protocol;
1582 return one_sock_name(name_return, svsk);
1583}
1584EXPORT_SYMBOL_GPL(svc_addsock);
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/*
1587 * Create socket for RPC service.
1588 */
Chuck Lever6b174332007-02-12 00:53:28 -08001589static int svc_create_socket(struct svc_serv *serv, int protocol,
1590 struct sockaddr_in *sin, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct svc_sock *svsk;
1593 struct socket *sock;
1594 int error;
1595 int type;
1596
1597 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1598 serv->sv_program->pg_name, protocol,
1599 NIPQUAD(sin->sin_addr.s_addr),
1600 ntohs(sin->sin_port));
1601
1602 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1603 printk(KERN_WARNING "svc: only UDP and TCP "
1604 "sockets supported\n");
1605 return -EINVAL;
1606 }
1607 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1608
1609 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1610 return error;
1611
Peter Zijlstraed075362006-12-06 20:35:24 -08001612 svc_reclassify_socket(sock);
1613
Eric Sesterhenn18114742006-09-28 14:37:07 -07001614 if (type == SOCK_STREAM)
1615 sock->sk->sk_reuse = 1; /* allow address reuse */
1616 error = kernel_bind(sock, (struct sockaddr *) sin,
1617 sizeof(*sin));
1618 if (error < 0)
1619 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001622 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto bummer;
1624 }
1625
NeilBrowne79eff12007-02-12 00:53:30 -08001626 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1627 svc_sock_received(svsk);
Chuck Lever6b174332007-02-12 00:53:28 -08001628 return ntohs(inet_sk(svsk->sk_sk)->sport);
NeilBrowne79eff12007-02-12 00:53:30 -08001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631bummer:
1632 dprintk("svc: svc_create_socket error = %d\n", -error);
1633 sock_release(sock);
1634 return error;
1635}
1636
1637/*
1638 * Remove a dead socket
1639 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001640static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641svc_delete_socket(struct svc_sock *svsk)
1642{
1643 struct svc_serv *serv;
1644 struct sock *sk;
1645
1646 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1647
1648 serv = svsk->sk_server;
1649 sk = svsk->sk_sk;
1650
1651 sk->sk_state_change = svsk->sk_ostate;
1652 sk->sk_data_ready = svsk->sk_odata;
1653 sk->sk_write_space = svsk->sk_owspace;
1654
1655 spin_lock_bh(&serv->sv_lock);
1656
Greg Banks36bdfc82006-10-02 02:17:54 -07001657 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1658 list_del_init(&svsk->sk_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001659 /*
Greg Banks3262c812006-10-02 02:17:58 -07001660 * We used to delete the svc_sock from whichever list
1661 * it's sk_ready node was on, but we don't actually
1662 * need to. This is because the only time we're called
1663 * while still attached to a queue, the queue itself
1664 * is about to be destroyed (in svc_destroy).
1665 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001666 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
1667 BUG_ON(atomic_read(&svsk->sk_inuse)<2);
1668 atomic_dec(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (test_bit(SK_TEMP, &svsk->sk_flags))
1670 serv->sv_tmpcnt--;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Neil Brownd6740df2006-10-29 22:46:45 -08001673 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001674}
1675
1676void svc_close_socket(struct svc_sock *svsk)
1677{
1678 set_bit(SK_CLOSE, &svsk->sk_flags);
1679 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
1680 /* someone else will have to effect the close */
1681 return;
1682
1683 atomic_inc(&svsk->sk_inuse);
1684 svc_delete_socket(svsk);
1685 clear_bit(SK_BUSY, &svsk->sk_flags);
Neil Brownd6740df2006-10-29 22:46:45 -08001686 svc_sock_put(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Chuck Lever6b174332007-02-12 00:53:28 -08001689/**
1690 * svc_makesock - Make a socket for nfsd and lockd
1691 * @serv: RPC server structure
1692 * @protocol: transport protocol to use
1693 * @port: port to use
Chuck Lever482fb942007-02-12 00:53:29 -08001694 * @flags: requested socket characteristics
Chuck Lever6b174332007-02-12 00:53:28 -08001695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Chuck Lever482fb942007-02-12 00:53:29 -08001697int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
1698 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Chuck Lever6b174332007-02-12 00:53:28 -08001700 struct sockaddr_in sin = {
1701 .sin_family = AF_INET,
1702 .sin_addr.s_addr = INADDR_ANY,
1703 .sin_port = htons(port),
1704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 dprintk("svc: creating socket proto = %d\n", protocol);
Chuck Lever482fb942007-02-12 00:53:29 -08001707 return svc_create_socket(serv, protocol, &sin, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001711 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
1713
1714static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1715{
1716 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct svc_sock *svsk;
1718
1719 if (too_many) {
1720 svc_sock_put(dr->svsk);
1721 kfree(dr);
1722 return;
1723 }
1724 dprintk("revisit queued\n");
1725 svsk = dr->svsk;
1726 dr->svsk = NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001727 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 list_add(&dr->handle.recent, &svsk->sk_deferred);
Greg Banks1a68d952006-10-02 02:17:55 -07001729 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 set_bit(SK_DEFERRED, &svsk->sk_flags);
1731 svc_sock_enqueue(svsk);
1732 svc_sock_put(svsk);
1733}
1734
1735static struct cache_deferred_req *
1736svc_defer(struct cache_req *req)
1737{
1738 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1739 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1740 struct svc_deferred_req *dr;
1741
1742 if (rqstp->rq_arg.page_len)
1743 return NULL; /* if more than a page, give up FIXME */
1744 if (rqstp->rq_deferred) {
1745 dr = rqstp->rq_deferred;
1746 rqstp->rq_deferred = NULL;
1747 } else {
1748 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1749 /* FIXME maybe discard if size too large */
1750 dr = kmalloc(size, GFP_KERNEL);
1751 if (dr == NULL)
1752 return NULL;
1753
1754 dr->handle.owner = rqstp->rq_server;
1755 dr->prot = rqstp->rq_prot;
1756 dr->addr = rqstp->rq_addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001757 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 dr->argslen = rqstp->rq_arg.len >> 2;
1759 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1760 }
Greg Banksc45c3572006-10-02 02:17:54 -07001761 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 dr->handle.revisit = svc_revisit;
1765 return &dr->handle;
1766}
1767
1768/*
1769 * recv data from a deferred request into an active one
1770 */
1771static int svc_deferred_recv(struct svc_rqst *rqstp)
1772{
1773 struct svc_deferred_req *dr = rqstp->rq_deferred;
1774
1775 rqstp->rq_arg.head[0].iov_base = dr->args;
1776 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1777 rqstp->rq_arg.page_len = 0;
1778 rqstp->rq_arg.len = dr->argslen<<2;
1779 rqstp->rq_prot = dr->prot;
1780 rqstp->rq_addr = dr->addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001781 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07001782 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return dr->argslen<<2;
1784}
1785
1786
1787static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1788{
1789 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1792 return NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001793 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1795 if (!list_empty(&svsk->sk_deferred)) {
1796 dr = list_entry(svsk->sk_deferred.next,
1797 struct svc_deferred_req,
1798 handle.recent);
1799 list_del_init(&dr->handle.recent);
1800 set_bit(SK_DEFERRED, &svsk->sk_flags);
1801 }
Greg Banks1a68d952006-10-02 02:17:55 -07001802 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return dr;
1804}