Import 2.4.0-test2pre7
[davej-history.git] / net / sunrpc / svcsock.c
blobe0a13d7259262588d83b73563ed8044e084a4b1f
1 /*
2 * linux/net/sunrpc/svcsock.c
4 * These are the RPC server socket internals.
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...
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.
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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>
29 #include <linux/version.h>
30 #include <linux/unistd.h>
31 #include <linux/malloc.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <net/ip.h>
36 #include <asm/uaccess.h>
38 #include <linux/sunrpc/types.h>
39 #include <linux/sunrpc/xdr.h>
40 #include <linux/sunrpc/svcsock.h>
41 #include <linux/sunrpc/stats.h>
43 /* SMP locking strategy:
45 * svc_sock->sk_lock and svc_serv->sv_lock protect their
46 * respective structures.
48 * Antideadlock ordering is sk_lock --> sv_lock.
51 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
54 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
55 int *errp, int pmap_reg);
56 static void svc_udp_data_ready(struct sock *, int);
57 static int svc_udp_recvfrom(struct svc_rqst *);
58 static int svc_udp_sendto(struct svc_rqst *);
62 * Queue up an idle server thread. Must have serv->sv_lock held.
64 static inline void
65 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
67 rpc_append_list(&serv->sv_threads, rqstp);
71 * Dequeue an nfsd thread. Must have serv->sv_lock held.
73 static inline void
74 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
76 rpc_remove_list(&serv->sv_threads, rqstp);
80 * Release an skbuff after use
82 static inline void
83 svc_release_skb(struct svc_rqst *rqstp)
85 struct sk_buff *skb = rqstp->rq_skbuff;
87 if (!skb)
88 return;
89 rqstp->rq_skbuff = NULL;
91 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
92 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
96 * Queue up a socket with data pending. If there are idle nfsd
97 * processes, wake 'em up.
99 * This must be called with svsk->sk_lock held.
101 static void
102 svc_sock_enqueue(struct svc_sock *svsk)
104 struct svc_serv *serv = svsk->sk_server;
105 struct svc_rqst *rqstp;
107 /* NOTE: Local BH is already disabled by our caller. */
108 spin_lock(&serv->sv_lock);
110 if (serv->sv_threads && serv->sv_sockets)
111 printk(KERN_ERR
112 "svc_sock_enqueue: threads and sockets both waiting??\n");
114 if (svsk->sk_busy) {
115 /* Don't enqueue socket while daemon is receiving */
116 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
117 goto out_unlock;
120 /* Mark socket as busy. It will remain in this state until the
121 * server has processed all pending data and put the socket back
122 * on the idle list.
124 svsk->sk_busy = 1;
126 if ((rqstp = serv->sv_threads) != NULL) {
127 dprintk("svc: socket %p served by daemon %p\n",
128 svsk->sk_sk, rqstp);
129 svc_serv_dequeue(serv, rqstp);
130 if (rqstp->rq_sock)
131 printk(KERN_ERR
132 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
133 rqstp, rqstp->rq_sock);
134 rqstp->rq_sock = svsk;
135 svsk->sk_inuse++;
136 wake_up(&rqstp->rq_wait);
137 } else {
138 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
139 rpc_append_list(&serv->sv_sockets, svsk);
140 svsk->sk_qued = 1;
143 out_unlock:
144 spin_unlock(&serv->sv_lock);
148 * Dequeue the first socket. Must be called with the serv->sv_lock held.
150 static inline struct svc_sock *
151 svc_sock_dequeue(struct svc_serv *serv)
153 struct svc_sock *svsk;
155 if ((svsk = serv->sv_sockets) != NULL)
156 rpc_remove_list(&serv->sv_sockets, svsk);
158 if (svsk) {
159 dprintk("svc: socket %p dequeued, inuse=%d\n",
160 svsk->sk_sk, svsk->sk_inuse);
161 svsk->sk_qued = 0;
164 return svsk;
168 * Having read count bytes from a socket, check whether it
169 * needs to be re-enqueued.
171 static inline void
172 svc_sock_received(struct svc_sock *svsk, int count)
174 spin_lock_bh(&svsk->sk_lock);
175 if ((svsk->sk_data -= count) < 0) {
176 printk(KERN_NOTICE "svc: sk_data negative!\n");
177 svsk->sk_data = 0;
179 svsk->sk_rqstp = NULL; /* XXX */
180 svsk->sk_busy = 0;
181 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
182 dprintk("svc: socket %p re-enqueued after receive\n",
183 svsk->sk_sk);
184 svc_sock_enqueue(svsk);
186 spin_unlock_bh(&svsk->sk_lock);
190 * Dequeue a new connection.
192 static inline void
193 svc_sock_accepted(struct svc_sock *svsk)
195 spin_lock_bh(&svsk->sk_lock);
196 svsk->sk_busy = 0;
197 svsk->sk_conn--;
198 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
199 dprintk("svc: socket %p re-enqueued after accept\n",
200 svsk->sk_sk);
201 svc_sock_enqueue(svsk);
203 spin_unlock_bh(&svsk->sk_lock);
207 * Release a socket after use.
209 static inline void
210 svc_sock_release(struct svc_rqst *rqstp)
212 struct svc_sock *svsk = rqstp->rq_sock;
214 if (!svsk)
215 return;
216 svc_release_skb(rqstp);
217 rqstp->rq_sock = NULL;
218 if (!--(svsk->sk_inuse) && svsk->sk_dead) {
219 dprintk("svc: releasing dead socket\n");
220 sock_release(svsk->sk_sock);
221 kfree(svsk);
226 * External function to wake up a server waiting for data
228 void
229 svc_wake_up(struct svc_serv *serv)
231 struct svc_rqst *rqstp;
233 spin_lock_bh(&serv->sv_lock);
234 if ((rqstp = serv->sv_threads) != NULL) {
235 dprintk("svc: daemon %p woken up.\n", rqstp);
237 svc_serv_dequeue(serv, rqstp);
238 rqstp->rq_sock = NULL;
240 wake_up(&rqstp->rq_wait);
242 spin_unlock_bh(&serv->sv_lock);
246 * Generic sendto routine
248 static int
249 svc_sendto(struct svc_rqst *rqstp, struct iovec *iov, int nr)
251 mm_segment_t oldfs;
252 struct socket *sock = rqstp->rq_sock->sk_sock;
253 struct msghdr msg;
254 int i, buflen, len;
256 for (i = buflen = 0; i < nr; i++)
257 buflen += iov[i].iov_len;
259 msg.msg_name = &rqstp->rq_addr;
260 msg.msg_namelen = sizeof(rqstp->rq_addr);
261 msg.msg_iov = iov;
262 msg.msg_iovlen = nr;
263 msg.msg_control = NULL;
264 msg.msg_controllen = 0;
266 msg.msg_flags = MSG_DONTWAIT;
268 oldfs = get_fs(); set_fs(KERNEL_DS);
269 len = sock_sendmsg(sock, &msg, buflen);
270 set_fs(oldfs);
272 dprintk("svc: socket %p sendto([%p %Zu... ], %d, %d) = %d\n",
273 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, nr, buflen, len);
275 return len;
279 * Check input queue length
281 static int
282 svc_recv_available(struct svc_sock *svsk)
284 mm_segment_t oldfs;
285 struct socket *sock = svsk->sk_sock;
286 int avail, err;
288 oldfs = get_fs(); set_fs(KERNEL_DS);
289 err = sock->ops->ioctl(sock, TIOCINQ, (unsigned long) &avail);
290 set_fs(oldfs);
292 return (err >= 0)? avail : err;
296 * Generic recvfrom routine.
298 static int
299 svc_recvfrom(struct svc_rqst *rqstp, struct iovec *iov, int nr, int buflen)
301 mm_segment_t oldfs;
302 struct msghdr msg;
303 struct socket *sock;
304 int len, alen;
306 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
307 sock = rqstp->rq_sock->sk_sock;
309 msg.msg_name = &rqstp->rq_addr;
310 msg.msg_namelen = sizeof(rqstp->rq_addr);
311 msg.msg_iov = iov;
312 msg.msg_iovlen = nr;
313 msg.msg_control = NULL;
314 msg.msg_controllen = 0;
316 msg.msg_flags = MSG_DONTWAIT;
318 oldfs = get_fs(); set_fs(KERNEL_DS);
319 len = sock_recvmsg(sock, &msg, buflen, MSG_DONTWAIT);
320 set_fs(oldfs);
322 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
323 * possibly we should cache this in the svc_sock structure
324 * at accept time. FIXME
326 alen = sizeof(rqstp->rq_addr);
327 sock->ops->getname(sock, (struct sockaddr *)&rqstp->rq_addr, &alen, 1);
329 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
330 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
332 return len;
336 * INET callback when data has been received on the socket.
338 static void
339 svc_udp_data_ready(struct sock *sk, int count)
341 struct svc_sock *svsk = (struct svc_sock *)(sk->user_data);
343 if (!svsk)
344 return;
345 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
346 svsk, sk, count, svsk->sk_busy);
347 spin_lock_bh(&svsk->sk_lock);
348 svsk->sk_data = 1;
349 svc_sock_enqueue(svsk);
350 spin_unlock_bh(&svsk->sk_lock);
354 * Receive a datagram from a UDP socket.
356 static int
357 svc_udp_recvfrom(struct svc_rqst *rqstp)
359 struct svc_sock *svsk = rqstp->rq_sock;
360 struct svc_serv *serv = svsk->sk_server;
361 struct sk_buff *skb;
362 u32 *data;
363 int err, len;
365 svsk->sk_data = 0;
366 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
367 svc_sock_received(svsk, 0);
368 if (err == -EAGAIN)
369 return err;
370 /* possibly an icmp error */
371 dprintk("svc: recvfrom returned error %d\n", -err);
374 /* There may be more data */
375 svsk->sk_data = 1;
377 len = skb->len - sizeof(struct udphdr);
378 data = (u32 *) (skb->h.raw + sizeof(struct udphdr));
380 rqstp->rq_skbuff = skb;
381 rqstp->rq_argbuf.base = data;
382 rqstp->rq_argbuf.buf = data;
383 rqstp->rq_argbuf.len = (len >> 2);
384 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
385 rqstp->rq_prot = IPPROTO_UDP;
387 /* Get sender address */
388 rqstp->rq_addr.sin_family = AF_INET;
389 rqstp->rq_addr.sin_port = skb->h.uh->source;
390 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
392 if (serv->sv_stats)
393 serv->sv_stats->netudpcnt++;
395 /* One down, maybe more to go... */
396 svsk->sk_sk->stamp = skb->stamp;
397 svc_sock_received(svsk, 0);
399 return len;
402 static int
403 svc_udp_sendto(struct svc_rqst *rqstp)
405 struct svc_buf *bufp = &rqstp->rq_resbuf;
406 int error;
408 /* Set up the first element of the reply iovec.
409 * Any other iovecs that may be in use have been taken
410 * care of by the server implementation itself.
412 /* bufp->base = bufp->area; */
413 bufp->iov[0].iov_base = bufp->base;
414 bufp->iov[0].iov_len = bufp->len << 2;
416 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
417 if (error == -ECONNREFUSED)
418 /* ICMP error on earlier request. */
419 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
420 else if (error == -EAGAIN)
421 /* Ignore and wait for re-xmit */
422 error = 0;
424 return error;
427 static int
428 svc_udp_init(struct svc_sock *svsk)
430 svsk->sk_sk->data_ready = svc_udp_data_ready;
431 svsk->sk_recvfrom = svc_udp_recvfrom;
432 svsk->sk_sendto = svc_udp_sendto;
434 return 0;
438 * A state change on a listening socket means there's a connection
439 * pending.
441 static void
442 svc_tcp_state_change1(struct sock *sk)
444 struct svc_sock *svsk;
446 dprintk("svc: socket %p TCP (listen) state change %d\n",
447 sk, sk->state);
449 if (sk->state != TCP_ESTABLISHED) {
450 /* Aborted connection, SYN_RECV or whatever... */
451 return;
453 if (!(svsk = (struct svc_sock *) sk->user_data)) {
454 printk("svc: socket %p: no user data\n", sk);
455 return;
457 spin_lock_bh(&svsk->sk_lock);
458 svsk->sk_conn++;
459 svc_sock_enqueue(svsk);
460 spin_unlock_bh(&svsk->sk_lock);
464 * A state change on a connected socket means it's dying or dead.
466 static void
467 svc_tcp_state_change2(struct sock *sk)
469 struct svc_sock *svsk;
471 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
472 sk, sk->state, sk->user_data);
474 if (!(svsk = (struct svc_sock *) sk->user_data)) {
475 printk("svc: socket %p: no user data\n", sk);
476 return;
478 spin_lock_bh(&svsk->sk_lock);
479 svsk->sk_close = 1;
480 svc_sock_enqueue(svsk);
481 spin_unlock_bh(&svsk->sk_lock);
484 static void
485 svc_tcp_data_ready(struct sock *sk, int count)
487 struct svc_sock * svsk;
489 /* Disconnect signalled through data_ready?!? */
490 if (sk->state != TCP_ESTABLISHED) {
491 svc_tcp_state_change2(sk);
492 return;
495 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
496 sk, sk->user_data);
497 if (!(svsk = (struct svc_sock *)(sk->user_data)))
498 return;
499 spin_lock_bh(&svsk->sk_lock);
500 svsk->sk_data++;
501 svc_sock_enqueue(svsk);
502 spin_unlock_bh(&svsk->sk_lock);
506 * Accept a TCP connection
508 static void
509 svc_tcp_accept(struct svc_sock *svsk)
511 struct sockaddr_in sin;
512 struct svc_serv *serv = svsk->sk_server;
513 struct socket *sock = svsk->sk_sock;
514 struct socket *newsock;
515 struct proto_ops *ops;
516 struct svc_sock *newsvsk;
517 int err, slen;
519 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
520 if (!sock)
521 return;
523 if (!(newsock = sock_alloc())) {
524 printk(KERN_WARNING "%s: no more sockets!\n", serv->sv_name);
525 return;
527 dprintk("svc: tcp_accept %p allocated\n", newsock);
529 newsock->type = sock->type;
530 newsock->ops = ops = sock->ops;
532 if ((err = ops->accept(sock, newsock, O_NONBLOCK)) < 0) {
533 if (net_ratelimit())
534 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
535 serv->sv_name, -err);
536 goto failed; /* aborted connection or whatever */
539 slen = sizeof(sin);
540 err = ops->getname(newsock, (struct sockaddr *) &sin, &slen, 1);
541 if (err < 0) {
542 if (net_ratelimit())
543 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
544 serv->sv_name, -err);
545 goto failed; /* aborted connection or whatever */
548 /* Ideally, we would want to reject connections from unauthorized
549 * hosts here, but when we get encription, the IP of the host won't
550 * tell us anything. For now just warn about unpriv connections.
552 if (ntohs(sin.sin_port) >= 1024) {
553 if (net_ratelimit())
554 printk(KERN_WARNING
555 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
556 serv->sv_name,
557 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
560 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
561 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
563 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
564 goto failed;
566 /* Precharge. Data may have arrived on the socket before we
567 * installed the data_ready callback.
569 spin_lock_bh(&newsvsk->sk_lock);
570 newsvsk->sk_data = 1;
571 newsvsk->sk_temp = 1;
572 svc_sock_enqueue(newsvsk);
573 spin_unlock_bh(&newsvsk->sk_lock);
575 if (serv->sv_stats)
576 serv->sv_stats->nettcpconn++;
578 return;
580 failed:
581 sock_release(newsock);
582 return;
586 * Receive data from a TCP socket.
588 static int
589 svc_tcp_recvfrom(struct svc_rqst *rqstp)
591 struct svc_sock *svsk = rqstp->rq_sock;
592 struct svc_serv *serv = svsk->sk_server;
593 struct svc_buf *bufp = &rqstp->rq_argbuf;
594 int len, ready, used;
596 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
597 svsk, svsk->sk_data, svsk->sk_conn, svsk->sk_close);
599 if (svsk->sk_close) {
600 svc_delete_socket(svsk);
601 return 0;
604 if (svsk->sk_conn) {
605 svc_tcp_accept(svsk);
606 svc_sock_accepted(svsk);
607 return 0;
610 ready = svsk->sk_data;
612 /* Receive data. If we haven't got the record length yet, get
613 * the next four bytes. Otherwise try to gobble up as much as
614 * possible up to the complete record length.
616 if (svsk->sk_tcplen < 4) {
617 unsigned long want = 4 - svsk->sk_tcplen;
618 struct iovec iov;
620 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
621 iov.iov_len = want;
622 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
623 goto error;
624 svsk->sk_tcplen += len;
626 svsk->sk_reclen = ntohl(svsk->sk_reclen);
627 if (!(svsk->sk_reclen & 0x80000000)) {
628 /* FIXME: technically, a record can be fragmented,
629 * and non-terminal fragments will not have the top
630 * bit set in the fragment length header.
631 * But apparently no known nfs clients send fragmented
632 * records. */
633 /* FIXME: shutdown socket */
634 printk(KERN_NOTICE "RPC: bad TCP reclen %08lx",
635 (unsigned long) svsk->sk_reclen);
636 return -EIO;
638 svsk->sk_reclen &= 0x7fffffff;
639 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
642 /* Check whether enough data is available */
643 len = svc_recv_available(svsk);
644 if (len < 0)
645 goto error;
647 if (len < svsk->sk_reclen) {
648 /* FIXME: if sk_reclen > window-size, then we will
649 * never be able to receive the record, so should
650 * shutdown the connection
652 dprintk("svc: incomplete TCP record (%d of %d)\n",
653 len, svsk->sk_reclen);
654 svc_sock_received(svsk, ready);
655 return -EAGAIN; /* record not complete */
657 /* if we think there is only one more record to read, but
658 * it is bigger than we expect, then two records must have arrived
659 * together, so pretend we aren't using the record.. */
660 if (len > svsk->sk_reclen && ready == 1)
661 used = 0;
662 else used = 1;
664 /* Frob argbuf */
665 bufp->iov[0].iov_base += 4;
666 bufp->iov[0].iov_len -= 4;
668 /* Now receive data */
669 len = svc_recvfrom(rqstp, bufp->iov, bufp->nriov, svsk->sk_reclen);
670 if (len < 0)
671 goto error;
673 dprintk("svc: TCP complete record (%d bytes)\n", len);
675 /* Position reply write pointer immediately after
676 * record length */
677 rqstp->rq_resbuf.buf += 1;
678 rqstp->rq_resbuf.len = 1;
680 rqstp->rq_skbuff = 0;
681 rqstp->rq_argbuf.buf += 1;
682 rqstp->rq_argbuf.len = (len >> 2);
683 rqstp->rq_prot = IPPROTO_TCP;
685 /* Reset TCP read info */
686 svsk->sk_reclen = 0;
687 svsk->sk_tcplen = 0;
689 svc_sock_received(svsk, used);
690 if (serv->sv_stats)
691 serv->sv_stats->nettcpcnt++;
693 return len;
695 error:
696 if (len == -EAGAIN) {
697 dprintk("RPC: TCP recvfrom got EAGAIN\n");
698 svc_sock_received(svsk, ready); /* Clear data ready */
699 } else {
700 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
701 svsk->sk_server->sv_name, -len);
702 svc_sock_received(svsk, 0);
705 return len;
709 * Send out data on TCP socket.
710 * FIXME: Make the sendto call non-blocking in order not to hang
711 * a daemon on a dead client. Requires write queue maintenance.
713 static int
714 svc_tcp_sendto(struct svc_rqst *rqstp)
716 struct svc_buf *bufp = &rqstp->rq_resbuf;
717 int sent;
719 /* Set up the first element of the reply iovec.
720 * Any other iovecs that may be in use have been taken
721 * care of by the server implementation itself.
723 bufp->iov[0].iov_base = bufp->base;
724 bufp->iov[0].iov_len = bufp->len << 2;
725 bufp->base[0] = htonl(0x80000000|((bufp->len << 2) - 4));
727 sent = svc_sendto(rqstp, bufp->iov, bufp->nriov);
728 if (sent != bufp->len<<2) {
729 printk(KERN_NOTICE "rpc-srv/tcp: %s: sent only %d bytes of %d - should shutdown socket\n",
730 rqstp->rq_sock->sk_server->sv_name,
731 sent, bufp->len << 2);
732 /* FIXME: should shutdown the socket, or allocate more memort
733 * or wait and try again or something. Otherwise
734 * client will get confused
737 return sent;
740 static int
741 svc_tcp_init(struct svc_sock *svsk)
743 struct sock *sk = svsk->sk_sk;
745 svsk->sk_recvfrom = svc_tcp_recvfrom;
746 svsk->sk_sendto = svc_tcp_sendto;
748 if (sk->state == TCP_LISTEN) {
749 dprintk("setting up TCP socket for listening\n");
750 sk->state_change = svc_tcp_state_change1;
751 } else {
752 dprintk("setting up TCP socket for reading\n");
753 sk->state_change = svc_tcp_state_change2;
754 sk->data_ready = svc_tcp_data_ready;
756 svsk->sk_reclen = 0;
757 svsk->sk_tcplen = 0;
760 return 0;
764 * Receive the next request on any socket.
767 svc_recv(struct svc_serv *serv, struct svc_rqst *rqstp, long timeout)
769 struct svc_sock *svsk;
770 int len;
771 DECLARE_WAITQUEUE(wait, current);
773 dprintk("svc: server %p waiting for data (to = %ld)\n",
774 rqstp, timeout);
776 if (rqstp->rq_sock)
777 printk(KERN_ERR
778 "svc_recv: service %p, socket not NULL!\n",
779 rqstp);
780 if (waitqueue_active(&rqstp->rq_wait))
781 printk(KERN_ERR
782 "svc_recv: service %p, wait queue active!\n",
783 rqstp);
785 again:
786 /* Initialize the buffers */
787 rqstp->rq_argbuf = rqstp->rq_defbuf;
788 rqstp->rq_resbuf = rqstp->rq_defbuf;
790 if (signalled())
791 return -EINTR;
793 spin_lock_bh(&serv->sv_lock);
794 if ((svsk = svc_sock_dequeue(serv)) != NULL) {
795 rqstp->rq_sock = svsk;
796 svsk->sk_inuse++;
797 } else {
798 /* No data pending. Go to sleep */
799 svc_serv_enqueue(serv, rqstp);
802 * We have to be able to interrupt this wait
803 * to bring down the daemons ...
805 set_current_state(TASK_INTERRUPTIBLE);
806 add_wait_queue(&rqstp->rq_wait, &wait);
807 spin_unlock_bh(&serv->sv_lock);
809 schedule_timeout(timeout);
811 spin_lock_bh(&serv->sv_lock);
812 remove_wait_queue(&rqstp->rq_wait, &wait);
814 if (!(svsk = rqstp->rq_sock)) {
815 svc_serv_dequeue(serv, rqstp);
816 spin_unlock_bh(&serv->sv_lock);
817 dprintk("svc: server %p, no data yet\n", rqstp);
818 return signalled()? -EINTR : -EAGAIN;
821 spin_unlock_bh(&serv->sv_lock);
823 dprintk("svc: server %p, socket %p, inuse=%d\n",
824 rqstp, svsk, svsk->sk_inuse);
825 len = svsk->sk_recvfrom(rqstp);
826 dprintk("svc: got len=%d\n", len);
828 /* No data, incomplete (TCP) read, or accept() */
829 if (len == 0 || len == -EAGAIN) {
830 svc_sock_release(rqstp);
831 goto again;
834 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
835 rqstp->rq_userset = 0;
836 rqstp->rq_verfed = 0;
838 svc_getlong(&rqstp->rq_argbuf, rqstp->rq_xid);
839 svc_putlong(&rqstp->rq_resbuf, rqstp->rq_xid);
841 /* Assume that the reply consists of a single buffer. */
842 rqstp->rq_resbuf.nriov = 1;
844 if (serv->sv_stats)
845 serv->sv_stats->netcnt++;
846 return len;
850 * Drop request
852 void
853 svc_drop(struct svc_rqst *rqstp)
855 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
856 svc_sock_release(rqstp);
860 * Return reply to client.
863 svc_send(struct svc_rqst *rqstp)
865 struct svc_sock *svsk;
866 int len;
868 if ((svsk = rqstp->rq_sock) == NULL) {
869 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
870 __FILE__, __LINE__);
871 return -EFAULT;
874 /* release the receive skb before sending the reply */
875 svc_release_skb(rqstp);
877 len = svsk->sk_sendto(rqstp);
878 svc_sock_release(rqstp);
880 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
881 return 0;
882 return len;
886 * Initialize socket for RPC use and create svc_sock struct
887 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
889 static struct svc_sock *
890 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
891 int *errp, int pmap_register)
893 struct svc_sock *svsk;
894 struct sock *inet;
896 dprintk("svc: svc_setup_socket %p\n", sock);
897 if (!(svsk = kmalloc(sizeof(*svsk), GFP_KERNEL))) {
898 *errp = -ENOMEM;
899 return NULL;
901 memset(svsk, 0, sizeof(*svsk));
903 inet = sock->sk;
904 inet->user_data = svsk;
905 svsk->sk_sock = sock;
906 svsk->sk_sk = inet;
907 svsk->sk_ostate = inet->state_change;
908 svsk->sk_odata = inet->data_ready;
909 svsk->sk_server = serv;
910 spin_lock_init(&svsk->sk_lock);
912 /* Initialize the socket */
913 if (sock->type == SOCK_DGRAM)
914 *errp = svc_udp_init(svsk);
915 else
916 *errp = svc_tcp_init(svsk);
917 if (svsk->sk_sk == NULL)
918 printk(KERN_WARNING "svsk->sk_sk == NULL after svc_prot_init!\n");
920 /* Register socket with portmapper */
921 if (*errp >= 0 && pmap_register)
922 *errp = svc_register(serv, inet->protocol, ntohs(inet->sport));
924 if (*errp < 0) {
925 inet->user_data = NULL;
926 kfree(svsk);
927 return NULL;
930 spin_lock_bh(&serv->sv_lock);
931 svsk->sk_list = serv->sv_allsocks;
932 serv->sv_allsocks = svsk;
933 spin_unlock_bh(&serv->sv_lock);
935 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
936 svsk, svsk->sk_sk);
937 return svsk;
941 * Create socket for RPC service.
943 static int
944 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
946 struct svc_sock *svsk;
947 struct socket *sock;
948 int error;
949 int type;
951 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
952 serv->sv_program->pg_name, protocol,
953 NIPQUAD(sin->sin_addr.s_addr),
954 ntohs(sin->sin_port));
956 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
957 printk(KERN_WARNING "svc: only UDP and TCP "
958 "sockets supported\n");
959 return -EINVAL;
961 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
963 if ((error = sock_create(PF_INET, type, protocol, &sock)) < 0)
964 return error;
966 if (sin != NULL) {
967 error = sock->ops->bind(sock, (struct sockaddr *) sin,
968 sizeof(*sin));
969 if (error < 0)
970 goto bummer;
973 if (protocol == IPPROTO_TCP) {
974 if ((error = sock->ops->listen(sock, 5)) < 0)
975 goto bummer;
978 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
979 return 0;
981 bummer:
982 dprintk("svc: svc_create_socket error = %d\n", -error);
983 sock_release(sock);
984 return error;
988 * Remove a dead socket
990 void
991 svc_delete_socket(struct svc_sock *svsk)
993 struct svc_sock **rsk;
994 struct svc_serv *serv;
995 struct sock *sk;
997 dprintk("svc: svc_delete_socket(%p)\n", svsk);
999 serv = svsk->sk_server;
1000 sk = svsk->sk_sk;
1002 sk->state_change = svsk->sk_ostate;
1003 sk->data_ready = svsk->sk_odata;
1005 spin_lock_bh(&serv->sv_lock);
1007 for (rsk = &serv->sv_allsocks; *rsk; rsk = &(*rsk)->sk_list) {
1008 if (*rsk == svsk)
1009 break;
1011 if (!*rsk) {
1012 spin_unlock_bh(&serv->sv_lock);
1013 return;
1015 *rsk = svsk->sk_list;
1016 if (svsk->sk_qued)
1017 rpc_remove_list(&serv->sv_sockets, svsk);
1019 spin_unlock_bh(&serv->sv_lock);
1021 svsk->sk_dead = 1;
1023 if (!svsk->sk_inuse) {
1024 sock_release(svsk->sk_sock);
1025 kfree(svsk);
1026 } else {
1027 printk(KERN_NOTICE "svc: server socket destroy delayed\n");
1028 /* svsk->sk_server = NULL; */
1033 * Make a socket for nfsd and lockd
1036 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1038 struct sockaddr_in sin;
1040 dprintk("svc: creating socket proto = %d\n", protocol);
1041 sin.sin_family = AF_INET;
1042 sin.sin_addr.s_addr = INADDR_ANY;
1043 sin.sin_port = htons(port);
1044 return svc_create_socket(serv, protocol, &sin);