Date: Tue, 11 Aug 2009 11:36:40 GMT From: Zhao Shuai <zhaoshuai@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 167206 for review Message-ID: <200908111136.n7BBaecd004264@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=167206 Change 167206 by zhaoshuai@zhaoshuai on 2009/08/11 11:36:35 integrate with HEAD Affected files ... .. //depot/projects/soc2009/fifo/sys/fs/fifofs/fifo_vnops.c#22 edit .. //depot/projects/soc2009/fifo/sys/kern/sys_pipe.c#20 edit .. //depot/projects/soc2009/fifo/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c#2 integrate .. //depot/projects/soc2009/fifo/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h#2 integrate .. //depot/projects/soc2009/fifo/sys/netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c#2 integrate Differences ... ==== //depot/projects/soc2009/fifo/sys/fs/fifofs/fifo_vnops.c#22 (text+ko) ==== @@ -3,6 +3,9 @@ * The Regents of the University of California. * Copyright (c) 2005 Robert N. M. Watson * All rights reserved. + * + * Copyright (c) 2009 Zhao Shuai <zhaoshuai@FreeBSD.org> + * Google Summer of Code project * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,50 +32,43 @@ * SUCH DAMAGE. * * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95 - * $FreeBSD: src/sys/fs/fifofs/fifo_vnops.c,v 1.152 2009/07/07 09:43:44 kib Exp $ + * $FreeBSD: src/sys/fs/fifofs/fifo_vnops.c, XXX */ #include <sys/param.h> -#include <sys/event.h> -#include <sys/file.h> #include <sys/filedesc.h> #include <sys/filio.h> #include <sys/fcntl.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/mutex.h> -#include <sys/malloc.h> #include <sys/poll.h> -#include <sys/proc.h> -#include <sys/signalvar.h> -#include <sys/socket.h> -#include <sys/socketvar.h> -#include <sys/sx.h> -#include <sys/systm.h> -#include <sys/un.h> +#include <sys/stat.h> #include <sys/unistd.h> #include <sys/vnode.h> +#include <sys/proc.h> +#include <sys/pipe.h> #include <fs/fifofs/fifo.h> -static fo_rdwr_t fifo_read_f; -static fo_rdwr_t fifo_write_f; -static fo_ioctl_t fifo_ioctl_f; -static fo_poll_t fifo_poll_f; -static fo_kqfilter_t fifo_kqfilter_f; -static fo_stat_t fifo_stat_f; -static fo_close_t fifo_close_f; -static fo_truncate_t fifo_truncate_f; +static fo_rdwr_t fifo_read_f; +static fo_rdwr_t fifo_write_f; +static fo_ioctl_t fifo_ioctl_f; +static fo_poll_t fifo_poll_f; +static fo_kqfilter_t fifo_kqfilter_f; +static fo_stat_t fifo_stat_f; +static fo_close_t fifo_close_f; +static fo_truncate_t fifo_truncate_f; struct fileops fifo_ops_f = { - .fo_read = fifo_read_f, - .fo_write = fifo_write_f, - .fo_truncate = fifo_truncate_f, - .fo_ioctl = fifo_ioctl_f, - .fo_poll = fifo_poll_f, - .fo_kqfilter = fifo_kqfilter_f, - .fo_stat = fifo_stat_f, - .fo_close = fifo_close_f, - .fo_flags = DFLAG_PASSABLE + .fo_read = fifo_read_f, + .fo_write = fifo_write_f, + .fo_truncate = fifo_truncate_f, + .fo_ioctl = fifo_ioctl_f, + .fo_poll = fifo_poll_f, + .fo_kqfilter = fifo_kqfilter_f, + .fo_stat = fifo_stat_f, + .fo_close = fifo_close_f, + .fo_flags = DFLAG_PASSABLE }; /* @@ -80,43 +76,42 @@ * the state associated with the FIFO. */ struct fifoinfo { - struct socket *fi_readsock; - struct socket *fi_writesock; + struct pipe *fi_rpipe; + struct pipe *fi_wpipe; long fi_readers; long fi_writers; - int fi_wgen; }; static vop_print_t fifo_print; static vop_open_t fifo_open; static vop_close_t fifo_close; +static vop_ioctl_t fifo_ioctl; +static vop_kqfilter_t fifo_kqfilter; static vop_pathconf_t fifo_pathconf; static vop_advlock_t fifo_advlock; -static void filt_fifordetach(struct knote *kn); +static void filt_fifodetach(struct knote *kn); static int filt_fiforead(struct knote *kn, long hint); -static void filt_fifowdetach(struct knote *kn); static int filt_fifowrite(struct knote *kn, long hint); static void filt_fifodetach_notsup(struct knote *kn); static int filt_fifo_notsup(struct knote *kn, long hint); static struct filterops fiforead_filtops = - { 1, NULL, filt_fifordetach, filt_fiforead }; + { 1, NULL, filt_fifodetach, filt_fiforead }; static struct filterops fifowrite_filtops = - { 1, NULL, filt_fifowdetach, filt_fifowrite }; -static struct filterops fifo_notsup_filtops = + { 1, NULL, filt_fifodetach, filt_fifowrite }; +static struct filterops fifo_notsup_filtops = { 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup }; struct vop_vector fifo_specops = { .vop_default = &default_vnodeops, - .vop_access = VOP_EBADF, .vop_advlock = fifo_advlock, .vop_close = fifo_close, .vop_create = VOP_PANIC, .vop_getattr = VOP_EBADF, - .vop_ioctl = VOP_PANIC, - .vop_kqfilter = VOP_PANIC, + .vop_ioctl = fifo_ioctl, + .vop_kqfilter = fifo_kqfilter, .vop_link = VOP_PANIC, .vop_mkdir = VOP_PANIC, .vop_mknod = VOP_PANIC, @@ -140,23 +135,6 @@ MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF); /* - * Dispose of fifo resources. - */ -static void -fifo_cleanup(struct vnode *vp) -{ - struct fifoinfo *fip = vp->v_fifoinfo; - - ASSERT_VOP_ELOCKED(vp, "fifo_cleanup"); - if (fip->fi_readers == 0 && fip->fi_writers == 0) { - vp->v_fifoinfo = NULL; - (void)soclose(fip->fi_readsock); - (void)soclose(fip->fi_writesock); - free(fip, M_VNODE); - } -} - -/* * Open called to set up a new instance of a fifo or * to find an active instance of a fifo. */ @@ -168,15 +146,14 @@ int a_mode; struct ucred *a_cred; struct thread *a_td; - struct file *a_fp; + int a_fdidx; } */ *ap; { struct vnode *vp = ap->a_vp; + struct file *fp = ap->a_fp; + struct thread *td = ap->a_td; struct fifoinfo *fip; - struct thread *td = ap->a_td; - struct ucred *cred = ap->a_cred; - struct file *fp = ap->a_fp; - struct socket *rso, *wso; + struct pipe *rpipe, *wpipe; int error; ASSERT_VOP_ELOCKED(vp, "fifo_open"); @@ -184,30 +161,15 @@ return (EINVAL); if ((fip = vp->v_fifoinfo) == NULL) { fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK); - error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td); - if (error) - goto fail1; - fip->fi_readsock = rso; - error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td); - if (error) - goto fail2; - fip->fi_writesock = wso; - error = soconnect2(wso, rso); + error = pipepair_create(td, &rpipe, &wpipe); if (error) { - (void)soclose(wso); -fail2: - (void)soclose(rso); -fail1: free(fip, M_VNODE); return (error); } + fip->fi_rpipe = rpipe; + fip->fi_wpipe = wpipe; fip->fi_readers = fip->fi_writers = 0; - wso->so_snd.sb_lowat = PIPE_BUF; - SOCKBUF_LOCK(&rso->so_rcv); - rso->so_rcv.sb_state |= SBS_CANTRCVMORE; - SOCKBUF_UNLOCK(&rso->so_rcv); - KASSERT(vp->v_fifoinfo == NULL, - ("fifo_open: v_fifoinfo race")); + KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race")); vp->v_fifoinfo = fip; } @@ -224,44 +186,34 @@ mtx_lock(&fifo_mtx); if (ap->a_mode & FREAD) { fip->fi_readers++; - if (fip->fi_readers == 1) { - SOCKBUF_LOCK(&fip->fi_writesock->so_snd); - fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE; - SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd); - if (fip->fi_writers > 0) { - wakeup(&fip->fi_writers); - sowwakeup(fip->fi_writesock); - } - } - fp->f_seqcount = fip->fi_wgen - fip->fi_writers; + if (fip->fi_readers == 1 && fip->fi_writers > 0) + wakeup(&fip->fi_writers); } if (ap->a_mode & FWRITE) { if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) { + /* XXX release all resources here? */ mtx_unlock(&fifo_mtx); return (ENXIO); } - fip->fi_writers++; - if (fip->fi_writers == 1) { - SOCKBUF_LOCK(&fip->fi_readsock->so_rcv); - fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE; - SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv); - if (fip->fi_readers > 0) { - wakeup(&fip->fi_readers); - sorwakeup(fip->fi_readsock); - } - } + fip->fi_writers++; + if (fip->fi_writers == 1 && fip->fi_readers > 0) + wakeup(&fip->fi_readers); } if ((ap->a_mode & O_NONBLOCK) == 0) { if ((ap->a_mode & FREAD) && fip->fi_writers == 0) { VOP_UNLOCK(vp, 0); - error = msleep(&fip->fi_readers, &fifo_mtx, - PDROP | PCATCH | PSOCK, "fifoor", 0); + error = msleep(&fip->fi_readers, &fifo_mtx, + PDROP | PCATCH, "fifoor", 0); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if (error) { fip->fi_readers--; if (fip->fi_readers == 0) { - socantsendmore(fip->fi_writesock); - fifo_cleanup(vp); + pipe_close(fip->fi_rpipe); + if (fip->fi_writers == 0) { + pipe_close(fip->fi_wpipe); + vp->v_fifoinfo = NULL; + free(fip, M_VNODE); + } } return (error); } @@ -270,21 +222,22 @@ * We must have got woken up because we had a writer. * That (and not still having one) is the condition * that we must wait for. - */ + */ } if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) { VOP_UNLOCK(vp, 0); error = msleep(&fip->fi_writers, &fifo_mtx, - PDROP | PCATCH | PSOCK, "fifoow", 0); + PDROP | PCATCH, "fifoow", 0); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if (error) { fip->fi_writers--; if (fip->fi_writers == 0) { - socantrcvmore(fip->fi_readsock); - mtx_lock(&fifo_mtx); - fip->fi_wgen++; - mtx_unlock(&fifo_mtx); - fifo_cleanup(vp); + pipe_close(fip->fi_wpipe); + if (fip->fi_readers == 0) { + pipe_close(fip->fi_rpipe); + vp->v_fifoinfo = NULL; + free(fip, M_VNODE); + } } return (error); } @@ -303,60 +256,95 @@ return (0); } +/* + * Now unused vnode ioctl routine. + */ +/* ARGSUSED */ +static int +fifo_ioctl(ap) + struct vop_ioctl_args /* { + struct vnode *a_vp; + u_long a_command; + caddr_t a_data; + int a_fflag; + struct ucred *a_cred; + struct thread *a_td; + } */ *ap; +{ + + printf("WARNING: fifo_ioctl called unexpectedly\n"); + return (ENOTTY); +} + +/* + * Now unused vnode kqfilter routine. + */ +/* ARGSUSED */ +static int +fifo_kqfilter(ap) + struct vop_kqfilter_args /* { + struct vnode *a_vp; + struct knote *a_kn; + } */ *ap; +{ + + printf("WARNING: fifo_kqfilter called unexpectedly\n"); + return (EINVAL); +} + static void -filt_fifordetach(struct knote *kn) +filt_fifodetach(struct knote *kn) { - struct socket *so = (struct socket *)kn->kn_hook; + struct pipe *pipe = (struct pipe *)kn->kn_hook; - SOCKBUF_LOCK(&so->so_rcv); - knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); - if (knlist_empty(&so->so_rcv.sb_sel.si_note)) - so->so_rcv.sb_flags &= ~SB_KNOTE; - SOCKBUF_UNLOCK(&so->so_rcv); + PIPE_LOCK(pipe); + knlist_remove(&pipe->pipe_sel.si_note, kn, 1); + PIPE_UNLOCK(pipe); } static int filt_fiforead(struct knote *kn, long hint) { - struct socket *so = (struct socket *)kn->kn_hook; + struct pipe *rpipe = (struct pipe *)kn->kn_hook; + struct pipe *wpipe = rpipe->pipe_peer; + int ret; + + PIPE_LOCK(rpipe); + kn->kn_data = rpipe->pipe_buffer.cnt; + if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) + kn->kn_data = rpipe->pipe_map.cnt; - SOCKBUF_LOCK_ASSERT(&so->so_rcv); - kn->kn_data = so->so_rcv.sb_cc; - if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { + if ((rpipe->pipe_state & PIPE_EOF) || + wpipe->pipe_present != PIPE_ACTIVE || + (wpipe->pipe_state & PIPE_EOF)) { kn->kn_flags |= EV_EOF; + PIPE_UNLOCK(rpipe); return (1); - } else { - kn->kn_flags &= ~EV_EOF; - return (kn->kn_data > 0); } + ret = kn->kn_data > 0; + PIPE_UNLOCK(rpipe); + return ret; } -static void -filt_fifowdetach(struct knote *kn) -{ - struct socket *so = (struct socket *)kn->kn_hook; - - SOCKBUF_LOCK(&so->so_snd); - knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1); - if (knlist_empty(&so->so_snd.sb_sel.si_note)) - so->so_snd.sb_flags &= ~SB_KNOTE; - SOCKBUF_UNLOCK(&so->so_snd); -} - static int filt_fifowrite(struct knote *kn, long hint) { - struct socket *so = (struct socket *)kn->kn_hook; + struct pipe *wpipe = (struct pipe *)kn->kn_hook; - SOCKBUF_LOCK_ASSERT(&so->so_snd); - kn->kn_data = sbspace(&so->so_snd); - if (so->so_snd.sb_state & SBS_CANTSENDMORE) { + PIPE_LOCK(wpipe); + if (wpipe->pipe_present != PIPE_ACTIVE || + (wpipe->pipe_state & PIPE_EOF)) { + kn->kn_data = 0; kn->kn_flags |= EV_EOF; + PIPE_UNLOCK(wpipe); return (1); - } else { - kn->kn_flags &= ~EV_EOF; - return (kn->kn_data >= so->so_snd.sb_lowat); } + kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; + if (wpipe->pipe_state & PIPE_DIRECTW) + kn->kn_data = 0; + + PIPE_UNLOCK(wpipe); + return (kn->kn_data >= PIPE_BUF); } static void @@ -389,25 +377,20 @@ struct fifoinfo *fip = vp->v_fifoinfo; ASSERT_VOP_ELOCKED(vp, "fifo_close"); - if (fip == NULL) { - printf("fifo_close: no v_fifoinfo %p\n", vp); - return (0); - } if (ap->a_fflag & FREAD) { fip->fi_readers--; - if (fip->fi_readers == 0) - socantsendmore(fip->fi_writesock); + if (fip->fi_readers == 0) + pipe_close(fip->fi_rpipe); } if (ap->a_fflag & FWRITE) { fip->fi_writers--; - if (fip->fi_writers == 0) { - socantrcvmore(fip->fi_readsock); - mtx_lock(&fifo_mtx); - fip->fi_wgen++; - mtx_unlock(&fifo_mtx); - } + if (fip->fi_writers == 0) + pipe_close(fip->fi_wpipe); + } + if (fip->fi_readers == 0 && fip->fi_writers == 0) { + vp->v_fifoinfo = NULL; + free(fip, M_VNODE); } - fifo_cleanup(vp); return (0); } @@ -415,12 +398,11 @@ * Print out internal contents of a fifo vnode. */ int -fifo_printinfo(vp) - struct vnode *vp; +fifo_printinfo(struct vnode *vp) { - register struct fifoinfo *fip = vp->v_fifoinfo; + struct fifoinfo *fip = vp->v_fifoinfo; - if (fip == NULL){ + if (fip == NULL) { printf(", NULL v_fifoinfo"); return (0); } @@ -473,14 +455,13 @@ } /* - * Fifo advisory byte-level locks. + * FIFO advisory byte-level locks. */ -/* ARGSUSED */ static int fifo_advlock(ap) struct vop_advlock_args /* { struct vnode *a_vp; - caddr_t a_id; + caddr_t a_id; int a_op; struct flock *a_fl; int a_flags; @@ -500,62 +481,32 @@ /* * The implementation of ioctl() for named fifos is complicated by the fact * that we permit O_RDWR fifo file descriptors, meaning that the actions of - * ioctls may have to be applied to both the underlying sockets rather than - * just one. The original implementation simply forward the ioctl to one - * or both sockets based on fp->f_flag. We now consider each ioctl - * separately, as the composition effect requires careful ordering. - * - * We do not blindly pass all ioctls through to the socket in order to avoid - * providing unnecessary ioctls that might be improperly depended on by - * applications (such as socket-specific, routing, and interface ioctls). + * ioctls may have to be applied to both the underlying pipes rather than + * just one. * * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and * TIOCGPGRP ioctls. Earlier implementations of fifos did forward SIOCSPGRP * and SIOCGPGRP ioctls, so we might need to re-add those here. */ static int -fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, +fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) { - struct fifoinfo *fi; - struct file filetmp; /* Local, so need not be locked. */ - int error; - - error = ENOTTY; - fi = fp->f_data; + int error = ENOTTY; + struct fifoinfo *fip = fp->f_data; switch (com) { case FIONBIO: - /* - * Non-blocking I/O is implemented at the fifo layer using - * MSG_NBIO, so does not need to be forwarded down the stack. - */ - return (0); - case FIOASYNC: case FIOSETOWN: case FIOGETOWN: - /* - * These socket ioctls don't have any ordering requirements, - * so are called in an arbitrary order, and only on the - * sockets indicated by the file descriptor rights. - * - * XXXRW: If O_RDWR and the read socket accepts an ioctl but - * the write socket doesn't, the socketpair is left in an - * inconsistent state. - */ if (fp->f_flag & FREAD) { - filetmp.f_data = fi->fi_readsock; - filetmp.f_cred = cred; - error = soo_ioctl(&filetmp, com, data, cred, td); + error = pipe_ioctl(fip->fi_rpipe, com, data, cred, td); if (error) return (error); } - if (fp->f_flag & FWRITE) { - filetmp.f_data = fi->fi_writesock; - filetmp.f_cred = cred; - error = soo_ioctl(&filetmp, com, data, cred, td); - } + if (fp->f_flag & FWRITE) + error = pipe_ioctl(fip->fi_wpipe, com, data, cred, td); return (error); case FIONREAD: @@ -568,12 +519,10 @@ *(int *)data = 0; return (0); } - filetmp.f_data = fi->fi_readsock; - filetmp.f_cred = cred; - return (soo_ioctl(&filetmp, com, data, cred, td)); + return (pipe_ioctl(fip->fi_rpipe, com, data, cred, td)); default: - return (ENOTTY); + return (ENOTTY); } } @@ -586,11 +535,8 @@ static int fifo_kqfilter_f(struct file *fp, struct knote *kn) { - struct fifoinfo *fi; - struct socket *so; - struct sockbuf *sb; - - fi = fp->f_data; + struct fifoinfo *fip = fp->f_data; + struct pipe *rpipe = fip->fi_rpipe; /* * If a filter is requested that is not supported by this file @@ -601,7 +547,6 @@ kn->kn_fop = &fifo_notsup_filtops; return (0); } - if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) { kn->kn_fop = &fifo_notsup_filtops; return (0); @@ -610,92 +555,83 @@ switch (kn->kn_filter) { case EVFILT_READ: kn->kn_fop = &fiforead_filtops; - so = fi->fi_readsock; - sb = &so->so_rcv; break; case EVFILT_WRITE: kn->kn_fop = &fifowrite_filtops; - so = fi->fi_writesock; - sb = &so->so_snd; + PIPE_LOCK(rpipe); + if (rpipe->pipe_present != PIPE_ACTIVE) { + /* other end of pipe has been closed */ + PIPE_UNLOCK(rpipe); + return (EPIPE); + } + PIPE_UNLOCK(rpipe); break; default: return (EINVAL); } - kn->kn_hook = (caddr_t)so; + kn->kn_hook = (void *)rpipe; - SOCKBUF_LOCK(sb); - knlist_add(&sb->sb_sel.si_note, kn, 1); - sb->sb_flags |= SB_KNOTE; - SOCKBUF_UNLOCK(sb); + PIPE_LOCK(rpipe); + knlist_add(&rpipe->pipe_sel.si_note, kn, 1); + PIPE_UNLOCK(rpipe); return (0); } -static int +static int fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) { - struct fifoinfo *fip; - struct file filetmp; + struct fifoinfo *fip = fp->f_data; int levents, revents = 0; - fip = fp->f_data; - levents = events & - (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); - if ((fp->f_flag & FREAD) && levents) { - filetmp.f_data = fip->fi_readsock; - filetmp.f_cred = cred; - mtx_lock(&fifo_mtx); - if (fp->f_seqcount == fip->fi_wgen) - levents |= POLLINIGNEOF; - mtx_unlock(&fifo_mtx); - revents |= soo_poll(&filetmp, levents, cred, td); - } - levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND); - if ((fp->f_flag & FWRITE) && levents) { - filetmp.f_data = fip->fi_writesock; - filetmp.f_cred = cred; - revents |= soo_poll(&filetmp, levents, cred, td); - } + levents = events & (POLLIN | POLLRDNORM); + if ((fp->f_flag & FREAD) && levents) + revents |= pipe_poll(fip->fi_rpipe, levents, cred, td); + levents = events & (POLLOUT | POLLWRNORM); + if ((fp->f_flag & FWRITE) && levents) + revents |= pipe_poll(fip->fi_wpipe, levents, cred, td); return (revents); } static int fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) { - struct fifoinfo *fip; - int sflags; + + struct fifoinfo *fip = fp->f_data; - fip = fp->f_data; - KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode")); - if (uio->uio_resid == 0) - return (0); - sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0; - return (soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags)); + /* The 4th argument of pipe_read is file flag */ + return (pipe_read(fip->fi_rpipe, uio, cred, fp->f_flag, td)); } static int fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) { + struct fifoinfo *fip = fp->f_data; + int error; - return (vnops.fo_stat(fp, sb, cred, td)); + error = pipe_stat(fip->fi_rpipe, sb, cred, td); + if (error) + return (error); + /* pipe_stat zeros all fields of *ub, following assignment should be done after it */ + sb->st_uid = fp->f_cred->cr_uid; + sb->st_gid = fp->f_cred->cr_gid; + return (0); } static int fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td) { - + return (vnops.fo_truncate(fp, length, cred, td)); } static int fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) { - struct fifoinfo *fip; - int sflags; + + struct fifoinfo *fip = fp->f_data; - fip = fp->f_data; - KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode")); - sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0; - return (sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td)); + /* The 4th argument of pipe_write is file flag */ + return (pipe_write(fip->fi_wpipe, uio, cred, fp->f_flag, td)); } ==== //depot/projects/soc2009/fifo/sys/kern/sys_pipe.c#20 (text+ko) ==== @@ -1,113 +1,43 @@ /*- - * Copyright (c) 1996 John S. Dyson + * Copyright (c) 2009 Zhao Shuai <zhaoshuai@FreeBSD.org> + * Google Summer of Code project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice immediately at the beginning of the file, without modification, - * this list of conditions, and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Absolutely no warranty of function or purpose is made by the author - * John S. Dyson. - * 4. Modifications may be freely made to this file if the above conditions - * are met. - */ - -/* - * This file contains a high-performance replacement for the socket-based - * pipes scheme originally used in FreeBSD/4.4Lite. It does not support - * all features of sockets, but does do everything that pipes normally - * do. - */ - -/* - * This code has two modes of operation, a small write mode and a large - * write mode. The small write mode acts like conventional pipes with - * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the - * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT - * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and - * the receiving process can copy it directly from the pages in the sending - * process. * - * If the sending process receives a signal, it is possible that it will - * go away, and certainly its address space can change, because control - * is returned back to the user-mode side. In that case, the pipe code - * arranges to copy the buffer supplied by the user process, to a pageable - * kernel buffer, and the receiving process will grab the data from the - * pageable kernel buffer. Since signals don't happen all that often, - * the copy operation is normally eliminated. - * - * The constant PIPE_MINDIRECT is chosen to make sure that buffering will - * happen for small transfers so that the system will not spend all of - * its time context switching. - * - * In order to limit the resource use of pipes, two sysctls exist: - * - * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable - * address space available to us in pipe_map. This value is normally - * autotuned, but may also be loader tuned. - * - * kern.ipc.pipekva - This read-only sysctl tracks the current amount of - * memory in use by pipes. - * - * Based on how large pipekva is relative to maxpipekva, the following - * will happen: - * - * 0% - 50%: - * New pipes are given 16K of memory backing, pipes may dynamically - * grow to as large as 64K where needed. - * 50% - 75%: - * New pipes are given 4K (or PAGE_SIZE) of memory backing, - * existing pipes may NOT grow. - * 75% - 100%: - * New pipes are given 4K (or PAGE_SIZE) of memory backing, - * existing pipes will be shrunk down to 4K whenever possible. - * - * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0. If - * that is set, the only resize that will occur is the 0 -> SMALL_PIPE_SIZE - * resize which MUST occur for reverse-direction pipes when they are - * first used. - * - * Additional information about the current state of pipes may be obtained - * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail, - * and kern.ipc.piperesizefail. - * - * Locking rules: There are two locks present here: A mutex, used via - * PIPE_LOCK, and a flag, used via pipelock(). All locking is done via - * the flag, as mutexes can not persist over uiomove. The mutex - * exists only to guard access to the flag, and is not in itself a - * locking mechanism. Also note that there is only a single mutex for - * both directions of a pipe. - * - * As pipelock() may have to sleep before it can acquire the flag, it - * is important to reread all data after a call to pipelock(); everything - * in the structure may have changed. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ #include <sys/cdefs.h> -__FBSDID("$FreeBSD: src/sys/kern/sys_pipe.c,v 1.205 2009/07/07 09:43:44 kib Exp $"); +__FBSDID("$FreeBSD"); #include <sys/param.h> #include <sys/systm.h> #include <sys/fcntl.h> -#include <sys/file.h> #include <sys/filedesc.h> #include <sys/filio.h> #include <sys/kernel.h> -#include <sys/lock.h> -#include <sys/mutex.h> -#include <sys/ttycom.h> #include <sys/stat.h> #include <sys/malloc.h> -#include <sys/poll.h> #include <sys/selinfo.h> -#include <sys/signalvar.h> #include <sys/syscallsubr.h> -#include <sys/sysctl.h> #include <sys/sysproto.h> #include <sys/pipe.h> #include <sys/proc.h> @@ -115,237 +45,50 @@ #include <sys/uio.h> #include <sys/event.h> -#include <security/mac/mac_framework.h> - -#include <vm/vm.h> -#include <vm/vm_param.h> -#include <vm/vm_object.h> -#include <vm/vm_kern.h> -#include <vm/vm_extern.h> -#include <vm/pmap.h> -#include <vm/vm_map.h> -#include <vm/vm_page.h> -#include <vm/uma.h> - -/* - * Use this define if you want to disable *fancy* VM things. Expect an - * approx 30% decrease in transfer rate. This could be useful for - * NetBSD or OpenBSD. - */ -/* #define PIPE_NODIRECT */ - /* * interfaces to the outside world */ -static fo_rdwr_t pipe_read; -static fo_rdwr_t pipe_write; -static fo_truncate_t pipe_truncate; -static fo_ioctl_t pipe_ioctl; -static fo_poll_t pipe_poll; -static fo_kqfilter_t pipe_kqfilter; -static fo_stat_t pipe_stat; -static fo_close_t pipe_close; +static fo_rdwr_t pipe_read_f; +static fo_rdwr_t pipe_write_f; +static fo_truncate_t pipe_truncate_f; +static fo_ioctl_t pipe_ioctl_f; +static fo_poll_t pipe_poll_f; +static fo_kqfilter_t pipe_kqfilter_f; +static fo_stat_t pipe_stat_f; +static fo_close_t pipe_close_f; static struct fileops pipeops = { - .fo_read = pipe_read, - .fo_write = pipe_write, - .fo_truncate = pipe_truncate, - .fo_ioctl = pipe_ioctl, - .fo_poll = pipe_poll, - .fo_kqfilter = pipe_kqfilter, - .fo_stat = pipe_stat, - .fo_close = pipe_close, - .fo_flags = DFLAG_PASSABLE + .fo_read = pipe_read_f, + .fo_write = pipe_write_f, + .fo_truncate = pipe_truncate_f, + .fo_ioctl = pipe_ioctl_f, + .fo_poll = pipe_poll_f, + .fo_kqfilter = pipe_kqfilter_f, + .fo_stat = pipe_stat_f, + .fo_close = pipe_close_f, + .fo_flags = DFLAG_PASSABLE }; -static void filt_pipedetach(struct knote *kn); -static int filt_piperead(struct knote *kn, long hint); -static int filt_pipewrite(struct knote *kn, long hint); - -static struct filterops pipe_rfiltops = - { 1, NULL, filt_pipedetach, filt_piperead }; -static struct filterops pipe_wfiltops = - { 1, NULL, filt_pipedetach, filt_pipewrite }; - /* - * Default pipe buffer size(s), this can be kind-of large now because pipe - * space is pageable. The pipe code will try to maintain locality of - * reference for performance reasons, so small amounts of outstanding I/O - * will not wipe the cache. - */ -#define MINPIPESIZE (PIPE_SIZE/3) -#define MAXPIPESIZE (2*PIPE_SIZE/3) - -static long amountpipekva; -static int pipefragretry; -static int pipeallocfail; -static int piperesizefail; -static int piperesizeallowed = 1; - -SYSCTL_LONG(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN, - &maxpipekva, 0, "Pipe KVA limit"); -SYSCTL_LONG(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD, - &amountpipekva, 0, "Pipe KVA usage"); -SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD, - &pipefragretry, 0, "Pipe allocation retries due to fragmentation"); -SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD, - &pipeallocfail, 0, "Pipe allocation failures"); -SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD, - &piperesizefail, 0, "Pipe resize failures"); -SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW, - &piperesizeallowed, 0, "Pipe resizing allowed"); - -static void pipeinit(void *dummy __unused); -static void pipeclose(struct pipe *cpipe); -static void pipe_free_kmem(struct pipe *cpipe); -static int pipe_create(struct pipe *pipe, int backing); -static __inline int pipelock(struct pipe *cpipe, int catch); -static __inline void pipeunlock(struct pipe *cpipe); -static __inline void pipeselwakeup(struct pipe *cpipe); -#ifndef PIPE_NODIRECT -static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio); -static void pipe_destroy_write_buffer(struct pipe *wpipe); -static int pipe_direct_write(struct pipe *wpipe, struct uio *uio); -static void pipe_clone_write_buffer(struct pipe *wpipe); -#endif >>> TRUNCATED FOR MAIL (1000 lines) <<<
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200908111136.n7BBaecd004264>