4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/malloc.h>
11 #include <linux/smp_lock.h>
13 #include <asm/uaccess.h>
16 * Define this if you want SunOS compatibility wrt braindead
17 * select behaviour on FIFO's.
20 #define FIFO_SUNOS_BRAINDAMAGE
22 #undef FIFO_SUNOS_BRAINDAMAGE
26 * We use a start+len construction, which provides full use of the
28 * -- Florian Coosmann (FGC)
30 * Reads with count = 0 should always return 0.
31 * -- Julian Bradfield 1999-06-07.
34 /* Drop the inode semaphore and wait for a pipe event, atomically */
35 static void pipe_wait(struct inode
* inode
)
37 DECLARE_WAITQUEUE(wait
, current
);
38 current
->state
= TASK_INTERRUPTIBLE
;
39 add_wait_queue(PIPE_WAIT(*inode
), &wait
);
42 remove_wait_queue(PIPE_WAIT(*inode
), &wait
);
43 current
->state
= TASK_RUNNING
;
47 pipe_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
49 struct inode
*inode
= filp
->f_dentry
->d_inode
;
50 ssize_t size
, read
, ret
;
52 /* Seeks are not allowed on pipes. */
54 if (ppos
!= &filp
->f_pos
)
57 /* Always return 0 on null read. */
62 /* Get the pipe semaphore */
64 if (down_interruptible(PIPE_SEM(*inode
)))
67 if (PIPE_EMPTY(*inode
)) {
69 if (!PIPE_WRITERS(*inode
))
73 if (filp
->f_flags
& O_NONBLOCK
)
79 if (signal_pending(current
))
81 if (down_interruptible(PIPE_SEM(*inode
)))
84 if (!PIPE_EMPTY(*inode
))
86 if (!PIPE_WRITERS(*inode
))
91 /* Read what data is available. */
94 while (count
> 0 && (size
= PIPE_LEN(*inode
))) {
95 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_START(*inode
);
96 ssize_t chars
= PIPE_MAX_RCHUNK(*inode
);
103 if (copy_to_user(buf
, pipebuf
, chars
))
107 PIPE_START(*inode
) += chars
;
108 PIPE_START(*inode
) &= (PIPE_SIZE
- 1);
109 PIPE_LEN(*inode
) -= chars
;
114 /* Cache behaviour optimization */
115 if (!PIPE_LEN(*inode
))
116 PIPE_START(*inode
) = 0;
118 /* Signal writers there is more room. */
119 wake_up_interruptible(PIPE_WAIT(*inode
));
126 up(PIPE_SEM(*inode
));
132 pipe_write(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
134 struct inode
*inode
= filp
->f_dentry
->d_inode
;
135 ssize_t free
, written
, ret
;
137 /* Seeks are not allowed on pipes. */
139 if (ppos
!= &filp
->f_pos
)
142 /* Null write succeeds. */
148 if (down_interruptible(PIPE_SEM(*inode
)))
151 /* No readers yields SIGPIPE. */
152 if (!PIPE_READERS(*inode
))
155 /* If count <= PIPE_BUF, we have to make it atomic. */
156 free
= (count
<= PIPE_BUF
? count
: 1);
159 /* Wait, or check for, available space. */
160 if (filp
->f_flags
& O_NONBLOCK
) {
162 if (PIPE_FREE(*inode
) < free
)
165 while (PIPE_FREE(*inode
) < free
) {
168 if (signal_pending(current
))
170 if (down_interruptible(PIPE_SEM(*inode
)))
173 if (!PIPE_READERS(*inode
))
178 /* Copy into available space. */
182 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_END(*inode
);
183 ssize_t chars
= PIPE_MAX_WCHUNK(*inode
);
185 if ((space
= PIPE_FREE(*inode
)) != 0) {
191 if (copy_from_user(pipebuf
, buf
, chars
))
195 PIPE_LEN(*inode
) += chars
;
198 space
= PIPE_FREE(*inode
);
203 if (filp
->f_flags
& O_NONBLOCK
)
207 /* This should be a synchronous wake-up: don't do idle reschedules! */
208 wake_up_interruptible(PIPE_WAIT(*inode
));
210 if (signal_pending(current
))
212 if (down_interruptible(PIPE_SEM(*inode
)))
214 if (!PIPE_READERS(*inode
))
216 } while (!PIPE_FREE(*inode
));
220 /* Signal readers there is more data. */
221 wake_up_interruptible(PIPE_WAIT(*inode
));
223 ret
= (written
? written
: -EAGAIN
);
224 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
225 mark_inode_dirty(inode
);
228 up(PIPE_SEM(*inode
));
233 up(PIPE_SEM(*inode
));
234 send_sig(SIGPIPE
, current
, 0);
239 pipe_lseek(struct file
*file
, loff_t offset
, int orig
)
245 bad_pipe_r(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
251 bad_pipe_w(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
257 pipe_ioctl(struct inode
*pino
, struct file
*filp
,
258 unsigned int cmd
, unsigned long arg
)
262 return put_user(PIPE_LEN(*pino
), (int *)arg
);
269 pipe_poll(struct file
*filp
, poll_table
*wait
)
272 struct inode
*inode
= filp
->f_dentry
->d_inode
;
274 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
276 /* Reading only -- no need for aquiring the semaphore. */
277 mask
= POLLIN
| POLLRDNORM
;
278 if (PIPE_EMPTY(*inode
))
279 mask
= POLLOUT
| POLLWRNORM
;
280 if (!PIPE_WRITERS(*inode
))
282 if (!PIPE_READERS(*inode
))
288 #ifdef FIFO_SUNOS_BRAINDAMAGE
290 * Argh! Why does SunOS have to have different select() behaviour
291 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
294 fifo_poll(struct file
*filp
, poll_table
*wait
)
297 struct inode
*inode
= filp
->f_dentry
->d_inode
;
299 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
301 /* Reading only -- no need for aquiring the semaphore. */
302 mask
= POLLIN
| POLLRDNORM
;
303 if (PIPE_EMPTY(*inode
))
304 mask
= POLLOUT
| POLLWRNORM
;
305 if (!PIPE_READERS(*inode
))
312 #define fifo_poll pipe_poll
314 #endif /* FIFO_SUNOS_BRAINDAMAGE */
317 * The 'connect_xxx()' functions are needed for named pipes when
318 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
319 * and we need to act differently until we do get a writer..
322 connect_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
324 struct inode
*inode
= filp
->f_dentry
->d_inode
;
326 /* Reading only -- no need for aquiring the semaphore. */
327 if (PIPE_EMPTY(*inode
) && !PIPE_WRITERS(*inode
))
330 filp
->f_op
= &read_fifo_fops
;
331 return pipe_read(filp
, buf
, count
, ppos
);
335 connect_poll(struct file
*filp
, poll_table
*wait
)
337 struct inode
*inode
= filp
->f_dentry
->d_inode
;
338 unsigned int mask
= 0;
340 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
342 /* Reading only -- no need for aquiring the semaphore. */
343 if (!PIPE_EMPTY(*inode
)) {
344 filp
->f_op
= &read_fifo_fops
;
345 mask
= POLLIN
| POLLRDNORM
;
346 } else if (PIPE_WRITERS(*inode
)) {
347 filp
->f_op
= &read_fifo_fops
;
348 mask
= POLLOUT
| POLLWRNORM
;
355 pipe_release(struct inode
*inode
, int decr
, int decw
)
357 down(PIPE_SEM(*inode
));
358 PIPE_READERS(*inode
) -= decr
;
359 PIPE_WRITERS(*inode
) -= decw
;
360 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
361 struct pipe_inode_info
*info
= inode
->i_pipe
;
362 inode
->i_pipe
= NULL
;
363 free_page((unsigned long) info
->base
);
366 wake_up_interruptible(PIPE_WAIT(*inode
));
368 up(PIPE_SEM(*inode
));
374 pipe_read_release(struct inode
*inode
, struct file
*filp
)
376 return pipe_release(inode
, 1, 0);
380 pipe_write_release(struct inode
*inode
, struct file
*filp
)
382 return pipe_release(inode
, 0, 1);
386 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
390 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
391 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
392 return pipe_release(inode
, decr
, decw
);
396 pipe_read_open(struct inode
*inode
, struct file
*filp
)
398 /* We could have perhaps used atomic_t, but this and friends
399 below are the only places. So it doesn't seem worthwhile. */
400 down(PIPE_SEM(*inode
));
401 PIPE_READERS(*inode
)++;
402 up(PIPE_SEM(*inode
));
408 pipe_write_open(struct inode
*inode
, struct file
*filp
)
410 down(PIPE_SEM(*inode
));
411 PIPE_WRITERS(*inode
)++;
412 up(PIPE_SEM(*inode
));
418 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
420 down(PIPE_SEM(*inode
));
421 if (filp
->f_mode
& FMODE_READ
)
422 PIPE_READERS(*inode
)++;
423 if (filp
->f_mode
& FMODE_WRITE
)
424 PIPE_WRITERS(*inode
)++;
425 up(PIPE_SEM(*inode
));
431 * The file_operations structs are not static because they
432 * are also used in linux/fs/fifo.c to do operations on FIFOs.
434 struct file_operations connecting_fifo_fops
= {
438 NULL
, /* no readdir */
441 NULL
, /* no mmap on pipes.. surprise */
448 struct file_operations read_fifo_fops
= {
452 NULL
, /* no readdir */
455 NULL
, /* no mmap on pipes.. surprise */
462 struct file_operations write_fifo_fops
= {
466 NULL
, /* no readdir */
476 struct file_operations rdwr_fifo_fops
= {
480 NULL
, /* no readdir */
490 struct file_operations read_pipe_fops
= {
494 NULL
, /* no readdir */
497 NULL
, /* no mmap on pipes.. surprise */
504 struct file_operations write_pipe_fops
= {
508 NULL
, /* no readdir */
518 struct file_operations rdwr_pipe_fops
= {
522 NULL
, /* no readdir */
532 static struct inode
* get_pipe_inode(void)
534 extern struct inode_operations pipe_inode_operations
;
535 struct inode
*inode
= get_empty_inode();
541 page
= __get_free_page(GFP_USER
);
545 inode
->i_pipe
= kmalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
549 inode
->i_op
= &pipe_inode_operations
;
551 init_waitqueue_head(PIPE_WAIT(*inode
));
552 PIPE_BASE(*inode
) = (char *) page
;
553 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
554 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
557 * Mark the inode dirty from the very beginning,
558 * that way it will never be moved to the dirty
559 * list because "mark_inode_dirty()" will think
560 * that it already _is_ on the dirty list.
562 inode
->i_state
= I_DIRTY
;
563 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
564 inode
->i_uid
= current
->fsuid
;
565 inode
->i_gid
= current
->fsgid
;
566 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
567 inode
->i_blksize
= PAGE_SIZE
;
578 struct inode_operations pipe_inode_operations
= {
590 NULL
, /* follow_link */
591 NULL
, /* get_block */
593 NULL
, /* writepage */
594 NULL
, /* flushpage */
596 NULL
, /* permission */
598 NULL
/* revalidate */
603 struct inode
* inode
;
604 struct file
*f1
, *f2
;
609 f1
= get_empty_filp();
613 f2
= get_empty_filp();
617 inode
= get_pipe_inode();
621 error
= get_unused_fd();
623 goto close_f12_inode
;
626 error
= get_unused_fd();
628 goto close_f12_inode_i
;
632 f1
->f_dentry
= f2
->f_dentry
= dget(d_alloc_root(inode
));
634 goto close_f12_inode_i_j
;
637 f1
->f_pos
= f2
->f_pos
= 0;
638 f1
->f_flags
= O_RDONLY
;
639 f1
->f_op
= &read_pipe_fops
;
643 f2
->f_flags
= O_WRONLY
;
644 f2
->f_op
= &write_pipe_fops
;
658 free_page((unsigned long) PIPE_BASE(*inode
));
659 kfree(inode
->i_pipe
);
660 inode
->i_pipe
= NULL
;