4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/signal.h>
11 #include <linux/fcntl.h>
12 #include <linux/termios.h>
14 #include <linux/file.h>
15 #include <linux/poll.h>
17 #include <asm/uaccess.h>
20 * Define this if you want SunOS compatibility wrt braindead
21 * select behaviour on FIFO's.
24 #define FIFO_SUNOS_BRAINDAMAGE
26 #undef FIFO_SUNOS_BRAINDAMAGE
29 /* We don't use the head/tail construction any more. Now we use the start/len*/
30 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
31 /* Florian Coosmann (FGC) ^ current = 1 */
32 /* Additionally, we now use locking technique. This prevents race condition */
33 /* in case of paging and multiple read/write on the same pipe. (FGC) */
36 static ssize_t
pipe_read(struct file
* filp
, char * buf
,
37 size_t count
, loff_t
*ppos
)
39 struct inode
* inode
= filp
->f_dentry
->d_inode
;
40 ssize_t chars
= 0, size
= 0, read
= 0;
43 if (ppos
!= &filp
->f_pos
)
46 if (filp
->f_flags
& O_NONBLOCK
) {
47 if (PIPE_LOCK(*inode
))
49 if (PIPE_EMPTY(*inode
)) {
50 if (PIPE_WRITERS(*inode
))
55 } else while (PIPE_EMPTY(*inode
) || PIPE_LOCK(*inode
)) {
56 if (PIPE_EMPTY(*inode
)) {
57 if (!PIPE_WRITERS(*inode
) || !count
)
60 if (signal_pending(current
))
62 interruptible_sleep_on(&PIPE_WAIT(*inode
));
65 while (count
>0 && (size
= PIPE_SIZE(*inode
))) {
66 chars
= PIPE_MAX_RCHUNK(*inode
);
72 pipebuf
= PIPE_BASE(*inode
)+PIPE_START(*inode
);
73 PIPE_START(*inode
) += chars
;
74 PIPE_START(*inode
) &= (PIPE_BUF
-1);
75 PIPE_LEN(*inode
) -= chars
;
77 copy_to_user(buf
, pipebuf
, chars
);
81 wake_up_interruptible(&PIPE_WAIT(*inode
));
86 if (PIPE_WRITERS(*inode
))
91 static ssize_t
pipe_write(struct file
* filp
, const char * buf
,
92 size_t count
, loff_t
*ppos
)
94 struct inode
* inode
= filp
->f_dentry
->d_inode
;
95 ssize_t chars
= 0, free
= 0, written
= 0;
98 if (ppos
!= &filp
->f_pos
)
101 if (!PIPE_READERS(*inode
)) { /* no readers */
102 send_sig(SIGPIPE
,current
,0);
105 /* if count <= PIPE_BUF, we have to make it atomic */
106 if (count
<= PIPE_BUF
)
109 free
= 1; /* can't do it atomically, wait for any free space */
111 while ((PIPE_FREE(*inode
) < free
) || PIPE_LOCK(*inode
)) {
112 if (!PIPE_READERS(*inode
)) { /* no readers */
113 send_sig(SIGPIPE
,current
,0);
114 return written
? :-EPIPE
;
116 if (signal_pending(current
))
117 return written
? :-ERESTARTSYS
;
118 if (filp
->f_flags
& O_NONBLOCK
)
119 return written
? :-EAGAIN
;
120 interruptible_sleep_on(&PIPE_WAIT(*inode
));
123 while (count
>0 && (free
= PIPE_FREE(*inode
))) {
124 chars
= PIPE_MAX_WCHUNK(*inode
);
129 pipebuf
= PIPE_BASE(*inode
)+PIPE_END(*inode
);
131 PIPE_LEN(*inode
) += chars
;
133 copy_from_user(pipebuf
, buf
, chars
);
137 wake_up_interruptible(&PIPE_WAIT(*inode
));
140 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
141 mark_inode_dirty(inode
);
145 static long long pipe_lseek(struct file
* file
, long long offset
, int orig
)
150 static ssize_t
bad_pipe_r(struct file
* filp
, char * buf
,
151 size_t count
, loff_t
*ppos
)
156 static ssize_t
bad_pipe_w(struct file
* filp
, const char * buf
,
157 size_t count
, loff_t
*ppos
)
162 static int pipe_ioctl(struct inode
*pino
, struct file
* filp
,
163 unsigned int cmd
, unsigned long arg
)
167 return put_user(PIPE_SIZE(*pino
),(int *) arg
);
173 static unsigned int pipe_poll(struct file
* filp
, poll_table
* wait
)
176 struct inode
* inode
= filp
->f_dentry
->d_inode
;
178 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
179 mask
= POLLIN
| POLLRDNORM
;
180 if (PIPE_EMPTY(*inode
))
181 mask
= POLLOUT
| POLLWRNORM
;
182 if (!PIPE_WRITERS(*inode
))
184 if (!PIPE_READERS(*inode
))
189 #ifdef FIFO_SUNOS_BRAINDAMAGE
191 * Argh! Why does SunOS have to have different select() behaviour
192 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
194 static unsigned int fifo_poll(struct file
* filp
, poll_table
* wait
)
197 struct inode
* inode
= filp
->f_dentry
->d_inode
;
199 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
200 mask
= POLLIN
| POLLRDNORM
;
201 if (PIPE_EMPTY(*inode
))
202 mask
= POLLOUT
| POLLWRNORM
;
203 if (!PIPE_READERS(*inode
))
209 #define fifo_poll pipe_poll
211 #endif /* FIFO_SUNOS_BRAINDAMAGE */
214 * The 'connect_xxx()' functions are needed for named pipes when
215 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
216 * and we need to act differently until we do get a writer..
218 static ssize_t
connect_read(struct file
* filp
, char * buf
,
219 size_t count
, loff_t
*ppos
)
221 struct inode
* inode
= filp
->f_dentry
->d_inode
;
222 if (PIPE_EMPTY(*inode
) && !PIPE_WRITERS(*inode
))
224 filp
->f_op
= &read_fifo_fops
;
225 return pipe_read(filp
,buf
,count
,ppos
);
228 static unsigned int connect_poll(struct file
* filp
, poll_table
* wait
)
230 struct inode
* inode
= filp
->f_dentry
->d_inode
;
232 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
233 if (!PIPE_EMPTY(*inode
)) {
234 filp
->f_op
= &read_fifo_fops
;
235 return POLLIN
| POLLRDNORM
;
237 if (PIPE_WRITERS(*inode
))
238 filp
->f_op
= &read_fifo_fops
;
239 return POLLOUT
| POLLWRNORM
;
242 static int pipe_release(struct inode
* inode
)
244 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
245 free_page((unsigned long) PIPE_BASE(*inode
));
246 PIPE_BASE(*inode
) = NULL
;
248 wake_up_interruptible(&PIPE_WAIT(*inode
));
252 static int pipe_read_release(struct inode
* inode
, struct file
* filp
)
254 PIPE_READERS(*inode
)--;
255 return pipe_release(inode
);
258 static int pipe_write_release(struct inode
* inode
, struct file
* filp
)
260 PIPE_WRITERS(*inode
)--;
261 return pipe_release(inode
);
264 static int pipe_rdwr_release(struct inode
* inode
, struct file
* filp
)
266 if (filp
->f_mode
& FMODE_READ
)
267 PIPE_READERS(*inode
)--;
268 if (filp
->f_mode
& FMODE_WRITE
)
269 PIPE_WRITERS(*inode
)--;
270 return pipe_release(inode
);
273 static int pipe_read_open(struct inode
* inode
, struct file
* filp
)
275 PIPE_READERS(*inode
)++;
279 static int pipe_write_open(struct inode
* inode
, struct file
* filp
)
281 PIPE_WRITERS(*inode
)++;
285 static int pipe_rdwr_open(struct inode
* inode
, struct file
* filp
)
287 if (filp
->f_mode
& FMODE_READ
)
288 PIPE_READERS(*inode
)++;
289 if (filp
->f_mode
& FMODE_WRITE
)
290 PIPE_WRITERS(*inode
)++;
295 * The file_operations structs are not static because they
296 * are also used in linux/fs/fifo.c to do operations on FIFOs.
298 struct file_operations connecting_fifo_fops
= {
302 NULL
, /* no readdir */
305 NULL
, /* no mmap on pipes.. surprise */
312 struct file_operations read_fifo_fops
= {
316 NULL
, /* no readdir */
319 NULL
, /* no mmap on pipes.. surprise */
326 struct file_operations write_fifo_fops
= {
330 NULL
, /* no readdir */
340 struct file_operations rdwr_fifo_fops
= {
344 NULL
, /* no readdir */
354 struct file_operations read_pipe_fops
= {
358 NULL
, /* no readdir */
361 NULL
, /* no mmap on pipes.. surprise */
368 struct file_operations write_pipe_fops
= {
372 NULL
, /* no readdir */
382 struct file_operations rdwr_pipe_fops
= {
386 NULL
, /* no readdir */
396 static struct inode
* get_pipe_inode(void)
398 extern struct inode_operations pipe_inode_operations
;
399 struct inode
*inode
= get_empty_inode();
402 unsigned long page
= __get_free_page(GFP_USER
);
408 PIPE_BASE(*inode
) = (char *) page
;
409 inode
->i_op
= &pipe_inode_operations
;
410 PIPE_WAIT(*inode
) = NULL
;
411 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
412 PIPE_RD_OPENERS(*inode
) = PIPE_WR_OPENERS(*inode
) = 0;
413 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
414 PIPE_LOCK(*inode
) = 0;
416 * Mark the inode dirty from the very beginning,
417 * that way it will never be moved to the dirty
418 * list because "mark_inode_dirty()" will think
419 * that it already _is_ on the dirty list.
421 inode
->i_state
= I_DIRTY
;
422 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
423 inode
->i_uid
= current
->fsuid
;
424 inode
->i_gid
= current
->fsgid
;
425 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
426 inode
->i_blksize
= PAGE_SIZE
;
432 struct inode_operations pipe_inode_operations
= {
445 NULL
, /* writepage */
448 NULL
/* permission */
453 struct inode
* inode
;
454 struct file
*f1
, *f2
;
459 f1
= get_empty_filp();
463 f2
= get_empty_filp();
467 inode
= get_pipe_inode();
471 error
= get_unused_fd();
473 goto close_f12_inode
;
476 error
= get_unused_fd();
478 goto close_f12_inode_i
;
482 f1
->f_dentry
= f2
->f_dentry
= dget(d_alloc_root(inode
, NULL
));
484 goto close_f12_inode_i_j
;
487 f1
->f_pos
= f2
->f_pos
= 0;
488 f1
->f_flags
= O_RDONLY
;
489 f1
->f_op
= &read_pipe_fops
;
493 f2
->f_flags
= O_WRONLY
;
494 f2
->f_op
= &write_pipe_fops
;
508 free_page((unsigned long) PIPE_BASE(*inode
));