Print this page
6474 event ports are broken with FIFOs
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/fifofs/fifovnops.c
+++ new/usr/src/uts/common/fs/fifofs/fifovnops.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All rights reserved. */
23 23
24 24
25 25 /*
26 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 /*
31 31 * Copyright 2015, Joyent, Inc.
32 32 */
33 33
34 34 /*
35 35 * FIFOFS file system vnode operations. This file system
36 36 * type supports STREAMS-based pipes and FIFOs.
37 37 */
38 38 #include <sys/types.h>
39 39 #include <sys/param.h>
40 40 #include <sys/systm.h>
41 41 #include <sys/sysmacros.h>
42 42 #include <sys/cred.h>
43 43 #include <sys/errno.h>
44 44 #include <sys/time.h>
45 45 #include <sys/file.h>
46 46 #include <sys/fcntl.h>
47 47 #include <sys/kmem.h>
48 48 #include <sys/uio.h>
49 49 #include <sys/vfs.h>
50 50 #include <sys/vnode.h>
51 51 #include <sys/vfs_opreg.h>
52 52 #include <sys/pathname.h>
53 53 #include <sys/signal.h>
54 54 #include <sys/user.h>
55 55 #include <sys/strsubr.h>
56 56 #include <sys/stream.h>
57 57 #include <sys/strsun.h>
58 58 #include <sys/strredir.h>
59 59 #include <sys/fs/fifonode.h>
60 60 #include <sys/fs/namenode.h>
61 61 #include <sys/stropts.h>
62 62 #include <sys/proc.h>
63 63 #include <sys/unistd.h>
64 64 #include <sys/debug.h>
65 65 #include <fs/fs_subr.h>
66 66 #include <sys/filio.h>
67 67 #include <sys/termio.h>
68 68 #include <sys/ddi.h>
69 69 #include <sys/vtrace.h>
70 70 #include <sys/policy.h>
71 71 #include <sys/tsol/label.h>
72 72
73 73 /*
74 74 * Define the routines/data structures used in this file.
75 75 */
76 76 static int fifo_read(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
77 77 static int fifo_write(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
78 78 static int fifo_getattr(vnode_t *, vattr_t *, int, cred_t *,
79 79 caller_context_t *);
80 80 static int fifo_setattr(vnode_t *, vattr_t *, int, cred_t *,
81 81 caller_context_t *);
82 82 static int fifo_realvp(vnode_t *, vnode_t **, caller_context_t *);
83 83 static int fifo_access(vnode_t *, int, int, cred_t *, caller_context_t *);
84 84 static int fifo_create(struct vnode *, char *, vattr_t *, enum vcexcl,
85 85 int, struct vnode **, struct cred *, int, caller_context_t *,
86 86 vsecattr_t *);
87 87 static int fifo_fid(vnode_t *, fid_t *, caller_context_t *);
88 88 static int fifo_fsync(vnode_t *, int, cred_t *, caller_context_t *);
89 89 static int fifo_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
90 90 static int fifo_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
91 91 caller_context_t *);
92 92 static int fifo_fastioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
93 93 static int fifo_strioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
94 94 static int fifo_poll(vnode_t *, short, int, short *, pollhead_t **,
95 95 caller_context_t *);
96 96 static int fifo_pathconf(vnode_t *, int, ulong_t *, cred_t *,
97 97 caller_context_t *);
98 98 static void fifo_inactive(vnode_t *, cred_t *, caller_context_t *);
99 99 static int fifo_rwlock(vnode_t *, int, caller_context_t *);
100 100 static void fifo_rwunlock(vnode_t *, int, caller_context_t *);
101 101 static int fifo_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
102 102 caller_context_t *);
103 103 static int fifo_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
104 104 caller_context_t *);
105 105
106 106 /* functions local to this file */
107 107 static boolean_t fifo_stayfast_enter(fifonode_t *);
108 108 static void fifo_stayfast_exit(fifonode_t *);
109 109
110 110 /*
111 111 * Define the data structures external to this file.
112 112 */
113 113 extern dev_t fifodev;
114 114 extern struct qinit fifo_stwdata;
115 115 extern struct qinit fifo_strdata;
116 116 extern kmutex_t ftable_lock;
117 117
118 118 struct streamtab fifoinfo = { &fifo_strdata, &fifo_stwdata, NULL, NULL };
119 119
120 120 struct vnodeops *fifo_vnodeops;
121 121
122 122 const fs_operation_def_t fifo_vnodeops_template[] = {
123 123 VOPNAME_OPEN, { .vop_open = fifo_open },
124 124 VOPNAME_CLOSE, { .vop_close = fifo_close },
125 125 VOPNAME_READ, { .vop_read = fifo_read },
126 126 VOPNAME_WRITE, { .vop_write = fifo_write },
127 127 VOPNAME_IOCTL, { .vop_ioctl = fifo_ioctl },
128 128 VOPNAME_GETATTR, { .vop_getattr = fifo_getattr },
129 129 VOPNAME_SETATTR, { .vop_setattr = fifo_setattr },
130 130 VOPNAME_ACCESS, { .vop_access = fifo_access },
131 131 VOPNAME_CREATE, { .vop_create = fifo_create },
132 132 VOPNAME_FSYNC, { .vop_fsync = fifo_fsync },
133 133 VOPNAME_INACTIVE, { .vop_inactive = fifo_inactive },
134 134 VOPNAME_FID, { .vop_fid = fifo_fid },
135 135 VOPNAME_RWLOCK, { .vop_rwlock = fifo_rwlock },
136 136 VOPNAME_RWUNLOCK, { .vop_rwunlock = fifo_rwunlock },
137 137 VOPNAME_SEEK, { .vop_seek = fifo_seek },
138 138 VOPNAME_REALVP, { .vop_realvp = fifo_realvp },
139 139 VOPNAME_POLL, { .vop_poll = fifo_poll },
140 140 VOPNAME_PATHCONF, { .vop_pathconf = fifo_pathconf },
141 141 VOPNAME_DISPOSE, { .error = fs_error },
142 142 VOPNAME_SETSECATTR, { .vop_setsecattr = fifo_setsecattr },
143 143 VOPNAME_GETSECATTR, { .vop_getsecattr = fifo_getsecattr },
144 144 NULL, NULL
145 145 };
146 146
147 147 /*
148 148 * Return the fifoinfo structure.
149 149 */
150 150 struct streamtab *
151 151 fifo_getinfo()
152 152 {
153 153 return (&fifoinfo);
154 154 }
155 155
156 156 /*
157 157 * Trusted Extensions enforces a restrictive policy for
158 158 * writing via cross-zone named pipes. A privileged global
159 159 * zone process may expose a named pipe by loopback mounting
160 160 * it from a lower-level zone to a higher-level zone. The
161 161 * kernel-enforced mount policy for lofs mounts ensures
162 162 * that such mounts are read-only in the higher-level
163 163 * zone. But this is not sufficient to prevent writing
164 164 * down via fifos. This function prevents writing down
165 165 * by comparing the zone of the process which is requesting
166 166 * write access with the zone owning the named pipe rendezvous.
167 167 * For write access the zone of the named pipe must equal the
168 168 * zone of the writing process. Writing up is possible since
169 169 * the named pipe can be opened for read by a process in a
170 170 * higher level zone.
171 171 *
172 172 * An exception is made for the global zone to support trusted
173 173 * processes which enforce their own data flow policies.
174 174 */
175 175 static boolean_t
176 176 tsol_fifo_access(vnode_t *vp, int flag, cred_t *crp)
177 177 {
178 178 fifonode_t *fnp = VTOF(vp);
179 179
180 180 if (is_system_labeled() &&
181 181 (flag & FWRITE) &&
182 182 (!(fnp->fn_flag & ISPIPE))) {
183 183 zone_t *proc_zone;
184 184
185 185 proc_zone = crgetzone(crp);
186 186 if (proc_zone != global_zone) {
187 187 char vpath[MAXPATHLEN];
188 188 zone_t *fifo_zone;
189 189
190 190 /*
191 191 * Get the pathname and use it to find
192 192 * the zone of the fifo.
193 193 */
194 194 if (vnodetopath(rootdir, vp, vpath, sizeof (vpath),
195 195 kcred) == 0) {
196 196 fifo_zone = zone_find_by_path(vpath);
197 197 zone_rele(fifo_zone);
198 198
199 199 if (fifo_zone != global_zone &&
200 200 fifo_zone != proc_zone) {
201 201 return (B_FALSE);
202 202 }
203 203 } else {
204 204 return (B_FALSE);
205 205 }
206 206 }
207 207 }
208 208 return (B_TRUE);
209 209 }
210 210
211 211 /*
212 212 * Open and stream a FIFO.
213 213 * If this is the first open of the file (FIFO is not streaming),
214 214 * initialize the fifonode and attach a stream to the vnode.
215 215 *
216 216 * Each end of a fifo must be synchronized with the other end.
217 217 * If not, the mated end may complete an open, I/O, close sequence
218 218 * before the end waiting in open ever wakes up.
219 219 * Note: namefs pipes come through this routine too.
220 220 */
221 221 int
222 222 fifo_open(vnode_t **vpp, int flag, cred_t *crp, caller_context_t *ct)
223 223 {
224 224 vnode_t *vp = *vpp;
225 225 fifonode_t *fnp = VTOF(vp);
226 226 fifolock_t *fn_lock = fnp->fn_lock;
227 227 int error;
228 228
229 229 ASSERT(vp->v_type == VFIFO);
230 230 ASSERT(vn_matchops(vp, fifo_vnodeops));
231 231
232 232 if (!tsol_fifo_access(vp, flag, crp))
233 233 return (EACCES);
234 234
235 235 mutex_enter(&fn_lock->flk_lock);
236 236 /*
237 237 * If we are the first reader, wake up any writers that
238 238 * may be waiting around. wait for all of them to
239 239 * wake up before proceeding (i.e. fn_wsynccnt == 0)
240 240 */
241 241 if (flag & FREAD) {
242 242 fnp->fn_rcnt++; /* record reader present */
243 243 if (! (fnp->fn_flag & ISPIPE))
244 244 fnp->fn_rsynccnt++; /* record reader in open */
245 245 }
246 246
247 247 /*
248 248 * If we are the first writer, wake up any readers that
249 249 * may be waiting around. wait for all of them to
250 250 * wake up before proceeding (i.e. fn_rsynccnt == 0)
251 251 */
252 252 if (flag & FWRITE) {
253 253 fnp->fn_wcnt++; /* record writer present */
254 254 if (! (fnp->fn_flag & ISPIPE))
255 255 fnp->fn_wsynccnt++; /* record writer in open */
256 256 }
257 257 /*
258 258 * fifo_stropen will take care of twisting the queues on the first
259 259 * open. The 1 being passed in means twist the queues on the first
260 260 * open.
261 261 */
262 262 error = fifo_stropen(vpp, flag, crp, 1, 1);
263 263 /*
264 264 * fifo_stropen() could have replaced vpp
265 265 * since fifo's are the only thing we need to sync up,
266 266 * everything else just returns;
267 267 * Note: don't need to hold lock since ISPIPE can't change
268 268 * and both old and new vp need to be pipes
269 269 */
270 270 ASSERT(MUTEX_HELD(&VTOF(*vpp)->fn_lock->flk_lock));
271 271 if (fnp->fn_flag & ISPIPE) {
272 272 ASSERT(VTOF(*vpp)->fn_flag & ISPIPE);
273 273 ASSERT(VTOF(*vpp)->fn_rsynccnt == 0);
274 274 ASSERT(VTOF(*vpp)->fn_rsynccnt == 0);
275 275 /*
276 276 * XXX note: should probably hold locks, but
277 277 * These values should not be changing
278 278 */
279 279 ASSERT(fnp->fn_rsynccnt == 0);
280 280 ASSERT(fnp->fn_wsynccnt == 0);
281 281 mutex_exit(&VTOF(*vpp)->fn_lock->flk_lock);
282 282 return (error);
283 283 }
284 284 /*
285 285 * vp can't change for FIFOS
286 286 */
287 287 ASSERT(vp == *vpp);
288 288 /*
289 289 * If we are opening for read (or writer)
290 290 * indicate that the reader (or writer) is done with open
291 291 * if there is a writer (or reader) waiting for us, wake them up
292 292 * and indicate that at least 1 read (or write) open has occurred
293 293 * this is need in the event the read (or write) side closes
294 294 * before the writer (or reader) has a chance to wake up
295 295 * i.e. it sees that a reader (or writer) was once there
296 296 */
297 297 if (flag & FREAD) {
298 298 fnp->fn_rsynccnt--; /* reader done with open */
299 299 if (fnp->fn_flag & FIFOSYNC) {
300 300 /*
301 301 * This indicates that a read open has occurred
302 302 * Only need to set if writer is actually asleep
303 303 * Flag will be consumed by writer.
304 304 */
305 305 fnp->fn_flag |= FIFOROCR;
306 306 cv_broadcast(&fnp->fn_wait_cv);
307 307 }
308 308 }
309 309 if (flag & FWRITE) {
310 310 fnp->fn_wsynccnt--; /* writer done with open */
311 311 if (fnp->fn_flag & FIFOSYNC) {
312 312 /*
313 313 * This indicates that a write open has occurred
314 314 * Only need to set if reader is actually asleep
315 315 * Flag will be consumed by reader.
316 316 */
317 317 fnp->fn_flag |= FIFOWOCR;
318 318 cv_broadcast(&fnp->fn_wait_cv);
319 319 }
320 320 }
321 321
322 322 fnp->fn_flag &= ~FIFOSYNC;
323 323
324 324 /*
325 325 * errors don't wait around.. just return
326 326 * Note: XXX other end will wake up and continue despite error.
327 327 * There is no defined semantic on the correct course of option
328 328 * so we do what we've done in the past
329 329 */
330 330 if (error != 0) {
331 331 mutex_exit(&fnp->fn_lock->flk_lock);
332 332 goto done;
333 333 }
334 334 ASSERT(fnp->fn_rsynccnt <= fnp->fn_rcnt);
335 335 ASSERT(fnp->fn_wsynccnt <= fnp->fn_wcnt);
336 336 /*
337 337 * FIFOWOCR (or FIFOROCR) indicates that the writer (or reader)
338 338 * has woken us up and is done with open (this way, if the other
339 339 * end has made it to close, we don't block forever in open)
340 340 * fn_wnct == fn_wsynccnt (or fn_rcnt == fn_rsynccnt) indicates
341 341 * that no writer (or reader) has yet made it through open
342 342 * This has the side benefit of that the first
343 343 * reader (or writer) will wait until the other end finishes open
344 344 */
345 345 if (flag & FREAD) {
346 346 while ((fnp->fn_flag & FIFOWOCR) == 0 &&
347 347 fnp->fn_wcnt == fnp->fn_wsynccnt) {
348 348 if (flag & (FNDELAY|FNONBLOCK)) {
349 349 mutex_exit(&fnp->fn_lock->flk_lock);
350 350 goto done;
351 351 }
352 352 fnp->fn_insync++;
353 353 fnp->fn_flag |= FIFOSYNC;
354 354 if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
355 355 &fnp->fn_lock->flk_lock)) {
356 356 /*
357 357 * Last reader to wakeup clear writer
358 358 * Clear both writer and reader open
359 359 * occurred flag incase other end is O_RDWR
360 360 */
361 361 if (--fnp->fn_insync == 0 &&
362 362 fnp->fn_flag & FIFOWOCR) {
363 363 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
364 364 }
365 365 mutex_exit(&fnp->fn_lock->flk_lock);
366 366 (void) fifo_close(*vpp, flag, 1, 0, crp, ct);
367 367 error = EINTR;
368 368 goto done;
369 369 }
370 370 /*
371 371 * Last reader to wakeup clear writer open occurred flag
372 372 * Clear both writer and reader open occurred flag
373 373 * incase other end is O_RDWR
374 374 */
375 375 if (--fnp->fn_insync == 0 &&
376 376 fnp->fn_flag & FIFOWOCR) {
377 377 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
378 378 break;
379 379 }
380 380 }
381 381 } else if (flag & FWRITE) {
382 382 while ((fnp->fn_flag & FIFOROCR) == 0 &&
383 383 fnp->fn_rcnt == fnp->fn_rsynccnt) {
384 384 if ((flag & (FNDELAY|FNONBLOCK)) && fnp->fn_rcnt == 0) {
385 385 mutex_exit(&fnp->fn_lock->flk_lock);
386 386 (void) fifo_close(*vpp, flag, 1, 0, crp, ct);
387 387 error = ENXIO;
388 388 goto done;
389 389 }
390 390 fnp->fn_flag |= FIFOSYNC;
391 391 fnp->fn_insync++;
392 392 if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
393 393 &fnp->fn_lock->flk_lock)) {
394 394 /*
395 395 * Last writer to wakeup clear
396 396 * Clear both writer and reader open
397 397 * occurred flag in case other end is O_RDWR
398 398 */
399 399 if (--fnp->fn_insync == 0 &&
400 400 (fnp->fn_flag & FIFOROCR) != 0) {
401 401 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
402 402 }
403 403 mutex_exit(&fnp->fn_lock->flk_lock);
404 404 (void) fifo_close(*vpp, flag, 1, 0, crp, ct);
405 405 error = EINTR;
406 406 goto done;
407 407 }
408 408 /*
409 409 * Last writer to wakeup clear reader open occurred flag
410 410 * Clear both writer and reader open
411 411 * occurred flag in case other end is O_RDWR
412 412 */
413 413 if (--fnp->fn_insync == 0 &&
414 414 (fnp->fn_flag & FIFOROCR) != 0) {
415 415 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
416 416 break;
417 417 }
418 418 }
419 419 }
420 420 mutex_exit(&fn_lock->flk_lock);
421 421 done:
422 422 return (error);
423 423 }
424 424
425 425 /*
426 426 * Close down a stream.
427 427 * Call cleanlocks() and strclean() on every close.
428 428 * For last close send hangup message and force
429 429 * the other end of a named pipe to be unmounted.
430 430 * Mount guarantees that the mounted end will only call fifo_close()
431 431 * with a count of 1 when the unmount occurs.
432 432 * This routine will close down one end of a pipe or FIFO
433 433 * and free the stream head via strclose()
434 434 */
435 435 /*ARGSUSED*/
436 436 int
437 437 fifo_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *crp,
438 438 caller_context_t *ct)
439 439 {
440 440 fifonode_t *fnp = VTOF(vp);
441 441 fifonode_t *fn_dest = fnp->fn_dest;
442 442 int error = 0;
443 443 fifolock_t *fn_lock = fnp->fn_lock;
444 444 queue_t *sd_wrq;
445 445 vnode_t *fn_dest_vp;
446 446 int senthang = 0;
447 447
448 448 ASSERT(vp->v_stream != NULL);
449 449 /*
450 450 * clean locks and clear events.
451 451 */
452 452 (void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
453 453 cleanshares(vp, ttoproc(curthread)->p_pid);
454 454 strclean(vp);
455 455
456 456 /*
457 457 * If a file still has the pipe/FIFO open, return.
458 458 */
459 459 if (count > 1)
460 460 return (0);
461 461
462 462
463 463 sd_wrq = strvp2wq(vp);
464 464 mutex_enter(&fn_lock->flk_lock);
465 465
466 466 /*
467 467 * wait for pending opens to finish up
468 468 * note: this also has the side effect of single threading closes
469 469 */
470 470 while (fn_lock->flk_ocsync)
471 471 cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
472 472
473 473 fn_lock->flk_ocsync = 1;
474 474
475 475 if (flag & FREAD) {
476 476 fnp->fn_rcnt--;
477 477 }
478 478 /*
479 479 * If we are last writer wake up sleeping readers
480 480 * (They'll figure out that there are no more writers
481 481 * and do the right thing)
482 482 * send hangup down stream so that stream head will do the
483 483 * right thing.
484 484 */
485 485 if (flag & FWRITE) {
486 486 if (--fnp->fn_wcnt == 0 && fn_dest->fn_rcnt > 0) {
487 487 if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTR)) ==
488 488 (FIFOFAST | FIFOWANTR)) {
489 489 /*
490 490 * While we're at it, clear FIFOWANTW too
491 491 * Wake up any sleeping readers or
492 492 * writers.
493 493 */
494 494 fn_dest->fn_flag &= ~(FIFOWANTR | FIFOWANTW);
495 495 cv_broadcast(&fn_dest->fn_wait_cv);
496 496 }
497 497 /*
498 498 * This is needed incase the other side
499 499 * was opened non-blocking. It is the
500 500 * only way we can tell that wcnt is 0 because
501 501 * of close instead of never having a writer
502 502 */
503 503 if (!(fnp->fn_flag & ISPIPE))
504 504 fnp->fn_flag |= FIFOCLOSE;
505 505 /*
506 506 * Note: sending hangup effectively shuts down
507 507 * both reader and writer at other end.
508 508 */
509 509 (void) putnextctl_wait(sd_wrq, M_HANGUP);
510 510 senthang = 1;
511 511 }
512 512 }
513 513
514 514 /*
515 515 * For FIFOs we need to indicate to stream head that last reader
516 516 * has gone away so that an error is generated
517 517 * Pipes just need to wake up the other end so that it can
518 518 * notice this end has gone away.
519 519 */
520 520
521 521 if (fnp->fn_rcnt == 0 && fn_dest->fn_wcnt > 0) {
522 522 if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTW)) ==
523 523 (FIFOFAST | FIFOWANTW)) {
524 524 /*
525 525 * wake up any sleeping writers
526 526 */
527 527 fn_dest->fn_flag &= ~FIFOWANTW;
528 528 cv_broadcast(&fn_dest->fn_wait_cv);
529 529 }
530 530 }
531 531
532 532 /*
533 533 * if there are still processes with this FIFO open
534 534 * clear open/close sync flag
535 535 * and just return;
536 536 */
537 537 if (--fnp->fn_open > 0) {
538 538 ASSERT((fnp->fn_rcnt + fnp->fn_wcnt) != 0);
539 539 fn_lock->flk_ocsync = 0;
540 540 cv_broadcast(&fn_lock->flk_wait_cv);
541 541 mutex_exit(&fn_lock->flk_lock);
542 542 return (0);
543 543 }
544 544
545 545 /*
546 546 * Need to send HANGUP if other side is still open
547 547 * (fnp->fn_rcnt or fnp->fn_wcnt may not be zero (some thread
548 548 * on this end of the pipe may still be in fifo_open())
549 549 *
550 550 * Note: we can get here with fn_rcnt and fn_wcnt != 0 if some
551 551 * thread is blocked somewhere in the fifo_open() path prior to
552 552 * fifo_stropen() incrementing fn_open. This can occur for
553 553 * normal FIFOs as well as named pipes. fn_rcnt and
554 554 * fn_wcnt only indicate attempts to open. fn_open indicates
555 555 * successful opens. Partially opened FIFOs should proceed
556 556 * normally; i.e. they will appear to be new opens. Partially
557 557 * opened pipes will probably fail.
558 558 */
559 559
560 560 if (fn_dest->fn_open && senthang == 0)
561 561 (void) putnextctl_wait(sd_wrq, M_HANGUP);
562 562
563 563
564 564 /*
565 565 * If this a pipe and this is the first end to close,
566 566 * then we have a bit of cleanup work to do.
567 567 * Mark both ends of pipe as closed.
568 568 * Wake up anybody blocked at the other end and for named pipes,
569 569 * Close down this end of the stream
570 570 * Allow other opens/closes to continue
571 571 * force an unmount of other end.
572 572 * Otherwise if this is last close,
573 573 * flush messages,
574 574 * close down the stream
575 575 * allow other opens/closes to continue
576 576 */
577 577 fnp->fn_flag &= ~FIFOISOPEN;
578 578 if ((fnp->fn_flag & ISPIPE) && !(fnp->fn_flag & FIFOCLOSE)) {
579 579 fnp->fn_flag |= FIFOCLOSE;
580 580 fn_dest->fn_flag |= FIFOCLOSE;
581 581 if (fnp->fn_flag & FIFOFAST)
582 582 fifo_fastflush(fnp);
583 583 if (vp->v_stream != NULL) {
584 584 mutex_exit(&fn_lock->flk_lock);
585 585 (void) strclose(vp, flag, crp);
586 586 mutex_enter(&fn_lock->flk_lock);
587 587 }
588 588 cv_broadcast(&fn_dest->fn_wait_cv);
589 589 /*
590 590 * allow opens and closes to proceed
591 591 * Since this end is now closed down, any attempt
592 592 * to do anything with this end will fail
593 593 */
594 594 fn_lock->flk_ocsync = 0;
595 595 cv_broadcast(&fn_lock->flk_wait_cv);
596 596 fn_dest_vp = FTOV(fn_dest);
597 597 /*
598 598 * if other end of pipe has been opened and it's
599 599 * a named pipe, unmount it
600 600 */
601 601 if (fn_dest_vp->v_stream &&
602 602 (fn_dest_vp->v_stream->sd_flag & STRMOUNT)) {
603 603 /*
604 604 * We must hold the destination vnode because
605 605 * nm_unmountall() causes close to be called
606 606 * for the other end of named pipe. This
607 607 * could free the vnode before we are ready.
608 608 */
609 609 VN_HOLD(fn_dest_vp);
610 610 mutex_exit(&fn_lock->flk_lock);
611 611 error = nm_unmountall(fn_dest_vp, crp);
612 612 ASSERT(error == 0);
613 613 VN_RELE(fn_dest_vp);
614 614 } else {
615 615 ASSERT(vp->v_count >= 1);
616 616 mutex_exit(&fn_lock->flk_lock);
617 617 }
618 618 } else {
619 619 if (fnp->fn_flag & FIFOFAST)
620 620 fifo_fastflush(fnp);
621 621 #if DEBUG
622 622 fn_dest_vp = FTOV(fn_dest);
623 623 if (fn_dest_vp->v_stream)
624 624 ASSERT((fn_dest_vp->v_stream->sd_flag & STRMOUNT) == 0);
625 625 #endif
626 626 if (vp->v_stream != NULL) {
627 627 mutex_exit(&fn_lock->flk_lock);
628 628 (void) strclose(vp, flag, crp);
629 629 mutex_enter(&fn_lock->flk_lock);
630 630 }
631 631 fn_lock->flk_ocsync = 0;
632 632 cv_broadcast(&fn_lock->flk_wait_cv);
633 633 cv_broadcast(&fn_dest->fn_wait_cv);
634 634 mutex_exit(&fn_lock->flk_lock);
635 635 }
636 636 return (error);
637 637 }
638 638
639 639 /*
640 640 * Read from a pipe or FIFO.
641 641 * return 0 if....
642 642 * (1) user read request is 0 or no stream
643 643 * (2) broken pipe with no data
644 644 * (3) write-only FIFO with no data
645 645 * (4) no data and FNDELAY flag is set.
646 646 * Otherwise return
647 647 * EAGAIN if FNONBLOCK is set and no data to read
648 648 * EINTR if signal received while waiting for data
649 649 *
650 650 * While there is no data to read....
651 651 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
652 652 * - wait for a write.
653 653 *
654 654 */
655 655 /*ARGSUSED*/
656 656
657 657 static int
658 658 fifo_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *crp,
659 659 caller_context_t *ct)
660 660 {
661 661 fifonode_t *fnp = VTOF(vp);
662 662 fifonode_t *fn_dest;
663 663 fifolock_t *fn_lock = fnp->fn_lock;
664 664 int error = 0;
665 665 mblk_t *bp;
666 666
667 667 ASSERT(vp->v_stream != NULL);
668 668 if (uiop->uio_resid == 0)
669 669 return (0);
670 670
671 671 mutex_enter(&fn_lock->flk_lock);
672 672
673 673 TRACE_2(TR_FAC_FIFO, TR_FIFOREAD_IN, "fifo_read in:%p fnp %p", vp, fnp);
674 674
675 675 if (! (fnp->fn_flag & FIFOFAST))
676 676 goto stream_mode;
677 677
678 678 fn_dest = fnp->fn_dest;
679 679 /*
680 680 * Check for data on our input queue
681 681 */
682 682
683 683 while (fnp->fn_count == 0) {
684 684 /*
685 685 * No data on first attempt and no writer, then EOF
686 686 */
687 687 if (fn_dest->fn_wcnt == 0 || fn_dest->fn_rcnt == 0) {
688 688 mutex_exit(&fn_lock->flk_lock);
689 689 return (0);
690 690 }
691 691 /*
692 692 * no data found.. if non-blocking, return EAGAIN
693 693 * otherwise 0.
694 694 */
695 695 if (uiop->uio_fmode & (FNDELAY|FNONBLOCK)) {
696 696 mutex_exit(&fn_lock->flk_lock);
697 697 if (uiop->uio_fmode & FNONBLOCK)
698 698 return (EAGAIN);
699 699 return (0);
700 700 }
701 701
702 702 /*
703 703 * Note: FIFOs can get here with FIFOCLOSE set if
704 704 * write side is in the middle of opeining after
705 705 * it once closed. Pipes better not have FIFOCLOSE set
706 706 */
707 707 ASSERT((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) !=
708 708 (ISPIPE|FIFOCLOSE));
709 709 /*
710 710 * wait for data
711 711 */
712 712 fnp->fn_flag |= FIFOWANTR;
713 713
714 714 TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAIT, "fiforead wait: %p", vp);
715 715
716 716 if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
717 717 &fn_lock->flk_lock)) {
718 718 error = EINTR;
719 719 goto done;
720 720 }
721 721
722 722 TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAKE,
723 723 "fiforead awake: %p", vp);
724 724
725 725 /*
726 726 * check to make sure we are still in fast mode
727 727 */
728 728 if (!(fnp->fn_flag & FIFOFAST))
729 729 goto stream_mode;
730 730 }
731 731
732 732 ASSERT(fnp->fn_mp != NULL);
733 733
734 734 /* For pipes copy should not bypass cache */
735 735 uiop->uio_extflg |= UIO_COPY_CACHED;
736 736
737 737 do {
738 738 int bpsize = MBLKL(fnp->fn_mp);
739 739 int uiosize = MIN(bpsize, uiop->uio_resid);
740 740
741 741 error = uiomove(fnp->fn_mp->b_rptr, uiosize, UIO_READ, uiop);
742 742 if (error != 0)
743 743 break;
744 744
745 745 fnp->fn_count -= uiosize;
746 746
747 747 if (bpsize <= uiosize) {
748 748 bp = fnp->fn_mp;
749 749 fnp->fn_mp = fnp->fn_mp->b_cont;
750 750 freeb(bp);
751 751
752 752 if (uiop->uio_resid == 0)
753 753 break;
754 754
755 755 while (fnp->fn_mp == NULL && fn_dest->fn_wwaitcnt > 0) {
756 756 ASSERT(fnp->fn_count == 0);
757 757
758 758 if (uiop->uio_fmode & (FNDELAY|FNONBLOCK))
759 759 goto trywake;
760 760
761 761 /*
762 762 * We've consumed all available data but there
763 763 * are threads waiting to write more, let them
764 764 * proceed before bailing.
765 765 */
766 766
767 767 fnp->fn_flag |= FIFOWANTR;
768 768 fifo_wakewriter(fn_dest, fn_lock);
769 769
770 770 if (!cv_wait_sig(&fnp->fn_wait_cv,
771 771 &fn_lock->flk_lock))
772 772 goto trywake;
773 773
774 774 if (!(fnp->fn_flag & FIFOFAST))
775 775 goto stream_mode;
776 776 }
777 777 } else {
778 778 fnp->fn_mp->b_rptr += uiosize;
779 779 ASSERT(uiop->uio_resid == 0);
780 780 }
781 781 } while (uiop->uio_resid != 0 && fnp->fn_mp != NULL);
782 782
783 783 trywake:
784 784 ASSERT(msgdsize(fnp->fn_mp) == fnp->fn_count);
785 785
786 786 /*
787 787 * wake up any blocked writers, processes
788 788 * sleeping on POLLWRNORM, or processes waiting for SIGPOLL
789 789 * Note: checking for fn_count < Fifohiwat emulates
790 790 * STREAMS functionality when low water mark is 0
791 791 */
792 792 if (fn_dest->fn_flag & (FIFOWANTW | FIFOHIWATW) &&
793 793 fnp->fn_count < Fifohiwat) {
794 794 fifo_wakewriter(fn_dest, fn_lock);
795 795 }
796 796 goto done;
797 797
798 798 /*
799 799 * FIFO is in streams mode.. let the stream head handle it
800 800 */
801 801 stream_mode:
802 802
803 803 mutex_exit(&fn_lock->flk_lock);
804 804 TRACE_1(TR_FAC_FIFO,
805 805 TR_FIFOREAD_STREAM, "fifo_read stream_mode:%p", vp);
806 806
807 807 error = strread(vp, uiop, crp);
808 808
809 809 mutex_enter(&fn_lock->flk_lock);
810 810
811 811 done:
812 812 /*
813 813 * vnode update access time
814 814 */
815 815 if (error == 0) {
816 816 time_t now = gethrestime_sec();
817 817
818 818 if (fnp->fn_flag & ISPIPE)
819 819 fnp->fn_dest->fn_atime = now;
820 820 fnp->fn_atime = now;
821 821 }
822 822 TRACE_2(TR_FAC_FIFO, TR_FIFOREAD_OUT,
823 823 "fifo_read out:%p error %d", vp, error);
824 824 mutex_exit(&fn_lock->flk_lock);
825 825 return (error);
826 826 }
827 827
828 828 /*
829 829 * send SIGPIPE and return EPIPE if ...
830 830 * (1) broken pipe (essentially, reader is gone)
831 831 * (2) FIFO is not open for reading
832 832 * return 0 if...
833 833 * (1) no stream
834 834 * (2) user write request is for 0 bytes and SW_SNDZERO is not set
835 835 * Note: SW_SNDZERO can't be set in fast mode
836 836 * While the stream is flow controlled....
837 837 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
838 838 * - unlock the fifonode and sleep waiting for a reader.
839 839 * - if a pipe and it has a mate, sleep waiting for its mate
840 840 * to read.
841 841 */
842 842 /*ARGSUSED*/
843 843 static int
844 844 fifo_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *crp,
845 845 caller_context_t *ct)
846 846 {
847 847 struct fifonode *fnp, *fn_dest;
848 848 fifolock_t *fn_lock;
849 849 struct stdata *stp;
850 850 int error = 0;
851 851 int write_size;
852 852 int size;
853 853 int fmode;
854 854 mblk_t *bp;
855 855 boolean_t hotread;
856 856
857 857 ASSERT(vp->v_stream);
858 858 uiop->uio_loffset = 0;
859 859 stp = vp->v_stream;
860 860
861 861 /*
862 862 * remember original number of bytes requested. Used to determine if
863 863 * we actually have written anything at all
864 864 */
865 865 write_size = uiop->uio_resid;
866 866
867 867 /*
868 868 * only send zero-length messages if SW_SNDZERO is set
869 869 * Note: we will be in streams mode if SW_SNDZERO is set
870 870 * XXX this streams interface should not be exposed
871 871 */
872 872 if ((write_size == 0) && !(stp->sd_wput_opt & SW_SNDZERO))
873 873 return (0);
874 874
875 875 fnp = VTOF(vp);
876 876 fn_lock = fnp->fn_lock;
877 877 fn_dest = fnp->fn_dest;
878 878
879 879 mutex_enter(&fn_lock->flk_lock);
880 880
881 881 TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_IN,
882 882 "fifo_write in:%p fnp %p size %d", vp, fnp, write_size);
883 883
884 884 /*
885 885 * oops, no readers, error
886 886 */
887 887 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
888 888 goto epipe;
889 889 }
890 890
891 891 /*
892 892 * if we are not in fast mode, let streams handle it
893 893 */
894 894 if (!(fnp->fn_flag & FIFOFAST))
895 895 goto stream_mode;
896 896
897 897 fmode = uiop->uio_fmode & (FNDELAY|FNONBLOCK);
898 898
899 899 /* For pipes copy should not bypass cache */
900 900 uiop->uio_extflg |= UIO_COPY_CACHED;
901 901
902 902 do {
903 903 /*
904 904 * check to make sure we are not over high water mark
905 905 */
906 906 while (fn_dest->fn_count >= Fifohiwat) {
907 907 /*
908 908 * Indicate that we have gone over high
909 909 * water mark
910 910 */
911 911 /*
912 912 * if non-blocking, return
913 913 * only happens first time through loop
914 914 */
915 915 if (fmode) {
916 916 fnp->fn_flag |= FIFOHIWATW;
917 917 if (uiop->uio_resid == write_size) {
918 918 mutex_exit(&fn_lock->flk_lock);
919 919 if (fmode & FNDELAY)
920 920 return (0);
921 921 else
922 922 return (EAGAIN);
923 923 }
924 924 goto done;
925 925 }
926 926
927 927 /*
928 928 * wait for things to drain
929 929 */
930 930 fnp->fn_flag |= FIFOWANTW;
931 931 fnp->fn_wwaitcnt++;
932 932 TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAIT,
933 933 "fifo_write wait: %p", vp);
934 934 if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
935 935 &fn_lock->flk_lock)) {
936 936 error = EINTR;
937 937 fnp->fn_wwaitcnt--;
938 938 fifo_wakereader(fn_dest, fn_lock);
939 939 goto done;
940 940 }
941 941 fnp->fn_wwaitcnt--;
942 942
943 943 TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAKE,
944 944 "fifo_write wake: %p", vp);
945 945
946 946 /*
947 947 * check to make sure we're still in fast mode
948 948 */
949 949 if (!(fnp->fn_flag & FIFOFAST))
950 950 goto stream_mode;
951 951
952 952 /*
953 953 * make sure readers didn't go away
954 954 */
955 955 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
956 956 goto epipe;
957 957 }
958 958 }
959 959 /*
960 960 * If the write will put us over the high water mark,
961 961 * then we must break the message up into PIPE_BUF
962 962 * chunks to stay compliant with STREAMS
963 963 */
964 964 if (uiop->uio_resid + fn_dest->fn_count > Fifohiwat)
965 965 size = MIN(uiop->uio_resid, PIPE_BUF);
966 966 else
967 967 size = uiop->uio_resid;
968 968
969 969 /*
970 970 * We don't need to hold flk_lock across the allocb() and
971 971 * uiomove(). However, on a multiprocessor machine where both
972 972 * the reader and writer thread are on cpu's, we must be
973 973 * careful to only drop the lock if there's data to be read.
974 974 * This forces threads entering fifo_read() to spin or block
975 975 * on flk_lock, rather than acquiring flk_lock only to
976 976 * discover there's no data to read and being forced to go
977 977 * back to sleep, only to be woken up microseconds later by
978 978 * this writer thread.
979 979 */
980 980 hotread = fn_dest->fn_count > 0;
981 981 if (hotread) {
982 982 if (!fifo_stayfast_enter(fnp))
983 983 goto stream_mode;
984 984 mutex_exit(&fn_lock->flk_lock);
985 985 }
986 986
987 987 ASSERT(size != 0);
988 988 /*
989 989 * Align the mblk with the user data so that
990 990 * copying in the data can take advantage of
991 991 * the double word alignment
992 992 */
993 993 if ((bp = allocb(size + 8, BPRI_MED)) == NULL) {
994 994 if (!hotread)
995 995 mutex_exit(&fn_lock->flk_lock);
996 996
997 997 error = strwaitbuf(size, BPRI_MED);
998 998
999 999 mutex_enter(&fn_lock->flk_lock);
1000 1000
1001 1001 if (hotread) {
1002 1002 /*
1003 1003 * As we dropped the mutex for a moment, we
1004 1004 * need to wake up any thread waiting to be
1005 1005 * allowed to go from fast mode to stream mode.
1006 1006 */
1007 1007 fifo_stayfast_exit(fnp);
1008 1008 }
1009 1009 if (error != 0) {
1010 1010 goto done;
1011 1011 }
1012 1012 /*
1013 1013 * check to make sure we're still in fast mode
1014 1014 */
1015 1015 if (!(fnp->fn_flag & FIFOFAST))
1016 1016 goto stream_mode;
1017 1017
1018 1018 /*
1019 1019 * make sure readers didn't go away
1020 1020 */
1021 1021 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1022 1022 goto epipe;
1023 1023 }
1024 1024 /*
1025 1025 * some other thread could have gotten in
1026 1026 * need to go back and check hi water mark
1027 1027 */
1028 1028 continue;
1029 1029 }
1030 1030 bp->b_rptr += ((uintptr_t)uiop->uio_iov->iov_base & 0x7);
1031 1031 bp->b_wptr = bp->b_rptr + size;
1032 1032 error = uiomove((caddr_t)bp->b_rptr, size, UIO_WRITE, uiop);
1033 1033 if (hotread) {
1034 1034 mutex_enter(&fn_lock->flk_lock);
1035 1035 /*
1036 1036 * As we dropped the mutex for a moment, we need to:
1037 1037 * - wake up any thread waiting to be allowed to go
1038 1038 * from fast mode to stream mode,
1039 1039 * - make sure readers didn't go away.
1040 1040 */
1041 1041 fifo_stayfast_exit(fnp);
1042 1042 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1043 1043 freeb(bp);
1044 1044 goto epipe;
1045 1045 }
1046 1046 }
1047 1047
1048 1048 if (error != 0) {
1049 1049 freeb(bp);
1050 1050 goto done;
1051 1051 }
1052 1052
1053 1053 fn_dest->fn_count += size;
1054 1054 if (fn_dest->fn_mp != NULL) {
1055 1055 fn_dest->fn_tail->b_cont = bp;
1056 1056 fn_dest->fn_tail = bp;
1057 1057 } else {
1058 1058 fn_dest->fn_mp = fn_dest->fn_tail = bp;
1059 1059 /*
1060 1060 * This is the first bit of data; wake up any sleeping
1061 1061 * readers, processes blocked in poll, and those
1062 1062 * expecting a SIGPOLL.
1063 1063 */
1064 1064 fifo_wakereader(fn_dest, fn_lock);
1065 1065 }
1066 1066 } while (uiop->uio_resid != 0);
1067 1067
1068 1068 goto done;
1069 1069
1070 1070 stream_mode:
1071 1071 /*
1072 1072 * streams mode
1073 1073 * let the stream head handle the write
1074 1074 */
1075 1075 ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1076 1076
1077 1077 mutex_exit(&fn_lock->flk_lock);
1078 1078 TRACE_1(TR_FAC_FIFO,
1079 1079 TR_FIFOWRITE_STREAM, "fifo_write stream_mode:%p", vp);
1080 1080
1081 1081 error = strwrite(vp, uiop, crp);
1082 1082
1083 1083 mutex_enter(&fn_lock->flk_lock);
1084 1084
1085 1085 done:
1086 1086 /*
1087 1087 * update vnode modification and change times
1088 1088 * make sure there were no errors and some data was transferred
1089 1089 */
1090 1090 if (error == 0 && write_size != uiop->uio_resid) {
1091 1091 time_t now = gethrestime_sec();
1092 1092
1093 1093 if (fnp->fn_flag & ISPIPE) {
1094 1094 fn_dest->fn_mtime = fn_dest->fn_ctime = now;
1095 1095 }
1096 1096 fnp->fn_mtime = fnp->fn_ctime = now;
1097 1097 } else if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1098 1098 goto epipe;
1099 1099 }
1100 1100 TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT,
1101 1101 "fifo_write out: vp %p error %d fnp %p", vp, error, fnp);
1102 1102 mutex_exit(&fn_lock->flk_lock);
1103 1103 return (error);
1104 1104 epipe:
1105 1105 error = EPIPE;
1106 1106 TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT,
1107 1107 "fifo_write out: vp %p error %d fnp %p", vp, error, fnp);
1108 1108 mutex_exit(&fn_lock->flk_lock);
1109 1109 tsignal(curthread, SIGPIPE);
1110 1110 return (error);
1111 1111 }
1112 1112
1113 1113 /*ARGSUSED6*/
1114 1114 static int
1115 1115 fifo_ioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1116 1116 cred_t *cr, int *rvalp, caller_context_t *ct)
1117 1117 {
1118 1118 /*
↓ open down ↓ |
1118 lines elided |
↑ open up ↑ |
1119 1119 * Just a quick check
1120 1120 * Once we go to streams mode we don't ever revert back
1121 1121 * So we do this quick check so as not to incur the overhead
1122 1122 * associated with acquiring the lock
1123 1123 */
1124 1124 return ((VTOF(vp)->fn_flag & FIFOFAST) ?
1125 1125 fifo_fastioctl(vp, cmd, arg, mode, cr, rvalp) :
1126 1126 fifo_strioctl(vp, cmd, arg, mode, cr, rvalp));
1127 1127 }
1128 1128
1129 +static inline int
1130 +fifo_ioctl_getpeercred(fifonode_t *fnp, intptr_t arg, int mode)
1131 +{
1132 + k_peercred_t *kp = (k_peercred_t *)arg;
1133 +
1134 + if (mode == FKIOCTL && fnp->fn_pcredp != NULL) {
1135 + crhold(fnp->fn_pcredp);
1136 + kp->pc_cr = fnp->fn_pcredp;
1137 + kp->pc_cpid = fnp->fn_cpid;
1138 + return (0);
1139 + } else {
1140 + return (ENOTSUP);
1141 + }
1142 +}
1143 +
1129 1144 static int
1130 1145 fifo_fastioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1131 1146 cred_t *cr, int *rvalp)
1132 1147 {
1133 1148 fifonode_t *fnp = VTOF(vp);
1134 1149 fifonode_t *fn_dest;
1135 1150 int error = 0;
1136 1151 fifolock_t *fn_lock = fnp->fn_lock;
1137 1152 int cnt;
1138 1153
1139 1154 /*
1140 1155 * tty operations not allowed
1141 1156 */
1142 1157 if (((cmd & IOCTYPE) == LDIOC) ||
1143 1158 ((cmd & IOCTYPE) == tIOC) ||
1144 1159 ((cmd & IOCTYPE) == TIOC)) {
1145 1160 return (EINVAL);
1146 1161 }
1147 1162
1148 1163 mutex_enter(&fn_lock->flk_lock);
1149 1164
1150 1165 if (!(fnp->fn_flag & FIFOFAST)) {
1151 1166 goto stream_mode;
1152 1167 }
1153 1168
1154 1169 switch (cmd) {
1155 1170
1156 1171 /*
1157 1172 * Things we can't handle
1158 1173 * These will switch us to streams mode.
1159 1174 */
1160 1175 default:
1161 1176 case I_STR:
1162 1177 case I_SRDOPT:
1163 1178 case I_PUSH:
1164 1179 case I_FDINSERT:
1165 1180 case I_SENDFD:
1166 1181 case I_RECVFD:
1167 1182 case I_E_RECVFD:
1168 1183 case I_ATMARK:
1169 1184 case I_CKBAND:
1170 1185 case I_GETBAND:
1171 1186 case I_SWROPT:
1172 1187 goto turn_fastoff;
1173 1188
1174 1189 /*
1175 1190 * Things that don't do damage
1176 1191 * These things don't adjust the state of the
1177 1192 * stream head (i_setcltime does, but we don't care)
1178 1193 */
1179 1194 case I_FIND:
1180 1195 case I_GETSIG:
1181 1196 case FIONBIO:
1182 1197 case FIOASYNC:
1183 1198 case I_GRDOPT: /* probably should not get this, but no harm */
1184 1199 case I_GWROPT:
1185 1200 case I_LIST:
1186 1201 case I_SETCLTIME:
1187 1202 case I_GETCLTIME:
1188 1203 mutex_exit(&fn_lock->flk_lock);
1189 1204 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
1190 1205
1191 1206 case I_CANPUT:
1192 1207 /*
1193 1208 * We can only handle normal band canputs.
1194 1209 * XXX : We could just always go to stream mode; after all
1195 1210 * canput is a streams semantics type thing
1196 1211 */
1197 1212 if (arg != 0) {
1198 1213 goto turn_fastoff;
1199 1214 }
1200 1215 *rvalp = (fnp->fn_dest->fn_count < Fifohiwat) ? 1 : 0;
1201 1216 mutex_exit(&fn_lock->flk_lock);
1202 1217 return (0);
1203 1218
1204 1219 case I_NREAD:
1205 1220 /*
1206 1221 * This may seem a bit silly for non-streams semantics,
1207 1222 * (After all, if they really want a message, they'll
1208 1223 * probably use getmsg() anyway). but it doesn't hurt
1209 1224 */
1210 1225 error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg,
1211 1226 sizeof (cnt));
1212 1227 if (error == 0) {
1213 1228 *rvalp = (fnp->fn_count == 0) ? 0 : 1;
1214 1229 }
1215 1230 break;
1216 1231
1217 1232 case FIORDCHK:
1218 1233 *rvalp = fnp->fn_count;
1219 1234 break;
1220 1235
1221 1236 case I_PEEK:
1222 1237 {
1223 1238 STRUCT_DECL(strpeek, strpeek);
1224 1239 struct uio uio;
1225 1240 struct iovec iov;
1226 1241 int count;
1227 1242 mblk_t *bp;
1228 1243 int len;
1229 1244
1230 1245 STRUCT_INIT(strpeek, mode);
1231 1246
1232 1247 if (fnp->fn_count == 0) {
1233 1248 *rvalp = 0;
1234 1249 break;
1235 1250 }
1236 1251
1237 1252 error = copyin((caddr_t)arg, STRUCT_BUF(strpeek),
1238 1253 STRUCT_SIZE(strpeek));
1239 1254 if (error)
1240 1255 break;
1241 1256
1242 1257 /*
1243 1258 * can't have any high priority message when in fast mode
1244 1259 */
1245 1260 if (STRUCT_FGET(strpeek, flags) & RS_HIPRI) {
1246 1261 *rvalp = 0;
1247 1262 break;
1248 1263 }
1249 1264
1250 1265 len = STRUCT_FGET(strpeek, databuf.maxlen);
1251 1266 if (len <= 0) {
1252 1267 STRUCT_FSET(strpeek, databuf.len, len);
1253 1268 } else {
1254 1269 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
1255 1270 iov.iov_len = len;
1256 1271 uio.uio_iov = &iov;
1257 1272 uio.uio_iovcnt = 1;
1258 1273 uio.uio_loffset = 0;
1259 1274 uio.uio_segflg = UIO_USERSPACE;
1260 1275 uio.uio_fmode = 0;
1261 1276 /* For pipes copy should not bypass cache */
1262 1277 uio.uio_extflg = UIO_COPY_CACHED;
1263 1278 uio.uio_resid = iov.iov_len;
1264 1279 count = fnp->fn_count;
1265 1280 bp = fnp->fn_mp;
1266 1281 while (count > 0 && uio.uio_resid) {
1267 1282 cnt = MIN(uio.uio_resid, MBLKL(bp));
1268 1283 if ((error = uiomove((char *)bp->b_rptr, cnt,
1269 1284 UIO_READ, &uio)) != 0) {
1270 1285 break;
1271 1286 }
1272 1287 count -= cnt;
1273 1288 bp = bp->b_cont;
1274 1289 }
1275 1290 STRUCT_FSET(strpeek, databuf.len, len - uio.uio_resid);
1276 1291 }
1277 1292 STRUCT_FSET(strpeek, flags, 0);
1278 1293 STRUCT_FSET(strpeek, ctlbuf.len, -1);
1279 1294
1280 1295 error = copyout(STRUCT_BUF(strpeek), (caddr_t)arg,
1281 1296 STRUCT_SIZE(strpeek));
1282 1297 if (error == 0 && len >= 0)
1283 1298 *rvalp = 1;
1284 1299 break;
1285 1300 }
1286 1301
1287 1302 case FIONREAD:
1288 1303 /*
1289 1304 * let user know total number of bytes in message queue
1290 1305 */
1291 1306 error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg,
1292 1307 sizeof (fnp->fn_count));
1293 1308 if (error == 0)
1294 1309 *rvalp = 0;
1295 1310 break;
1296 1311
1297 1312 case I_SETSIG:
1298 1313 /*
1299 1314 * let streams set up the signal masking for us
1300 1315 * we just check to see if it's set
1301 1316 * XXX : this interface should not be visible
1302 1317 * i.e. STREAM's framework is exposed.
1303 1318 */
1304 1319 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
1305 1320 if (vp->v_stream->sd_sigflags & (S_INPUT|S_RDNORM|S_WRNORM))
1306 1321 fnp->fn_flag |= FIFOSETSIG;
1307 1322 else
1308 1323 fnp->fn_flag &= ~FIFOSETSIG;
1309 1324 break;
1310 1325
1311 1326 case I_FLUSH:
1312 1327 /*
1313 1328 * flush them message queues
1314 1329 */
1315 1330 if (arg & ~FLUSHRW) {
1316 1331 error = EINVAL;
1317 1332 break;
1318 1333 }
1319 1334 if (arg & FLUSHR) {
1320 1335 fifo_fastflush(fnp);
1321 1336 }
1322 1337 fn_dest = fnp->fn_dest;
1323 1338 if ((arg & FLUSHW)) {
1324 1339 fifo_fastflush(fn_dest);
1325 1340 }
1326 1341 /*
1327 1342 * wake up any sleeping readers or writers
1328 1343 * (waking readers probably doesn't make sense, but it
1329 1344 * doesn't hurt; i.e. we just got rid of all the data
1330 1345 * what's to read ?)
1331 1346 */
1332 1347 if (fn_dest->fn_flag & (FIFOWANTW | FIFOWANTR)) {
1333 1348 fn_dest->fn_flag &= ~(FIFOWANTW | FIFOWANTR);
1334 1349 cv_broadcast(&fn_dest->fn_wait_cv);
1335 1350 }
1336 1351 *rvalp = 0;
1337 1352 break;
↓ open down ↓ |
199 lines elided |
↑ open up ↑ |
1338 1353
1339 1354 /*
1340 1355 * Since no band data can ever get on a fifo in fast mode
1341 1356 * just return 0.
1342 1357 */
1343 1358 case I_FLUSHBAND:
1344 1359 error = 0;
1345 1360 *rvalp = 0;
1346 1361 break;
1347 1362
1363 + case _I_GETPEERCRED:
1364 + error = fifo_ioctl_getpeercred(fnp, arg, mode);
1365 + break;
1366 +
1348 1367 /*
1349 1368 * invalid calls for stream head or fifos
1350 1369 */
1351 1370
1352 1371 case I_POP: /* shouldn't happen */
1353 1372 case I_LOOK:
1354 1373 case I_LINK:
1355 1374 case I_PLINK:
1356 1375 case I_UNLINK:
1357 1376 case I_PUNLINK:
1358 1377
1359 1378 /*
1360 1379 * more invalid tty type of ioctls
1361 1380 */
1362 1381
1363 1382 case SRIOCSREDIR:
1364 1383 case SRIOCISREDIR:
1365 1384 error = EINVAL;
1366 1385 break;
1367 1386
1368 1387 }
1369 1388 mutex_exit(&fn_lock->flk_lock);
1370 1389 return (error);
1371 1390
1372 1391 turn_fastoff:
1373 1392 fifo_fastoff(fnp);
1374 1393
1375 1394 stream_mode:
1376 1395 /*
1377 1396 * streams mode
1378 1397 */
1379 1398 mutex_exit(&fn_lock->flk_lock);
1380 1399 return (fifo_strioctl(vp, cmd, arg, mode, cr, rvalp));
1381 1400
1382 1401 }
1383 1402
1384 1403 /*
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
1385 1404 * FIFO is in STREAMS mode; STREAMS framework does most of the work.
1386 1405 */
1387 1406 static int
1388 1407 fifo_strioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1389 1408 cred_t *cr, int *rvalp)
1390 1409 {
1391 1410 fifonode_t *fnp = VTOF(vp);
1392 1411 int error;
1393 1412 fifolock_t *fn_lock;
1394 1413
1395 - if (cmd == _I_GETPEERCRED) {
1396 - if (mode == FKIOCTL && fnp->fn_pcredp != NULL) {
1397 - k_peercred_t *kp = (k_peercred_t *)arg;
1398 - crhold(fnp->fn_pcredp);
1399 - kp->pc_cr = fnp->fn_pcredp;
1400 - kp->pc_cpid = fnp->fn_cpid;
1401 - return (0);
1402 - } else {
1403 - return (ENOTSUP);
1404 - }
1405 - }
1414 + if (cmd == _I_GETPEERCRED)
1415 + return (fifo_ioctl_getpeercred(fnp, arg, mode));
1406 1416
1407 1417 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
1408 1418
1409 1419 switch (cmd) {
1410 1420 /*
1411 1421 * The FIFOSEND flag is set to inform other processes that a file
1412 1422 * descriptor is pending at the stream head of this pipe.
1413 1423 * The flag is cleared and the sending process is awoken when
1414 1424 * this process has completed receiving the file descriptor.
1415 1425 * XXX This could become out of sync if the process does I_SENDFDs
1416 1426 * and opens on connld attached to the same pipe.
1417 1427 */
1418 1428 case I_RECVFD:
1419 1429 case I_E_RECVFD:
1420 1430 if (error == 0) {
1421 1431 fn_lock = fnp->fn_lock;
1422 1432 mutex_enter(&fn_lock->flk_lock);
1423 1433 if (fnp->fn_flag & FIFOSEND) {
1424 1434 fnp->fn_flag &= ~FIFOSEND;
1425 1435 cv_broadcast(&fnp->fn_dest->fn_wait_cv);
1426 1436 }
1427 1437 mutex_exit(&fn_lock->flk_lock);
1428 1438 }
1429 1439 break;
1430 1440 default:
1431 1441 break;
1432 1442 }
1433 1443
1434 1444 return (error);
1435 1445 }
1436 1446
1437 1447 /*
1438 1448 * If shadowing a vnode (FIFOs), apply the VOP_GETATTR to the shadowed
1439 1449 * vnode to Obtain the node information. If not shadowing (pipes), obtain
1440 1450 * the node information from the credentials structure.
1441 1451 */
1442 1452 int
1443 1453 fifo_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *crp,
1444 1454 caller_context_t *ct)
1445 1455 {
1446 1456 int error = 0;
1447 1457 fifonode_t *fnp = VTOF(vp);
1448 1458 queue_t *qp;
1449 1459 qband_t *bandp;
1450 1460 fifolock_t *fn_lock = fnp->fn_lock;
1451 1461
1452 1462 if (fnp->fn_realvp) {
1453 1463 /*
1454 1464 * for FIFOs or mounted pipes
1455 1465 */
1456 1466 if (error = VOP_GETATTR(fnp->fn_realvp, vap, flags, crp, ct))
1457 1467 return (error);
1458 1468 mutex_enter(&fn_lock->flk_lock);
1459 1469 /* set current times from fnode, even if older than vnode */
1460 1470 vap->va_atime.tv_sec = fnp->fn_atime;
1461 1471 vap->va_atime.tv_nsec = 0;
1462 1472 vap->va_mtime.tv_sec = fnp->fn_mtime;
1463 1473 vap->va_mtime.tv_nsec = 0;
1464 1474 vap->va_ctime.tv_sec = fnp->fn_ctime;
1465 1475 vap->va_ctime.tv_nsec = 0;
1466 1476 } else {
1467 1477 /*
1468 1478 * for non-attached/ordinary pipes
1469 1479 */
1470 1480 vap->va_mode = 0;
1471 1481 mutex_enter(&fn_lock->flk_lock);
1472 1482 vap->va_atime.tv_sec = fnp->fn_atime;
1473 1483 vap->va_atime.tv_nsec = 0;
1474 1484 vap->va_mtime.tv_sec = fnp->fn_mtime;
1475 1485 vap->va_mtime.tv_nsec = 0;
1476 1486 vap->va_ctime.tv_sec = fnp->fn_ctime;
1477 1487 vap->va_ctime.tv_nsec = 0;
1478 1488 vap->va_uid = crgetuid(crp);
1479 1489 vap->va_gid = crgetgid(crp);
1480 1490 vap->va_nlink = 0;
1481 1491 vap->va_fsid = fifodev;
1482 1492 vap->va_nodeid = (ino64_t)fnp->fn_ino;
1483 1493 vap->va_rdev = 0;
1484 1494 }
1485 1495 vap->va_type = VFIFO;
1486 1496 vap->va_blksize = PIPE_BUF;
1487 1497 /*
1488 1498 * Size is number of un-read bytes at the stream head and
1489 1499 * nblocks is the unread bytes expressed in blocks.
1490 1500 */
1491 1501 if (vp->v_stream && (fnp->fn_flag & FIFOISOPEN)) {
1492 1502 if ((fnp->fn_flag & FIFOFAST)) {
1493 1503 vap->va_size = (u_offset_t)fnp->fn_count;
1494 1504 } else {
1495 1505 qp = RD((strvp2wq(vp)));
1496 1506 vap->va_size = (u_offset_t)qp->q_count;
1497 1507 if (qp->q_nband != 0) {
1498 1508 mutex_enter(QLOCK(qp));
1499 1509 for (bandp = qp->q_bandp; bandp;
1500 1510 bandp = bandp->qb_next)
1501 1511 vap->va_size += bandp->qb_count;
1502 1512 mutex_exit(QLOCK(qp));
1503 1513 }
1504 1514 }
1505 1515 vap->va_nblocks = (fsblkcnt64_t)btod(vap->va_size);
1506 1516 } else {
1507 1517 vap->va_size = (u_offset_t)0;
1508 1518 vap->va_nblocks = (fsblkcnt64_t)0;
1509 1519 }
1510 1520 mutex_exit(&fn_lock->flk_lock);
1511 1521 vap->va_seq = 0;
1512 1522 return (0);
1513 1523 }
1514 1524
1515 1525 /*
1516 1526 * If shadowing a vnode, apply the VOP_SETATTR to it, and to the fnode.
1517 1527 * Otherwise, set the time and return 0.
1518 1528 */
1519 1529 int
1520 1530 fifo_setattr(
1521 1531 vnode_t *vp,
1522 1532 vattr_t *vap,
1523 1533 int flags,
1524 1534 cred_t *crp,
1525 1535 caller_context_t *ctp)
1526 1536 {
1527 1537 fifonode_t *fnp = VTOF(vp);
1528 1538 int error = 0;
1529 1539 fifolock_t *fn_lock;
1530 1540
1531 1541 if (fnp->fn_realvp)
1532 1542 error = VOP_SETATTR(fnp->fn_realvp, vap, flags, crp, ctp);
1533 1543 if (error == 0) {
1534 1544 fn_lock = fnp->fn_lock;
1535 1545 mutex_enter(&fn_lock->flk_lock);
1536 1546 if (vap->va_mask & AT_ATIME)
1537 1547 fnp->fn_atime = vap->va_atime.tv_sec;
1538 1548 if (vap->va_mask & AT_MTIME)
1539 1549 fnp->fn_mtime = vap->va_mtime.tv_sec;
1540 1550 fnp->fn_ctime = gethrestime_sec();
1541 1551 mutex_exit(&fn_lock->flk_lock);
1542 1552 }
1543 1553 return (error);
1544 1554 }
1545 1555
1546 1556 /*
1547 1557 * If shadowing a vnode, apply VOP_ACCESS to it.
1548 1558 * Otherwise, return 0 (allow all access).
1549 1559 */
1550 1560 int
1551 1561 fifo_access(vnode_t *vp, int mode, int flags, cred_t *crp, caller_context_t *ct)
1552 1562 {
1553 1563 if (VTOF(vp)->fn_realvp)
1554 1564 return (VOP_ACCESS(VTOF(vp)->fn_realvp, mode, flags, crp, ct));
1555 1565 else
1556 1566 return (0);
1557 1567 }
1558 1568
1559 1569 /*
1560 1570 * This can be called if creat or an open with O_CREAT is done on the root
1561 1571 * of a lofs mount where the mounted entity is a fifo.
1562 1572 */
1563 1573 /*ARGSUSED*/
1564 1574 static int
1565 1575 fifo_create(struct vnode *dvp, char *name, vattr_t *vap, enum vcexcl excl,
1566 1576 int mode, struct vnode **vpp, struct cred *cr, int flag,
1567 1577 caller_context_t *ct, vsecattr_t *vsecp)
1568 1578 {
1569 1579 int error;
1570 1580
1571 1581 ASSERT(dvp && (dvp->v_flag & VROOT) && *name == '\0');
1572 1582 if (excl == NONEXCL) {
1573 1583 if (mode && (error = fifo_access(dvp, mode, 0, cr, ct)))
1574 1584 return (error);
1575 1585 VN_HOLD(dvp);
1576 1586 return (0);
1577 1587 }
1578 1588 return (EEXIST);
1579 1589 }
1580 1590
1581 1591 /*
1582 1592 * If shadowing a vnode, apply the VOP_FSYNC to it.
1583 1593 * Otherwise, return 0.
1584 1594 */
1585 1595 int
1586 1596 fifo_fsync(vnode_t *vp, int syncflag, cred_t *crp, caller_context_t *ct)
1587 1597 {
1588 1598 fifonode_t *fnp = VTOF(vp);
1589 1599 vattr_t va;
1590 1600
1591 1601 if (fnp->fn_realvp == NULL)
1592 1602 return (0);
1593 1603
1594 1604 bzero((caddr_t)&va, sizeof (va));
1595 1605 va.va_mask = AT_MTIME | AT_ATIME;
1596 1606 if (VOP_GETATTR(fnp->fn_realvp, &va, 0, crp, ct) == 0) {
1597 1607 va.va_mask = 0;
1598 1608 if (fnp->fn_mtime > va.va_mtime.tv_sec) {
1599 1609 va.va_mtime.tv_sec = fnp->fn_mtime;
1600 1610 va.va_mask = AT_MTIME;
1601 1611 }
1602 1612 if (fnp->fn_atime > va.va_atime.tv_sec) {
1603 1613 va.va_atime.tv_sec = fnp->fn_atime;
1604 1614 va.va_mask |= AT_ATIME;
1605 1615 }
1606 1616 if (va.va_mask != 0)
1607 1617 (void) VOP_SETATTR(fnp->fn_realvp, &va, 0, crp, ct);
1608 1618 }
1609 1619 return (VOP_FSYNC(fnp->fn_realvp, syncflag, crp, ct));
1610 1620 }
1611 1621
1612 1622 /*
1613 1623 * Called when the upper level no longer holds references to the
1614 1624 * vnode. Sync the file system and free the fifonode.
1615 1625 */
1616 1626 void
1617 1627 fifo_inactive(vnode_t *vp, cred_t *crp, caller_context_t *ct)
1618 1628 {
1619 1629 fifonode_t *fnp;
1620 1630 fifolock_t *fn_lock;
1621 1631
1622 1632 mutex_enter(&ftable_lock);
1623 1633 mutex_enter(&vp->v_lock);
1624 1634 ASSERT(vp->v_count >= 1);
1625 1635 if (--vp->v_count != 0) {
1626 1636 /*
1627 1637 * Somebody accessed the fifo before we got a chance to
1628 1638 * remove it. They will remove it when they do a vn_rele.
1629 1639 */
1630 1640 mutex_exit(&vp->v_lock);
1631 1641 mutex_exit(&ftable_lock);
1632 1642 return;
1633 1643 }
1634 1644 mutex_exit(&vp->v_lock);
1635 1645
1636 1646 fnp = VTOF(vp);
1637 1647
1638 1648 /*
1639 1649 * remove fifo from fifo list so that no other process
1640 1650 * can grab it.
1641 1651 * Drop the reference count on the fifo node's
1642 1652 * underlying vfs.
1643 1653 */
1644 1654 if (fnp->fn_realvp) {
1645 1655 (void) fiforemove(fnp);
1646 1656 mutex_exit(&ftable_lock);
1647 1657 (void) fifo_fsync(vp, FSYNC, crp, ct);
1648 1658 VN_RELE(fnp->fn_realvp);
1649 1659 VFS_RELE(vp->v_vfsp);
1650 1660 vp->v_vfsp = NULL;
1651 1661 } else
1652 1662 mutex_exit(&ftable_lock);
1653 1663
1654 1664 fn_lock = fnp->fn_lock;
1655 1665
1656 1666 mutex_enter(&fn_lock->flk_lock);
1657 1667 ASSERT(vp->v_stream == NULL);
1658 1668 ASSERT(vp->v_count == 0);
1659 1669 /*
1660 1670 * if this is last reference to the lock, then we can
1661 1671 * free everything up.
1662 1672 */
1663 1673 if (--fn_lock->flk_ref == 0) {
1664 1674 mutex_exit(&fn_lock->flk_lock);
1665 1675 ASSERT(fnp->fn_open == 0);
1666 1676 ASSERT(fnp->fn_dest->fn_open == 0);
1667 1677 if (fnp->fn_mp) {
1668 1678 freemsg(fnp->fn_mp);
1669 1679 fnp->fn_mp = NULL;
1670 1680 fnp->fn_count = 0;
1671 1681 }
1672 1682 if (fnp->fn_pcredp != NULL) {
1673 1683 crfree(fnp->fn_pcredp);
1674 1684 fnp->fn_pcredp = NULL;
1675 1685 }
1676 1686 if (fnp->fn_flag & ISPIPE) {
1677 1687 fifonode_t *fn_dest = fnp->fn_dest;
1678 1688
1679 1689 vp = FTOV(fn_dest);
1680 1690 if (fn_dest->fn_mp) {
1681 1691 freemsg(fn_dest->fn_mp);
1682 1692 fn_dest->fn_mp = NULL;
1683 1693 fn_dest->fn_count = 0;
1684 1694 }
1685 1695 if (fn_dest->fn_pcredp != NULL) {
1686 1696 crfree(fn_dest->fn_pcredp);
1687 1697 fn_dest->fn_pcredp = NULL;
1688 1698 }
1689 1699 kmem_cache_free(pipe_cache, (fifodata_t *)fn_lock);
1690 1700 } else
1691 1701 kmem_cache_free(fnode_cache, (fifodata_t *)fn_lock);
1692 1702 } else {
1693 1703 mutex_exit(&fn_lock->flk_lock);
1694 1704 }
1695 1705 }
1696 1706
1697 1707 /*
1698 1708 * If shadowing a vnode, apply the VOP_FID to it.
1699 1709 * Otherwise, return EINVAL.
1700 1710 */
1701 1711 int
1702 1712 fifo_fid(vnode_t *vp, fid_t *fidfnp, caller_context_t *ct)
1703 1713 {
1704 1714 if (VTOF(vp)->fn_realvp)
1705 1715 return (VOP_FID(VTOF(vp)->fn_realvp, fidfnp, ct));
1706 1716 else
1707 1717 return (EINVAL);
1708 1718 }
1709 1719
1710 1720 /*
1711 1721 * Lock a fifonode.
1712 1722 */
1713 1723 /* ARGSUSED */
1714 1724 int
1715 1725 fifo_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
1716 1726 {
1717 1727 return (-1);
1718 1728 }
1719 1729
1720 1730 /*
1721 1731 * Unlock a fifonode.
1722 1732 */
1723 1733 /* ARGSUSED */
1724 1734 void
1725 1735 fifo_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
1726 1736 {
1727 1737 }
1728 1738
1729 1739 /*
1730 1740 * Return error since seeks are not allowed on pipes.
1731 1741 */
1732 1742 /*ARGSUSED*/
1733 1743 int
1734 1744 fifo_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
1735 1745 {
1736 1746 return (ESPIPE);
1737 1747 }
1738 1748
1739 1749 /*
1740 1750 * If there is a realvp associated with vp, return it.
1741 1751 */
1742 1752 int
1743 1753 fifo_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
1744 1754 {
1745 1755 vnode_t *rvp;
1746 1756
1747 1757 if ((rvp = VTOF(vp)->fn_realvp) != NULL) {
1748 1758 vp = rvp;
1749 1759 if (VOP_REALVP(vp, &rvp, ct) == 0)
1750 1760 vp = rvp;
1751 1761 }
1752 1762
1753 1763 *vpp = vp;
1754 1764 return (0);
1755 1765 }
1756 1766
1757 1767 /*
1758 1768 * Poll for interesting events on a stream pipe
1759 1769 */
1760 1770 /* ARGSUSED */
1761 1771 int
1762 1772 fifo_poll(vnode_t *vp, short events, int anyyet, short *reventsp,
1763 1773 pollhead_t **phpp, caller_context_t *ct)
1764 1774 {
1765 1775 fifonode_t *fnp, *fn_dest;
1766 1776 fifolock_t *fn_lock;
1767 1777 int retevents;
1768 1778 struct stdata *stp;
1769 1779
1770 1780 ASSERT(vp->v_stream != NULL);
1771 1781
1772 1782 stp = vp->v_stream;
1773 1783 retevents = 0;
1774 1784 fnp = VTOF(vp);
1775 1785 fn_dest = fnp->fn_dest;
1776 1786 fn_lock = fnp->fn_lock;
1777 1787
1778 1788 if (polllock(&stp->sd_pollist, &fn_lock->flk_lock) != 0) {
1779 1789 *reventsp = POLLNVAL;
1780 1790 return (0);
1781 1791 }
1782 1792
1783 1793 /*
1784 1794 * see if FIFO/pipe open
1785 1795 */
1786 1796 if ((fnp->fn_flag & FIFOISOPEN) == 0) {
1787 1797 if (((events & (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND)) &&
1788 1798 fnp->fn_rcnt == 0) ||
1789 1799 ((events & (POLLWRNORM | POLLWRBAND)) &&
1790 1800 fnp->fn_wcnt == 0)) {
1791 1801 mutex_exit(&fnp->fn_lock->flk_lock);
1792 1802 *reventsp = POLLERR;
1793 1803 return (0);
1794 1804 }
1795 1805 }
1796 1806
1797 1807 /*
1798 1808 * if not in fast mode, let the stream head take care of it
1799 1809 */
1800 1810 if (!(fnp->fn_flag & FIFOFAST)) {
1801 1811 mutex_exit(&fnp->fn_lock->flk_lock);
1802 1812 goto stream_mode;
1803 1813 }
1804 1814
1805 1815 /*
1806 1816 * If this is a pipe.. check to see if the other
1807 1817 * end is gone. If we are a fifo, check to see
1808 1818 * if write end is gone.
1809 1819 */
1810 1820
1811 1821 if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_open == 0)) {
1812 1822 retevents = POLLHUP;
1813 1823 } else if ((fnp->fn_flag & (FIFOCLOSE | ISPIPE)) == FIFOCLOSE &&
1814 1824 (fn_dest->fn_wcnt == 0)) {
1815 1825 /*
1816 1826 * no writer at other end.
1817 1827 * it was closed (versus yet to be opened)
1818 1828 */
1819 1829 retevents = POLLHUP;
1820 1830 } else if (events & (POLLWRNORM | POLLWRBAND)) {
1821 1831 if (events & POLLWRNORM) {
1822 1832 if (fn_dest->fn_count < Fifohiwat)
1823 1833 retevents = POLLWRNORM;
1824 1834 else
1825 1835 fnp->fn_flag |= FIFOHIWATW;
1826 1836 }
1827 1837 /*
1828 1838 * This is always true for fast pipes
1829 1839 * (Note: will go to STREAMS mode if band data is written)
1830 1840 */
1831 1841 if (events & POLLWRBAND)
1832 1842 retevents |= POLLWRBAND;
1833 1843 }
1834 1844 if (events & (POLLIN | POLLRDNORM)) {
1835 1845 if (fnp->fn_count)
1836 1846 retevents |= (events & (POLLIN | POLLRDNORM));
1837 1847 }
1838 1848
1839 1849 /*
1840 1850 * if we happened to get something and we're not edge-triggered, return
1841 1851 */
1842 1852 if ((*reventsp = (short)retevents) != 0 && !(events & POLLET)) {
1843 1853 mutex_exit(&fnp->fn_lock->flk_lock);
1844 1854 return (0);
1845 1855 }
1846 1856
1847 1857 /*
1848 1858 * If poll() has not found any events yet or we're edge-triggered, set
1849 1859 * up event cell to wake up the poll if a requested event occurs on this
1850 1860 * pipe/fifo.
1851 1861 */
1852 1862 if (!anyyet) {
1853 1863 if (events & POLLWRNORM)
1854 1864 fnp->fn_flag |= FIFOPOLLW;
1855 1865 if (events & (POLLIN | POLLRDNORM))
1856 1866 fnp->fn_flag |= FIFOPOLLR;
1857 1867 if (events & POLLRDBAND)
1858 1868 fnp->fn_flag |= FIFOPOLLRBAND;
1859 1869 /*
1860 1870 * XXX Don't like exposing this from streams
1861 1871 */
1862 1872 *phpp = &stp->sd_pollist;
1863 1873 }
1864 1874 mutex_exit(&fnp->fn_lock->flk_lock);
1865 1875 return (0);
1866 1876 stream_mode:
1867 1877 return (strpoll(stp, events, anyyet, reventsp, phpp));
1868 1878 }
1869 1879
1870 1880 /*
1871 1881 * POSIX pathconf() support.
1872 1882 */
1873 1883 /* ARGSUSED */
1874 1884 int
1875 1885 fifo_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
1876 1886 caller_context_t *ct)
1877 1887 {
1878 1888 ulong_t val;
1879 1889 int error = 0;
1880 1890
1881 1891 switch (cmd) {
1882 1892
1883 1893 case _PC_LINK_MAX:
1884 1894 val = MAXLINK;
1885 1895 break;
1886 1896
1887 1897 case _PC_MAX_CANON:
1888 1898 val = MAX_CANON;
1889 1899 break;
1890 1900
1891 1901 case _PC_MAX_INPUT:
1892 1902 val = MAX_INPUT;
1893 1903 break;
1894 1904
1895 1905 case _PC_NAME_MAX:
1896 1906 error = EINVAL;
1897 1907 break;
1898 1908
1899 1909 case _PC_PATH_MAX:
1900 1910 case _PC_SYMLINK_MAX:
1901 1911 val = MAXPATHLEN;
1902 1912 break;
1903 1913
1904 1914 case _PC_PIPE_BUF:
1905 1915 val = PIPE_BUF;
1906 1916 break;
1907 1917
1908 1918 case _PC_NO_TRUNC:
1909 1919 if (vp->v_vfsp->vfs_flag & VFS_NOTRUNC)
1910 1920 val = 1; /* NOTRUNC is enabled for vp */
1911 1921 else
1912 1922 val = (ulong_t)-1;
1913 1923 break;
1914 1924
1915 1925 case _PC_VDISABLE:
1916 1926 val = _POSIX_VDISABLE;
1917 1927 break;
1918 1928
1919 1929 case _PC_CHOWN_RESTRICTED:
1920 1930 if (rstchown)
1921 1931 val = rstchown; /* chown restricted enabled */
1922 1932 else
1923 1933 val = (ulong_t)-1;
1924 1934 break;
1925 1935
1926 1936 case _PC_FILESIZEBITS:
1927 1937 val = (ulong_t)-1;
1928 1938 break;
1929 1939
1930 1940 default:
1931 1941 if (VTOF(vp)->fn_realvp)
1932 1942 error = VOP_PATHCONF(VTOF(vp)->fn_realvp, cmd,
1933 1943 &val, cr, ct);
1934 1944 else
1935 1945 error = EINVAL;
1936 1946 break;
1937 1947 }
1938 1948
1939 1949 if (error == 0)
1940 1950 *valp = val;
1941 1951 return (error);
1942 1952 }
1943 1953
1944 1954 /*
1945 1955 * If shadowing a vnode, apply VOP_SETSECATTR to it.
1946 1956 * Otherwise, return NOSYS.
1947 1957 */
1948 1958 int
1949 1959 fifo_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp,
1950 1960 caller_context_t *ct)
1951 1961 {
1952 1962 int error;
1953 1963
1954 1964 /*
1955 1965 * The acl(2) system call tries to grab the write lock on the
1956 1966 * file when setting an ACL, but fifofs does not implement
1957 1967 * VOP_RWLOCK or VOP_RWUNLOCK, so we do it here instead.
1958 1968 */
1959 1969 if (VTOF(vp)->fn_realvp) {
1960 1970 (void) VOP_RWLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, ct);
1961 1971 error = VOP_SETSECATTR(VTOF(vp)->fn_realvp, vsap, flag,
1962 1972 crp, ct);
1963 1973 VOP_RWUNLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, ct);
1964 1974 return (error);
1965 1975 } else
1966 1976 return (fs_nosys());
1967 1977 }
1968 1978
1969 1979 /*
1970 1980 * If shadowing a vnode, apply VOP_GETSECATTR to it. Otherwise, fabricate
1971 1981 * an ACL from the permission bits that fifo_getattr() makes up.
1972 1982 */
1973 1983 int
1974 1984 fifo_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp,
1975 1985 caller_context_t *ct)
1976 1986 {
1977 1987 if (VTOF(vp)->fn_realvp)
1978 1988 return (VOP_GETSECATTR(VTOF(vp)->fn_realvp, vsap, flag,
1979 1989 crp, ct));
1980 1990 else
1981 1991 return (fs_fab_acl(vp, vsap, flag, crp, ct));
1982 1992 }
1983 1993
1984 1994
1985 1995 /*
1986 1996 * Set the FIFOSTAYFAST flag so nobody can turn the fifo into stream mode.
1987 1997 * If the flag is already set then wait until it is removed - releasing
1988 1998 * the lock.
1989 1999 * If the fifo switches into stream mode while we are waiting, return failure.
1990 2000 */
1991 2001 static boolean_t
1992 2002 fifo_stayfast_enter(fifonode_t *fnp)
1993 2003 {
1994 2004 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1995 2005 while (fnp->fn_flag & FIFOSTAYFAST) {
1996 2006 fnp->fn_flag |= FIFOWAITMODE;
1997 2007 cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
1998 2008 fnp->fn_flag &= ~FIFOWAITMODE;
1999 2009 }
2000 2010 if (!(fnp->fn_flag & FIFOFAST))
2001 2011 return (B_FALSE);
2002 2012
2003 2013 fnp->fn_flag |= FIFOSTAYFAST;
2004 2014 return (B_TRUE);
2005 2015 }
2006 2016
2007 2017 /*
2008 2018 * Unset the FIFOSTAYFAST flag and notify anybody waiting for this flag
2009 2019 * to be removed:
2010 2020 * - threads wanting to turn into stream mode waiting in fifo_fastoff(),
2011 2021 * - other writers threads waiting in fifo_stayfast_enter().
2012 2022 */
2013 2023 static void
2014 2024 fifo_stayfast_exit(fifonode_t *fnp)
2015 2025 {
2016 2026 fifonode_t *fn_dest = fnp->fn_dest;
2017 2027
2018 2028 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
2019 2029
2020 2030 fnp->fn_flag &= ~FIFOSTAYFAST;
2021 2031
2022 2032 if (fnp->fn_flag & FIFOWAITMODE)
2023 2033 cv_broadcast(&fnp->fn_wait_cv);
2024 2034
2025 2035 if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_flag & FIFOWAITMODE))
2026 2036 cv_broadcast(&fn_dest->fn_wait_cv);
2027 2037 }
↓ open down ↓ |
612 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX