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