| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1990, 1993, 1995 | |
| 3 | * The Regents of the University of California. All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * 3. All advertising materials mentioning features or use of this software | |
| 14 | * must display the following acknowledgement: | |
| 15 | * This product includes software developed by the University of | |
| 16 | * California, Berkeley and its contributors. | |
| 17 | * 4. Neither the name of the University nor the names of its contributors | |
| 18 | * may be used to endorse or promote products derived from this software | |
| 19 | * without specific prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 31 | * SUCH DAMAGE. | |
| 984263bc | 32 | */ |
| aac0aabd MD |
33 | /* |
| 34 | * Filesystem FIFO type ops. All entry points are MPSAFE. We primarily | |
| 35 | * use v_token to interlock operations. | |
| 36 | */ | |
| 984263bc MD |
37 | #include <sys/param.h> |
| 38 | #include <sys/systm.h> | |
| 39 | #include <sys/unistd.h> | |
| 40 | #include <sys/kernel.h> | |
| 41 | #include <sys/lock.h> | |
| 42 | #include <sys/malloc.h> | |
| 87de5057 | 43 | #include <sys/thread2.h> |
| 984263bc MD |
44 | #include <sys/vnode.h> |
| 45 | #include <sys/socket.h> | |
| 46 | #include <sys/socketvar.h> | |
| 47 | #include <sys/filio.h> | |
| 48 | #include <sys/fcntl.h> | |
| 49 | #include <sys/file.h> | |
| 50 | #include <sys/event.h> | |
| 51 | #include <sys/poll.h> | |
| 52 | #include <sys/un.h> | |
| 87de5057 MD |
53 | |
| 54 | #include <sys/thread2.h> | |
| 55 | ||
| 1f2de5d4 | 56 | #include "fifo.h" |
| 984263bc MD |
57 | |
| 58 | /* | |
| 59 | * This structure is associated with the FIFO vnode and stores | |
| 60 | * the state associated with the FIFO. | |
| 61 | */ | |
| 62 | struct fifoinfo { | |
| 63 | struct socket *fi_readsock; | |
| 64 | struct socket *fi_writesock; | |
| 65 | long fi_readers; | |
| 66 | long fi_writers; | |
| 67 | }; | |
| 68 | ||
| a6ee311a RG |
69 | static int fifo_badop (void); |
| 70 | static int fifo_print (struct vop_print_args *); | |
| e62afb5f | 71 | static int fifo_lookup (struct vop_old_lookup_args *); |
| a6ee311a RG |
72 | static int fifo_open (struct vop_open_args *); |
| 73 | static int fifo_close (struct vop_close_args *); | |
| 74 | static int fifo_read (struct vop_read_args *); | |
| 75 | static int fifo_write (struct vop_write_args *); | |
| 76 | static int fifo_ioctl (struct vop_ioctl_args *); | |
| 77 | static int fifo_poll (struct vop_poll_args *); | |
| 78 | static int fifo_kqfilter (struct vop_kqfilter_args *); | |
| 79 | static int fifo_inactive (struct vop_inactive_args *); | |
| 80 | static int fifo_bmap (struct vop_bmap_args *); | |
| 81 | static int fifo_pathconf (struct vop_pathconf_args *); | |
| 82 | static int fifo_advlock (struct vop_advlock_args *); | |
| 984263bc MD |
83 | |
| 84 | static void filt_fifordetach(struct knote *kn); | |
| 85 | static int filt_fiforead(struct knote *kn, long hint); | |
| 86 | static void filt_fifowdetach(struct knote *kn); | |
| 87 | static int filt_fifowrite(struct knote *kn, long hint); | |
| 88 | ||
| 89 | static struct filterops fiforead_filtops = | |
| 90 | { 1, NULL, filt_fifordetach, filt_fiforead }; | |
| 91 | static struct filterops fifowrite_filtops = | |
| 92 | { 1, NULL, filt_fifowdetach, filt_fifowrite }; | |
| 93 | ||
| 66a1ddf5 MD |
94 | struct vop_ops fifo_vnode_vops = { |
| 95 | .vop_default = vop_defaultop, | |
| 96 | .vop_access = (void *)vop_ebadf, | |
| 97 | .vop_advlock = fifo_advlock, | |
| 98 | .vop_bmap = fifo_bmap, | |
| 99 | .vop_close = fifo_close, | |
| 100 | .vop_old_create = (void *)fifo_badop, | |
| 101 | .vop_getattr = (void *)vop_ebadf, | |
| 102 | .vop_inactive = fifo_inactive, | |
| 103 | .vop_ioctl = fifo_ioctl, | |
| 104 | .vop_kqfilter = fifo_kqfilter, | |
| 105 | .vop_old_link = (void *)fifo_badop, | |
| 106 | .vop_old_lookup = fifo_lookup, | |
| 107 | .vop_old_mkdir = (void *)fifo_badop, | |
| 108 | .vop_old_mknod = (void *)fifo_badop, | |
| 109 | .vop_open = fifo_open, | |
| 110 | .vop_pathconf = fifo_pathconf, | |
| 111 | .vop_poll = fifo_poll, | |
| 112 | .vop_print = fifo_print, | |
| 113 | .vop_read = fifo_read, | |
| 114 | .vop_readdir = (void *)fifo_badop, | |
| 115 | .vop_readlink = (void *)fifo_badop, | |
| 116 | .vop_reallocblks = (void *)fifo_badop, | |
| 117 | .vop_reclaim = (void *)vop_null, | |
| 118 | .vop_old_remove = (void *)fifo_badop, | |
| 119 | .vop_old_rename = (void *)fifo_badop, | |
| 120 | .vop_old_rmdir = (void *)fifo_badop, | |
| 121 | .vop_setattr = (void *)vop_ebadf, | |
| 122 | .vop_old_symlink = (void *)fifo_badop, | |
| 123 | .vop_write = fifo_write | |
| 984263bc | 124 | }; |
| 984263bc | 125 | |
| 66a1ddf5 | 126 | VNODEOP_SET(fifo_vnode_vops); |
| 984263bc | 127 | |
| 5fd012e0 MD |
128 | static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries"); |
| 129 | ||
| b5f12e21 | 130 | /* |
| 31bd717a | 131 | * fifo_vnoperate() |
| b5f12e21 | 132 | */ |
| 984263bc | 133 | int |
| b5f12e21 | 134 | fifo_vnoperate(struct vop_generic_args *ap) |
| 984263bc | 135 | { |
| 66a1ddf5 | 136 | return (VOCALL(&fifo_vnode_vops, ap)); |
| 984263bc MD |
137 | } |
| 138 | ||
| 139 | /* | |
| 140 | * Trivial lookup routine that always fails. | |
| b5f12e21 CP |
141 | * |
| 142 | * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp, | |
| 143 | * struct componentname *a_cnp) | |
| 984263bc MD |
144 | */ |
| 145 | /* ARGSUSED */ | |
| 146 | static int | |
| e62afb5f | 147 | fifo_lookup(struct vop_old_lookup_args *ap) |
| 984263bc | 148 | { |
| 984263bc MD |
149 | *ap->a_vpp = NULL; |
| 150 | return (ENOTDIR); | |
| 151 | } | |
| 152 | ||
| 153 | /* | |
| 154 | * Open called to set up a new instance of a fifo or | |
| 155 | * to find an active instance of a fifo. | |
| b5f12e21 CP |
156 | * |
| 157 | * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, | |
| b478fdce | 158 | * struct file *a_fp) |
| 984263bc MD |
159 | */ |
| 160 | /* ARGSUSED */ | |
| 161 | static int | |
| b5f12e21 | 162 | fifo_open(struct vop_open_args *ap) |
| 984263bc | 163 | { |
| 87de5057 | 164 | struct thread *td = curthread; |
| 984263bc MD |
165 | struct vnode *vp = ap->a_vp; |
| 166 | struct fifoinfo *fip; | |
| 984263bc MD |
167 | struct socket *rso, *wso; |
| 168 | int error; | |
| 169 | ||
| 3b998fa9 | 170 | lwkt_gettoken(&vp->v_token); |
| 984263bc | 171 | if ((fip = vp->v_fifoinfo) == NULL) { |
| 5fd012e0 | 172 | MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_FIFOINFO, M_WAITOK); |
| 984263bc | 173 | vp->v_fifoinfo = fip; |
| 87de5057 | 174 | error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td); |
| 984263bc | 175 | if (error) { |
| efda3bd0 | 176 | kfree(fip, M_FIFOINFO); |
| 984263bc | 177 | vp->v_fifoinfo = NULL; |
| aac0aabd | 178 | goto done; |
| 984263bc MD |
179 | } |
| 180 | fip->fi_readsock = rso; | |
| 87de5057 | 181 | error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td); |
| 984263bc | 182 | if (error) { |
| 9ba76b73 | 183 | soclose(rso, FNONBLOCK); |
| efda3bd0 | 184 | kfree(fip, M_FIFOINFO); |
| 984263bc | 185 | vp->v_fifoinfo = NULL; |
| aac0aabd | 186 | goto done; |
| 984263bc MD |
187 | } |
| 188 | fip->fi_writesock = wso; | |
| 189 | error = unp_connect2(wso, rso); | |
| 190 | if (error) { | |
| 9ba76b73 MD |
191 | soclose(wso, FNONBLOCK); |
| 192 | soclose(rso, FNONBLOCK); | |
| efda3bd0 | 193 | kfree(fip, M_FIFOINFO); |
| 984263bc | 194 | vp->v_fifoinfo = NULL; |
| aac0aabd | 195 | goto done; |
| 984263bc MD |
196 | } |
| 197 | fip->fi_readers = fip->fi_writers = 0; | |
| 6d49aa6f | 198 | wso->so_snd.ssb_lowat = PIPE_BUF; |
| 984263bc MD |
199 | rso->so_state |= SS_CANTRCVMORE; |
| 200 | } | |
| 201 | if (ap->a_mode & FREAD) { | |
| 202 | fip->fi_readers++; | |
| 203 | if (fip->fi_readers == 1) { | |
| 204 | fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; | |
| 205 | if (fip->fi_writers > 0) { | |
| 206 | wakeup((caddr_t)&fip->fi_writers); | |
| 207 | sowwakeup(fip->fi_writesock); | |
| 208 | } | |
| 209 | } | |
| 210 | } | |
| 211 | if (ap->a_mode & FWRITE) { | |
| 212 | fip->fi_writers++; | |
| 213 | if (fip->fi_writers == 1) { | |
| 214 | fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; | |
| 215 | if (fip->fi_readers > 0) { | |
| 216 | wakeup((caddr_t)&fip->fi_readers); | |
| 217 | sorwakeup(fip->fi_writesock); | |
| 218 | } | |
| 219 | } | |
| 220 | } | |
| 221 | if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { | |
| 222 | if (fip->fi_writers == 0) { | |
| a11aaa81 | 223 | vn_unlock(vp); |
| 984263bc | 224 | error = tsleep((caddr_t)&fip->fi_readers, |
| 377d4740 | 225 | PCATCH, "fifoor", 0); |
| ca466bae | 226 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 984263bc MD |
227 | if (error) |
| 228 | goto bad; | |
| 229 | /* | |
| 230 | * We must have got woken up because we had a writer. | |
| 231 | * That (and not still having one) is the condition | |
| 232 | * that we must wait for. | |
| 233 | */ | |
| 234 | } | |
| 235 | } | |
| 236 | if (ap->a_mode & FWRITE) { | |
| 237 | if (ap->a_mode & O_NONBLOCK) { | |
| 238 | if (fip->fi_readers == 0) { | |
| 239 | error = ENXIO; | |
| 240 | goto bad; | |
| 241 | } | |
| 242 | } else { | |
| 243 | if (fip->fi_readers == 0) { | |
| a11aaa81 | 244 | vn_unlock(vp); |
| 984263bc | 245 | error = tsleep((caddr_t)&fip->fi_writers, |
| 377d4740 | 246 | PCATCH, "fifoow", 0); |
| ca466bae | 247 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 984263bc MD |
248 | if (error) |
| 249 | goto bad; | |
| 250 | /* | |
| 251 | * We must have got woken up because we had | |
| 252 | * a reader. That (and not still having one) | |
| 253 | * is the condition that we must wait for. | |
| 254 | */ | |
| 255 | } | |
| 256 | } | |
| 257 | } | |
| 2247fe02 | 258 | vsetflags(vp, VNOTSEEKABLE); |
| aac0aabd | 259 | error = vop_stdopen(ap); |
| 3b998fa9 | 260 | lwkt_reltoken(&vp->v_token); |
| aac0aabd | 261 | return (error); |
| 984263bc | 262 | bad: |
| cbeb1f72 | 263 | vop_stdopen(ap); /* bump opencount/writecount as appropriate */ |
| 87de5057 | 264 | VOP_CLOSE(vp, ap->a_mode); |
| aac0aabd | 265 | done: |
| 3b998fa9 | 266 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
267 | return (error); |
| 268 | } | |
| 269 | ||
| 270 | /* | |
| 271 | * Vnode op for read | |
| b5f12e21 CP |
272 | * |
| 273 | * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
| 274 | * struct ucred *a_cred) | |
| 984263bc MD |
275 | */ |
| 276 | /* ARGSUSED */ | |
| 277 | static int | |
| b5f12e21 | 278 | fifo_read(struct vop_read_args *ap) |
| 984263bc MD |
279 | { |
| 280 | struct uio *uio = ap->a_uio; | |
| 3b998fa9 MD |
281 | struct vnode *vp = ap->a_vp; |
| 282 | struct socket *rso = vp->v_fifoinfo->fi_readsock; | |
| 984263bc | 283 | int error, startresid; |
| 9ba76b73 | 284 | int flags; |
| 984263bc MD |
285 | |
| 286 | #ifdef DIAGNOSTIC | |
| 287 | if (uio->uio_rw != UIO_READ) | |
| 288 | panic("fifo_read mode"); | |
| 289 | #endif | |
| 290 | if (uio->uio_resid == 0) | |
| 291 | return (0); | |
| 292 | if (ap->a_ioflag & IO_NDELAY) | |
| 9ba76b73 MD |
293 | flags = MSG_FNONBLOCKING; |
| 294 | else | |
| 295 | flags = 0; | |
| 984263bc | 296 | startresid = uio->uio_resid; |
| 3b998fa9 MD |
297 | vn_unlock(vp); |
| 298 | lwkt_gettoken(&vp->v_token); | |
| d8a9a23b | 299 | error = soreceive(rso, NULL, uio, NULL, NULL, &flags); |
| 3b998fa9 MD |
300 | lwkt_reltoken(&vp->v_token); |
| 301 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); | |
| 984263bc MD |
302 | return (error); |
| 303 | } | |
| 304 | ||
| 305 | /* | |
| 306 | * Vnode op for write | |
| b5f12e21 CP |
307 | * |
| 308 | * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
| 309 | * struct ucred *a_cred) | |
| 984263bc MD |
310 | */ |
| 311 | /* ARGSUSED */ | |
| 312 | static int | |
| b5f12e21 | 313 | fifo_write(struct vop_write_args *ap) |
| 984263bc | 314 | { |
| dadab5e9 | 315 | struct thread *td = ap->a_uio->uio_td; |
| 3b998fa9 MD |
316 | struct vnode *vp = ap->a_vp; |
| 317 | struct socket *wso = vp->v_fifoinfo->fi_writesock; | |
| 984263bc | 318 | int error; |
| 9ba76b73 | 319 | int flags; |
| 984263bc MD |
320 | |
| 321 | #ifdef DIAGNOSTIC | |
| 322 | if (ap->a_uio->uio_rw != UIO_WRITE) | |
| 323 | panic("fifo_write mode"); | |
| 324 | #endif | |
| 325 | if (ap->a_ioflag & IO_NDELAY) | |
| 9ba76b73 MD |
326 | flags = MSG_FNONBLOCKING; |
| 327 | else | |
| 328 | flags = 0; | |
| 3b998fa9 MD |
329 | vn_unlock(vp); |
| 330 | lwkt_gettoken(&vp->v_token); | |
| 60233e58 | 331 | error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td); |
| 3b998fa9 MD |
332 | lwkt_reltoken(&vp->v_token); |
| 333 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); | |
| 984263bc MD |
334 | return (error); |
| 335 | } | |
| 336 | ||
| 337 | /* | |
| 338 | * Device ioctl operation. | |
| b5f12e21 CP |
339 | * |
| 340 | * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag, | |
| 87baaf0c | 341 | * struct ucred *a_cred, struct sysmsg *a_sysmsg) |
| 984263bc MD |
342 | */ |
| 343 | /* ARGSUSED */ | |
| 344 | static int | |
| b5f12e21 | 345 | fifo_ioctl(struct vop_ioctl_args *ap) |
| 984263bc | 346 | { |
| 9443ca22 | 347 | struct file filetmp; /* Local */ |
| 3b998fa9 | 348 | struct vnode *vp = ap->a_vp; |
| 984263bc MD |
349 | int error; |
| 350 | ||
| 984263bc | 351 | if (ap->a_fflag & FREAD) { |
| 3b998fa9 MD |
352 | filetmp.f_data = vp->v_fifoinfo->fi_readsock; |
| 353 | lwkt_gettoken(&vp->v_token); | |
| 87baaf0c MD |
354 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, |
| 355 | ap->a_cred, ap->a_sysmsg); | |
| 3b998fa9 | 356 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
357 | if (error) |
| 358 | return (error); | |
| 359 | } | |
| 360 | if (ap->a_fflag & FWRITE) { | |
| 3b998fa9 MD |
361 | filetmp.f_data = vp->v_fifoinfo->fi_writesock; |
| 362 | lwkt_gettoken(&vp->v_token); | |
| 87baaf0c MD |
363 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, |
| 364 | ap->a_cred, ap->a_sysmsg); | |
| 3b998fa9 | 365 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
366 | if (error) |
| 367 | return (error); | |
| 368 | } | |
| 369 | return (0); | |
| 370 | } | |
| 371 | ||
| b5f12e21 CP |
372 | /* |
| 373 | * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn) | |
| 374 | */ | |
| 984263bc MD |
375 | /* ARGSUSED */ |
| 376 | static int | |
| b5f12e21 | 377 | fifo_kqfilter(struct vop_kqfilter_args *ap) |
| 984263bc | 378 | { |
| aac0aabd MD |
379 | struct vnode *vp = ap->a_vp; |
| 380 | struct fifoinfo *fi = vp->v_fifoinfo; | |
| 984263bc | 381 | struct socket *so; |
| 6d49aa6f | 382 | struct signalsockbuf *ssb; |
| 984263bc | 383 | |
| 3b998fa9 | 384 | lwkt_gettoken(&vp->v_token); |
| b58e725b | 385 | |
| 984263bc MD |
386 | switch (ap->a_kn->kn_filter) { |
| 387 | case EVFILT_READ: | |
| 388 | ap->a_kn->kn_fop = &fiforead_filtops; | |
| 389 | so = fi->fi_readsock; | |
| 6d49aa6f | 390 | ssb = &so->so_rcv; |
| 984263bc MD |
391 | break; |
| 392 | case EVFILT_WRITE: | |
| 393 | ap->a_kn->kn_fop = &fifowrite_filtops; | |
| 394 | so = fi->fi_writesock; | |
| 6d49aa6f | 395 | ssb = &so->so_snd; |
| 984263bc MD |
396 | break; |
| 397 | default: | |
| 3b998fa9 | 398 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
399 | return (1); |
| 400 | } | |
| 401 | ||
| aac0aabd | 402 | ap->a_kn->kn_hook = (caddr_t)vp; |
| 6d49aa6f | 403 | ssb_insert_knote(ssb, ap->a_kn); |
| 984263bc | 404 | |
| 3b998fa9 | 405 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
406 | return (0); |
| 407 | } | |
| 408 | ||
| 409 | static void | |
| 410 | filt_fifordetach(struct knote *kn) | |
| 411 | { | |
| aac0aabd MD |
412 | struct vnode *vp = (void *)kn->kn_hook; |
| 413 | struct socket *so = vp->v_fifoinfo->fi_readsock; | |
| 984263bc | 414 | |
| 3b998fa9 | 415 | lwkt_gettoken(&vp->v_token); |
| 6d49aa6f | 416 | ssb_remove_knote(&so->so_rcv, kn); |
| 3b998fa9 | 417 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
418 | } |
| 419 | ||
| 420 | static int | |
| 421 | filt_fiforead(struct knote *kn, long hint) | |
| 422 | { | |
| aac0aabd | 423 | struct vnode *vp = (void *)kn->kn_hook; |
| 7a5447ef | 424 | struct socket *so = vp->v_fifoinfo->fi_readsock; |
| 984263bc | 425 | |
| 3b998fa9 | 426 | lwkt_gettoken(&vp->v_token); |
| 6d49aa6f | 427 | kn->kn_data = so->so_rcv.ssb_cc; |
| 984263bc MD |
428 | if (so->so_state & SS_CANTRCVMORE) { |
| 429 | kn->kn_flags |= EV_EOF; | |
| 3b998fa9 | 430 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
431 | return (1); |
| 432 | } | |
| 433 | kn->kn_flags &= ~EV_EOF; | |
| 3b998fa9 | 434 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
435 | return (kn->kn_data > 0); |
| 436 | } | |
| 437 | ||
| 438 | static void | |
| 439 | filt_fifowdetach(struct knote *kn) | |
| 440 | { | |
| aac0aabd MD |
441 | struct vnode *vp = (void *)kn->kn_hook; |
| 442 | struct socket *so = vp->v_fifoinfo->fi_writesock; | |
| 984263bc | 443 | |
| 3b998fa9 | 444 | lwkt_gettoken(&vp->v_token); |
| 6d49aa6f | 445 | ssb_remove_knote(&so->so_snd, kn); |
| 3b998fa9 | 446 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
447 | } |
| 448 | ||
| 449 | static int | |
| 450 | filt_fifowrite(struct knote *kn, long hint) | |
| 451 | { | |
| aac0aabd MD |
452 | struct vnode *vp = (void *)kn->kn_hook; |
| 453 | struct socket *so = vp->v_fifoinfo->fi_writesock; | |
| 984263bc | 454 | |
| 3b998fa9 | 455 | lwkt_gettoken(&vp->v_token); |
| 6d49aa6f | 456 | kn->kn_data = ssb_space(&so->so_snd); |
| 984263bc MD |
457 | if (so->so_state & SS_CANTSENDMORE) { |
| 458 | kn->kn_flags |= EV_EOF; | |
| 3b998fa9 | 459 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
460 | return (1); |
| 461 | } | |
| 462 | kn->kn_flags &= ~EV_EOF; | |
| 3b998fa9 | 463 | lwkt_reltoken(&vp->v_token); |
| 6d49aa6f | 464 | return (kn->kn_data >= so->so_snd.ssb_lowat); |
| 984263bc MD |
465 | } |
| 466 | ||
| b5f12e21 | 467 | /* |
| b478fdce | 468 | * fifo_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred) |
| b5f12e21 | 469 | */ |
| 984263bc MD |
470 | /* ARGSUSED */ |
| 471 | static int | |
| b5f12e21 | 472 | fifo_poll(struct vop_poll_args *ap) |
| 984263bc | 473 | { |
| 3b998fa9 | 474 | struct vnode *vp = ap->a_vp; |
| 984263bc | 475 | struct file filetmp; |
| d08a3c4d HP |
476 | int events, revents = 0; |
| 477 | ||
| 3b998fa9 | 478 | lwkt_gettoken(&vp->v_token); |
| 630a348c | 479 | events = ap->a_events & |
| 9443ca22 | 480 | (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); |
| d08a3c4d HP |
481 | if (events) { |
| 482 | /* | |
| 9443ca22 HP |
483 | * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is |
| 484 | * not, then convert the first two to the last one. This | |
| 485 | * tells the socket poll function to ignore EOF so that we | |
| 486 | * block if there is no writer (and no data). Callers can | |
| 487 | * set POLLINIGNEOF to get non-blocking behavior. | |
| d08a3c4d | 488 | */ |
| ad4bd3a5 HP |
489 | if (events & (POLLIN | POLLRDNORM) && |
| 490 | !(events & POLLINIGNEOF)) { | |
| d08a3c4d HP |
491 | events &= ~(POLLIN | POLLRDNORM); |
| 492 | events |= POLLINIGNEOF; | |
| 493 | } | |
| 9443ca22 | 494 | |
| 3b998fa9 | 495 | filetmp.f_data = vp->v_fifoinfo->fi_readsock; |
| 984263bc | 496 | if (filetmp.f_data) |
| 87de5057 | 497 | revents |= soo_poll(&filetmp, events, ap->a_cred); |
| d08a3c4d | 498 | |
| 9443ca22 HP |
499 | /* Reverse the above conversion. */ |
| 500 | if ((revents & POLLINIGNEOF) && | |
| 501 | !(ap->a_events & POLLINIGNEOF)) { | |
| 502 | revents |= (ap->a_events & (POLLIN | POLLRDNORM)); | |
| d08a3c4d | 503 | revents &= ~POLLINIGNEOF; |
| d08a3c4d | 504 | } |
| 984263bc | 505 | } |
| d08a3c4d HP |
506 | events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND); |
| 507 | if (events) { | |
| 3b998fa9 | 508 | filetmp.f_data = vp->v_fifoinfo->fi_writesock; |
| 984263bc | 509 | if (filetmp.f_data) |
| 87de5057 | 510 | revents |= soo_poll(&filetmp, events, ap->a_cred); |
| 984263bc | 511 | } |
| 3b998fa9 | 512 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
513 | return (revents); |
| 514 | } | |
| 515 | ||
| b5f12e21 | 516 | /* |
| b478fdce | 517 | * fifo_inactive(struct vnode *a_vp) |
| b5f12e21 | 518 | */ |
| 984263bc | 519 | static int |
| b5f12e21 | 520 | fifo_inactive(struct vop_inactive_args *ap) |
| 984263bc | 521 | { |
| 984263bc MD |
522 | return (0); |
| 523 | } | |
| 524 | ||
| 525 | /* | |
| 526 | * This is a noop, simply returning what one has been given. | |
| b5f12e21 | 527 | * |
| 08daea96 | 528 | * fifo_bmap(struct vnode *a_vp, off_t a_loffset, |
| 54078292 | 529 | * off_t *a_doffsetp, int *a_runp, int *a_runb) |
| 984263bc MD |
530 | */ |
| 531 | static int | |
| b5f12e21 | 532 | fifo_bmap(struct vop_bmap_args *ap) |
| 984263bc | 533 | { |
| 54078292 MD |
534 | if (ap->a_doffsetp != NULL) |
| 535 | *ap->a_doffsetp = ap->a_loffset; | |
| 984263bc MD |
536 | if (ap->a_runp != NULL) |
| 537 | *ap->a_runp = 0; | |
| 538 | if (ap->a_runb != NULL) | |
| 539 | *ap->a_runb = 0; | |
| 540 | return (0); | |
| 541 | } | |
| 542 | ||
| 543 | /* | |
| 544 | * Device close routine | |
| b5f12e21 | 545 | * |
| b478fdce | 546 | * fifo_close(struct vnode *a_vp, int a_fflag) |
| 984263bc MD |
547 | */ |
| 548 | /* ARGSUSED */ | |
| 549 | static int | |
| b5f12e21 | 550 | fifo_close(struct vop_close_args *ap) |
| 984263bc | 551 | { |
| 0e99e805 | 552 | struct vnode *vp = ap->a_vp; |
| aac0aabd | 553 | struct fifoinfo *fip; |
| 984263bc MD |
554 | int error1, error2; |
| 555 | ||
| 3b998fa9 | 556 | lwkt_gettoken(&vp->v_token); |
| aac0aabd | 557 | fip = vp->v_fifoinfo; |
| 984263bc MD |
558 | if (ap->a_fflag & FREAD) { |
| 559 | fip->fi_readers--; | |
| 560 | if (fip->fi_readers == 0) | |
| 561 | socantsendmore(fip->fi_writesock); | |
| 562 | } | |
| 563 | if (ap->a_fflag & FWRITE) { | |
| 564 | fip->fi_writers--; | |
| 565 | if (fip->fi_writers == 0) | |
| 566 | socantrcvmore(fip->fi_readsock); | |
| 567 | } | |
| 3c37c940 | 568 | if (vp->v_sysref.refcnt > 1) { |
| 8ddc6004 | 569 | vop_stdclose(ap); |
| 3b998fa9 | 570 | lwkt_reltoken(&vp->v_token); |
| 984263bc | 571 | return (0); |
| 8ddc6004 | 572 | } |
| 9ba76b73 MD |
573 | error1 = soclose(fip->fi_readsock, FNONBLOCK); |
| 574 | error2 = soclose(fip->fi_writesock, FNONBLOCK); | |
| 5fd012e0 | 575 | FREE(fip, M_FIFOINFO); |
| 984263bc | 576 | vp->v_fifoinfo = NULL; |
| aac0aabd MD |
577 | if (error1) { |
| 578 | error2 = error1; | |
| 579 | } else { | |
| 580 | vop_stdclose(ap); | |
| 581 | } | |
| 3b998fa9 | 582 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
583 | return (error2); |
| 584 | } | |
| 585 | ||
| 586 | ||
| 587 | /* | |
| 588 | * Print out internal contents of a fifo vnode. | |
| 589 | */ | |
| 590 | int | |
| b5f12e21 | 591 | fifo_printinfo(struct vnode *vp) |
| 984263bc | 592 | { |
| 0e99e805 | 593 | struct fifoinfo *fip = vp->v_fifoinfo; |
| 984263bc | 594 | |
| 086c1d7e | 595 | kprintf(", fifo with %ld readers and %ld writers", |
| 984263bc MD |
596 | fip->fi_readers, fip->fi_writers); |
| 597 | return (0); | |
| 598 | } | |
| 599 | ||
| 600 | /* | |
| 601 | * Print out the contents of a fifo vnode. | |
| b5f12e21 CP |
602 | * |
| 603 | * fifo_print(struct vnode *a_vp) | |
| 984263bc MD |
604 | */ |
| 605 | static int | |
| b5f12e21 | 606 | fifo_print(struct vop_print_args *ap) |
| 984263bc | 607 | { |
| 086c1d7e | 608 | kprintf("tag VT_NON"); |
| 984263bc | 609 | fifo_printinfo(ap->a_vp); |
| 086c1d7e | 610 | kprintf("\n"); |
| 984263bc MD |
611 | return (0); |
| 612 | } | |
| 613 | ||
| 614 | /* | |
| 615 | * Return POSIX pathconf information applicable to fifo's. | |
| b5f12e21 CP |
616 | * |
| 617 | * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval) | |
| 984263bc MD |
618 | */ |
| 619 | int | |
| b5f12e21 | 620 | fifo_pathconf(struct vop_pathconf_args *ap) |
| 984263bc | 621 | { |
| 984263bc MD |
622 | switch (ap->a_name) { |
| 623 | case _PC_LINK_MAX: | |
| 624 | *ap->a_retval = LINK_MAX; | |
| 625 | return (0); | |
| 626 | case _PC_PIPE_BUF: | |
| 627 | *ap->a_retval = PIPE_BUF; | |
| 628 | return (0); | |
| 629 | case _PC_CHOWN_RESTRICTED: | |
| 630 | *ap->a_retval = 1; | |
| 631 | return (0); | |
| 632 | default: | |
| 633 | return (EINVAL); | |
| 634 | } | |
| 635 | /* NOTREACHED */ | |
| 636 | } | |
| 637 | ||
| 638 | /* | |
| 639 | * Fifo advisory byte-level locks. | |
| b5f12e21 CP |
640 | * |
| 641 | * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl, | |
| 642 | * int a_flags) | |
| 984263bc MD |
643 | */ |
| 644 | /* ARGSUSED */ | |
| 645 | static int | |
| b5f12e21 | 646 | fifo_advlock(struct vop_advlock_args *ap) |
| 984263bc | 647 | { |
| 71c18fe3 | 648 | return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP); |
| 984263bc MD |
649 | } |
| 650 | ||
| 651 | /* | |
| 652 | * Fifo bad operation | |
| 653 | */ | |
| 654 | static int | |
| b5f12e21 | 655 | fifo_badop(void) |
| 984263bc | 656 | { |
| 984263bc MD |
657 | panic("fifo_badop called"); |
| 658 | /* NOTREACHED */ | |
| 659 | } |