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 * 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. 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 * - fi_wgen and fi_seqcount are protected by the pipe mutex. 63 */ 64 struct fifoinfo { 65 struct pipe *fi_pipe; 66 long fi_readers; 67 long fi_writers; 68 int fi_wgen; 69 int fi_seqcount; 70 }; 71 72 #define FIFO_UPDWGEN(fip, pip) do { \ 73 if ((fip)->fi_wgen == (fip)->fi_seqcount) \ 74 (pip)->pipe_state |= PIPE_SAMEWGEN; \ 75 else \ 76 (pip)->pipe_state &= ~PIPE_SAMEWGEN; \ 77 } while (0) 78 79 static vop_print_t fifo_print; 80 static vop_open_t fifo_open; 81 static vop_close_t fifo_close; 82 static vop_pathconf_t fifo_pathconf; 83 static vop_advlock_t fifo_advlock; 84 85 struct vop_vector fifo_specops = { 86 .vop_default = &default_vnodeops, 87 88 .vop_advlock = fifo_advlock, 89 .vop_close = fifo_close, 90 .vop_create = VOP_PANIC, 91 .vop_getattr = VOP_EBADF, 92 .vop_ioctl = VOP_PANIC, 93 .vop_kqfilter = VOP_PANIC, 94 .vop_link = VOP_PANIC, 95 .vop_mkdir = VOP_PANIC, 96 .vop_mknod = VOP_PANIC, 97 .vop_open = fifo_open, 98 .vop_pathconf = fifo_pathconf, 99 .vop_print = fifo_print, 100 .vop_read = VOP_PANIC, 101 .vop_readdir = VOP_PANIC, 102 .vop_readlink = VOP_PANIC, 103 .vop_reallocblks = VOP_PANIC, 104 .vop_reclaim = VOP_NULL, 105 .vop_remove = VOP_PANIC, 106 .vop_rename = VOP_PANIC, 107 .vop_rmdir = VOP_PANIC, 108 .vop_setattr = VOP_EBADF, 109 .vop_symlink = VOP_PANIC, 110 .vop_write = VOP_PANIC, 111 }; 112 113 /* 114 * Dispose of fifo resources. 115 */ 116 static void 117 fifo_cleanup(struct vnode *vp) 118 { 119 struct fifoinfo *fip; 120 121 ASSERT_VOP_ELOCKED(vp, "fifo_cleanup"); 122 fip = vp->v_fifoinfo; 123 if (fip->fi_readers == 0 && fip->fi_writers == 0) { 124 vp->v_fifoinfo = NULL; 125 pipe_dtor(fip->fi_pipe); 126 free(fip, M_VNODE); 127 } 128 } 129 130 /* 131 * Open called to set up a new instance of a fifo or 132 * to find an active instance of a fifo. 133 */ 134 /* ARGSUSED */ 135 static int 136 fifo_open(ap) 137 struct vop_open_args /* { 138 struct vnode *a_vp; 139 int a_mode; 140 struct ucred *a_cred; 141 struct thread *a_td; 142 struct file *a_fp; 143 } */ *ap; 144 { 145 struct vnode *vp; 146 struct file *fp; 147 struct thread *td; 148 struct fifoinfo *fip; 149 struct pipe *fpipe; 150 int error; 151 152 vp = ap->a_vp; 153 fp = ap->a_fp; 154 td = ap->a_td; 155 ASSERT_VOP_ELOCKED(vp, "fifo_open"); 156 if (fp == NULL) 157 return (EINVAL); 158 if ((fip = vp->v_fifoinfo) == NULL) { 159 error = pipe_named_ctor(&fpipe, td); 160 if (error != 0) 161 return (error); 162 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK); 163 fip->fi_pipe = fpipe; 164 fip->fi_wgen = fip->fi_readers = fip->fi_writers = 0; 165 KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race")); 166 vp->v_fifoinfo = fip; 167 } 168 fpipe = fip->fi_pipe; 169 KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL")); 170 171 /* 172 * Use the pipe mutex here, in addition to the vnode lock, 173 * in order to allow vnode lock dropping before msleep() calls 174 * and still avoiding missed wakeups. 175 */ 176 PIPE_LOCK(fpipe); 177 if (ap->a_mode & FREAD) { 178 fip->fi_readers++; 179 if (fip->fi_readers == 1) { 180 fpipe->pipe_state &= ~PIPE_EOF; 181 if (fip->fi_writers > 0) 182 wakeup(&fip->fi_writers); 183 } 184 fip->fi_seqcount = fip->fi_wgen - fip->fi_writers; 185 FIFO_UPDWGEN(fip, fpipe); 186 } 187 if (ap->a_mode & FWRITE) { 188 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) { 189 PIPE_UNLOCK(fpipe); 190 if (fip->fi_writers == 0) 191 fifo_cleanup(vp); 192 return (ENXIO); 193 } 194 fip->fi_writers++; 195 if (fip->fi_writers == 1) { 196 fpipe->pipe_state &= ~PIPE_EOF; 197 if (fip->fi_readers > 0) 198 wakeup(&fip->fi_readers); 199 } 200 } 201 if ((ap->a_mode & O_NONBLOCK) == 0) { 202 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) { 203 VOP_UNLOCK(vp, 0); 204 error = msleep(&fip->fi_readers, PIPE_MTX(fpipe), 205 PDROP | PCATCH | PSOCK, "fifoor", 0); 206 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 207 if (error) { 208 fip->fi_readers--; 209 if (fip->fi_readers == 0) { 210 PIPE_LOCK(fpipe); 211 fpipe->pipe_state |= PIPE_EOF; 212 if (fpipe->pipe_state & PIPE_WANTW) 213 wakeup(fpipe); 214 PIPE_UNLOCK(fpipe); 215 fifo_cleanup(vp); 216 } 217 return (error); 218 } 219 PIPE_LOCK(fpipe); 220 /* 221 * We must have got woken up because we had a writer. 222 * That (and not still having one) is the condition 223 * that we must wait for. 224 */ 225 } 226 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) { 227 VOP_UNLOCK(vp, 0); 228 error = msleep(&fip->fi_writers, PIPE_MTX(fpipe), 229 PDROP | PCATCH | PSOCK, "fifoow", 0); 230 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 231 if (error) { 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 fip->fi_wgen++; 239 FIFO_UPDWGEN(fip, fpipe); 240 PIPE_UNLOCK(fpipe); 241 fifo_cleanup(vp); 242 } 243 return (error); 244 } 245 /* 246 * We must have got woken up because we had 247 * a reader. That (and not still having one) 248 * is the condition that we must wait for. 249 */ 250 PIPE_LOCK(fpipe); 251 } 252 } 253 PIPE_UNLOCK(fpipe); 254 KASSERT(fp != NULL, ("can't fifo/vnode bypass")); 255 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open")); 256 finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops); 257 return (0); 258 } 259 260 /* 261 * Device close routine 262 */ 263 /* ARGSUSED */ 264 static int 265 fifo_close(ap) 266 struct vop_close_args /* { 267 struct vnode *a_vp; 268 int a_fflag; 269 struct ucred *a_cred; 270 struct thread *a_td; 271 } */ *ap; 272 { 273 struct vnode *vp; 274 struct fifoinfo *fip; 275 struct pipe *cpipe; 276 277 vp = ap->a_vp; 278 fip = vp->v_fifoinfo; 279 cpipe = fip->fi_pipe; 280 ASSERT_VOP_ELOCKED(vp, "fifo_close"); 281 if (ap->a_fflag & FREAD) { 282 fip->fi_readers--; 283 if (fip->fi_readers == 0) { 284 PIPE_LOCK(cpipe); 285 cpipe->pipe_state |= PIPE_EOF; 286 if (cpipe->pipe_state & PIPE_WANTW) 287 wakeup(cpipe); 288 PIPE_UNLOCK(cpipe); 289 } 290 } 291 if (ap->a_fflag & FWRITE) { 292 fip->fi_writers--; 293 if (fip->fi_writers == 0) { 294 PIPE_LOCK(cpipe); 295 cpipe->pipe_state |= PIPE_EOF; 296 if (cpipe->pipe_state & PIPE_WANTR) 297 wakeup(cpipe); 298 fip->fi_wgen++; 299 FIFO_UPDWGEN(fip, cpipe); 300 PIPE_UNLOCK(cpipe); 301 } 302 } 303 fifo_cleanup(vp); 304 return (0); 305 } 306 307 /* 308 * Print out internal contents of a fifo vnode. 309 */ 310 int 311 fifo_printinfo(vp) 312 struct vnode *vp; 313 { 314 register struct fifoinfo *fip = vp->v_fifoinfo; 315 316 if (fip == NULL){ 317 printf(", NULL v_fifoinfo"); 318 return (0); 319 } 320 printf(", fifo with %ld readers and %ld writers", 321 fip->fi_readers, fip->fi_writers); 322 return (0); 323 } 324 325 /* 326 * Print out the contents of a fifo vnode. 327 */ 328 static int 329 fifo_print(ap) 330 struct vop_print_args /* { 331 struct vnode *a_vp; 332 } */ *ap; 333 { 334 printf(" "); 335 fifo_printinfo(ap->a_vp); 336 printf("\n"); 337 return (0); 338 } 339 340 /* 341 * Return POSIX pathconf information applicable to fifo's. 342 */ 343 static int 344 fifo_pathconf(ap) 345 struct vop_pathconf_args /* { 346 struct vnode *a_vp; 347 int a_name; 348 int *a_retval; 349 } */ *ap; 350 { 351 352 switch (ap->a_name) { 353 case _PC_LINK_MAX: 354 *ap->a_retval = LINK_MAX; 355 return (0); 356 case _PC_PIPE_BUF: 357 *ap->a_retval = PIPE_BUF; 358 return (0); 359 case _PC_CHOWN_RESTRICTED: 360 *ap->a_retval = 1; 361 return (0); 362 default: 363 return (EINVAL); 364 } 365 /* NOTREACHED */ 366 } 367 368 /* 369 * Fifo advisory byte-level locks. 370 */ 371 /* ARGSUSED */ 372 static int 373 fifo_advlock(ap) 374 struct vop_advlock_args /* { 375 struct vnode *a_vp; 376 caddr_t a_id; 377 int a_op; 378 struct flock *a_fl; 379 int a_flags; 380 } */ *ap; 381 { 382 383 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 384 } 385 386