4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All rights reserved. */
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
31 * Copyright 2015, Joyent, Inc.
32 * Copyright (c) 2017 by Delphix. All rights reserved.
36 * FIFOFS file system vnode operations. This file system
37 * type supports STREAMS-based pipes and FIFOs.
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysmacros.h>
44 #include <sys/errno.h>
47 #include <sys/fcntl.h>
51 #include <sys/vnode.h>
52 #include <sys/vfs_opreg.h>
53 #include <sys/pathname.h>
54 #include <sys/signal.h>
56 #include <sys/strsubr.h>
57 #include <sys/stream.h>
58 #include <sys/strsun.h>
59 #include <sys/strredir.h>
60 #include <sys/fs/fifonode.h>
61 #include <sys/fs/namenode.h>
62 #include <sys/stropts.h>
64 #include <sys/unistd.h>
65 #include <sys/debug.h>
66 #include <fs/fs_subr.h>
67 #include <sys/filio.h>
68 #include <sys/termio.h>
70 #include <sys/vtrace.h>
71 #include <sys/policy.h>
72 #include <sys/tsol/label.h>
75 * Define the routines/data structures used in this file.
77 static int fifo_read(vnode_t
*, uio_t
*, int, cred_t
*, caller_context_t
*);
78 static int fifo_write(vnode_t
*, uio_t
*, int, cred_t
*, caller_context_t
*);
79 static int fifo_getattr(vnode_t
*, vattr_t
*, int, cred_t
*,
81 static int fifo_setattr(vnode_t
*, vattr_t
*, int, cred_t
*,
83 static int fifo_realvp(vnode_t
*, vnode_t
**, caller_context_t
*);
84 static int fifo_access(vnode_t
*, int, int, cred_t
*, caller_context_t
*);
85 static int fifo_create(struct vnode
*, char *, vattr_t
*, enum vcexcl
,
86 int, struct vnode
**, struct cred
*, int, caller_context_t
*,
88 static int fifo_fid(vnode_t
*, fid_t
*, caller_context_t
*);
89 static int fifo_fsync(vnode_t
*, int, cred_t
*, caller_context_t
*);
90 static int fifo_seek(vnode_t
*, offset_t
, offset_t
*, caller_context_t
*);
91 static int fifo_ioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *,
93 static int fifo_fastioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *);
94 static int fifo_strioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *);
95 static int fifo_poll(vnode_t
*, short, int, short *, pollhead_t
**,
97 static int fifo_pathconf(vnode_t
*, int, ulong_t
*, cred_t
*,
99 static void fifo_inactive(vnode_t
*, cred_t
*, caller_context_t
*);
100 static int fifo_rwlock(vnode_t
*, int, caller_context_t
*);
101 static void fifo_rwunlock(vnode_t
*, int, caller_context_t
*);
102 static int fifo_setsecattr(struct vnode
*, vsecattr_t
*, int, struct cred
*,
104 static int fifo_getsecattr(struct vnode
*, vsecattr_t
*, int, struct cred
*,
107 /* functions local to this file */
108 static boolean_t
fifo_stayfast_enter(fifonode_t
*);
109 static void fifo_stayfast_exit(fifonode_t
*);
112 * Define the data structures external to this file.
114 extern dev_t fifodev
;
115 extern struct qinit fifo_stwdata
;
116 extern struct qinit fifo_strdata
;
117 extern kmutex_t ftable_lock
;
119 struct streamtab fifoinfo
= { &fifo_strdata
, &fifo_stwdata
, NULL
, NULL
};
121 struct vnodeops
*fifo_vnodeops
;
123 const fs_operation_def_t fifo_vnodeops_template
[] = {
124 VOPNAME_OPEN
, { .vop_open
= fifo_open
},
125 VOPNAME_CLOSE
, { .vop_close
= fifo_close
},
126 VOPNAME_READ
, { .vop_read
= fifo_read
},
127 VOPNAME_WRITE
, { .vop_write
= fifo_write
},
128 VOPNAME_IOCTL
, { .vop_ioctl
= fifo_ioctl
},
129 VOPNAME_GETATTR
, { .vop_getattr
= fifo_getattr
},
130 VOPNAME_SETATTR
, { .vop_setattr
= fifo_setattr
},
131 VOPNAME_ACCESS
, { .vop_access
= fifo_access
},
132 VOPNAME_CREATE
, { .vop_create
= fifo_create
},
133 VOPNAME_FSYNC
, { .vop_fsync
= fifo_fsync
},
134 VOPNAME_INACTIVE
, { .vop_inactive
= fifo_inactive
},
135 VOPNAME_FID
, { .vop_fid
= fifo_fid
},
136 VOPNAME_RWLOCK
, { .vop_rwlock
= fifo_rwlock
},
137 VOPNAME_RWUNLOCK
, { .vop_rwunlock
= fifo_rwunlock
},
138 VOPNAME_SEEK
, { .vop_seek
= fifo_seek
},
139 VOPNAME_REALVP
, { .vop_realvp
= fifo_realvp
},
140 VOPNAME_POLL
, { .vop_poll
= fifo_poll
},
141 VOPNAME_PATHCONF
, { .vop_pathconf
= fifo_pathconf
},
142 VOPNAME_DISPOSE
, { .error
= fs_error
},
143 VOPNAME_SETSECATTR
, { .vop_setsecattr
= fifo_setsecattr
},
144 VOPNAME_GETSECATTR
, { .vop_getsecattr
= fifo_getsecattr
},
149 * Return the fifoinfo structure.
158 * Trusted Extensions enforces a restrictive policy for
159 * writing via cross-zone named pipes. A privileged global
160 * zone process may expose a named pipe by loopback mounting
161 * it from a lower-level zone to a higher-level zone. The
162 * kernel-enforced mount policy for lofs mounts ensures
163 * that such mounts are read-only in the higher-level
164 * zone. But this is not sufficient to prevent writing
165 * down via fifos. This function prevents writing down
166 * by comparing the zone of the process which is requesting
167 * write access with the zone owning the named pipe rendezvous.
168 * For write access the zone of the named pipe must equal the
169 * zone of the writing process. Writing up is possible since
170 * the named pipe can be opened for read by a process in a
173 * An exception is made for the global zone to support trusted
174 * processes which enforce their own data flow policies.
177 tsol_fifo_access(vnode_t
*vp
, int flag
, cred_t
*crp
)
179 fifonode_t
*fnp
= VTOF(vp
);
181 if (is_system_labeled() &&
183 (!(fnp
->fn_flag
& ISPIPE
))) {
186 proc_zone
= crgetzone(crp
);
187 if (proc_zone
!= global_zone
) {
188 char vpath
[MAXPATHLEN
];
192 * Get the pathname and use it to find
193 * the zone of the fifo.
195 if (vnodetopath(rootdir
, vp
, vpath
, sizeof (vpath
),
197 fifo_zone
= zone_find_by_path(vpath
);
198 zone_rele(fifo_zone
);
200 if (fifo_zone
!= global_zone
&&
201 fifo_zone
!= proc_zone
) {
213 * Open and stream a FIFO.
214 * If this is the first open of the file (FIFO is not streaming),
215 * initialize the fifonode and attach a stream to the vnode.
217 * Each end of a fifo must be synchronized with the other end.
218 * If not, the mated end may complete an open, I/O, close sequence
219 * before the end waiting in open ever wakes up.
220 * Note: namefs pipes come through this routine too.
223 fifo_open(vnode_t
**vpp
, int flag
, cred_t
*crp
, caller_context_t
*ct
)
226 fifonode_t
*fnp
= VTOF(vp
);
227 fifolock_t
*fn_lock
= fnp
->fn_lock
;
230 ASSERT(vp
->v_type
== VFIFO
);
231 ASSERT(vn_matchops(vp
, fifo_vnodeops
));
233 if (!tsol_fifo_access(vp
, flag
, crp
))
236 mutex_enter(&fn_lock
->flk_lock
);
238 * If we are the first reader, wake up any writers that
239 * may be waiting around. wait for all of them to
240 * wake up before proceeding (i.e. fn_wsynccnt == 0)
243 fnp
->fn_rcnt
++; /* record reader present */
244 if (! (fnp
->fn_flag
& ISPIPE
))
245 fnp
->fn_rsynccnt
++; /* record reader in open */
249 * If we are the first writer, wake up any readers that
250 * may be waiting around. wait for all of them to
251 * wake up before proceeding (i.e. fn_rsynccnt == 0)
254 fnp
->fn_wcnt
++; /* record writer present */
255 if (! (fnp
->fn_flag
& ISPIPE
))
256 fnp
->fn_wsynccnt
++; /* record writer in open */
259 * fifo_stropen will take care of twisting the queues on the first
260 * open. The 1 being passed in means twist the queues on the first
263 error
= fifo_stropen(vpp
, flag
, crp
, 1, 1);
265 * fifo_stropen() could have replaced vpp
266 * since fifo's are the only thing we need to sync up,
267 * everything else just returns;
268 * Note: don't need to hold lock since ISPIPE can't change
269 * and both old and new vp need to be pipes
271 ASSERT(MUTEX_HELD(&VTOF(*vpp
)->fn_lock
->flk_lock
));
272 if (fnp
->fn_flag
& ISPIPE
) {
273 ASSERT(VTOF(*vpp
)->fn_flag
& ISPIPE
);
274 ASSERT(VTOF(*vpp
)->fn_rsynccnt
== 0);
275 ASSERT(VTOF(*vpp
)->fn_rsynccnt
== 0);
277 * XXX note: should probably hold locks, but
278 * These values should not be changing
280 ASSERT(fnp
->fn_rsynccnt
== 0);
281 ASSERT(fnp
->fn_wsynccnt
== 0);
282 mutex_exit(&VTOF(*vpp
)->fn_lock
->flk_lock
);
286 * vp can't change for FIFOS
290 * If we are opening for read (or writer)
291 * indicate that the reader (or writer) is done with open
292 * if there is a writer (or reader) waiting for us, wake them up
293 * and indicate that at least 1 read (or write) open has occurred
294 * this is need in the event the read (or write) side closes
295 * before the writer (or reader) has a chance to wake up
296 * i.e. it sees that a reader (or writer) was once there
299 fnp
->fn_rsynccnt
--; /* reader done with open */
300 if (fnp
->fn_flag
& FIFOSYNC
) {
302 * This indicates that a read open has occurred
303 * Only need to set if writer is actually asleep
304 * Flag will be consumed by writer.
306 fnp
->fn_flag
|= FIFOROCR
;
307 cv_broadcast(&fnp
->fn_wait_cv
);
311 fnp
->fn_wsynccnt
--; /* writer done with open */
312 if (fnp
->fn_flag
& FIFOSYNC
) {
314 * This indicates that a write open has occurred
315 * Only need to set if reader is actually asleep
316 * Flag will be consumed by reader.
318 fnp
->fn_flag
|= FIFOWOCR
;
319 cv_broadcast(&fnp
->fn_wait_cv
);
323 fnp
->fn_flag
&= ~FIFOSYNC
;
326 * errors don't wait around.. just return
327 * Note: XXX other end will wake up and continue despite error.
328 * There is no defined semantic on the correct course of option
329 * so we do what we've done in the past
332 mutex_exit(&fnp
->fn_lock
->flk_lock
);
335 ASSERT(fnp
->fn_rsynccnt
<= fnp
->fn_rcnt
);
336 ASSERT(fnp
->fn_wsynccnt
<= fnp
->fn_wcnt
);
338 * FIFOWOCR (or FIFOROCR) indicates that the writer (or reader)
339 * has woken us up and is done with open (this way, if the other
340 * end has made it to close, we don't block forever in open)
341 * fn_wnct == fn_wsynccnt (or fn_rcnt == fn_rsynccnt) indicates
342 * that no writer (or reader) has yet made it through open
343 * This has the side benefit of that the first
344 * reader (or writer) will wait until the other end finishes open
347 while ((fnp
->fn_flag
& FIFOWOCR
) == 0 &&
348 fnp
->fn_wcnt
== fnp
->fn_wsynccnt
) {
349 if (flag
& (FNDELAY
|FNONBLOCK
)) {
350 mutex_exit(&fnp
->fn_lock
->flk_lock
);
354 fnp
->fn_flag
|= FIFOSYNC
;
355 if (!cv_wait_sig_swap(&fnp
->fn_wait_cv
,
356 &fnp
->fn_lock
->flk_lock
)) {
358 * Last reader to wakeup clear writer
359 * Clear both writer and reader open
360 * occurred flag incase other end is O_RDWR
362 if (--fnp
->fn_insync
== 0 &&
363 fnp
->fn_flag
& FIFOWOCR
) {
364 fnp
->fn_flag
&= ~(FIFOWOCR
|FIFOROCR
);
366 mutex_exit(&fnp
->fn_lock
->flk_lock
);
367 (void) fifo_close(*vpp
, flag
, 1, 0, crp
, ct
);
372 * Last reader to wakeup clear writer open occurred flag
373 * Clear both writer and reader open occurred flag
374 * incase other end is O_RDWR
376 if (--fnp
->fn_insync
== 0 &&
377 fnp
->fn_flag
& FIFOWOCR
) {
378 fnp
->fn_flag
&= ~(FIFOWOCR
|FIFOROCR
);
382 } else if (flag
& FWRITE
) {
383 while ((fnp
->fn_flag
& FIFOROCR
) == 0 &&
384 fnp
->fn_rcnt
== fnp
->fn_rsynccnt
) {
385 if ((flag
& (FNDELAY
|FNONBLOCK
)) && fnp
->fn_rcnt
== 0) {
386 mutex_exit(&fnp
->fn_lock
->flk_lock
);
387 (void) fifo_close(*vpp
, flag
, 1, 0, crp
, ct
);
391 fnp
->fn_flag
|= FIFOSYNC
;
393 if (!cv_wait_sig_swap(&fnp
->fn_wait_cv
,
394 &fnp
->fn_lock
->flk_lock
)) {
396 * Last writer to wakeup clear
397 * Clear both writer and reader open
398 * occurred flag in case other end is O_RDWR
400 if (--fnp
->fn_insync
== 0 &&
401 (fnp
->fn_flag
& FIFOROCR
) != 0) {
402 fnp
->fn_flag
&= ~(FIFOWOCR
|FIFOROCR
);
404 mutex_exit(&fnp
->fn_lock
->flk_lock
);
405 (void) fifo_close(*vpp
, flag
, 1, 0, crp
, ct
);
410 * Last writer to wakeup clear reader open occurred flag
411 * Clear both writer and reader open
412 * occurred flag in case other end is O_RDWR
414 if (--fnp
->fn_insync
== 0 &&
415 (fnp
->fn_flag
& FIFOROCR
) != 0) {
416 fnp
->fn_flag
&= ~(FIFOWOCR
|FIFOROCR
);
421 mutex_exit(&fn_lock
->flk_lock
);
427 * Close down a stream.
428 * Call cleanlocks() and strclean() on every close.
429 * For last close send hangup message and force
430 * the other end of a named pipe to be unmounted.
431 * Mount guarantees that the mounted end will only call fifo_close()
432 * with a count of 1 when the unmount occurs.
433 * This routine will close down one end of a pipe or FIFO
434 * and free the stream head via strclose()
438 fifo_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*crp
,
439 caller_context_t
*ct
)
441 fifonode_t
*fnp
= VTOF(vp
);
442 fifonode_t
*fn_dest
= fnp
->fn_dest
;
444 fifolock_t
*fn_lock
= fnp
->fn_lock
;
449 ASSERT(vp
->v_stream
!= NULL
);
451 * clean locks and clear events.
453 (void) cleanlocks(vp
, ttoproc(curthread
)->p_pid
, 0);
454 cleanshares(vp
, ttoproc(curthread
)->p_pid
);
458 * If a file still has the pipe/FIFO open, return.
464 sd_wrq
= strvp2wq(vp
);
465 mutex_enter(&fn_lock
->flk_lock
);
468 * wait for pending opens to finish up
469 * note: this also has the side effect of single threading closes
471 while (fn_lock
->flk_ocsync
)
472 cv_wait(&fn_lock
->flk_wait_cv
, &fn_lock
->flk_lock
);
474 fn_lock
->flk_ocsync
= 1;
480 * If we are last writer wake up sleeping readers
481 * (They'll figure out that there are no more writers
482 * and do the right thing)
483 * send hangup down stream so that stream head will do the
487 if (--fnp
->fn_wcnt
== 0 && fn_dest
->fn_rcnt
> 0) {
488 if ((fn_dest
->fn_flag
& (FIFOFAST
| FIFOWANTR
)) ==
489 (FIFOFAST
| FIFOWANTR
)) {
491 * While we're at it, clear FIFOWANTW too
492 * Wake up any sleeping readers or
495 fn_dest
->fn_flag
&= ~(FIFOWANTR
| FIFOWANTW
);
496 cv_broadcast(&fn_dest
->fn_wait_cv
);
499 * This is needed incase the other side
500 * was opened non-blocking. It is the
501 * only way we can tell that wcnt is 0 because
502 * of close instead of never having a writer
504 if (!(fnp
->fn_flag
& ISPIPE
))
505 fnp
->fn_flag
|= FIFOCLOSE
;
507 * Note: sending hangup effectively shuts down
508 * both reader and writer at other end.
510 (void) putnextctl_wait(sd_wrq
, M_HANGUP
);
516 * For FIFOs we need to indicate to stream head that last reader
517 * has gone away so that an error is generated
518 * Pipes just need to wake up the other end so that it can
519 * notice this end has gone away.
522 if (fnp
->fn_rcnt
== 0 && fn_dest
->fn_wcnt
> 0) {
523 if ((fn_dest
->fn_flag
& (FIFOFAST
| FIFOWANTW
)) ==
524 (FIFOFAST
| FIFOWANTW
)) {
526 * wake up any sleeping writers
528 fn_dest
->fn_flag
&= ~FIFOWANTW
;
529 cv_broadcast(&fn_dest
->fn_wait_cv
);
534 * if there are still processes with this FIFO open
535 * clear open/close sync flag
538 if (--fnp
->fn_open
> 0) {
539 ASSERT((fnp
->fn_rcnt
+ fnp
->fn_wcnt
) != 0);
540 fn_lock
->flk_ocsync
= 0;
541 cv_broadcast(&fn_lock
->flk_wait_cv
);
542 mutex_exit(&fn_lock
->flk_lock
);
547 * Need to send HANGUP if other side is still open
548 * (fnp->fn_rcnt or fnp->fn_wcnt may not be zero (some thread
549 * on this end of the pipe may still be in fifo_open())
551 * Note: we can get here with fn_rcnt and fn_wcnt != 0 if some
552 * thread is blocked somewhere in the fifo_open() path prior to
553 * fifo_stropen() incrementing fn_open. This can occur for
554 * normal FIFOs as well as named pipes. fn_rcnt and
555 * fn_wcnt only indicate attempts to open. fn_open indicates
556 * successful opens. Partially opened FIFOs should proceed
557 * normally; i.e. they will appear to be new opens. Partially
558 * opened pipes will probably fail.
561 if (fn_dest
->fn_open
&& senthang
== 0)
562 (void) putnextctl_wait(sd_wrq
, M_HANGUP
);
566 * If this a pipe and this is the first end to close,
567 * then we have a bit of cleanup work to do.
568 * Mark both ends of pipe as closed.
569 * Wake up anybody blocked at the other end and for named pipes,
570 * Close down this end of the stream
571 * Allow other opens/closes to continue
572 * force an unmount of other end.
573 * Otherwise if this is last close,
575 * close down the stream
576 * allow other opens/closes to continue
578 fnp
->fn_flag
&= ~FIFOISOPEN
;
579 if ((fnp
->fn_flag
& ISPIPE
) && !(fnp
->fn_flag
& FIFOCLOSE
)) {
580 fnp
->fn_flag
|= FIFOCLOSE
;
581 fn_dest
->fn_flag
|= FIFOCLOSE
;
582 if (fnp
->fn_flag
& FIFOFAST
)
584 if (vp
->v_stream
!= NULL
) {
585 mutex_exit(&fn_lock
->flk_lock
);
586 (void) strclose(vp
, flag
, crp
);
587 mutex_enter(&fn_lock
->flk_lock
);
589 cv_broadcast(&fn_dest
->fn_wait_cv
);
591 * allow opens and closes to proceed
592 * Since this end is now closed down, any attempt
593 * to do anything with this end will fail
595 fn_lock
->flk_ocsync
= 0;
596 cv_broadcast(&fn_lock
->flk_wait_cv
);
597 fn_dest_vp
= FTOV(fn_dest
);
599 * if other end of pipe has been opened and it's
600 * a named pipe, unmount it
602 if (fn_dest_vp
->v_stream
&&
603 (fn_dest_vp
->v_stream
->sd_flag
& STRMOUNT
)) {
605 * We must hold the destination vnode because
606 * nm_unmountall() causes close to be called
607 * for the other end of named pipe. This
608 * could free the vnode before we are ready.
611 mutex_exit(&fn_lock
->flk_lock
);
612 error
= nm_unmountall(fn_dest_vp
, crp
);
616 ASSERT(vp
->v_count
>= 1);
617 mutex_exit(&fn_lock
->flk_lock
);
620 if (fnp
->fn_flag
& FIFOFAST
)
623 fn_dest_vp
= FTOV(fn_dest
);
624 if (fn_dest_vp
->v_stream
)
625 ASSERT((fn_dest_vp
->v_stream
->sd_flag
& STRMOUNT
) == 0);
627 if (vp
->v_stream
!= NULL
) {
628 mutex_exit(&fn_lock
->flk_lock
);
629 (void) strclose(vp
, flag
, crp
);
630 mutex_enter(&fn_lock
->flk_lock
);
632 fn_lock
->flk_ocsync
= 0;
633 cv_broadcast(&fn_lock
->flk_wait_cv
);
634 cv_broadcast(&fn_dest
->fn_wait_cv
);
635 mutex_exit(&fn_lock
->flk_lock
);
641 * Read from a pipe or FIFO.
643 * (1) user read request is 0 or no stream
644 * (2) broken pipe with no data
645 * (3) write-only FIFO with no data
646 * (4) no data and FNDELAY flag is set.
648 * EAGAIN if FNONBLOCK is set and no data to read
649 * EINTR if signal received while waiting for data
651 * While there is no data to read....
652 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
653 * - wait for a write.
659 fifo_read(struct vnode
*vp
, struct uio
*uiop
, int ioflag
, struct cred
*crp
,
660 caller_context_t
*ct
)
662 fifonode_t
*fnp
= VTOF(vp
);
664 fifolock_t
*fn_lock
= fnp
->fn_lock
;
668 ASSERT(vp
->v_stream
!= NULL
);
669 if (uiop
->uio_resid
== 0)
672 mutex_enter(&fn_lock
->flk_lock
);
674 TRACE_2(TR_FAC_FIFO
, TR_FIFOREAD_IN
, "fifo_read in:%p fnp %p", vp
, fnp
);
676 if (! (fnp
->fn_flag
& FIFOFAST
))
679 fn_dest
= fnp
->fn_dest
;
681 * Check for data on our input queue
684 while (fnp
->fn_count
== 0) {
686 * No data on first attempt and no writer, then EOF
688 if (fn_dest
->fn_wcnt
== 0 || fn_dest
->fn_rcnt
== 0) {
689 mutex_exit(&fn_lock
->flk_lock
);
693 * no data found.. if non-blocking, return EAGAIN
696 if (uiop
->uio_fmode
& (FNDELAY
|FNONBLOCK
)) {
697 mutex_exit(&fn_lock
->flk_lock
);
698 if (uiop
->uio_fmode
& FNONBLOCK
)
704 * Note: FIFOs can get here with FIFOCLOSE set if
705 * write side is in the middle of opeining after
706 * it once closed. Pipes better not have FIFOCLOSE set
708 ASSERT((fnp
->fn_flag
& (ISPIPE
|FIFOCLOSE
)) !=
713 fnp
->fn_flag
|= FIFOWANTR
;
715 TRACE_1(TR_FAC_FIFO
, TR_FIFOREAD_WAIT
, "fiforead wait: %p", vp
);
717 if (!cv_wait_sig_swap(&fnp
->fn_wait_cv
,
718 &fn_lock
->flk_lock
)) {
723 TRACE_1(TR_FAC_FIFO
, TR_FIFOREAD_WAKE
,
724 "fiforead awake: %p", vp
);
727 * check to make sure we are still in fast mode
729 if (!(fnp
->fn_flag
& FIFOFAST
))
733 ASSERT(fnp
->fn_mp
!= NULL
);
735 /* For pipes copy should not bypass cache */
736 uiop
->uio_extflg
|= UIO_COPY_CACHED
;
739 int bpsize
= MBLKL(fnp
->fn_mp
);
740 int uiosize
= MIN(bpsize
, uiop
->uio_resid
);
742 error
= uiomove(fnp
->fn_mp
->b_rptr
, uiosize
, UIO_READ
, uiop
);
746 fnp
->fn_count
-= uiosize
;
748 if (bpsize
<= uiosize
) {
750 fnp
->fn_mp
= fnp
->fn_mp
->b_cont
;
753 if (uiop
->uio_resid
== 0)
756 while (fnp
->fn_mp
== NULL
&& fn_dest
->fn_wwaitcnt
> 0) {
757 ASSERT(fnp
->fn_count
== 0);
759 if (uiop
->uio_fmode
& (FNDELAY
|FNONBLOCK
))
763 * We've consumed all available data but there
764 * are threads waiting to write more, let them
765 * proceed before bailing.
768 fnp
->fn_flag
|= FIFOWANTR
;
769 fifo_wakewriter(fn_dest
, fn_lock
);
771 if (!cv_wait_sig(&fnp
->fn_wait_cv
,
775 if (!(fnp
->fn_flag
& FIFOFAST
))
779 fnp
->fn_mp
->b_rptr
+= uiosize
;
780 ASSERT(uiop
->uio_resid
== 0);
782 } while (uiop
->uio_resid
!= 0 && fnp
->fn_mp
!= NULL
);
785 ASSERT(msgdsize(fnp
->fn_mp
) == fnp
->fn_count
);
788 * wake up any blocked writers, processes
789 * sleeping on POLLWRNORM, or processes waiting for SIGPOLL
790 * Note: checking for fn_count < Fifohiwat emulates
791 * STREAMS functionality when low water mark is 0
793 if (fn_dest
->fn_flag
& (FIFOWANTW
| FIFOHIWATW
) &&
794 fnp
->fn_count
< Fifohiwat
) {
795 fifo_wakewriter(fn_dest
, fn_lock
);
800 * FIFO is in streams mode.. let the stream head handle it
804 mutex_exit(&fn_lock
->flk_lock
);
806 TR_FIFOREAD_STREAM
, "fifo_read stream_mode:%p", vp
);
808 error
= strread(vp
, uiop
, crp
);
810 mutex_enter(&fn_lock
->flk_lock
);
814 * vnode update access time
817 time_t now
= gethrestime_sec();
819 if (fnp
->fn_flag
& ISPIPE
)
820 fnp
->fn_dest
->fn_atime
= now
;
823 TRACE_2(TR_FAC_FIFO
, TR_FIFOREAD_OUT
,
824 "fifo_read out:%p error %d", vp
, error
);
825 mutex_exit(&fn_lock
->flk_lock
);
830 * send SIGPIPE and return EPIPE if ...
831 * (1) broken pipe (essentially, reader is gone)
832 * (2) FIFO is not open for reading
835 * (2) user write request is for 0 bytes and SW_SNDZERO is not set
836 * Note: SW_SNDZERO can't be set in fast mode
837 * While the stream is flow controlled....
838 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
839 * - unlock the fifonode and sleep waiting for a reader.
840 * - if a pipe and it has a mate, sleep waiting for its mate
845 fifo_write(vnode_t
*vp
, uio_t
*uiop
, int ioflag
, cred_t
*crp
,
846 caller_context_t
*ct
)
848 struct fifonode
*fnp
, *fn_dest
;
858 ASSERT(vp
->v_stream
);
859 uiop
->uio_loffset
= 0;
863 * remember original number of bytes requested. Used to determine if
864 * we actually have written anything at all
866 write_size
= uiop
->uio_resid
;
869 * only send zero-length messages if SW_SNDZERO is set
870 * Note: we will be in streams mode if SW_SNDZERO is set
871 * XXX this streams interface should not be exposed
873 if ((write_size
== 0) && !(stp
->sd_wput_opt
& SW_SNDZERO
))
877 fn_lock
= fnp
->fn_lock
;
878 fn_dest
= fnp
->fn_dest
;
880 mutex_enter(&fn_lock
->flk_lock
);
882 TRACE_3(TR_FAC_FIFO
, TR_FIFOWRITE_IN
,
883 "fifo_write in:%p fnp %p size %d", vp
, fnp
, write_size
);
886 * oops, no readers, error
888 if (fn_dest
->fn_rcnt
== 0 || fn_dest
->fn_wcnt
== 0) {
893 * if we are not in fast mode, let streams handle it
895 if (!(fnp
->fn_flag
& FIFOFAST
))
898 fmode
= uiop
->uio_fmode
& (FNDELAY
|FNONBLOCK
);
900 /* For pipes copy should not bypass cache */
901 uiop
->uio_extflg
|= UIO_COPY_CACHED
;
905 * check to make sure we are not over high water mark
907 while (fn_dest
->fn_count
>= Fifohiwat
) {
909 * Indicate that we have gone over high
913 * if non-blocking, return
914 * only happens first time through loop
917 fnp
->fn_flag
|= FIFOHIWATW
;
918 if (uiop
->uio_resid
== write_size
) {
919 mutex_exit(&fn_lock
->flk_lock
);
929 * wait for things to drain
931 fnp
->fn_flag
|= FIFOWANTW
;
933 TRACE_1(TR_FAC_FIFO
, TR_FIFOWRITE_WAIT
,
934 "fifo_write wait: %p", vp
);
935 if (!cv_wait_sig_swap(&fnp
->fn_wait_cv
,
936 &fn_lock
->flk_lock
)) {
939 fifo_wakereader(fn_dest
, fn_lock
);
944 TRACE_1(TR_FAC_FIFO
, TR_FIFOWRITE_WAKE
,
945 "fifo_write wake: %p", vp
);
948 * check to make sure we're still in fast mode
950 if (!(fnp
->fn_flag
& FIFOFAST
))
954 * make sure readers didn't go away
956 if (fn_dest
->fn_rcnt
== 0 || fn_dest
->fn_wcnt
== 0) {
961 * If the write will put us over the high water mark,
962 * then we must break the message up into PIPE_BUF
963 * chunks to stay compliant with STREAMS
965 if (uiop
->uio_resid
+ fn_dest
->fn_count
> Fifohiwat
)
966 size
= MIN(uiop
->uio_resid
, PIPE_BUF
);
968 size
= uiop
->uio_resid
;
971 * We don't need to hold flk_lock across the allocb() and
972 * uiomove(). However, on a multiprocessor machine where both
973 * the reader and writer thread are on cpu's, we must be
974 * careful to only drop the lock if there's data to be read.
975 * This forces threads entering fifo_read() to spin or block
976 * on flk_lock, rather than acquiring flk_lock only to
977 * discover there's no data to read and being forced to go
978 * back to sleep, only to be woken up microseconds later by
979 * this writer thread.
981 hotread
= fn_dest
->fn_count
> 0;
983 if (!fifo_stayfast_enter(fnp
))
985 mutex_exit(&fn_lock
->flk_lock
);
990 * Align the mblk with the user data so that
991 * copying in the data can take advantage of
992 * the double word alignment
994 if ((bp
= allocb(size
+ 8, BPRI_MED
)) == NULL
) {
996 mutex_exit(&fn_lock
->flk_lock
);
998 error
= strwaitbuf(size
, BPRI_MED
);
1000 mutex_enter(&fn_lock
->flk_lock
);
1004 * As we dropped the mutex for a moment, we
1005 * need to wake up any thread waiting to be
1006 * allowed to go from fast mode to stream mode.
1008 fifo_stayfast_exit(fnp
);
1014 * check to make sure we're still in fast mode
1016 if (!(fnp
->fn_flag
& FIFOFAST
))
1020 * make sure readers didn't go away
1022 if (fn_dest
->fn_rcnt
== 0 || fn_dest
->fn_wcnt
== 0) {
1026 * some other thread could have gotten in
1027 * need to go back and check hi water mark
1031 bp
->b_rptr
+= ((uintptr_t)uiop
->uio_iov
->iov_base
& 0x7);
1032 bp
->b_wptr
= bp
->b_rptr
+ size
;
1033 error
= uiomove((caddr_t
)bp
->b_rptr
, size
, UIO_WRITE
, uiop
);
1035 mutex_enter(&fn_lock
->flk_lock
);
1037 * As we dropped the mutex for a moment, we need to:
1038 * - wake up any thread waiting to be allowed to go
1039 * from fast mode to stream mode,
1040 * - make sure readers didn't go away.
1042 fifo_stayfast_exit(fnp
);
1043 if (fn_dest
->fn_rcnt
== 0 || fn_dest
->fn_wcnt
== 0) {
1054 fn_dest
->fn_count
+= size
;
1055 if (fn_dest
->fn_mp
!= NULL
) {
1056 fn_dest
->fn_tail
->b_cont
= bp
;
1057 fn_dest
->fn_tail
= bp
;
1059 fn_dest
->fn_mp
= fn_dest
->fn_tail
= bp
;
1061 * This is the first bit of data; wake up any sleeping
1062 * readers, processes blocked in poll, and those
1063 * expecting a SIGPOLL.
1065 fifo_wakereader(fn_dest
, fn_lock
);
1067 } while (uiop
->uio_resid
!= 0);
1074 * let the stream head handle the write
1076 ASSERT(MUTEX_HELD(&fn_lock
->flk_lock
));
1078 mutex_exit(&fn_lock
->flk_lock
);
1079 TRACE_1(TR_FAC_FIFO
,
1080 TR_FIFOWRITE_STREAM
, "fifo_write stream_mode:%p", vp
);
1082 error
= strwrite(vp
, uiop
, crp
);
1084 mutex_enter(&fn_lock
->flk_lock
);
1088 * update vnode modification and change times
1089 * make sure there were no errors and some data was transferred
1091 if (error
== 0 && write_size
!= uiop
->uio_resid
) {
1092 time_t now
= gethrestime_sec();
1094 if (fnp
->fn_flag
& ISPIPE
) {
1095 fn_dest
->fn_mtime
= fn_dest
->fn_ctime
= now
;
1097 fnp
->fn_mtime
= fnp
->fn_ctime
= now
;
1098 } else if (fn_dest
->fn_rcnt
== 0 || fn_dest
->fn_wcnt
== 0) {
1101 TRACE_3(TR_FAC_FIFO
, TR_FIFOWRITE_OUT
,
1102 "fifo_write out: vp %p error %d fnp %p", vp
, error
, fnp
);
1103 mutex_exit(&fn_lock
->flk_lock
);
1107 TRACE_3(TR_FAC_FIFO
, TR_FIFOWRITE_OUT
,
1108 "fifo_write out: vp %p error %d fnp %p", vp
, error
, fnp
);
1109 mutex_exit(&fn_lock
->flk_lock
);
1110 tsignal(curthread
, SIGPIPE
);
1116 fifo_ioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int mode
, cred_t
*cr
,
1117 int *rvalp
, caller_context_t
*ct
)
1120 * Just a quick check
1121 * Once we go to streams mode we don't ever revert back
1122 * So we do this quick check so as not to incur the overhead
1123 * associated with acquiring the lock
1125 return ((VTOF(vp
)->fn_flag
& FIFOFAST
) ?
1126 fifo_fastioctl(vp
, cmd
, arg
, mode
, cr
, rvalp
) :
1127 fifo_strioctl(vp
, cmd
, arg
, mode
, cr
, rvalp
));
1131 fifo_fastioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int mode
, cred_t
*cr
,
1134 fifonode_t
*fnp
= VTOF(vp
);
1135 fifonode_t
*fn_dest
;
1137 fifolock_t
*fn_lock
= fnp
->fn_lock
;
1141 * tty operations not allowed
1143 if (((cmd
& IOCTYPE
) == LDIOC
) ||
1144 ((cmd
& IOCTYPE
) == tIOC
) ||
1145 ((cmd
& IOCTYPE
) == TIOC
)) {
1149 mutex_enter(&fn_lock
->flk_lock
);
1151 if (!(fnp
->fn_flag
& FIFOFAST
)) {
1158 * Things we can't handle
1159 * These will switch us to streams mode.
1176 * Things that don't do damage
1177 * These things don't adjust the state of the
1178 * stream head (i_setcltime does, but we don't care)
1184 case I_GRDOPT
: /* probably should not get this, but no harm */
1189 mutex_exit(&fn_lock
->flk_lock
);
1190 return (strioctl(vp
, cmd
, arg
, mode
, U_TO_K
, cr
, rvalp
));
1194 * We can only handle normal band canputs.
1195 * XXX : We could just always go to stream mode; after all
1196 * canput is a streams semantics type thing
1201 *rvalp
= (fnp
->fn_dest
->fn_count
< Fifohiwat
) ? 1 : 0;
1202 mutex_exit(&fn_lock
->flk_lock
);
1207 * This may seem a bit silly for non-streams semantics,
1208 * (After all, if they really want a message, they'll
1209 * probably use getmsg() anyway). but it doesn't hurt
1211 error
= copyout((caddr_t
)&fnp
->fn_count
, (caddr_t
)arg
,
1214 *rvalp
= (fnp
->fn_count
== 0) ? 0 : 1;
1219 *rvalp
= fnp
->fn_count
;
1224 STRUCT_DECL(strpeek
, strpeek
);
1231 STRUCT_INIT(strpeek
, mode
);
1233 if (fnp
->fn_count
== 0) {
1238 error
= copyin((caddr_t
)arg
, STRUCT_BUF(strpeek
),
1239 STRUCT_SIZE(strpeek
));
1244 * can't have any high priority message when in fast mode
1246 if (STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
) {
1251 len
= STRUCT_FGET(strpeek
, databuf
.maxlen
);
1253 STRUCT_FSET(strpeek
, databuf
.len
, len
);
1255 iov
.iov_base
= STRUCT_FGETP(strpeek
, databuf
.buf
);
1259 uio
.uio_loffset
= 0;
1260 uio
.uio_segflg
= UIO_USERSPACE
;
1262 /* For pipes copy should not bypass cache */
1263 uio
.uio_extflg
= UIO_COPY_CACHED
;
1264 uio
.uio_resid
= iov
.iov_len
;
1265 count
= fnp
->fn_count
;
1267 while (count
> 0 && uio
.uio_resid
) {
1268 cnt
= MIN(uio
.uio_resid
, MBLKL(bp
));
1269 if ((error
= uiomove((char *)bp
->b_rptr
, cnt
,
1270 UIO_READ
, &uio
)) != 0) {
1276 STRUCT_FSET(strpeek
, databuf
.len
, len
- uio
.uio_resid
);
1278 STRUCT_FSET(strpeek
, flags
, 0);
1279 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
1281 error
= copyout(STRUCT_BUF(strpeek
), (caddr_t
)arg
,
1282 STRUCT_SIZE(strpeek
));
1283 if (error
== 0 && len
>= 0)
1290 * let user know total number of bytes in message queue
1292 error
= copyout((caddr_t
)&fnp
->fn_count
, (caddr_t
)arg
,
1293 sizeof (fnp
->fn_count
));
1300 * let streams set up the signal masking for us
1301 * we just check to see if it's set
1302 * XXX : this interface should not be visible
1303 * i.e. STREAM's framework is exposed.
1305 error
= strioctl(vp
, cmd
, arg
, mode
, U_TO_K
, cr
, rvalp
);
1306 if (vp
->v_stream
->sd_sigflags
& (S_INPUT
|S_RDNORM
|S_WRNORM
))
1307 fnp
->fn_flag
|= FIFOSETSIG
;
1309 fnp
->fn_flag
&= ~FIFOSETSIG
;
1314 * flush them message queues
1316 if (arg
& ~FLUSHRW
) {
1321 fifo_fastflush(fnp
);
1323 fn_dest
= fnp
->fn_dest
;
1324 if ((arg
& FLUSHW
)) {
1325 fifo_fastflush(fn_dest
);
1328 * wake up any sleeping readers or writers
1329 * (waking readers probably doesn't make sense, but it
1330 * doesn't hurt; i.e. we just got rid of all the data
1333 if (fn_dest
->fn_flag
& (FIFOWANTW
| FIFOWANTR
)) {
1334 fn_dest
->fn_flag
&= ~(FIFOWANTW
| FIFOWANTR
);
1335 cv_broadcast(&fn_dest
->fn_wait_cv
);
1341 * Since no band data can ever get on a fifo in fast mode
1350 * invalid calls for stream head or fifos
1353 case I_POP
: /* shouldn't happen */
1361 * more invalid tty type of ioctls
1370 mutex_exit(&fn_lock
->flk_lock
);
1380 mutex_exit(&fn_lock
->flk_lock
);
1381 return (fifo_strioctl(vp
, cmd
, arg
, mode
, cr
, rvalp
));
1386 * FIFO is in STREAMS mode; STREAMS framework does most of the work.
1389 fifo_strioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int mode
, cred_t
*cr
,
1392 fifonode_t
*fnp
= VTOF(vp
);
1394 fifolock_t
*fn_lock
;
1396 if (cmd
== _I_GETPEERCRED
) {
1397 if (mode
== FKIOCTL
&& fnp
->fn_pcredp
!= NULL
) {
1398 k_peercred_t
*kp
= (k_peercred_t
*)arg
;
1399 crhold(fnp
->fn_pcredp
);
1400 kp
->pc_cr
= fnp
->fn_pcredp
;
1401 kp
->pc_cpid
= fnp
->fn_cpid
;
1408 error
= strioctl(vp
, cmd
, arg
, mode
, U_TO_K
, cr
, rvalp
);
1412 * The FIFOSEND flag is set to inform other processes that a file
1413 * descriptor is pending at the stream head of this pipe.
1414 * The flag is cleared and the sending process is awoken when
1415 * this process has completed receiving the file descriptor.
1416 * XXX This could become out of sync if the process does I_SENDFDs
1417 * and opens on connld attached to the same pipe.
1422 fn_lock
= fnp
->fn_lock
;
1423 mutex_enter(&fn_lock
->flk_lock
);
1424 if (fnp
->fn_flag
& FIFOSEND
) {
1425 fnp
->fn_flag
&= ~FIFOSEND
;
1426 cv_broadcast(&fnp
->fn_dest
->fn_wait_cv
);
1428 mutex_exit(&fn_lock
->flk_lock
);
1439 * If shadowing a vnode (FIFOs), apply the VOP_GETATTR to the shadowed
1440 * vnode to Obtain the node information. If not shadowing (pipes), obtain
1441 * the node information from the credentials structure.
1444 fifo_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*crp
,
1445 caller_context_t
*ct
)
1448 fifonode_t
*fnp
= VTOF(vp
);
1451 fifolock_t
*fn_lock
= fnp
->fn_lock
;
1453 if (fnp
->fn_realvp
) {
1455 * for FIFOs or mounted pipes
1457 if (error
= VOP_GETATTR(fnp
->fn_realvp
, vap
, flags
, crp
, ct
))
1459 mutex_enter(&fn_lock
->flk_lock
);
1460 /* set current times from fnode, even if older than vnode */
1461 vap
->va_atime
.tv_sec
= fnp
->fn_atime
;
1462 vap
->va_atime
.tv_nsec
= 0;
1463 vap
->va_mtime
.tv_sec
= fnp
->fn_mtime
;
1464 vap
->va_mtime
.tv_nsec
= 0;
1465 vap
->va_ctime
.tv_sec
= fnp
->fn_ctime
;
1466 vap
->va_ctime
.tv_nsec
= 0;
1469 * for non-attached/ordinary pipes
1472 mutex_enter(&fn_lock
->flk_lock
);
1473 vap
->va_atime
.tv_sec
= fnp
->fn_atime
;
1474 vap
->va_atime
.tv_nsec
= 0;
1475 vap
->va_mtime
.tv_sec
= fnp
->fn_mtime
;
1476 vap
->va_mtime
.tv_nsec
= 0;
1477 vap
->va_ctime
.tv_sec
= fnp
->fn_ctime
;
1478 vap
->va_ctime
.tv_nsec
= 0;
1479 vap
->va_uid
= crgetuid(crp
);
1480 vap
->va_gid
= crgetgid(crp
);
1482 vap
->va_fsid
= fifodev
;
1483 vap
->va_nodeid
= (ino64_t
)fnp
->fn_ino
;
1486 vap
->va_type
= VFIFO
;
1487 vap
->va_blksize
= PIPE_BUF
;
1489 * Size is number of un-read bytes at the stream head and
1490 * nblocks is the unread bytes expressed in blocks.
1492 if (vp
->v_stream
&& (fnp
->fn_flag
& FIFOISOPEN
)) {
1493 if ((fnp
->fn_flag
& FIFOFAST
)) {
1494 vap
->va_size
= (u_offset_t
)fnp
->fn_count
;
1496 qp
= RD((strvp2wq(vp
)));
1497 vap
->va_size
= (u_offset_t
)qp
->q_count
;
1498 if (qp
->q_nband
!= 0) {
1499 mutex_enter(QLOCK(qp
));
1500 for (bandp
= qp
->q_bandp
; bandp
;
1501 bandp
= bandp
->qb_next
)
1502 vap
->va_size
+= bandp
->qb_count
;
1503 mutex_exit(QLOCK(qp
));
1506 vap
->va_nblocks
= (fsblkcnt64_t
)btod(vap
->va_size
);
1508 vap
->va_size
= (u_offset_t
)0;
1509 vap
->va_nblocks
= (fsblkcnt64_t
)0;
1511 mutex_exit(&fn_lock
->flk_lock
);
1517 * If shadowing a vnode, apply the VOP_SETATTR to it, and to the fnode.
1518 * Otherwise, set the time and return 0.
1521 fifo_setattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*crp
,
1522 caller_context_t
*ctp
)
1524 fifonode_t
*fnp
= VTOF(vp
);
1526 fifolock_t
*fn_lock
;
1529 error
= VOP_SETATTR(fnp
->fn_realvp
, vap
, flags
, crp
, ctp
);
1531 fn_lock
= fnp
->fn_lock
;
1532 mutex_enter(&fn_lock
->flk_lock
);
1533 if (vap
->va_mask
& AT_ATIME
)
1534 fnp
->fn_atime
= vap
->va_atime
.tv_sec
;
1535 if (vap
->va_mask
& AT_MTIME
)
1536 fnp
->fn_mtime
= vap
->va_mtime
.tv_sec
;
1537 fnp
->fn_ctime
= gethrestime_sec();
1538 mutex_exit(&fn_lock
->flk_lock
);
1544 * If shadowing a vnode, apply VOP_ACCESS to it.
1545 * Otherwise, return 0 (allow all access).
1548 fifo_access(vnode_t
*vp
, int mode
, int flags
, cred_t
*crp
, caller_context_t
*ct
)
1550 if (VTOF(vp
)->fn_realvp
)
1551 return (VOP_ACCESS(VTOF(vp
)->fn_realvp
, mode
, flags
, crp
, ct
));
1557 * This can be called if creat or an open with O_CREAT is done on the root
1558 * of a lofs mount where the mounted entity is a fifo.
1562 fifo_create(struct vnode
*dvp
, char *name
, vattr_t
*vap
, enum vcexcl excl
,
1563 int mode
, struct vnode
**vpp
, struct cred
*cr
, int flag
,
1564 caller_context_t
*ct
, vsecattr_t
*vsecp
)
1568 ASSERT(dvp
&& (dvp
->v_flag
& VROOT
) && *name
== '\0');
1569 if (excl
== NONEXCL
) {
1570 if (mode
&& (error
= fifo_access(dvp
, mode
, 0, cr
, ct
)))
1579 * If shadowing a vnode, apply the VOP_FSYNC to it.
1580 * Otherwise, return 0.
1583 fifo_fsync(vnode_t
*vp
, int syncflag
, cred_t
*crp
, caller_context_t
*ct
)
1585 fifonode_t
*fnp
= VTOF(vp
);
1588 if (fnp
->fn_realvp
== NULL
)
1591 bzero((caddr_t
)&va
, sizeof (va
));
1592 va
.va_mask
= AT_MTIME
| AT_ATIME
;
1593 if (VOP_GETATTR(fnp
->fn_realvp
, &va
, 0, crp
, ct
) == 0) {
1595 if (fnp
->fn_mtime
> va
.va_mtime
.tv_sec
) {
1596 va
.va_mtime
.tv_sec
= fnp
->fn_mtime
;
1597 va
.va_mask
= AT_MTIME
;
1599 if (fnp
->fn_atime
> va
.va_atime
.tv_sec
) {
1600 va
.va_atime
.tv_sec
= fnp
->fn_atime
;
1601 va
.va_mask
|= AT_ATIME
;
1603 if (va
.va_mask
!= 0)
1604 (void) VOP_SETATTR(fnp
->fn_realvp
, &va
, 0, crp
, ct
);
1606 return (VOP_FSYNC(fnp
->fn_realvp
, syncflag
, crp
, ct
));
1610 * Called when the upper level no longer holds references to the
1611 * vnode. Sync the file system and free the fifonode.
1614 fifo_inactive(vnode_t
*vp
, cred_t
*crp
, caller_context_t
*ct
)
1617 fifolock_t
*fn_lock
;
1619 mutex_enter(&ftable_lock
);
1620 mutex_enter(&vp
->v_lock
);
1621 ASSERT(vp
->v_count
>= 1);
1623 if (vp
->v_count
!= 0) {
1625 * Somebody accessed the fifo before we got a chance to
1626 * remove it. They will remove it when they do a vn_rele.
1628 mutex_exit(&vp
->v_lock
);
1629 mutex_exit(&ftable_lock
);
1632 mutex_exit(&vp
->v_lock
);
1637 * remove fifo from fifo list so that no other process
1639 * Drop the reference count on the fifo node's
1642 if (fnp
->fn_realvp
) {
1643 (void) fiforemove(fnp
);
1644 mutex_exit(&ftable_lock
);
1645 (void) fifo_fsync(vp
, FSYNC
, crp
, ct
);
1646 VN_RELE(fnp
->fn_realvp
);
1647 VFS_RELE(vp
->v_vfsp
);
1650 mutex_exit(&ftable_lock
);
1652 fn_lock
= fnp
->fn_lock
;
1654 mutex_enter(&fn_lock
->flk_lock
);
1655 ASSERT(vp
->v_stream
== NULL
);
1656 ASSERT(vp
->v_count
== 0);
1658 * if this is last reference to the lock, then we can
1659 * free everything up.
1661 if (--fn_lock
->flk_ref
== 0) {
1662 mutex_exit(&fn_lock
->flk_lock
);
1663 ASSERT(fnp
->fn_open
== 0);
1664 ASSERT(fnp
->fn_dest
->fn_open
== 0);
1666 freemsg(fnp
->fn_mp
);
1670 if (fnp
->fn_pcredp
!= NULL
) {
1671 crfree(fnp
->fn_pcredp
);
1672 fnp
->fn_pcredp
= NULL
;
1674 if (fnp
->fn_flag
& ISPIPE
) {
1675 fifonode_t
*fn_dest
= fnp
->fn_dest
;
1678 if (fn_dest
->fn_mp
) {
1679 freemsg(fn_dest
->fn_mp
);
1680 fn_dest
->fn_mp
= NULL
;
1681 fn_dest
->fn_count
= 0;
1683 if (fn_dest
->fn_pcredp
!= NULL
) {
1684 crfree(fn_dest
->fn_pcredp
);
1685 fn_dest
->fn_pcredp
= NULL
;
1687 kmem_cache_free(pipe_cache
, (fifodata_t
*)fn_lock
);
1689 kmem_cache_free(fnode_cache
, (fifodata_t
*)fn_lock
);
1691 mutex_exit(&fn_lock
->flk_lock
);
1696 * If shadowing a vnode, apply the VOP_FID to it.
1697 * Otherwise, return EINVAL.
1700 fifo_fid(vnode_t
*vp
, fid_t
*fidfnp
, caller_context_t
*ct
)
1702 if (VTOF(vp
)->fn_realvp
)
1703 return (VOP_FID(VTOF(vp
)->fn_realvp
, fidfnp
, ct
));
1713 fifo_rwlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
1719 * Unlock a fifonode.
1723 fifo_rwunlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
1728 * Return error since seeks are not allowed on pipes.
1732 fifo_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, caller_context_t
*ct
)
1738 * If there is a realvp associated with vp, return it.
1741 fifo_realvp(vnode_t
*vp
, vnode_t
**vpp
, caller_context_t
*ct
)
1745 if ((rvp
= VTOF(vp
)->fn_realvp
) != NULL
) {
1747 if (VOP_REALVP(vp
, &rvp
, ct
) == 0)
1756 * Poll for interesting events on a stream pipe
1760 fifo_poll(vnode_t
*vp
, short events
, int anyyet
, short *reventsp
,
1761 pollhead_t
**phpp
, caller_context_t
*ct
)
1763 fifonode_t
*fnp
, *fn_dest
;
1764 fifolock_t
*fn_lock
;
1768 ASSERT(vp
->v_stream
!= NULL
);
1773 fn_dest
= fnp
->fn_dest
;
1774 fn_lock
= fnp
->fn_lock
;
1776 if (polllock(&stp
->sd_pollist
, &fn_lock
->flk_lock
) != 0) {
1777 *reventsp
= POLLNVAL
;
1782 * see if FIFO/pipe open
1784 if ((fnp
->fn_flag
& FIFOISOPEN
) == 0) {
1785 if (((events
& (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
)) &&
1786 fnp
->fn_rcnt
== 0) ||
1787 ((events
& (POLLWRNORM
| POLLWRBAND
)) &&
1788 fnp
->fn_wcnt
== 0)) {
1789 mutex_exit(&fnp
->fn_lock
->flk_lock
);
1790 *reventsp
= POLLERR
;
1796 * if not in fast mode, let the stream head take care of it
1798 if (!(fnp
->fn_flag
& FIFOFAST
)) {
1799 mutex_exit(&fnp
->fn_lock
->flk_lock
);
1804 * If this is a pipe.. check to see if the other
1805 * end is gone. If we are a fifo, check to see
1806 * if write end is gone.
1809 if ((fnp
->fn_flag
& ISPIPE
) && (fn_dest
->fn_open
== 0)) {
1810 retevents
= POLLHUP
;
1811 } else if ((fnp
->fn_flag
& (FIFOCLOSE
| ISPIPE
)) == FIFOCLOSE
&&
1812 (fn_dest
->fn_wcnt
== 0)) {
1814 * no writer at other end.
1815 * it was closed (versus yet to be opened)
1817 retevents
= POLLHUP
;
1818 } else if (events
& (POLLWRNORM
| POLLWRBAND
)) {
1819 if (events
& POLLWRNORM
) {
1820 if (fn_dest
->fn_count
< Fifohiwat
)
1821 retevents
= POLLWRNORM
;
1823 fnp
->fn_flag
|= FIFOHIWATW
;
1826 * This is always true for fast pipes
1827 * (Note: will go to STREAMS mode if band data is written)
1829 if (events
& POLLWRBAND
)
1830 retevents
|= POLLWRBAND
;
1832 if (events
& (POLLIN
| POLLRDNORM
)) {
1834 retevents
|= (events
& (POLLIN
| POLLRDNORM
));
1838 * if we happened to get something and we're not edge-triggered, return
1840 if ((*reventsp
= (short)retevents
) != 0 && !(events
& POLLET
)) {
1841 mutex_exit(&fnp
->fn_lock
->flk_lock
);
1846 * If poll() has not found any events yet or we're edge-triggered, set
1847 * up event cell to wake up the poll if a requested event occurs on this
1851 if (events
& POLLWRNORM
)
1852 fnp
->fn_flag
|= FIFOPOLLW
;
1853 if (events
& (POLLIN
| POLLRDNORM
))
1854 fnp
->fn_flag
|= FIFOPOLLR
;
1855 if (events
& POLLRDBAND
)
1856 fnp
->fn_flag
|= FIFOPOLLRBAND
;
1858 * XXX Don't like exposing this from streams
1860 *phpp
= &stp
->sd_pollist
;
1862 mutex_exit(&fnp
->fn_lock
->flk_lock
);
1865 return (strpoll(stp
, events
, anyyet
, reventsp
, phpp
));
1869 * POSIX pathconf() support.
1873 fifo_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
1874 caller_context_t
*ct
)
1898 case _PC_SYMLINK_MAX
:
1907 if (vp
->v_vfsp
->vfs_flag
& VFS_NOTRUNC
)
1908 val
= 1; /* NOTRUNC is enabled for vp */
1914 val
= _POSIX_VDISABLE
;
1917 case _PC_CHOWN_RESTRICTED
:
1919 val
= rstchown
; /* chown restricted enabled */
1924 case _PC_FILESIZEBITS
:
1929 if (VTOF(vp
)->fn_realvp
)
1930 error
= VOP_PATHCONF(VTOF(vp
)->fn_realvp
, cmd
,
1943 * If shadowing a vnode, apply VOP_SETSECATTR to it.
1944 * Otherwise, return NOSYS.
1947 fifo_setsecattr(struct vnode
*vp
, vsecattr_t
*vsap
, int flag
, struct cred
*crp
,
1948 caller_context_t
*ct
)
1953 * The acl(2) system call tries to grab the write lock on the
1954 * file when setting an ACL, but fifofs does not implement
1955 * VOP_RWLOCK or VOP_RWUNLOCK, so we do it here instead.
1957 if (VTOF(vp
)->fn_realvp
) {
1958 (void) VOP_RWLOCK(VTOF(vp
)->fn_realvp
, V_WRITELOCK_TRUE
, ct
);
1959 error
= VOP_SETSECATTR(VTOF(vp
)->fn_realvp
, vsap
, flag
,
1961 VOP_RWUNLOCK(VTOF(vp
)->fn_realvp
, V_WRITELOCK_TRUE
, ct
);
1964 return (fs_nosys());
1968 * If shadowing a vnode, apply VOP_GETSECATTR to it. Otherwise, fabricate
1969 * an ACL from the permission bits that fifo_getattr() makes up.
1972 fifo_getsecattr(struct vnode
*vp
, vsecattr_t
*vsap
, int flag
, struct cred
*crp
,
1973 caller_context_t
*ct
)
1975 if (VTOF(vp
)->fn_realvp
)
1976 return (VOP_GETSECATTR(VTOF(vp
)->fn_realvp
, vsap
, flag
,
1979 return (fs_fab_acl(vp
, vsap
, flag
, crp
, ct
));
1984 * Set the FIFOSTAYFAST flag so nobody can turn the fifo into stream mode.
1985 * If the flag is already set then wait until it is removed - releasing
1987 * If the fifo switches into stream mode while we are waiting, return failure.
1990 fifo_stayfast_enter(fifonode_t
*fnp
)
1992 ASSERT(MUTEX_HELD(&fnp
->fn_lock
->flk_lock
));
1993 while (fnp
->fn_flag
& FIFOSTAYFAST
) {
1994 fnp
->fn_flag
|= FIFOWAITMODE
;
1995 cv_wait(&fnp
->fn_wait_cv
, &fnp
->fn_lock
->flk_lock
);
1996 fnp
->fn_flag
&= ~FIFOWAITMODE
;
1998 if (!(fnp
->fn_flag
& FIFOFAST
))
2001 fnp
->fn_flag
|= FIFOSTAYFAST
;
2006 * Unset the FIFOSTAYFAST flag and notify anybody waiting for this flag
2008 * - threads wanting to turn into stream mode waiting in fifo_fastoff(),
2009 * - other writers threads waiting in fifo_stayfast_enter().
2012 fifo_stayfast_exit(fifonode_t
*fnp
)
2014 fifonode_t
*fn_dest
= fnp
->fn_dest
;
2016 ASSERT(MUTEX_HELD(&fnp
->fn_lock
->flk_lock
));
2018 fnp
->fn_flag
&= ~FIFOSTAYFAST
;
2020 if (fnp
->fn_flag
& FIFOWAITMODE
)
2021 cv_broadcast(&fnp
->fn_wait_cv
);
2023 if ((fnp
->fn_flag
& ISPIPE
) && (fn_dest
->fn_flag
& FIFOWAITMODE
))
2024 cv_broadcast(&fn_dest
->fn_wait_cv
);