1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993, 1995 5 * The Regents of the University of California. 6 * Copyright (c) 2005 Robert N. M. Watson 7 * Copyright (c) 2012 Giovanni Trematerra 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95 35 * $FreeBSD$ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/event.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/filio.h> 43 #include <sys/fcntl.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/malloc.h> 48 #include <sys/selinfo.h> 49 #include <sys/pipe.h> 50 #include <sys/proc.h> 51 #include <sys/signalvar.h> 52 #include <sys/sx.h> 53 #include <sys/systm.h> 54 #include <sys/un.h> 55 #include <sys/unistd.h> 56 #include <sys/vnode.h> 57 58 /* 59 * This structure is associated with the FIFO vnode and stores 60 * the state associated with the FIFO. 61 * Notes about locking: 62 * - fi_pipe is invariant since init time. 63 * - fi_readers and fi_writers are protected by the vnode lock. 64 */ 65 struct fifoinfo { 66 struct pipe *fi_pipe; 67 long fi_readers; 68 long fi_writers; 69 u_int fi_rgen; 70 u_int fi_wgen; 71 }; 72 73 static vop_print_t fifo_print; 74 static vop_open_t fifo_open; 75 static vop_close_t fifo_close; 76 static vop_advlock_t fifo_advlock; 77 78 struct vop_vector fifo_specops = { 79 .vop_default = &default_vnodeops, 80 81 .vop_advlock = fifo_advlock, 82 .vop_close = fifo_close, 83 .vop_create = VOP_PANIC, 84 .vop_getattr = VOP_EBADF, 85 .vop_ioctl = VOP_PANIC, 86 .vop_kqfilter = VOP_PANIC, 87 .vop_link = VOP_PANIC, 88 .vop_mkdir = VOP_PANIC, 89 .vop_mknod = VOP_PANIC, 90 .vop_open = fifo_open, 91 .vop_pathconf = VOP_PANIC, 92 .vop_print = fifo_print, 93 .vop_read = VOP_PANIC, 94 .vop_readdir = VOP_PANIC, 95 .vop_readlink = VOP_PANIC, 96 .vop_reallocblks = VOP_PANIC, 97 .vop_reclaim = VOP_NULL, 98 .vop_remove = VOP_PANIC, 99 .vop_rename = VOP_PANIC, 100 .vop_rmdir = VOP_PANIC, 101 .vop_setattr = VOP_EBADF, 102 .vop_symlink = VOP_PANIC, 103 .vop_write = VOP_PANIC, 104 }; 105 VFS_VOP_VECTOR_REGISTER(fifo_specops); 106 107 /* 108 * Dispose of fifo resources. 109 */ 110 static void 111 fifo_cleanup(struct vnode *vp) 112 { 113 struct fifoinfo *fip; 114 115 ASSERT_VOP_ELOCKED(vp, "fifo_cleanup"); 116 fip = vp->v_fifoinfo; 117 if (fip->fi_readers == 0 && fip->fi_writers == 0) { 118 vp->v_fifoinfo = NULL; 119 pipe_dtor(fip->fi_pipe); 120 free(fip, M_VNODE); 121 } 122 } 123 124 /* 125 * Open called to set up a new instance of a fifo or 126 * to find an active instance of a fifo. 127 */ 128 /* ARGSUSED */ 129 static int 130 fifo_open(ap) 131 struct vop_open_args /* { 132 struct vnode *a_vp; 133 int a_mode; 134 struct ucred *a_cred; 135 struct thread *a_td; 136 struct file *a_fp; 137 } */ *ap; 138 { 139 struct vnode *vp; 140 struct file *fp; 141 struct thread *td; 142 struct fifoinfo *fip; 143 struct pipe *fpipe; 144 u_int gen; 145 int error, stops_deferred; 146 147 vp = ap->a_vp; 148 fp = ap->a_fp; 149 td = ap->a_td; 150 ASSERT_VOP_ELOCKED(vp, "fifo_open"); 151 if (fp == NULL || (ap->a_mode & FEXEC) != 0) 152 return (EINVAL); 153 if ((fip = vp->v_fifoinfo) == NULL) { 154 pipe_named_ctor(&fpipe, td); 155 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK); 156 fip->fi_pipe = fpipe; 157 fpipe->pipe_wgen = fip->fi_readers = fip->fi_writers = 0; 158 KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race")); 159 vp->v_fifoinfo = fip; 160 } 161 fpipe = fip->fi_pipe; 162 KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL")); 163 164 /* 165 * Use the pipe mutex here, in addition to the vnode lock, 166 * in order to allow vnode lock dropping before msleep() calls 167 * and still avoiding missed wakeups. 168 */ 169 PIPE_LOCK(fpipe); 170 if (ap->a_mode & FREAD) { 171 fip->fi_readers++; 172 fip->fi_rgen++; 173 if (fip->fi_readers == 1) { 174 fpipe->pipe_state &= ~PIPE_EOF; 175 if (fip->fi_writers > 0) 176 wakeup(&fip->fi_writers); 177 } 178 fp->f_pipegen = fpipe->pipe_wgen - fip->fi_writers; 179 } 180 if (ap->a_mode & FWRITE) { 181 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) { 182 PIPE_UNLOCK(fpipe); 183 if (fip->fi_writers == 0) 184 fifo_cleanup(vp); 185 return (ENXIO); 186 } 187 fip->fi_writers++; 188 fip->fi_wgen++; 189 if (fip->fi_writers == 1) { 190 fpipe->pipe_state &= ~PIPE_EOF; 191 if (fip->fi_readers > 0) 192 wakeup(&fip->fi_readers); 193 } 194 } 195 if ((ap->a_mode & O_NONBLOCK) == 0) { 196 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) { 197 gen = fip->fi_wgen; 198 VOP_UNLOCK(vp, 0); 199 stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF); 200 error = msleep(&fip->fi_readers, PIPE_MTX(fpipe), 201 PDROP | PCATCH | PSOCK, "fifoor", 0); 202 sigallowstop(stops_deferred); 203 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 204 if (error != 0 && gen == fip->fi_wgen) { 205 fip->fi_readers--; 206 if (fip->fi_readers == 0) { 207 PIPE_LOCK(fpipe); 208 fpipe->pipe_state |= PIPE_EOF; 209 if (fpipe->pipe_state & PIPE_WANTW) 210 wakeup(fpipe); 211 PIPE_UNLOCK(fpipe); 212 fifo_cleanup(vp); 213 } 214 return (error); 215 } 216 PIPE_LOCK(fpipe); 217 /* 218 * We must have got woken up because we had a writer. 219 * That (and not still having one) is the condition 220 * that we must wait for. 221 */ 222 } 223 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) { 224 gen = fip->fi_rgen; 225 VOP_UNLOCK(vp, 0); 226 stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF); 227 error = msleep(&fip->fi_writers, PIPE_MTX(fpipe), 228 PDROP | PCATCH | PSOCK, "fifoow", 0); 229 sigallowstop(stops_deferred); 230 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 231 if (error != 0 && gen == fip->fi_rgen) { 232 fip->fi_writers--; 233 if (fip->fi_writers == 0) { 234 PIPE_LOCK(fpipe); 235 fpipe->pipe_state |= PIPE_EOF; 236 if (fpipe->pipe_state & PIPE_WANTR) 237 wakeup(fpipe); 238 fpipe->pipe_wgen++; 239 PIPE_UNLOCK(fpipe); 240 fifo_cleanup(vp); 241 } 242 return (error); 243 } 244 /* 245 * We must have got woken up because we had 246 * a reader. That (and not still having one) 247 * is the condition that we must wait for. 248 */ 249 PIPE_LOCK(fpipe); 250 } 251 } 252 PIPE_UNLOCK(fpipe); 253 KASSERT(fp != NULL, ("can't fifo/vnode bypass")); 254 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open")); 255 finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops); 256 return (0); 257 } 258 259 /* 260 * Device close routine 261 */ 262 /* ARGSUSED */ 263 static int 264 fifo_close(ap) 265 struct vop_close_args /* { 266 struct vnode *a_vp; 267 int a_fflag; 268 struct ucred *a_cred; 269 struct thread *a_td; 270 } */ *ap; 271 { 272 struct vnode *vp; 273 struct fifoinfo *fip; 274 struct pipe *cpipe; 275 276 vp = ap->a_vp; 277 fip = vp->v_fifoinfo; 278 cpipe = fip->fi_pipe; 279 ASSERT_VOP_ELOCKED(vp, "fifo_close"); 280 if (ap->a_fflag & FREAD) { 281 fip->fi_readers--; 282 if (fip->fi_readers == 0) { 283 PIPE_LOCK(cpipe); 284 cpipe->pipe_state |= PIPE_EOF; 285 if ((cpipe->pipe_state & PIPE_WANTW)) { 286 cpipe->pipe_state &= ~PIPE_WANTW; 287 wakeup(cpipe); 288 } 289 pipeselwakeup(cpipe); 290 PIPE_UNLOCK(cpipe); 291 } 292 } 293 if (ap->a_fflag & FWRITE) { 294 fip->fi_writers--; 295 if (fip->fi_writers == 0) { 296 PIPE_LOCK(cpipe); 297 cpipe->pipe_state |= PIPE_EOF; 298 if ((cpipe->pipe_state & PIPE_WANTR)) { 299 cpipe->pipe_state &= ~PIPE_WANTR; 300 wakeup(cpipe); 301 } 302 cpipe->pipe_wgen++; 303 pipeselwakeup(cpipe); 304 PIPE_UNLOCK(cpipe); 305 } 306 } 307 fifo_cleanup(vp); 308 return (0); 309 } 310 311 /* 312 * Print out internal contents of a fifo vnode. 313 */ 314 int 315 fifo_printinfo(vp) 316 struct vnode *vp; 317 { 318 struct fifoinfo *fip = vp->v_fifoinfo; 319 320 if (fip == NULL){ 321 printf(", NULL v_fifoinfo"); 322 return (0); 323 } 324 printf(", fifo with %ld readers and %ld writers", 325 fip->fi_readers, fip->fi_writers); 326 return (0); 327 } 328 329 /* 330 * Print out the contents of a fifo vnode. 331 */ 332 static int 333 fifo_print(ap) 334 struct vop_print_args /* { 335 struct vnode *a_vp; 336 } */ *ap; 337 { 338 printf(" "); 339 fifo_printinfo(ap->a_vp); 340 printf("\n"); 341 return (0); 342 } 343 344 /* 345 * Fifo advisory byte-level locks. 346 */ 347 /* ARGSUSED */ 348 static int 349 fifo_advlock(ap) 350 struct vop_advlock_args /* { 351 struct vnode *a_vp; 352 caddr_t a_id; 353 int a_op; 354 struct flock *a_fl; 355 int a_flags; 356 } */ *ap; 357 { 358 359 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 360 } 361 362