2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_capsicum.h"
40 #include "opt_inet6.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/capsicum.h>
47 #include <sys/condvar.h>
48 #include <sys/kernel.h>
50 #include <sys/mutex.h>
51 #include <sys/sysproto.h>
52 #include <sys/malloc.h>
53 #include <sys/filedesc.h>
54 #include <sys/event.h>
56 #include <sys/fcntl.h>
58 #include <sys/filio.h>
61 #include <sys/mount.h>
63 #include <sys/protosw.h>
64 #include <sys/rwlock.h>
65 #include <sys/sf_buf.h>
66 #include <sys/sysent.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
73 #include <sys/vnode.h>
75 #include <sys/ktrace.h>
77 #ifdef COMPAT_FREEBSD32
78 #include <compat/freebsd32/freebsd32_util.h>
83 #include <security/audit/audit.h>
84 #include <security/mac/mac_framework.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
95 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
98 #define ACCEPT4_INHERIT 0x1
99 #define ACCEPT4_COMPAT 0x2
101 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
102 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
104 static int accept1(struct thread *td, int s, struct sockaddr *uname,
105 socklen_t *anamelen, int flags);
106 static int do_sendfile(struct thread *td, struct sendfile_args *uap,
108 static int getsockname1(struct thread *td, struct getsockname_args *uap,
110 static int getpeername1(struct thread *td, struct getpeername_args *uap,
113 counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
116 * sendfile(2)-related variables and associated sysctls
118 static SYSCTL_NODE(_kern_ipc, OID_AUTO, sendfile, CTLFLAG_RW, 0,
119 "sendfile(2) tunables");
120 static int sfreadahead = 1;
121 SYSCTL_INT(_kern_ipc_sendfile, OID_AUTO, readahead, CTLFLAG_RW,
122 &sfreadahead, 0, "Number of sendfile(2) read-ahead MAXBSIZE blocks");
126 sfstat_init(const void *unused)
129 COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
132 SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
135 sfstat_sysctl(SYSCTL_HANDLER_ARGS)
139 COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
141 COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
142 return (SYSCTL_OUT(req, &s, sizeof(s)));
144 SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW,
145 NULL, 0, sfstat_sysctl, "I", "sendfile statistics");
148 * Convert a user file descriptor to a kernel file entry and check if required
149 * capability rights are present.
150 * A reference on the file entry is held upon returning.
153 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
154 struct file **fpp, u_int *fflagp)
159 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, 0, &fp, NULL);
162 if (fp->f_type != DTYPE_SOCKET) {
167 *fflagp = fp->f_flag;
173 * System call interface to the socket abstraction.
175 #if defined(COMPAT_43)
176 #define COMPAT_OLDSOCK
182 struct socket_args /* {
190 int fd, error, type, oflag, fflag;
192 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
197 if ((type & SOCK_CLOEXEC) != 0) {
198 type &= ~SOCK_CLOEXEC;
201 if ((type & SOCK_NONBLOCK) != 0) {
202 type &= ~SOCK_NONBLOCK;
207 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
212 error = falloc(td, &fp, &fd, oflag);
215 /* An extra reference on `fp' has been held for us by falloc(). */
216 error = socreate(uap->domain, &so, type, uap->protocol,
221 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
222 if ((fflag & FNONBLOCK) != 0)
223 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
224 td->td_retval[0] = fd;
234 struct bind_args /* {
243 error = getsockaddr(&sa, uap->name, uap->namelen);
245 error = kern_bind(td, uap->s, sa);
252 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
260 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
261 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
267 if (KTRPOINT(td, KTR_STRUCT))
271 error = mac_socket_check_bind(td->td_ucred, so, sa);
274 if (dirfd == AT_FDCWD)
275 error = sobind(so, sa, td);
277 error = sobindat(dirfd, so, sa, td);
286 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
289 return (kern_bindat(td, AT_FDCWD, fd, sa));
296 struct bindat_args /* {
306 error = getsockaddr(&sa, uap->name, uap->namelen);
308 error = kern_bindat(td, uap->fd, uap->s, sa);
318 struct listen_args /* {
328 AUDIT_ARG_FD(uap->s);
329 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
334 error = mac_socket_check_listen(td->td_ucred, so);
337 error = solisten(so, uap->backlog, td);
347 accept1(td, s, uname, anamelen, flags)
350 struct sockaddr *uname;
354 struct sockaddr *name;
360 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
362 error = copyin(anamelen, &namelen, sizeof (namelen));
366 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
369 * return a namelen of zero for older code which might
370 * ignore the return value from accept.
373 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
377 if (error == 0 && uname != NULL) {
378 #ifdef COMPAT_OLDSOCK
379 if (flags & ACCEPT4_COMPAT)
380 ((struct osockaddr *)name)->sa_family =
383 error = copyout(name, uname, namelen);
386 error = copyout(&namelen, anamelen,
389 fdclose(td, fp, td->td_retval[0]);
391 free(name, M_SONAME);
396 kern_accept(struct thread *td, int s, struct sockaddr **name,
397 socklen_t *namelen, struct file **fp)
399 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
403 kern_accept4(struct thread *td, int s, struct sockaddr **name,
404 socklen_t *namelen, int flags, struct file **fp)
406 struct file *headfp, *nfp = NULL;
407 struct sockaddr *sa = NULL;
408 struct socket *head, *so;
418 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
422 head = headfp->f_data;
423 if ((head->so_options & SO_ACCEPTCONN) == 0) {
428 error = mac_socket_check_accept(td->td_ucred, head);
432 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
436 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
441 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
442 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
443 head->so_error = ECONNABORTED;
446 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
453 if (head->so_error) {
454 error = head->so_error;
459 so = TAILQ_FIRST(&head->so_comp);
460 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
461 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
464 * Before changing the flags on the socket, we have to bump the
465 * reference count. Otherwise, if the protocol calls sofree(),
466 * the socket will be released due to a zero refcount.
468 SOCK_LOCK(so); /* soref() and so_state update */
469 soref(so); /* file descriptor reference */
471 TAILQ_REMOVE(&head->so_comp, so, so_list);
473 if (flags & ACCEPT4_INHERIT)
474 so->so_state |= (head->so_state & SS_NBIO);
476 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
477 so->so_qstate &= ~SQ_COMP;
483 /* An extra reference on `nfp' has been held for us by falloc(). */
484 td->td_retval[0] = fd;
486 /* connection has been removed from the listen queue */
487 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
489 if (flags & ACCEPT4_INHERIT) {
490 pgid = fgetown(&head->so_sigio);
492 fsetown(pgid, &so->so_sigio);
494 fflag &= ~(FNONBLOCK | FASYNC);
495 if (flags & SOCK_NONBLOCK)
499 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
500 /* Sync socket nonblocking/async state with file flags */
501 tmp = fflag & FNONBLOCK;
502 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
503 tmp = fflag & FASYNC;
504 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
506 error = soaccept(so, &sa);
509 * return a namelen of zero for older code which might
510 * ignore the return value from accept.
521 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
523 /* check sa_len before it is destroyed */
524 if (*namelen > sa->sa_len)
525 *namelen = sa->sa_len;
527 if (KTRPOINT(td, KTR_STRUCT))
537 * close the new descriptor, assuming someone hasn't ripped it
541 fdclose(td, nfp, fd);
544 * Release explicitly held references before returning. We return
545 * a reference on nfp to the caller on success if they request it.
564 struct accept_args *uap;
567 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
573 struct accept4_args *uap;
576 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
579 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
582 #ifdef COMPAT_OLDSOCK
586 struct accept_args *uap;
589 return (accept1(td, uap->s, uap->name, uap->anamelen,
590 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
592 #endif /* COMPAT_OLDSOCK */
598 struct connect_args /* {
607 error = getsockaddr(&sa, uap->name, uap->namelen);
609 error = kern_connect(td, uap->s, sa);
616 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
621 int error, interrupted = 0;
624 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
625 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
630 if (so->so_state & SS_ISCONNECTING) {
635 if (KTRPOINT(td, KTR_STRUCT))
639 error = mac_socket_check_connect(td->td_ucred, so, sa);
643 if (dirfd == AT_FDCWD)
644 error = soconnect(so, sa, td);
646 error = soconnectat(dirfd, so, sa, td);
649 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
654 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
655 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
658 if (error == EINTR || error == ERESTART)
664 error = so->so_error;
670 so->so_state &= ~SS_ISCONNECTING;
671 if (error == ERESTART)
679 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
682 return (kern_connectat(td, AT_FDCWD, fd, sa));
687 sys_connectat(td, uap)
689 struct connectat_args /* {
699 error = getsockaddr(&sa, uap->name, uap->namelen);
701 error = kern_connectat(td, uap->fd, uap->s, sa);
708 kern_socketpair(struct thread *td, int domain, int type, int protocol,
711 struct file *fp1, *fp2;
712 struct socket *so1, *so2;
713 int fd, error, oflag, fflag;
715 AUDIT_ARG_SOCKET(domain, type, protocol);
719 if ((type & SOCK_CLOEXEC) != 0) {
720 type &= ~SOCK_CLOEXEC;
723 if ((type & SOCK_NONBLOCK) != 0) {
724 type &= ~SOCK_NONBLOCK;
728 /* We might want to have a separate check for socket pairs. */
729 error = mac_socket_check_create(td->td_ucred, domain, type,
734 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
737 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
740 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
741 error = falloc(td, &fp1, &fd, oflag);
745 fp1->f_data = so1; /* so1 already has ref count */
746 error = falloc(td, &fp2, &fd, oflag);
749 fp2->f_data = so2; /* so2 already has ref count */
751 error = soconnect2(so1, so2);
754 if (type == SOCK_DGRAM) {
756 * Datagram socket connection is asymmetric.
758 error = soconnect2(so2, so1);
762 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
764 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
766 if ((fflag & FNONBLOCK) != 0) {
767 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
768 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
774 fdclose(td, fp2, rsv[1]);
777 fdclose(td, fp1, rsv[0]);
789 sys_socketpair(struct thread *td, struct socketpair_args *uap)
793 error = kern_socketpair(td, uap->domain, uap->type,
797 error = copyout(sv, uap->rsv, 2 * sizeof(int));
799 (void)kern_close(td, sv[0]);
800 (void)kern_close(td, sv[1]);
806 sendit(td, s, mp, flags)
812 struct mbuf *control;
816 #ifdef CAPABILITY_MODE
817 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
821 if (mp->msg_name != NULL) {
822 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
832 if (mp->msg_control) {
833 if (mp->msg_controllen < sizeof(struct cmsghdr)
834 #ifdef COMPAT_OLDSOCK
835 && mp->msg_flags != MSG_COMPAT
841 error = sockargs(&control, mp->msg_control,
842 mp->msg_controllen, MT_CONTROL);
845 #ifdef COMPAT_OLDSOCK
846 if (mp->msg_flags == MSG_COMPAT) {
849 M_PREPEND(control, sizeof(*cm), M_WAITOK);
850 cm = mtod(control, struct cmsghdr *);
851 cm->cmsg_len = control->m_len;
852 cm->cmsg_level = SOL_SOCKET;
853 cm->cmsg_type = SCM_RIGHTS;
860 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
868 kern_sendit(td, s, mp, flags, control, segflg)
873 struct mbuf *control;
882 struct uio *ktruio = NULL;
888 cap_rights_init(&rights, CAP_SEND);
889 if (mp->msg_name != NULL) {
890 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
891 cap_rights_set(&rights, CAP_CONNECT);
893 error = getsock_cap(td, s, &rights, &fp, NULL);
896 so = (struct socket *)fp->f_data;
899 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
900 ktrsockaddr(mp->msg_name);
903 if (mp->msg_name != NULL) {
904 error = mac_socket_check_connect(td->td_ucred, so,
909 error = mac_socket_check_send(td->td_ucred, so);
914 auio.uio_iov = mp->msg_iov;
915 auio.uio_iovcnt = mp->msg_iovlen;
916 auio.uio_segflg = segflg;
917 auio.uio_rw = UIO_WRITE;
919 auio.uio_offset = 0; /* XXX */
922 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
923 if ((auio.uio_resid += iov->iov_len) < 0) {
929 if (KTRPOINT(td, KTR_GENIO))
930 ktruio = cloneuio(&auio);
932 len = auio.uio_resid;
933 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
935 if (auio.uio_resid != len && (error == ERESTART ||
936 error == EINTR || error == EWOULDBLOCK))
938 /* Generation of SIGPIPE can be controlled per socket */
939 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
940 !(flags & MSG_NOSIGNAL)) {
941 PROC_LOCK(td->td_proc);
942 tdsignal(td, SIGPIPE);
943 PROC_UNLOCK(td->td_proc);
947 td->td_retval[0] = len - auio.uio_resid;
949 if (ktruio != NULL) {
950 ktruio->uio_resid = td->td_retval[0];
951 ktrgenio(s, UIO_WRITE, ktruio, error);
962 struct sendto_args /* {
974 msg.msg_name = uap->to;
975 msg.msg_namelen = uap->tolen;
979 #ifdef COMPAT_OLDSOCK
982 aiov.iov_base = uap->buf;
983 aiov.iov_len = uap->len;
984 return (sendit(td, uap->s, &msg, uap->flags));
987 #ifdef COMPAT_OLDSOCK
991 struct osend_args /* {
1002 msg.msg_namelen = 0;
1003 msg.msg_iov = &aiov;
1005 aiov.iov_base = uap->buf;
1006 aiov.iov_len = uap->len;
1007 msg.msg_control = 0;
1009 return (sendit(td, uap->s, &msg, uap->flags));
1015 struct osendmsg_args /* {
1025 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1028 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1032 msg.msg_flags = MSG_COMPAT;
1033 error = sendit(td, uap->s, &msg, uap->flags);
1040 sys_sendmsg(td, uap)
1042 struct sendmsg_args /* {
1052 error = copyin(uap->msg, &msg, sizeof (msg));
1055 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1059 #ifdef COMPAT_OLDSOCK
1062 error = sendit(td, uap->s, &msg, uap->flags);
1068 kern_recvit(td, s, mp, fromseg, controlp)
1072 enum uio_seg fromseg;
1073 struct mbuf **controlp;
1077 struct mbuf *m, *control = NULL;
1081 struct sockaddr *fromsa = NULL;
1082 cap_rights_t rights;
1084 struct uio *ktruio = NULL;
1089 if (controlp != NULL)
1093 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
1100 error = mac_socket_check_receive(td->td_ucred, so);
1107 auio.uio_iov = mp->msg_iov;
1108 auio.uio_iovcnt = mp->msg_iovlen;
1109 auio.uio_segflg = UIO_USERSPACE;
1110 auio.uio_rw = UIO_READ;
1112 auio.uio_offset = 0; /* XXX */
1115 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1116 if ((auio.uio_resid += iov->iov_len) < 0) {
1122 if (KTRPOINT(td, KTR_GENIO))
1123 ktruio = cloneuio(&auio);
1125 len = auio.uio_resid;
1126 error = soreceive(so, &fromsa, &auio, NULL,
1127 (mp->msg_control || controlp) ? &control : NULL,
1130 if (auio.uio_resid != len && (error == ERESTART ||
1131 error == EINTR || error == EWOULDBLOCK))
1135 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1137 if (ktruio != NULL) {
1138 ktruio->uio_resid = len - auio.uio_resid;
1139 ktrgenio(s, UIO_READ, ktruio, error);
1144 td->td_retval[0] = len - auio.uio_resid;
1146 len = mp->msg_namelen;
1147 if (len <= 0 || fromsa == NULL)
1150 /* save sa_len before it is destroyed by MSG_COMPAT */
1151 len = MIN(len, fromsa->sa_len);
1152 #ifdef COMPAT_OLDSOCK
1153 if (mp->msg_flags & MSG_COMPAT)
1154 ((struct osockaddr *)fromsa)->sa_family =
1157 if (fromseg == UIO_USERSPACE) {
1158 error = copyout(fromsa, mp->msg_name,
1163 bcopy(fromsa, mp->msg_name, len);
1165 mp->msg_namelen = len;
1167 if (mp->msg_control && controlp == NULL) {
1168 #ifdef COMPAT_OLDSOCK
1170 * We assume that old recvmsg calls won't receive access
1171 * rights and other control info, esp. as control info
1172 * is always optional and those options didn't exist in 4.3.
1173 * If we receive rights, trim the cmsghdr; anything else
1176 if (control && mp->msg_flags & MSG_COMPAT) {
1177 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1179 mtod(control, struct cmsghdr *)->cmsg_type !=
1181 mp->msg_controllen = 0;
1184 control->m_len -= sizeof (struct cmsghdr);
1185 control->m_data += sizeof (struct cmsghdr);
1188 len = mp->msg_controllen;
1190 mp->msg_controllen = 0;
1191 ctlbuf = mp->msg_control;
1193 while (m && len > 0) {
1194 unsigned int tocopy;
1196 if (len >= m->m_len)
1199 mp->msg_flags |= MSG_CTRUNC;
1203 if ((error = copyout(mtod(m, caddr_t),
1204 ctlbuf, tocopy)) != 0)
1211 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1216 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1217 ktrsockaddr(fromsa);
1219 free(fromsa, M_SONAME);
1221 if (error == 0 && controlp != NULL)
1222 *controlp = control;
1230 recvit(td, s, mp, namelenp)
1238 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1241 if (namelenp != NULL) {
1242 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1243 #ifdef COMPAT_OLDSOCK
1244 if (mp->msg_flags & MSG_COMPAT)
1245 error = 0; /* old recvfrom didn't check */
1252 sys_recvfrom(td, uap)
1254 struct recvfrom_args /* {
1259 struct sockaddr * __restrict from;
1260 socklen_t * __restrict fromlenaddr;
1267 if (uap->fromlenaddr) {
1268 error = copyin(uap->fromlenaddr,
1269 &msg.msg_namelen, sizeof (msg.msg_namelen));
1273 msg.msg_namelen = 0;
1275 msg.msg_name = uap->from;
1276 msg.msg_iov = &aiov;
1278 aiov.iov_base = uap->buf;
1279 aiov.iov_len = uap->len;
1280 msg.msg_control = 0;
1281 msg.msg_flags = uap->flags;
1282 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1287 #ifdef COMPAT_OLDSOCK
1291 struct recvfrom_args *uap;
1294 uap->flags |= MSG_COMPAT;
1295 return (sys_recvfrom(td, uap));
1299 #ifdef COMPAT_OLDSOCK
1303 struct orecv_args /* {
1314 msg.msg_namelen = 0;
1315 msg.msg_iov = &aiov;
1317 aiov.iov_base = uap->buf;
1318 aiov.iov_len = uap->len;
1319 msg.msg_control = 0;
1320 msg.msg_flags = uap->flags;
1321 return (recvit(td, uap->s, &msg, NULL));
1325 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1326 * overlays the new one, missing only the flags, and with the (old) access
1327 * rights where the control fields are now.
1332 struct orecvmsg_args /* {
1334 struct omsghdr *msg;
1342 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1345 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1348 msg.msg_flags = uap->flags | MSG_COMPAT;
1350 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1351 if (msg.msg_controllen && error == 0)
1352 error = copyout(&msg.msg_controllen,
1353 &uap->msg->msg_accrightslen, sizeof (int));
1360 sys_recvmsg(td, uap)
1362 struct recvmsg_args /* {
1369 struct iovec *uiov, *iov;
1372 error = copyin(uap->msg, &msg, sizeof (msg));
1375 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1378 msg.msg_flags = uap->flags;
1379 #ifdef COMPAT_OLDSOCK
1380 msg.msg_flags &= ~MSG_COMPAT;
1384 error = recvit(td, uap->s, &msg, NULL);
1387 error = copyout(&msg, uap->msg, sizeof(msg));
1395 sys_shutdown(td, uap)
1397 struct shutdown_args /* {
1404 cap_rights_t rights;
1407 AUDIT_ARG_FD(uap->s);
1408 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
1412 error = soshutdown(so, uap->how);
1420 sys_setsockopt(td, uap)
1422 struct setsockopt_args /* {
1431 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1432 uap->val, UIO_USERSPACE, uap->valsize));
1436 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1442 enum uio_seg valseg;
1447 struct sockopt sopt;
1448 cap_rights_t rights;
1451 if (val == NULL && valsize != 0)
1453 if ((int)valsize < 0)
1456 sopt.sopt_dir = SOPT_SET;
1457 sopt.sopt_level = level;
1458 sopt.sopt_name = name;
1459 sopt.sopt_val = val;
1460 sopt.sopt_valsize = valsize;
1466 sopt.sopt_td = NULL;
1469 panic("kern_setsockopt called with bad valseg");
1473 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1477 error = sosetopt(so, &sopt);
1485 sys_getsockopt(td, uap)
1487 struct getsockopt_args /* {
1491 void * __restrict val;
1492 socklen_t * __restrict avalsize;
1499 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1504 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1505 uap->val, UIO_USERSPACE, &valsize);
1508 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1513 * Kernel version of getsockopt.
1514 * optval can be a userland or userspace. optlen is always a kernel pointer.
1517 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1523 enum uio_seg valseg;
1528 struct sockopt sopt;
1529 cap_rights_t rights;
1534 if ((int)*valsize < 0)
1537 sopt.sopt_dir = SOPT_GET;
1538 sopt.sopt_level = level;
1539 sopt.sopt_name = name;
1540 sopt.sopt_val = val;
1541 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1547 sopt.sopt_td = NULL;
1550 panic("kern_getsockopt called with bad valseg");
1554 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1558 error = sogetopt(so, &sopt);
1559 *valsize = sopt.sopt_valsize;
1566 * getsockname1() - Get socket name.
1570 getsockname1(td, uap, compat)
1572 struct getsockname_args /* {
1574 struct sockaddr * __restrict asa;
1575 socklen_t * __restrict alen;
1579 struct sockaddr *sa;
1583 error = copyin(uap->alen, &len, sizeof(len));
1587 error = kern_getsockname(td, uap->fdes, &sa, &len);
1592 #ifdef COMPAT_OLDSOCK
1594 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1596 error = copyout(sa, uap->asa, (u_int)len);
1600 error = copyout(&len, uap->alen, sizeof(len));
1605 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1610 cap_rights_t rights;
1615 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1621 CURVNET_SET(so->so_vnet);
1622 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1629 len = MIN(*alen, (*sa)->sa_len);
1632 if (KTRPOINT(td, KTR_STRUCT))
1637 if (error != 0 && *sa != NULL) {
1638 free(*sa, M_SONAME);
1645 sys_getsockname(td, uap)
1647 struct getsockname_args *uap;
1650 return (getsockname1(td, uap, 0));
1653 #ifdef COMPAT_OLDSOCK
1655 ogetsockname(td, uap)
1657 struct getsockname_args *uap;
1660 return (getsockname1(td, uap, 1));
1662 #endif /* COMPAT_OLDSOCK */
1665 * getpeername1() - Get name of peer for connected socket.
1669 getpeername1(td, uap, compat)
1671 struct getpeername_args /* {
1673 struct sockaddr * __restrict asa;
1674 socklen_t * __restrict alen;
1678 struct sockaddr *sa;
1682 error = copyin(uap->alen, &len, sizeof (len));
1686 error = kern_getpeername(td, uap->fdes, &sa, &len);
1691 #ifdef COMPAT_OLDSOCK
1693 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1695 error = copyout(sa, uap->asa, (u_int)len);
1699 error = copyout(&len, uap->alen, sizeof(len));
1704 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1709 cap_rights_t rights;
1714 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1719 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1724 CURVNET_SET(so->so_vnet);
1725 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1732 len = MIN(*alen, (*sa)->sa_len);
1735 if (KTRPOINT(td, KTR_STRUCT))
1739 if (error != 0 && *sa != NULL) {
1740 free(*sa, M_SONAME);
1749 sys_getpeername(td, uap)
1751 struct getpeername_args *uap;
1754 return (getpeername1(td, uap, 0));
1757 #ifdef COMPAT_OLDSOCK
1759 ogetpeername(td, uap)
1761 struct ogetpeername_args *uap;
1764 /* XXX uap should have type `getpeername_args *' to begin with. */
1765 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1767 #endif /* COMPAT_OLDSOCK */
1770 sockargs(mp, buf, buflen, type)
1775 struct sockaddr *sa;
1782 if (buflen > MLEN) {
1783 #ifdef COMPAT_OLDSOCK
1784 if (type == MT_SONAME && buflen <= 112)
1785 buflen = MLEN; /* unix domain compat. hack */
1788 if (buflen > MCLBYTES)
1791 m = m_get2(buflen, M_WAITOK, type, 0);
1793 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1798 if (type == MT_SONAME) {
1799 sa = mtod(m, struct sockaddr *);
1801 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1802 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1803 sa->sa_family = sa->sa_len;
1805 sa->sa_len = buflen;
1812 getsockaddr(namp, uaddr, len)
1813 struct sockaddr **namp;
1817 struct sockaddr *sa;
1820 if (len > SOCK_MAXADDRLEN)
1821 return (ENAMETOOLONG);
1822 if (len < offsetof(struct sockaddr, sa_data[0]))
1824 sa = malloc(len, M_SONAME, M_WAITOK);
1825 error = copyin(uaddr, sa, len);
1829 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1830 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1831 sa->sa_family = sa->sa_len;
1839 struct sendfile_sync {
1846 * Detach mapped page and release resources back to the system.
1849 sf_buf_mext(struct mbuf *mb, void *addr, void *args)
1852 struct sendfile_sync *sfs;
1854 m = sf_buf_page(args);
1857 vm_page_unwire(m, 0);
1859 * Check for the object going away on us. This can
1860 * happen since we don't hold a reference to it.
1861 * If so, we're responsible for freeing the page.
1863 if (m->wire_count == 0 && m->object == NULL)
1867 return (EXT_FREE_OK);
1869 mtx_lock(&sfs->mtx);
1870 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1871 if (--sfs->count == 0)
1872 cv_signal(&sfs->cv);
1873 mtx_unlock(&sfs->mtx);
1874 return (EXT_FREE_OK);
1880 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1881 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1883 * Send a file specified by 'fd' and starting at 'offset' to a socket
1884 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1885 * 0. Optionally add a header and/or trailer to the socket output. If
1886 * specified, write the total number of bytes sent into *sbytes.
1889 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1892 return (do_sendfile(td, uap, 0));
1896 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1898 struct sf_hdtr hdtr;
1899 struct uio *hdr_uio, *trl_uio;
1901 cap_rights_t rights;
1905 * File offset must be positive. If it goes beyond EOF
1906 * we send only the header/trailer and no payload data.
1908 if (uap->offset < 0)
1911 hdr_uio = trl_uio = NULL;
1913 if (uap->hdtr != NULL) {
1914 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1917 if (hdtr.headers != NULL) {
1918 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1922 if (hdtr.trailers != NULL) {
1923 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1930 AUDIT_ARG_FD(uap->fd);
1933 * sendfile(2) can start at any offset within a file so we require
1934 * CAP_READ+CAP_SEEK = CAP_PREAD.
1936 if ((error = fget_read(td, uap->fd,
1937 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1941 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1942 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1946 free(hdr_uio, M_IOV);
1947 free(trl_uio, M_IOV);
1951 #ifdef COMPAT_FREEBSD4
1953 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1955 struct sendfile_args args;
1959 args.offset = uap->offset;
1960 args.nbytes = uap->nbytes;
1961 args.hdtr = uap->hdtr;
1962 args.sbytes = uap->sbytes;
1963 args.flags = uap->flags;
1965 return (do_sendfile(td, &args, 1));
1967 #endif /* COMPAT_FREEBSD4 */
1970 sendfile_readpage(vm_object_t obj, struct vnode *vp, int nd,
1971 off_t off, int xfsize, int bsize, struct thread *td, vm_page_t *res)
1976 int error, readahead, rv;
1978 pindex = OFF_TO_IDX(off);
1979 VM_OBJECT_WLOCK(obj);
1980 m = vm_page_grab(obj, pindex, (vp != NULL ? VM_ALLOC_NOBUSY |
1981 VM_ALLOC_IGN_SBUSY : 0) | VM_ALLOC_WIRED | VM_ALLOC_NORMAL);
1984 * Check if page is valid for what we need, otherwise initiate I/O.
1986 * The non-zero nd argument prevents disk I/O, instead we
1987 * return the caller what he specified in nd. In particular,
1988 * if we already turned some pages into mbufs, nd == EAGAIN
1989 * and the main function send them the pages before we come
1990 * here again and block.
1992 if (m->valid != 0 && vm_page_is_valid(m, off & PAGE_MASK, xfsize)) {
1995 VM_OBJECT_WUNLOCK(obj);
1998 } else if (nd != 0) {
2006 * Get the page from backing store.
2010 VM_OBJECT_WUNLOCK(obj);
2011 readahead = sfreadahead * MAXBSIZE;
2014 * Use vn_rdwr() instead of the pager interface for
2015 * the vnode, to allow the read-ahead.
2017 * XXXMAC: Because we don't have fp->f_cred here, we
2018 * pass in NOCRED. This is probably wrong, but is
2019 * consistent with our original implementation.
2021 error = vn_rdwr(UIO_READ, vp, NULL, readahead, trunc_page(off),
2022 UIO_NOCOPY, IO_NODELOCKED | IO_VMIO | ((readahead /
2023 bsize) << IO_SEQSHIFT), td->td_ucred, NOCRED, &resid, td);
2024 SFSTAT_INC(sf_iocnt);
2025 VM_OBJECT_WLOCK(obj);
2027 if (vm_pager_has_page(obj, pindex, NULL, NULL)) {
2028 rv = vm_pager_get_pages(obj, &m, 1, 0);
2029 SFSTAT_INC(sf_iocnt);
2030 m = vm_page_lookup(obj, pindex);
2033 else if (rv != VM_PAGER_OK) {
2042 m->valid = VM_PAGE_BITS_ALL;
2050 } else if (m != NULL) {
2053 vm_page_unwire(m, 0);
2056 * See if anyone else might know about this page. If
2057 * not and it is not valid, then free it.
2059 if (m->wire_count == 0 && m->valid == 0 && !vm_page_busied(m))
2063 KASSERT(error != 0 || (m->wire_count > 0 &&
2064 vm_page_is_valid(m, off & PAGE_MASK, xfsize)),
2065 ("wrong page state m %p off %#jx xfsize %d", m, (uintmax_t)off,
2067 VM_OBJECT_WUNLOCK(obj);
2072 sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res,
2073 struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size,
2079 struct shmfd *shmfd;
2082 vp = *vp_res = NULL;
2084 shmfd = *shmfd_res = NULL;
2088 * The file descriptor must be a regular file and have a
2089 * backing VM object.
2091 if (fp->f_type == DTYPE_VNODE) {
2093 vn_lock(vp, LK_SHARED | LK_RETRY);
2094 if (vp->v_type != VREG) {
2098 *bsize = vp->v_mount->mnt_stat.f_iosize;
2099 error = VOP_GETATTR(vp, &va, td->td_ucred);
2102 *obj_size = va.va_size;
2108 } else if (fp->f_type == DTYPE_SHM) {
2111 obj = shmfd->shm_object;
2112 *obj_size = shmfd->shm_size;
2118 VM_OBJECT_WLOCK(obj);
2119 if ((obj->flags & OBJ_DEAD) != 0) {
2120 VM_OBJECT_WUNLOCK(obj);
2126 * Temporarily increase the backing VM object's reference
2127 * count so that a forced reclamation of its vnode does not
2128 * immediately destroy it.
2130 vm_object_reference_locked(obj);
2131 VM_OBJECT_WUNLOCK(obj);
2143 kern_sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
2146 cap_rights_t rights;
2153 * The socket must be a stream socket and connected.
2155 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SEND),
2159 *so = (*sock_fp)->f_data;
2160 if ((*so)->so_type != SOCK_STREAM)
2162 if (((*so)->so_state & SS_ISCONNECTED) == 0)
2168 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
2169 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
2170 int kflags, struct thread *td)
2172 struct file *sock_fp;
2174 struct vm_object *obj;
2179 struct shmfd *shmfd;
2180 struct sendfile_sync *sfs;
2182 off_t off, xfsize, fsbytes, sbytes, rem, obj_size;
2183 int error, bsize, nd, hdrlen, mnw;
2184 bool inflight_called;
2191 fsbytes = sbytes = 0;
2195 inflight_called = false;
2197 error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize);
2203 error = kern_sendfile_getsock(td, sockfd, &sock_fp, &so);
2208 * Do not wait on memory allocations but return ENOMEM for
2209 * caller to retry later.
2210 * XXX: Experimental.
2212 if (flags & SF_MNOWAIT)
2215 if (flags & SF_SYNC) {
2216 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2217 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2218 cv_init(&sfs->cv, "sendfile");
2222 error = mac_socket_check_send(td->td_ucred, so);
2227 /* If headers are specified copy them into mbufs. */
2228 if (hdr_uio != NULL) {
2229 hdr_uio->uio_td = td;
2230 hdr_uio->uio_rw = UIO_WRITE;
2231 if (hdr_uio->uio_resid > 0) {
2233 * In FBSD < 5.0 the nbytes to send also included
2234 * the header. If compat is specified subtract the
2235 * header size from nbytes.
2237 if (kflags & SFK_COMPAT) {
2238 if (nbytes > hdr_uio->uio_resid)
2239 nbytes -= hdr_uio->uio_resid;
2243 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2246 error = mnw ? EAGAIN : ENOBUFS;
2249 hdrlen = m_length(m, NULL);
2254 * Protect against multiple writers to the socket.
2256 * XXXRW: Historically this has assumed non-interruptibility, so now
2257 * we implement that, but possibly shouldn't.
2259 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2262 * Loop through the pages of the file, starting with the requested
2263 * offset. Get a file page (do I/O if necessary), map the file page
2264 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2266 * This is done in two loops. The inner loop turns as many pages
2267 * as it can, up to available socket buffer space, without blocking
2268 * into mbufs to have it bulk delivered into the socket send buffer.
2269 * The outer loop checks the state and available space of the socket
2270 * and takes care of the overall progress.
2272 for (off = offset; ; ) {
2278 if ((nbytes != 0 && nbytes == fsbytes) ||
2279 (nbytes == 0 && obj_size == fsbytes))
2288 * Check the socket state for ongoing connection,
2289 * no errors and space in socket buffer.
2290 * If space is low allow for the remainder of the
2291 * file to be processed if it fits the socket buffer.
2292 * Otherwise block in waiting for sufficient space
2293 * to proceed, or if the socket is nonblocking, return
2294 * to userland with EAGAIN while reporting how far
2296 * We wait until the socket buffer has significant free
2297 * space to do bulk sends. This makes good use of file
2298 * system read ahead and allows packet segmentation
2299 * offloading hardware to take over lots of work. If
2300 * we were not careful here we would send off only one
2303 SOCKBUF_LOCK(&so->so_snd);
2304 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2305 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2307 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2309 SOCKBUF_UNLOCK(&so->so_snd);
2311 } else if (so->so_error) {
2312 error = so->so_error;
2314 SOCKBUF_UNLOCK(&so->so_snd);
2317 space = sbspace(&so->so_snd);
2320 space < so->so_snd.sb_lowat)) {
2321 if (so->so_state & SS_NBIO) {
2322 SOCKBUF_UNLOCK(&so->so_snd);
2327 * sbwait drops the lock while sleeping.
2328 * When we loop back to retry_space the
2329 * state may have changed and we retest
2332 error = sbwait(&so->so_snd);
2334 * An error from sbwait usually indicates that we've
2335 * been interrupted by a signal. If we've sent anything
2336 * then return bytes sent, otherwise return the error.
2339 SOCKBUF_UNLOCK(&so->so_snd);
2344 SOCKBUF_UNLOCK(&so->so_snd);
2347 * Reduce space in the socket buffer by the size of
2348 * the header mbuf chain.
2349 * hdrlen is set to 0 after the first loop.
2354 error = vn_lock(vp, LK_SHARED);
2357 error = VOP_GETATTR(vp, &va, td->td_ucred);
2358 if (error != 0 || off >= va.va_size) {
2362 obj_size = va.va_size;
2366 * Loop and construct maximum sized mbuf chain to be bulk
2367 * dumped into socket buffer.
2369 while (space > loopbytes) {
2374 * Calculate the amount to transfer.
2375 * Not to exceed a page, the EOF,
2376 * or the passed in nbytes.
2378 pgoff = (vm_offset_t)(off & PAGE_MASK);
2379 rem = obj_size - offset;
2381 rem = omin(rem, nbytes);
2382 rem -= fsbytes + loopbytes;
2383 xfsize = omin(PAGE_SIZE - pgoff, rem);
2384 xfsize = omin(space - loopbytes, xfsize);
2386 done = 1; /* all data sent */
2391 * Attempt to look up the page. Allocate
2392 * if not found or wait and loop if busy.
2395 nd = EAGAIN; /* send what we already got */
2396 else if ((flags & SF_NODISKIO) != 0)
2400 error = sendfile_readpage(obj, vp, nd, off,
2401 xfsize, bsize, td, &pg);
2403 if (error == EAGAIN)
2404 error = 0; /* not a real error */
2409 * Get a sendfile buf. When allocating the
2410 * first buffer for mbuf chain, we usually
2411 * wait as long as necessary, but this wait
2412 * can be interrupted. For consequent
2413 * buffers, do not sleep, since several
2414 * threads might exhaust the buffers and then
2417 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2420 SFSTAT_INC(sf_allocfail);
2422 vm_page_unwire(pg, 0);
2423 KASSERT(pg->object != NULL,
2424 ("%s: object disappeared", __func__));
2427 error = (mnw ? EAGAIN : EINTR);
2432 * Get an mbuf and set it up as having
2435 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2437 error = (mnw ? EAGAIN : ENOBUFS);
2438 (void)sf_buf_mext(NULL, NULL, sf);
2441 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2442 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2443 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2444 error = (mnw ? EAGAIN : ENOBUFS);
2445 (void)sf_buf_mext(NULL, NULL, sf);
2449 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2452 /* Append to mbuf chain. */
2456 m_last(m)->m_next = m0;
2461 /* Keep track of bits processed. */
2462 loopbytes += xfsize;
2466 mtx_lock(&sfs->mtx);
2468 mtx_unlock(&sfs->mtx);
2475 /* Add the buffer chain to the socket buffer. */
2479 mlen = m_length(m, NULL);
2480 SOCKBUF_LOCK(&so->so_snd);
2481 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2483 SOCKBUF_UNLOCK(&so->so_snd);
2486 SOCKBUF_UNLOCK(&so->so_snd);
2487 CURVNET_SET(so->so_vnet);
2488 /* Avoid error aliasing. */
2489 err = (*so->so_proto->pr_usrreqs->pru_send)
2490 (so, 0, m, NULL, NULL, td);
2494 * We need two counters to get the
2495 * file offset and nbytes to send
2497 * - sbytes contains the total amount
2498 * of bytes sent, including headers.
2499 * - fsbytes contains the total amount
2500 * of bytes sent from the file.
2508 } else if (error == 0)
2510 m = NULL; /* pru_send always consumes */
2513 /* Quit outer loop on error or when we're done. */
2521 * Send trailers. Wimp out and use writev(2).
2523 if (trl_uio != NULL) {
2524 sbunlock(&so->so_snd);
2525 error = kern_writev(td, sockfd, trl_uio);
2527 sbytes += td->td_retval[0];
2532 sbunlock(&so->so_snd);
2535 * If there was no error we have to clear td->td_retval[0]
2536 * because it may have been set by writev.
2539 td->td_retval[0] = 0;
2542 copyout(&sbytes, sent, sizeof(off_t));
2545 vm_object_deallocate(obj);
2552 mtx_lock(&sfs->mtx);
2553 if (sfs->count != 0)
2554 cv_wait(&sfs->cv, &sfs->mtx);
2555 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2556 cv_destroy(&sfs->cv);
2557 mtx_destroy(&sfs->mtx);
2561 if (error == ERESTART)