Merge with Linux 2.3.40.
[linux-2.6/linux-mips.git] / net / sunrpc / svcsock.c
blob5e86578fd160b65948719cc5a287bf2606367344
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 #if LINUX_VERSION_CODE >= 0x020100
37 #include <asm/uaccess.h>
38 #endif
40 #include <linux/sunrpc/types.h>
41 #include <linux/sunrpc/xdr.h>
42 #include <linux/sunrpc/svcsock.h>
43 #include <linux/sunrpc/stats.h>
46 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
49 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
50 int *errp, int pmap_reg);
51 static void svc_udp_data_ready(struct sock *, int);
52 static int svc_udp_recvfrom(struct svc_rqst *);
53 static int svc_udp_sendto(struct svc_rqst *);
57 * Queue up an idle server thread.
59 static inline void
60 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
62 rpc_append_list(&serv->sv_threads, rqstp);
66 * Dequeue an nfsd thread.
68 static inline void
69 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
71 rpc_remove_list(&serv->sv_threads, rqstp);
75 * Release an skbuff after use
77 static inline void
78 svc_release_skb(struct svc_rqst *rqstp)
80 struct sk_buff *skb = rqstp->rq_skbuff;
82 if (!skb)
83 return;
84 rqstp->rq_skbuff = NULL;
86 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
87 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
91 * Queue up a socket with data pending. If there are idle nfsd
92 * processes, wake 'em up.
93 * When calling this function, you should make sure it can't be interrupted
94 * by the network bottom half.
96 static void
97 svc_sock_enqueue(struct svc_sock *svsk)
99 struct svc_serv *serv = svsk->sk_server;
100 struct svc_rqst *rqstp;
102 if (serv->sv_threads && serv->sv_sockets)
103 printk(KERN_ERR
104 "svc_sock_enqueue: threads and sockets both waiting??\n");
106 if (svsk->sk_busy) {
107 /* Don't enqueue socket while daemon is receiving */
108 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
109 return;
112 /* Mark socket as busy. It will remain in this state until the
113 * server has processed all pending data and put the socket back
114 * on the idle list.
116 svsk->sk_busy = 1;
118 if ((rqstp = serv->sv_threads) != NULL) {
119 dprintk("svc: socket %p served by daemon %p\n",
120 svsk->sk_sk, rqstp);
121 svc_serv_dequeue(serv, rqstp);
122 if (rqstp->rq_sock)
123 printk(KERN_ERR
124 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
125 rqstp, rqstp->rq_sock);
126 rqstp->rq_sock = svsk;
127 svsk->sk_inuse++;
128 wake_up(&rqstp->rq_wait);
129 } else {
130 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
131 rpc_append_list(&serv->sv_sockets, svsk);
132 svsk->sk_qued = 1;
137 * Dequeue the first socket.
139 static inline struct svc_sock *
140 svc_sock_dequeue(struct svc_serv *serv)
142 struct svc_sock *svsk;
144 start_bh_atomic();
145 if ((svsk = serv->sv_sockets) != NULL)
146 rpc_remove_list(&serv->sv_sockets, svsk);
147 end_bh_atomic();
149 if (svsk) {
150 dprintk("svc: socket %p dequeued, inuse=%d\n",
151 svsk->sk_sk, svsk->sk_inuse);
152 svsk->sk_qued = 0;
155 return svsk;
159 * Having read count bytes from a socket, check whether it
160 * needs to be re-enqueued.
162 static inline void
163 svc_sock_received(struct svc_sock *svsk, int count)
165 start_bh_atomic();
166 if ((svsk->sk_data -= count) < 0) {
167 printk(KERN_NOTICE "svc: sk_data negative!\n");
168 svsk->sk_data = 0;
170 svsk->sk_rqstp = NULL; /* XXX */
171 svsk->sk_busy = 0;
172 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
173 dprintk("svc: socket %p re-enqueued after receive\n",
174 svsk->sk_sk);
175 svc_sock_enqueue(svsk);
177 end_bh_atomic();
181 * Dequeue a new connection.
183 static inline void
184 svc_sock_accepted(struct svc_sock *svsk)
186 start_bh_atomic();
187 svsk->sk_busy = 0;
188 svsk->sk_conn--;
189 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
190 dprintk("svc: socket %p re-enqueued after accept\n",
191 svsk->sk_sk);
192 svc_sock_enqueue(svsk);
194 end_bh_atomic();
198 * Release a socket after use.
200 static inline void
201 svc_sock_release(struct svc_rqst *rqstp)
203 struct svc_sock *svsk = rqstp->rq_sock;
205 if (!svsk)
206 return;
207 svc_release_skb(rqstp);
208 rqstp->rq_sock = NULL;
209 if (!--(svsk->sk_inuse) && svsk->sk_dead) {
210 dprintk("svc: releasing dead socket\n");
211 sock_release(svsk->sk_sock);
212 kfree(svsk);
217 * External function to wake up a server waiting for data
219 void
220 svc_wake_up(struct svc_serv *serv)
222 struct svc_rqst *rqstp;
224 if ((rqstp = serv->sv_threads) != NULL) {
225 dprintk("svc: daemon %p woken up.\n", rqstp);
227 svc_serv_dequeue(serv, rqstp);
228 rqstp->rq_sock = NULL;
230 wake_up(&rqstp->rq_wait);
235 * Generic sendto routine
237 static int
238 svc_sendto(struct svc_rqst *rqstp, struct iovec *iov, int nr)
240 mm_segment_t oldfs;
241 struct socket *sock = rqstp->rq_sock->sk_sock;
242 struct msghdr msg;
243 int i, buflen, len;
245 for (i = buflen = 0; i < nr; i++)
246 buflen += iov[i].iov_len;
248 msg.msg_name = &rqstp->rq_addr;
249 msg.msg_namelen = sizeof(rqstp->rq_addr);
250 msg.msg_iov = iov;
251 msg.msg_iovlen = nr;
252 msg.msg_control = NULL;
253 msg.msg_controllen = 0;
255 #if LINUX_VERSION_CODE >= 0x020100
256 msg.msg_flags = MSG_DONTWAIT;
258 oldfs = get_fs(); set_fs(KERNEL_DS);
259 len = sock_sendmsg(sock, &msg, buflen);
260 set_fs(oldfs);
261 #else
262 msg.msg_flags = 0;
264 oldfs = get_fs(); set_fs(KERNEL_DS);
265 len = sock->ops->sendmsg(sock, &msg, buflen, 1, 0);
266 set_fs(oldfs);
267 #endif
269 dprintk("svc: socket %p sendto([%p %lu... ], %d, %d) = %d\n",
270 rqstp->rq_sock, iov[0].iov_base,
271 (unsigned long) iov[0].iov_len, nr,
272 buflen, len);
274 return len;
278 * Check input queue length
280 static int
281 svc_recv_available(struct svc_sock *svsk)
283 mm_segment_t oldfs;
284 struct socket *sock = svsk->sk_sock;
285 int avail, err;
287 oldfs = get_fs(); set_fs(KERNEL_DS);
288 err = sock->ops->ioctl(sock, TIOCINQ, (unsigned long) &avail);
289 set_fs(oldfs);
291 return (err >= 0)? avail : err;
295 * Generic recvfrom routine.
297 static int
298 svc_recvfrom(struct svc_rqst *rqstp, struct iovec *iov, int nr, int buflen)
300 mm_segment_t oldfs;
301 struct msghdr msg;
302 struct socket *sock;
303 int len;
305 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
306 sock = rqstp->rq_sock->sk_sock;
308 msg.msg_name = &rqstp->rq_addr;
309 msg.msg_namelen = sizeof(rqstp->rq_addr);
310 msg.msg_iov = iov;
311 msg.msg_iovlen = nr;
312 msg.msg_control = NULL;
313 msg.msg_controllen = 0;
315 #if LINUX_VERSION_CODE >= 0x020100
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);
321 #else
322 msg.msg_flags = 0;
324 oldfs = get_fs(); set_fs(KERNEL_DS);
325 len = sock->ops->recvmsg(sock, &msg, buflen, 0, 1, &rqstp->rq_addrlen);
326 set_fs(oldfs);
327 #endif
329 dprintk("svc: socket %p recvfrom(%p, %lu) = %d\n", rqstp->rq_sock,
330 iov[0].iov_base, (unsigned long) 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 svsk->sk_data = 1;
348 svc_sock_enqueue(svsk);
352 * Receive a datagram from a UDP socket.
354 static int
355 svc_udp_recvfrom(struct svc_rqst *rqstp)
357 struct svc_sock *svsk = rqstp->rq_sock;
358 struct svc_serv *serv = svsk->sk_server;
359 struct sk_buff *skb;
360 u32 *data;
361 int err, len;
363 svsk->sk_data = 0;
364 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
365 svc_sock_received(svsk, 0);
366 if (err == -EAGAIN)
367 return err;
368 /* possibly an icmp error */
369 dprintk("svc: recvfrom returned error %d\n", -err);
372 /* There may be more data */
373 svsk->sk_data = 1;
375 len = skb->len - sizeof(struct udphdr);
376 data = (u32 *) (skb->h.raw + sizeof(struct udphdr));
378 rqstp->rq_skbuff = skb;
379 rqstp->rq_argbuf.base = data;
380 rqstp->rq_argbuf.buf = data;
381 rqstp->rq_argbuf.len = (len >> 2);
382 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
383 rqstp->rq_prot = IPPROTO_UDP;
385 /* Get sender address */
386 rqstp->rq_addr.sin_family = AF_INET;
387 rqstp->rq_addr.sin_port = skb->h.uh->source;
388 #if LINUX_VERSION_CODE >= 0x020100
389 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
390 #else
391 rqstp->rq_addr.sin_addr.s_addr = skb->saddr;
392 #endif
394 if (serv->sv_stats)
395 serv->sv_stats->netudpcnt++;
397 /* One down, maybe more to go... */
398 svsk->sk_sk->stamp = skb->stamp;
399 svc_sock_received(svsk, 0);
401 return len;
404 static int
405 svc_udp_sendto(struct svc_rqst *rqstp)
407 struct svc_buf *bufp = &rqstp->rq_resbuf;
408 int error;
410 /* Set up the first element of the reply iovec.
411 * Any other iovecs that may be in use have been taken
412 * care of by the server implementation itself.
414 /* bufp->base = bufp->area; */
415 bufp->iov[0].iov_base = bufp->base;
416 bufp->iov[0].iov_len = bufp->len << 2;
418 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
419 if (error == -ECONNREFUSED)
420 /* ICMP error on earlier request. */
421 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
422 else if (error == -EAGAIN)
423 /* Ignore and wait for re-xmit */
424 error = 0;
426 return error;
429 static int
430 svc_udp_init(struct svc_sock *svsk)
432 svsk->sk_sk->data_ready = svc_udp_data_ready;
433 svsk->sk_recvfrom = svc_udp_recvfrom;
434 svsk->sk_sendto = svc_udp_sendto;
436 return 0;
440 * A state change on a listening socket means there's a connection
441 * pending.
443 static void
444 svc_tcp_state_change1(struct sock *sk)
446 struct svc_sock *svsk;
448 dprintk("svc: socket %p TCP (listen) state change %d\n",
449 sk, sk->state);
451 if (sk->state != TCP_ESTABLISHED) {
452 /* Aborted connection, SYN_RECV or whatever... */
453 return;
455 if (!(svsk = (struct svc_sock *) sk->user_data)) {
456 printk("svc: socket %p: no user data\n", sk);
457 return;
459 svsk->sk_conn++;
460 svc_sock_enqueue(svsk);
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 svsk->sk_close = 1;
479 svc_sock_enqueue(svsk);
482 static void
483 svc_tcp_data_ready(struct sock *sk, int count)
485 struct svc_sock * svsk;
487 /* Disconnect signalled through data_ready?!? */
488 if (sk->state != TCP_ESTABLISHED) {
489 svc_tcp_state_change2(sk);
490 return;
493 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
494 sk, sk->user_data);
495 if (!(svsk = (struct svc_sock *)(sk->user_data)))
496 return;
497 svsk->sk_data++;
498 svc_sock_enqueue(svsk);
502 * Accept a TCP connection
504 static void
505 svc_tcp_accept(struct svc_sock *svsk)
507 struct sockaddr_in sin;
508 struct svc_serv *serv = svsk->sk_server;
509 struct socket *sock = svsk->sk_sock;
510 struct socket *newsock;
511 struct proto_ops *ops;
512 struct svc_sock *newsvsk;
513 int err, slen;
515 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
516 if (!sock)
517 return;
519 if (!(newsock = sock_alloc())) {
520 printk(KERN_WARNING "%s: no more sockets!\n", serv->sv_name);
521 return;
523 dprintk("svc: tcp_accept %p allocated\n", newsock);
525 newsock->type = sock->type;
526 newsock->ops = ops = sock->ops;
528 if ((err = ops->accept(sock, newsock, O_NONBLOCK)) < 0) {
529 if (net_ratelimit())
530 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
531 serv->sv_name, -err);
532 goto failed; /* aborted connection or whatever */
535 slen = sizeof(sin);
536 err = ops->getname(newsock, (struct sockaddr *) &sin, &slen, 1);
537 if (err < 0) {
538 if (net_ratelimit())
539 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
540 serv->sv_name, -err);
541 goto failed; /* aborted connection or whatever */
544 /* Ideally, we would want to reject connections from unauthorized
545 * hosts here, but we have no generic client tables. For now,
546 * we just punt connects from unprivileged ports. */
547 if (ntohs(sin.sin_port) >= 1024) {
548 if (net_ratelimit())
549 printk(KERN_WARNING
550 "%s: connect from unprivileged port: %s:%d",
551 serv->sv_name,
552 in_ntoa(sin.sin_addr.s_addr), ntohs(sin.sin_port));
553 goto failed;
556 dprintk("%s: connect from %s:%04x\n", serv->sv_name,
557 in_ntoa(sin.sin_addr.s_addr), ntohs(sin.sin_port));
559 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
560 goto failed;
562 /* Precharge. Data may have arrived on the socket before we
563 * installed the data_ready callback.
565 newsvsk->sk_data = 1;
566 newsvsk->sk_temp = 1;
567 svc_sock_enqueue(newsvsk);
569 if (serv->sv_stats)
570 serv->sv_stats->nettcpconn++;
572 return;
574 failed:
575 sock_release(newsock);
576 return;
580 * Receive data from a TCP socket.
582 static int
583 svc_tcp_recvfrom(struct svc_rqst *rqstp)
585 struct svc_sock *svsk = rqstp->rq_sock;
586 struct svc_serv *serv = svsk->sk_server;
587 struct svc_buf *bufp = &rqstp->rq_argbuf;
588 int len, ready;
590 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
591 svsk, svsk->sk_data, svsk->sk_conn, svsk->sk_close);
593 if (svsk->sk_close) {
594 svc_delete_socket(svsk);
595 return 0;
598 if (svsk->sk_conn) {
599 svc_tcp_accept(svsk);
600 svc_sock_accepted(svsk);
601 return 0;
604 ready = svsk->sk_data;
606 /* Receive data. If we haven't got the record length yet, get
607 * the next four bytes. Otherwise try to gobble up as much as
608 * possible up to the complete record length.
610 if (svsk->sk_tcplen < 4) {
611 unsigned long want = 4 - svsk->sk_tcplen;
612 struct iovec iov;
614 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
615 iov.iov_len = want;
616 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
617 goto error;
618 svsk->sk_tcplen += len;
620 svsk->sk_reclen = ntohl(svsk->sk_reclen);
621 if (!(svsk->sk_reclen & 0x80000000)) {
622 /* FIXME: shutdown socket */
623 printk(KERN_NOTICE "RPC: bad TCP reclen %08lx",
624 (unsigned long) svsk->sk_reclen);
625 return -EIO;
627 svsk->sk_reclen &= 0x7fffffff;
628 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
631 /* Check whether enough data is available */
632 len = svc_recv_available(svsk);
633 if (len < 0)
634 goto error;
636 if (len < svsk->sk_reclen) {
637 dprintk("svc: incomplete TCP record (%d of %d)\n",
638 len, svsk->sk_reclen);
639 svc_sock_received(svsk, ready);
640 len = -EAGAIN; /* record not complete */
643 /* Frob argbuf */
644 bufp->iov[0].iov_base += 4;
645 bufp->iov[0].iov_len -= 4;
647 /* Now receive data */
648 len = svc_recvfrom(rqstp, bufp->iov, bufp->nriov, svsk->sk_reclen);
649 if (len < 0)
650 goto error;
652 dprintk("svc: TCP complete record (%d bytes)\n", len);
654 /* Position reply write pointer immediately after
655 * record length */
656 rqstp->rq_resbuf.buf += 1;
657 rqstp->rq_resbuf.len = 1;
659 rqstp->rq_skbuff = 0;
660 rqstp->rq_argbuf.buf += 1;
661 rqstp->rq_argbuf.len = (len >> 2);
662 rqstp->rq_prot = IPPROTO_TCP;
664 /* Reset TCP read info */
665 svsk->sk_reclen = 0;
666 svsk->sk_tcplen = 0;
668 svc_sock_received(svsk, 1);
669 if (serv->sv_stats)
670 serv->sv_stats->nettcpcnt++;
672 return len;
674 error:
675 if (len == -EAGAIN) {
676 dprintk("RPC: TCP recvfrom got EAGAIN\n");
677 svc_sock_received(svsk, ready); /* Clear data ready */
678 } else {
679 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
680 svsk->sk_server->sv_name, -len);
681 svc_sock_received(svsk, 0);
684 return len;
688 * Send out data on TCP socket.
689 * FIXME: Make the sendto call non-blocking in order not to hang
690 * a daemon on a dead client. Requires write queue maintenance.
692 static int
693 svc_tcp_sendto(struct svc_rqst *rqstp)
695 struct svc_buf *bufp = &rqstp->rq_resbuf;
697 /* Set up the first element of the reply iovec.
698 * Any other iovecs that may be in use have been taken
699 * care of by the server implementation itself.
701 bufp->iov[0].iov_base = bufp->base;
702 bufp->iov[0].iov_len = bufp->len << 2;
703 bufp->base[0] = htonl(0x80000000|((bufp->len << 2) - 4));
705 return svc_sendto(rqstp, bufp->iov, bufp->nriov);
708 static int
709 svc_tcp_init(struct svc_sock *svsk)
711 struct sock *sk = svsk->sk_sk;
713 svsk->sk_recvfrom = svc_tcp_recvfrom;
714 svsk->sk_sendto = svc_tcp_sendto;
716 if (sk->state == TCP_LISTEN) {
717 dprintk("setting up TCP socket for listening\n");
718 sk->state_change = svc_tcp_state_change1;
719 } else {
720 dprintk("setting up TCP socket for reading\n");
721 sk->state_change = svc_tcp_state_change2;
722 sk->data_ready = svc_tcp_data_ready;
724 svsk->sk_reclen = 0;
725 svsk->sk_tcplen = 0;
728 return 0;
732 * Receive the next request on any socket.
735 svc_recv(struct svc_serv *serv, struct svc_rqst *rqstp, long timeout)
737 struct svc_sock *svsk;
738 int len;
739 DECLARE_WAITQUEUE(wait, current);
741 dprintk("svc: server %p waiting for data (to = %ld)\n",
742 rqstp, timeout);
744 if (rqstp->rq_sock)
745 printk(KERN_ERR
746 "svc_recv: service %p, socket not NULL!\n",
747 rqstp);
748 if (waitqueue_active(&rqstp->rq_wait))
749 printk(KERN_ERR
750 "svc_recv: service %p, wait queue active!\n",
751 rqstp);
753 again:
754 /* Initialize the buffers */
755 rqstp->rq_argbuf = rqstp->rq_defbuf;
756 rqstp->rq_resbuf = rqstp->rq_defbuf;
758 if (signalled())
759 return -EINTR;
761 start_bh_atomic();
762 if ((svsk = svc_sock_dequeue(serv)) != NULL) {
763 rqstp->rq_sock = svsk;
764 svsk->sk_inuse++;
765 } else {
766 /* No data pending. Go to sleep */
767 svc_serv_enqueue(serv, rqstp);
770 * We have to be able to interrupt this wait
771 * to bring down the daemons ...
773 current->state = TASK_INTERRUPTIBLE;
774 add_wait_queue(&rqstp->rq_wait, &wait);
775 end_bh_atomic();
776 schedule_timeout(timeout);
778 remove_wait_queue(&rqstp->rq_wait, &wait);
780 start_bh_atomic();
781 if (!(svsk = rqstp->rq_sock)) {
782 svc_serv_dequeue(serv, rqstp);
783 end_bh_atomic();
784 dprintk("svc: server %p, no data yet\n", rqstp);
785 return signalled()? -EINTR : -EAGAIN;
788 end_bh_atomic();
790 dprintk("svc: server %p, socket %p, inuse=%d\n",
791 rqstp, svsk, svsk->sk_inuse);
792 len = svsk->sk_recvfrom(rqstp);
793 dprintk("svc: got len=%d\n", len);
795 /* No data, incomplete (TCP) read, or accept() */
796 if (len == 0 || len == -EAGAIN) {
797 svc_sock_release(rqstp);
798 goto again;
801 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
802 rqstp->rq_userset = 0;
803 rqstp->rq_verfed = 0;
805 svc_getlong(&rqstp->rq_argbuf, rqstp->rq_xid);
806 svc_putlong(&rqstp->rq_resbuf, rqstp->rq_xid);
808 /* Assume that the reply consists of a single buffer. */
809 rqstp->rq_resbuf.nriov = 1;
811 if (serv->sv_stats)
812 serv->sv_stats->netcnt++;
813 return len;
817 * Drop request
819 void
820 svc_drop(struct svc_rqst *rqstp)
822 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
823 svc_sock_release(rqstp);
827 * Return reply to client.
830 svc_send(struct svc_rqst *rqstp)
832 struct svc_sock *svsk;
833 int len;
835 if ((svsk = rqstp->rq_sock) == NULL) {
836 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
837 __FILE__, __LINE__);
838 return -EFAULT;
841 /* release the receive skb before sending the reply */
842 svc_release_skb(rqstp);
844 len = svsk->sk_sendto(rqstp);
845 svc_sock_release(rqstp);
847 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
848 return 0;
849 return len;
853 * Initialize socket for RPC use and create svc_sock struct
854 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
856 static struct svc_sock *
857 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
858 int *errp, int pmap_register)
860 struct svc_sock *svsk;
861 struct sock *inet;
863 dprintk("svc: svc_setup_socket %p\n", sock);
864 if (!(svsk = kmalloc(sizeof(*svsk), GFP_KERNEL))) {
865 *errp = -ENOMEM;
866 return NULL;
868 memset(svsk, 0, sizeof(*svsk));
870 #if LINUX_VERSION_CODE >= 0x020100
871 inet = sock->sk;
872 #else
873 inet = (struct sock *) sock->data;
874 #endif
875 inet->user_data = svsk;
876 svsk->sk_sock = sock;
877 svsk->sk_sk = inet;
878 svsk->sk_ostate = inet->state_change;
879 svsk->sk_odata = inet->data_ready;
880 svsk->sk_server = serv;
882 /* Initialize the socket */
883 if (sock->type == SOCK_DGRAM)
884 *errp = svc_udp_init(svsk);
885 else
886 *errp = svc_tcp_init(svsk);
887 if (svsk->sk_sk == NULL)
888 printk(KERN_WARNING "svsk->sk_sk == NULL after svc_prot_init!\n");
890 /* Register socket with portmapper */
891 if (*errp >= 0 && pmap_register)
892 *errp = svc_register(serv, inet->protocol, ntohs(inet->sport));
894 if (*errp < 0) {
895 inet->user_data = NULL;
896 kfree(svsk);
897 return NULL;
900 svsk->sk_list = serv->sv_allsocks;
901 serv->sv_allsocks = svsk;
903 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
904 svsk, svsk->sk_sk);
905 return svsk;
909 * Create socket for RPC service.
911 static int
912 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
914 struct svc_sock *svsk;
915 struct socket *sock;
916 int error;
917 int type;
919 dprintk("svc: svc_create_socket(%s, %d, %08x:%d)\n",
920 serv->sv_program->pg_name, protocol,
921 ntohl(sin->sin_addr.s_addr),
922 ntohs(sin->sin_port));
924 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
925 printk(KERN_WARNING "svc: only UDP and TCP "
926 "sockets supported\n");
927 return -EINVAL;
929 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
931 if ((error = sock_create(PF_INET, type, protocol, &sock)) < 0)
932 return error;
934 if (sin != NULL) {
935 error = sock->ops->bind(sock, (struct sockaddr *) sin,
936 sizeof(*sin));
937 if (error < 0)
938 goto bummer;
941 if (protocol == IPPROTO_TCP) {
942 if ((error = sock->ops->listen(sock, 5)) < 0)
943 goto bummer;
944 sock->flags |= SO_ACCEPTCON;
947 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
948 return 0;
950 bummer:
951 dprintk("svc: svc_create_socket error = %d\n", -error);
952 sock_release(sock);
953 return error;
957 * Remove a dead socket
959 void
960 svc_delete_socket(struct svc_sock *svsk)
962 struct svc_sock **rsk;
963 struct svc_serv *serv;
964 struct sock *sk;
966 dprintk("svc: svc_delete_socket(%p)\n", svsk);
968 serv = svsk->sk_server;
969 sk = svsk->sk_sk;
971 sk->state_change = svsk->sk_ostate;
972 sk->data_ready = svsk->sk_odata;
974 for (rsk = &serv->sv_allsocks; *rsk; rsk = &(*rsk)->sk_list) {
975 if (*rsk == svsk)
976 break;
978 if (!*rsk)
979 return;
980 *rsk = svsk->sk_list;
982 if (svsk->sk_qued)
983 rpc_remove_list(&serv->sv_sockets, svsk);
984 svsk->sk_dead = 1;
986 if (!svsk->sk_inuse) {
987 sock_release(svsk->sk_sock);
988 kfree(svsk);
989 } else {
990 printk(KERN_NOTICE "svc: server socket destroy delayed\n");
991 /* svsk->sk_server = NULL; */
996 * Make a socket for nfsd and lockd
999 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1001 struct sockaddr_in sin;
1003 dprintk("svc: creating socket proto = %d\n", protocol);
1004 sin.sin_family = AF_INET;
1005 sin.sin_addr.s_addr = INADDR_ANY;
1006 sin.sin_port = htons(port);
1007 return svc_create_socket(serv, protocol, &sin);