2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1989, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * sendfile(2) and related extensions:
30 * Copyright (c) 1998, David Greenman. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/filedesc.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/ktrace.h>
79 #include <sys/kernel.h>
80 #include <sys/kern_audit.h>
82 #include <sys/kdebug.h>
86 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
87 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
88 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
89 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
90 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
91 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
92 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
93 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
94 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
95 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
99 struct getsockname_args
{
105 struct getsockopt_args
{
119 struct getpeername_args
{
129 static void sf_buf_init(void *arg
);
130 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
131 static struct sf_buf
*sf_buf_alloc(void);
132 static void sf_buf_ref(caddr_t addr
, u_int size
);
133 static void sf_buf_free(caddr_t addr
, u_int size
);
135 static SLIST_HEAD(, sf_buf
) sf_freelist
;
136 static vm_offset_t sf_base
;
137 static struct sf_buf
*sf_bufs
;
138 static int sf_buf_alloc_want
;
141 static int sendit
__P((struct proc
*p
, int s
, struct msghdr
*mp
, int flags
, register_t
*retval
));
142 static int recvit
__P((struct proc
*p
, int s
, struct msghdr
*mp
,
143 caddr_t namelenp
, register_t
*retval
));
145 static int accept1
__P((struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
));
146 static int getsockname1
__P((struct proc
*p
, struct getsockname_args
*uap
,
147 register_t
*retval
, int compat
));
148 static int getpeername1
__P((struct proc
*p
, struct getpeername_args
*uap
,
149 register_t
*retval
, int compat
));
152 * System call interface to the socket abstraction.
154 #if COMPAT_43 || defined(COMPAT_SUNOS)
155 #define COMPAT_OLDSOCK
158 extern struct fileops socketops
;
166 socket(p
, uap
, retval
)
168 register struct socket_args
*uap
;
171 struct filedesc
*fdp
= p
->p_fd
;
176 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
177 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
178 error
= falloc(p
, &fp
, &fd
);
179 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
183 fp
->f_flag
= FREAD
|FWRITE
;
184 fp
->f_type
= DTYPE_SOCKET
;
185 fp
->f_ops
= &socketops
;
186 if (error
= socreate(uap
->domain
, &so
, uap
->type
,
188 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
191 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
193 fp
->f_data
= (caddr_t
)so
;
194 *fdflags(p
, fd
) &= ~UF_RESERVED
;
210 register struct bind_args
*uap
;
217 AUDIT_ARG(fd
, uap
->s
);
218 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
221 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
224 AUDIT_ARG(sockaddr
, p
, sa
);
225 if (fp
->f_data
!= NULL
)
226 error
= sobind((struct socket
*)fp
->f_data
, sa
);
241 listen(p
, uap
, retval
)
243 register struct listen_args
*uap
;
249 AUDIT_ARG(fd
, uap
->s
);
250 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
253 if (fp
->f_data
!= NULL
)
254 return (solisten((struct socket
*)fp
->f_data
, uap
->backlog
));
259 #ifndef COMPAT_OLDSOCK
260 #define accept1 accept
266 accept1(p
, uap
, retval
, compat
)
268 register struct accept_args
*uap
;
276 struct socket
*head
, *so
;
278 short fflag
; /* type must match fp->f_flag */
281 AUDIT_ARG(fd
, uap
->s
);
283 error
= copyin((caddr_t
)uap
->anamelen
, (caddr_t
)&namelen
,
288 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
292 head
= (struct socket
*)fp
->f_data
;
297 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
301 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
303 return (EWOULDBLOCK
);
305 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
306 if (head
->so_state
& SS_CANTRCVMORE
) {
307 head
->so_error
= ECONNABORTED
;
310 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
317 if (head
->so_error
) {
318 error
= head
->so_error
;
326 * At this point we know that there is at least one connection
327 * ready to be accepted. Remove it from the queue prior to
328 * allocating the file descriptor for it since falloc() may
329 * block allowing another process to accept the connection
332 so
= TAILQ_FIRST(&head
->so_comp
);
333 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
337 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
338 error
= falloc(p
, &fp
, &fd
);
339 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
342 * Probably ran out of file descriptors. Put the
343 * unaccepted connection back onto the queue and
344 * do another wakeup so some other process might
345 * have a chance at it.
347 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
349 wakeup_one(&head
->so_timeo
);
353 *fdflags(p
, fd
) &= ~UF_RESERVED
;
357 so
->so_state
&= ~SS_COMP
;
359 fp
->f_type
= DTYPE_SOCKET
;
361 fp
->f_ops
= &socketops
;
362 fp
->f_data
= (caddr_t
)so
;
364 (void) soaccept(so
, &sa
);
371 AUDIT_ARG(sockaddr
, p
, sa
);
373 /* check sa_len before it is destroyed */
374 if (namelen
> sa
->sa_len
)
375 namelen
= sa
->sa_len
;
376 #ifdef COMPAT_OLDSOCK
378 ((struct osockaddr
*)sa
)->sa_family
=
381 error
= copyout(sa
, (caddr_t
)uap
->name
, (u_int
)namelen
);
384 error
= copyout((caddr_t
)&namelen
,
385 (caddr_t
)uap
->anamelen
, sizeof (*uap
->anamelen
));
393 accept(p
, uap
, retval
)
395 struct accept_args
*uap
;
399 return (accept1(p
, uap
, retval
, 0));
402 #ifdef COMPAT_OLDSOCK
404 oaccept(p
, uap
, retval
)
406 struct accept_args
*uap
;
410 return (accept1(p
, uap
, retval
, 1));
412 #endif /* COMPAT_OLDSOCK */
414 struct connect_args
{
421 connect(p
, uap
, retval
)
423 register struct connect_args
*uap
;
427 register struct socket
*so
;
431 AUDIT_ARG(fd
, uap
->s
);
432 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
435 so
= (struct socket
*)fp
->f_data
;
438 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
))
440 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
443 AUDIT_ARG(sockaddr
, p
, sa
);
444 error
= soconnect(so
, sa
);
447 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
449 return (EINPROGRESS
);
452 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
453 error
= tsleep((caddr_t
)&so
->so_timeo
, PSOCK
| PCATCH
,
459 error
= so
->so_error
;
464 so
->so_state
&= ~SS_ISCONNECTING
;
466 if (error
== ERESTART
)
471 struct socketpair_args
{
478 socketpair(p
, uap
, retval
)
480 register struct socketpair_args
*uap
;
483 register struct filedesc
*fdp
= p
->p_fd
;
484 struct file
*fp1
, *fp2
;
485 struct socket
*so1
, *so2
;
486 int fd
, error
, sv
[2];
488 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
489 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
492 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
495 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
496 error
= falloc(p
, &fp1
, &fd
);
500 fp1
->f_flag
= FREAD
|FWRITE
;
501 fp1
->f_type
= DTYPE_SOCKET
;
502 fp1
->f_ops
= &socketops
;
503 fp1
->f_data
= (caddr_t
)so1
;
504 error
= falloc(p
, &fp2
, &fd
);
507 fp2
->f_flag
= FREAD
|FWRITE
;
508 fp2
->f_type
= DTYPE_SOCKET
;
509 fp2
->f_ops
= &socketops
;
510 fp2
->f_data
= (caddr_t
)so2
;
512 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
513 error
= soconnect2(so1
, so2
);
515 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
519 if (uap
->type
== SOCK_DGRAM
) {
521 * Datagram socket connection is asymmetric.
523 error
= soconnect2(so2
, so1
);
525 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
529 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
530 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
531 error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->rsv
,
533 #if 0 /* old pipe(2) syscall compatability, unused these days */
534 retval
[0] = sv
[0]; /* XXX ??? */
535 retval
[1] = sv
[1]; /* XXX ??? */
545 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
553 sendit(p
, s
, mp
, flags
, retsize
)
554 register struct proc
*p
;
556 register struct msghdr
*mp
;
562 register struct iovec
*iov
;
564 struct mbuf
*control
;
569 struct iovec
*ktriov
= NULL
;
573 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
575 if (error
= getsock(p
->p_fd
, s
, &fp
))
577 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
581 auio
.uio_iov
= mp
->msg_iov
;
582 auio
.uio_iovcnt
= mp
->msg_iovlen
;
583 auio
.uio_segflg
= UIO_USERSPACE
;
584 auio
.uio_rw
= UIO_WRITE
;
586 auio
.uio_offset
= 0; /* XXX */
589 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
590 if (iov
->iov_len
< 0)
592 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
596 if ((auio
.uio_resid
+= iov
->iov_len
) < 0)
598 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
603 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
605 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
608 AUDIT_ARG(sockaddr
, p
, to
);
611 if (mp
->msg_control
) {
612 if (mp
->msg_controllen
< sizeof(struct cmsghdr
)
613 #ifdef COMPAT_OLDSOCK
614 && mp
->msg_flags
!= MSG_COMPAT
620 error
= sockargs(&control
, mp
->msg_control
,
621 mp
->msg_controllen
, MT_CONTROL
);
624 #ifdef COMPAT_OLDSOCK
625 if (mp
->msg_flags
== MSG_COMPAT
) {
626 register struct cmsghdr
*cm
;
628 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
633 cm
= mtod(control
, struct cmsghdr
*);
634 cm
->cmsg_len
= control
->m_len
;
635 cm
->cmsg_level
= SOL_SOCKET
;
636 cm
->cmsg_type
= SCM_RIGHTS
;
644 if (KTRPOINT(p
, KTR_GENIO
)) {
645 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
647 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
648 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
652 len
= auio
.uio_resid
;
653 so
= (struct socket
*)fp
->f_data
;
657 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, &auio
, 0, control
,
660 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
661 error
== EINTR
|| error
== EWOULDBLOCK
))
663 /* Generation of SIGPIPE can be controlled per socket */
664 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
668 *retsize
= len
- auio
.uio_resid
;
670 if (ktriov
!= NULL
) {
672 ktruio
.uio_iov
= ktriov
;
673 ktruio
.uio_resid
= retsize
[0];
674 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
676 FREE(ktriov
, M_TEMP
);
682 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
697 sendto(p
, uap
, retval
)
699 register struct sendto_args
/* {
714 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
715 AUDIT_ARG(fd
, uap
->s
);
717 msg
.msg_name
= uap
->to
;
718 msg
.msg_namelen
= uap
->tolen
;
722 #ifdef COMPAT_OLDSOCK
725 aiov
.iov_base
= uap
->buf
;
726 aiov
.iov_len
= uap
->len
;
727 stat
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
728 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, stat
, *retval
,0,0,0);
732 #ifdef COMPAT_OLDSOCK
741 osend(p
, uap
, retval
)
743 register struct osend_args
/* {
759 aiov
.iov_base
= uap
->buf
;
760 aiov
.iov_len
= uap
->len
;
763 return (sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
));
765 struct osendmsg_args
{
772 osendmsg(p
, uap
, retval
)
774 register struct osendmsg_args
/* {
783 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
786 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (struct omsghdr
));
789 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
790 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
792 MALLOC(iov
, struct iovec
*,
793 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
797 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
798 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
801 msg
.msg_flags
= MSG_COMPAT
;
803 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
811 struct sendmsg_args
{
818 sendmsg(p
, uap
, retval
)
820 register struct sendmsg_args
*uap
;
824 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
827 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
828 AUDIT_ARG(fd
, uap
->s
);
829 if (error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (msg
)))
831 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
835 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
836 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
837 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
840 MALLOC(iov
, struct iovec
*,
841 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
845 if (msg
.msg_iovlen
&&
846 (error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
847 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)))))
850 #ifdef COMPAT_OLDSOCK
853 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
857 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
862 recvit(p
, s
, mp
, namelenp
, retval
)
863 register struct proc
*p
;
865 register struct msghdr
*mp
;
871 register struct iovec
*iov
;
874 struct mbuf
*m
, *control
= 0;
877 struct sockaddr
*fromsa
= 0;
879 struct iovec
*ktriov
= NULL
;
883 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
884 if (error
= getsock(p
->p_fd
, s
, &fp
))
886 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
890 auio
.uio_iov
= mp
->msg_iov
;
891 auio
.uio_iovcnt
= mp
->msg_iovlen
;
892 auio
.uio_segflg
= UIO_USERSPACE
;
893 auio
.uio_rw
= UIO_READ
;
895 auio
.uio_offset
= 0; /* XXX */
898 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
899 if ((auio
.uio_resid
+= iov
->iov_len
) < 0) {
900 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
905 if (KTRPOINT(p
, KTR_GENIO
)) {
906 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
908 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
909 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
913 len
= auio
.uio_resid
;
914 so
= (struct socket
*)fp
->f_data
;
918 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, &auio
,
919 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
921 AUDIT_ARG(sockaddr
, p
, fromsa
);
923 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
924 error
== EINTR
|| error
== EWOULDBLOCK
))
928 if (ktriov
!= NULL
) {
930 ktruio
.uio_iov
= ktriov
;
931 ktruio
.uio_resid
= len
- auio
.uio_resid
;
932 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
934 FREE(ktriov
, M_TEMP
);
939 *retval
= len
- auio
.uio_resid
;
941 len
= mp
->msg_namelen
;
942 if (len
<= 0 || fromsa
== 0)
946 #define MIN(a,b) ((a)>(b)?(b):(a))
948 /* save sa_len before it is destroyed by MSG_COMPAT */
949 len
= MIN(len
, fromsa
->sa_len
);
950 #ifdef COMPAT_OLDSOCK
951 if (mp
->msg_flags
& MSG_COMPAT
)
952 ((struct osockaddr
*)fromsa
)->sa_family
=
955 error
= copyout(fromsa
,
956 (caddr_t
)mp
->msg_name
, (unsigned)len
);
960 mp
->msg_namelen
= len
;
962 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
963 #ifdef COMPAT_OLDSOCK
964 if (mp
->msg_flags
& MSG_COMPAT
)
965 error
= 0; /* old recvfrom didn't check */
971 if (mp
->msg_control
) {
972 #ifdef COMPAT_OLDSOCK
974 * We assume that old recvmsg calls won't receive access
975 * rights and other control info, esp. as control info
976 * is always optional and those options didn't exist in 4.3.
977 * If we receive rights, trim the cmsghdr; anything else
980 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
981 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
983 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
985 mp
->msg_controllen
= 0;
988 control
->m_len
-= sizeof (struct cmsghdr
);
989 control
->m_data
+= sizeof (struct cmsghdr
);
992 len
= mp
->msg_controllen
;
994 mp
->msg_controllen
= 0;
995 ctlbuf
= (caddr_t
) mp
->msg_control
;
997 while (m
&& len
> 0) {
1000 if (len
>= m
->m_len
)
1003 mp
->msg_flags
|= MSG_CTRUNC
;
1007 if (error
= copyout((caddr_t
)mtod(m
, caddr_t
),
1015 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
1019 FREE(fromsa
, M_SONAME
);
1022 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
1027 struct recvfrom_args
{
1037 recvfrom(p
, uap
, retval
)
1039 register struct recvfrom_args
/* {
1053 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1054 AUDIT_ARG(fd
, uap
->s
);
1056 if (uap
->fromlenaddr
) {
1057 error
= copyin((caddr_t
)uap
->fromlenaddr
,
1058 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1062 msg
.msg_namelen
= 0;
1063 msg
.msg_name
= uap
->from
;
1064 msg
.msg_iov
= &aiov
;
1066 aiov
.iov_base
= uap
->buf
;
1067 aiov
.iov_len
= uap
->len
;
1068 msg
.msg_control
= 0;
1069 msg
.msg_flags
= uap
->flags
;
1070 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1071 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)uap
->fromlenaddr
, retval
));
1074 #ifdef COMPAT_OLDSOCK
1076 orecvfrom(p
, uap
, retval
)
1078 struct recvfrom_args
*uap
;
1082 uap
->flags
|= MSG_COMPAT
;
1083 return (recvfrom(p
, uap
, retval
));
1088 #ifdef COMPAT_OLDSOCK
1097 orecv(p
, uap
, retval
)
1099 struct orecv_args
*uap
;
1106 msg
.msg_namelen
= 0;
1107 msg
.msg_iov
= &aiov
;
1109 aiov
.iov_base
= uap
->buf
;
1110 aiov
.iov_len
= uap
->len
;
1111 msg
.msg_control
= 0;
1112 msg
.msg_flags
= uap
->flags
;
1113 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
));
1117 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1118 * overlays the new one, missing only the flags, and with the (old) access
1119 * rights where the control fields are now.
1121 struct orecvmsg_args
{
1123 struct omsghdr
*msg
;
1128 orecvmsg(p
, uap
, retval
)
1130 struct orecvmsg_args
*uap
;
1134 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
1137 error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1138 sizeof (struct omsghdr
));
1141 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1142 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
1144 MALLOC(iov
, struct iovec
*,
1145 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1149 msg
.msg_flags
= uap
->flags
| MSG_COMPAT
;
1150 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
1151 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1155 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)&uap
->msg
->msg_namelen
, retval
);
1157 if (msg
.msg_controllen
&& error
== 0)
1158 error
= copyout((caddr_t
)&msg
.msg_controllen
,
1159 (caddr_t
)&uap
->msg
->msg_accrightslen
, sizeof (int));
1167 struct recvmsg_args
{
1174 recvmsg(p
, uap
, retval
)
1176 struct recvmsg_args
*uap
;
1180 struct iovec aiov
[UIO_SMALLIOV
], *uiov
, *iov
;
1183 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1184 AUDIT_ARG(fd
, uap
->s
);
1185 if (error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1188 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1192 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1193 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
1194 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1197 MALLOC(iov
, struct iovec
*,
1198 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1202 #ifdef COMPAT_OLDSOCK
1203 msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1205 msg
.msg_flags
= uap
->flags
;
1209 error
= copyin((caddr_t
)uiov
, (caddr_t
)iov
,
1210 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1213 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
);
1216 error
= copyout((caddr_t
)&msg
, (caddr_t
)uap
->msg
, sizeof(msg
));
1221 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1226 struct shutdown_args
{
1232 shutdown(p
, uap
, retval
)
1234 struct shutdown_args
*uap
;
1240 AUDIT_ARG(fd
, uap
->s
);
1241 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1244 if (fp
->f_data
== NULL
)
1246 return (soshutdown((struct socket
*)fp
->f_data
, uap
->how
));
1254 struct setsockopt_args
{
1263 setsockopt(p
, uap
, retval
)
1265 struct setsockopt_args
*uap
;
1269 struct sockopt sopt
;
1272 AUDIT_ARG(fd
, uap
->s
);
1273 if (uap
->val
== 0 && uap
->valsize
!= 0)
1275 if (uap
->valsize
< 0)
1278 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1282 sopt
.sopt_dir
= SOPT_SET
;
1283 sopt
.sopt_level
= uap
->level
;
1284 sopt
.sopt_name
= uap
->name
;
1285 sopt
.sopt_val
= uap
->val
;
1286 sopt
.sopt_valsize
= uap
->valsize
;
1289 if (fp
->f_data
== NULL
)
1291 return (sosetopt((struct socket
*)fp
->f_data
, &sopt
));
1297 getsockopt(p
, uap
, retval
)
1299 struct getsockopt_args
*uap
;
1304 struct sockopt sopt
;
1306 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1310 error
= copyin((caddr_t
)uap
->avalsize
, (caddr_t
)&valsize
,
1319 sopt
.sopt_dir
= SOPT_GET
;
1320 sopt
.sopt_level
= uap
->level
;
1321 sopt
.sopt_name
= uap
->name
;
1322 sopt
.sopt_val
= uap
->val
;
1323 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1326 if (fp
->f_data
== NULL
)
1328 error
= sogetopt((struct socket
*)fp
->f_data
, &sopt
);
1330 valsize
= sopt
.sopt_valsize
;
1331 error
= copyout((caddr_t
)&valsize
,
1332 (caddr_t
)uap
->avalsize
, sizeof (valsize
));
1344 pipe(p
, uap
, retval
)
1346 struct pipe_args
*uap
;
1349 struct file
*rf
, *wf
;
1350 struct socket
*rso
, *wso
;
1353 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1354 if (error
= socreate(AF_UNIX
, &rso
, SOCK_STREAM
, 0)) {
1355 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1358 if (error
= socreate(AF_UNIX
, &wso
, SOCK_STREAM
, 0)) {
1361 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1362 error
= falloc(p
, &rf
, &fd
);
1367 rf
->f_type
= DTYPE_SOCKET
;
1368 rf
->f_ops
= &socketops
;
1369 rf
->f_data
= (caddr_t
)rso
;
1370 if (error
= falloc(p
, &wf
, &fd
))
1372 wf
->f_flag
= FWRITE
;
1373 wf
->f_type
= DTYPE_SOCKET
;
1374 wf
->f_ops
= &socketops
;
1375 wf
->f_data
= (caddr_t
)wso
;
1378 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1379 error
= unp_connect2(wso
, rso
);
1380 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1383 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
1384 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
1387 fdrelse(p
, retval
[1]);
1390 fdrelse(p
, retval
[0]);
1393 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1398 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1408 getsockname1(p
, uap
, retval
, compat
)
1410 register struct getsockname_args
*uap
;
1415 register struct socket
*so
;
1416 struct sockaddr
*sa
;
1420 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1423 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1426 so
= (struct socket
*)fp
->f_data
;
1430 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1438 len
= MIN(len
, sa
->sa_len
);
1439 #ifdef COMPAT_OLDSOCK
1441 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1443 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1446 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
,
1455 getsockname(p
, uap
, retval
)
1457 struct getsockname_args
*uap
;
1461 return (getsockname1(p
, uap
, retval
, 0));
1464 #ifdef COMPAT_OLDSOCK
1466 ogetsockname(p
, uap
, retval
)
1468 struct getsockname_args
*uap
;
1472 return (getsockname1(p
, uap
, retval
, 1));
1474 #endif /* COMPAT_OLDSOCK */
1477 * Get name of peer for connected socket.
1481 getpeername1(p
, uap
, retval
, compat
)
1483 register struct getpeername_args
*uap
;
1488 register struct socket
*so
;
1489 struct sockaddr
*sa
;
1493 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1496 so
= (struct socket
*)fp
->f_data
;
1499 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
1501 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1505 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1512 len
= MIN(len
, sa
->sa_len
);
1513 #ifdef COMPAT_OLDSOCK
1515 ((struct osockaddr
*)sa
)->sa_family
=
1518 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1522 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
, sizeof (len
));
1524 if (sa
) FREE(sa
, M_SONAME
);
1529 getpeername(p
, uap
, retval
)
1531 struct getpeername_args
*uap
;
1535 return (getpeername1(p
, uap
, retval
, 0));
1538 #ifdef COMPAT_OLDSOCK
1540 ogetpeername(p
, uap
, retval
)
1542 struct ogetpeername_args
*uap
;
1546 /* XXX uap should have type `getpeername_args *' to begin with. */
1547 return (getpeername1(p
, (struct getpeername_args
*)uap
, retval
, 1));
1549 #endif /* COMPAT_OLDSOCK */
1552 sockargs(mp
, buf
, buflen
, type
)
1557 register struct sockaddr
*sa
;
1558 register struct mbuf
*m
;
1561 if ((u_int
)buflen
> MLEN
) {
1562 #ifdef COMPAT_OLDSOCK
1563 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1564 buflen
= MLEN
; /* unix domain compat. hack */
1569 m
= m_get(M_WAIT
, type
);
1573 error
= copyin(buf
, mtod(m
, caddr_t
), (u_int
)buflen
);
1578 if (type
== MT_SONAME
) {
1579 sa
= mtod(m
, struct sockaddr
*);
1581 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1582 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1583 sa
->sa_family
= sa
->sa_len
;
1585 sa
->sa_len
= buflen
;
1592 getsockaddr(namp
, uaddr
, len
)
1593 struct sockaddr
**namp
;
1597 struct sockaddr
*sa
;
1600 if (len
> SOCK_MAXADDRLEN
)
1601 return ENAMETOOLONG
;
1606 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1607 error
= copyin(uaddr
, sa
, len
);
1611 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1612 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1613 sa
->sa_family
= sa
->sa_len
;
1622 getsock(fdp
, fdes
, fpp
)
1623 struct filedesc
*fdp
;
1627 register struct file
*fp
;
1629 if ((unsigned)fdes
>= fdp
->fd_nfiles
||
1630 (fp
= fdp
->fd_ofiles
[fdes
]) == NULL
||
1631 (fdp
->fd_ofileflags
[fdes
] & UF_RESERVED
))
1633 if (fp
->f_type
!= DTYPE_SOCKET
)
1641 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1642 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1643 * been made static, but may be useful in the future for doing zero-copy in
1644 * other parts of the networking code.
1647 sf_buf_init(void *arg
)
1651 SLIST_INIT(&sf_freelist
);
1652 sf_base
= kmem_alloc_pageable(kernel_map
, nsfbufs
* PAGE_SIZE
);
1653 sf_bufs
= _MALLOC(nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
);
1654 bzero(sf_bufs
, nsfbufs
* sizeof(struct sf_buf
));
1655 for (i
= 0; i
< nsfbufs
; i
++) {
1656 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1657 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1662 * Get an sf_buf from the freelist. Will block if none are available.
1664 static struct sf_buf
*
1671 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1672 sf_buf_alloc_want
= 1;
1673 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1675 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1681 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1683 sf_buf_ref(caddr_t addr
, u_int size
)
1688 if (sf
->refcnt
== 0)
1689 panic("sf_buf_ref: referencing a free sf_buf");
1694 * Lose a reference to an sf_buf. When none left, detach mapped page
1695 * and release resources back to the system.
1697 * Must be called at splimp.
1700 sf_buf_free(caddr_t addr
, u_int size
)
1707 if (sf
->refcnt
== 0)
1708 panic("sf_buf_free: freeing free sf_buf");
1710 if (sf
->refcnt
== 0) {
1711 pmap_qremove((vm_offset_t
)addr
, 1);
1714 vm_page_unwire(m
, 0);
1716 * Check for the object going away on us. This can
1717 * happen since we don't hold a reference to it.
1718 * If so, we're responsible for freeing the page.
1720 if (m
->wire_count
== 0 && m
->object
== NULL
)
1721 vm_page_lock_queues();
1723 vm_page_unlock_queues();
1726 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1727 if (sf_buf_alloc_want
) {
1728 sf_buf_alloc_want
= 0;
1729 wakeup(&sf_freelist
);
1736 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1737 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1739 * Send a file specified by 'fd' and starting at 'offset' to a socket
1740 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1741 * nbytes == 0. Optionally add a header and/or trailer to the socket
1742 * output. If specified, write the total number of bytes sent into *sbytes.
1745 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1748 struct filedesc
*fdp
= p
->p_fd
;
1750 struct vm_object
*obj
;
1755 struct writev_args nuap
;
1756 struct sf_hdtr hdtr
;
1757 off_t off
, xfsize
, sbytes
= 0;
1761 * Do argument checking. Must be a regular file in, stream
1762 * type and connected socket out, positive offset.
1764 if (((u_int
)uap
->fd
) >= fdp
->fd_nfiles
||
1765 (fp
= fdp
->fd_ofiles
[uap
->fd
]) == NULL
||
1766 (fp
->f_flag
& FREAD
) == 0) {
1770 if (fp
->f_type
!= DTYPE_VNODE
) {
1774 vp
= (struct vnode
*)fp
->f_data
;
1776 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1780 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1783 so
= (struct socket
*)fp
->f_data
;
1788 if (so
->so_type
!= SOCK_STREAM
) {
1792 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1796 if (uap
->offset
< 0) {
1802 * If specified, get the pointer to the sf_hdtr struct for
1803 * any headers/trailers.
1805 if (uap
->hdtr
!= NULL
) {
1806 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1810 * Send any headers. Wimp out and use writev(2).
1812 if (hdtr
.headers
!= NULL
) {
1814 nuap
.iovp
= hdtr
.headers
;
1815 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1816 error
= writev(p
, &nuap
);
1819 sbytes
+= p
->p_retval
[0];
1824 * Protect against multiple writers to the socket.
1826 (void) sblock(&so
->so_snd
, M_WAIT
);
1829 * Loop through the pages in the file, starting with the requested
1830 * offset. Get a file page (do I/O if necessary), map the file page
1831 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1834 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1835 vm_object_offset_t pindex
;
1836 vm_object_offset_t pgoff
;
1838 pindex
= OFF_TO_IDX(off
);
1841 * Calculate the amount to transfer. Not to exceed a page,
1842 * the EOF, or the passed in nbytes.
1844 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1845 if (xfsize
> PAGE_SIZE_64
)
1847 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1848 if (PAGE_SIZE
- pgoff
< xfsize
)
1849 xfsize
= PAGE_SIZE_64
- pgoff
;
1850 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1851 xfsize
= uap
->nbytes
- sbytes
;
1855 * Optimize the non-blocking case by looking at the socket space
1856 * before going to the extra work of constituting the sf_buf.
1858 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1859 if (so
->so_state
& SS_CANTSENDMORE
)
1863 sbunlock(&so
->so_snd
);
1867 * Attempt to look up the page. If the page doesn't exist or the
1868 * part we're interested in isn't valid, then read it from disk.
1869 * If some other part of the kernel has this page (i.e. it's busy),
1870 * then disk I/O may be occuring on it, so wait and retry.
1872 pg
= vm_page_lookup(obj
, pindex
);
1873 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1874 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1880 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1886 * don't just clear PG_BUSY manually -
1887 * vm_page_alloc() should be considered opaque,
1888 * use the VM routine provided to clear
1895 * Ensure that our page is still around when the I/O completes.
1897 vm_page_io_start(pg
);
1900 * Get the page from backing store.
1902 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1903 auio
.uio_iov
= &aiov
;
1904 auio
.uio_iovcnt
= 1;
1906 aiov
.iov_len
= MAXBSIZE
;
1907 auio
.uio_resid
= MAXBSIZE
;
1908 auio
.uio_offset
= trunc_page(off
);
1909 auio
.uio_segflg
= UIO_NOCOPY
;
1910 auio
.uio_rw
= UIO_READ
;
1912 vn_lock(vp
, LK_SHARED
| LK_NOPAUSE
| LK_RETRY
, p
);
1913 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1915 VOP_UNLOCK(vp
, 0, p
);
1916 vm_page_flag_clear(pg
, PG_ZERO
);
1917 vm_page_io_finish(pg
);
1919 vm_page_unwire(pg
, 0);
1921 * See if anyone else might know about this page.
1922 * If not and it is not valid, then free it.
1924 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1925 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1926 pg
->hold_count
== 0)
1927 vm_page_lock_queues();
1929 vm_page_unlock_queues();
1930 sbunlock(&so
->so_snd
);
1934 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1936 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1938 * Page is busy. Wait and retry.
1940 vm_page_flag_set(pg
, PG_WANTED
);
1941 tsleep(pg
, PVM
, "sfpbsy", 0);
1948 * Protect from having the page ripped out from beneath us.
1953 * Allocate a kernel virtual page and insert the physical page
1956 sf
= sf_buf_alloc();
1958 pmap_qenter(sf
->kva
, &pg
, 1);
1960 * Get an mbuf header and set it up as having external storage.
1962 MGETHDR(m
, M_WAIT
, MT_DATA
);
1963 m
->m_ext
.ext_free
= sf_buf_free
;
1964 m
->m_ext
.ext_ref
= sf_buf_ref
;
1965 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1966 m
->m_ext
.ext_size
= PAGE_SIZE
;
1967 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1968 m
->m_flags
|= M_EXT
;
1969 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1971 * Add the buffer to the socket buffer chain.
1976 * Make sure that the socket is still able to take more data.
1977 * CANTSENDMORE being true usually means that the connection
1978 * was closed. so_error is true when an error was sensed after
1980 * The state is checked after the page mapping and buffer
1981 * allocation above since those operations may block and make
1982 * any socket checks stale. From this point forward, nothing
1983 * blocks before the pru_send (or more accurately, any blocking
1984 * results in a loop back to here to re-check).
1986 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1987 if (so
->so_state
& SS_CANTSENDMORE
) {
1990 error
= so
->so_error
;
1994 sbunlock(&so
->so_snd
);
1999 * Wait for socket space to become available. We do this just
2000 * after checking the connection state above in order to avoid
2001 * a race condition with sbwait().
2003 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
2004 if (so
->so_state
& SS_NBIO
) {
2006 sbunlock(&so
->so_snd
);
2011 error
= sbwait(&so
->so_snd
);
2013 * An error from sbwait usually indicates that we've
2014 * been interrupted by a signal. If we've sent anything
2015 * then return bytes sent, otherwise return the error.
2019 sbunlock(&so
->so_snd
);
2025 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
2028 sbunlock(&so
->so_snd
);
2032 sbunlock(&so
->so_snd
);
2035 * Send trailers. Wimp out and use writev(2).
2037 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
2039 nuap
.iovp
= hdtr
.trailers
;
2040 nuap
.iovcnt
= hdtr
.trl_cnt
;
2041 error
= writev(p
, &nuap
);
2044 sbytes
+= p
->p_retval
[0];
2048 if (uap
->sbytes
!= NULL
) {
2049 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));