4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/malloc.h>
12 #include <asm/uaccess.h>
15 * Define this if you want SunOS compatibility wrt braindead
16 * select behaviour on FIFO's.
19 #define FIFO_SUNOS_BRAINDAMAGE
21 #undef FIFO_SUNOS_BRAINDAMAGE
24 /* We don't use the head/tail construction any more. Now we use the start/len*/
25 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
26 /* Florian Coosmann (FGC) ^ current = 1 */
27 /* Additionally, we now use locking technique. This prevents race condition */
28 /* in case of paging and multiple read/write on the same pipe. (FGC) */
31 static ssize_t
pipe_read(struct file
* filp
, char * buf
,
32 size_t count
, loff_t
*ppos
)
34 struct inode
* inode
= filp
->f_dentry
->d_inode
;
35 ssize_t chars
= 0, size
= 0, read
= 0;
38 if (ppos
!= &filp
->f_pos
)
41 if (filp
->f_flags
& O_NONBLOCK
) {
42 if (PIPE_LOCK(*inode
))
44 if (PIPE_EMPTY(*inode
)) {
45 if (PIPE_WRITERS(*inode
))
50 } else while (PIPE_EMPTY(*inode
) || PIPE_LOCK(*inode
)) {
51 if (PIPE_EMPTY(*inode
)) {
52 if (!PIPE_WRITERS(*inode
) || !count
)
55 if (signal_pending(current
))
57 interruptible_sleep_on(&PIPE_WAIT(*inode
));
60 while (count
>0 && (size
= PIPE_SIZE(*inode
))) {
61 chars
= PIPE_MAX_RCHUNK(*inode
);
67 pipebuf
= PIPE_BASE(*inode
)+PIPE_START(*inode
);
68 PIPE_START(*inode
) += chars
;
69 PIPE_START(*inode
) &= (PIPE_BUF
-1);
70 PIPE_LEN(*inode
) -= chars
;
72 copy_to_user(buf
, pipebuf
, chars
);
76 wake_up_interruptible(&PIPE_WAIT(*inode
));
81 if (PIPE_WRITERS(*inode
))
86 static ssize_t
pipe_write(struct file
* filp
, const char * buf
,
87 size_t count
, loff_t
*ppos
)
89 struct inode
* inode
= filp
->f_dentry
->d_inode
;
90 ssize_t chars
= 0, free
= 0, written
= 0, err
=0;
93 if (ppos
!= &filp
->f_pos
)
96 if (!PIPE_READERS(*inode
)) { /* no readers */
97 send_sig(SIGPIPE
,current
,0);
100 /* if count <= PIPE_BUF, we have to make it atomic */
101 if (count
<= PIPE_BUF
)
104 free
= 1; /* can't do it atomically, wait for any free space */
105 if (down_interruptible(&inode
->i_sem
)) {
109 while ((PIPE_FREE(*inode
) < free
) || PIPE_LOCK(*inode
)) {
110 if (!PIPE_READERS(*inode
)) { /* no readers */
111 send_sig(SIGPIPE
,current
,0);
115 if (signal_pending(current
)) {
119 if (filp
->f_flags
& O_NONBLOCK
) {
123 interruptible_sleep_on(&PIPE_WAIT(*inode
));
126 while (count
>0 && (free
= PIPE_FREE(*inode
))) {
127 chars
= PIPE_MAX_WCHUNK(*inode
);
132 pipebuf
= PIPE_BASE(*inode
)+PIPE_END(*inode
);
134 PIPE_LEN(*inode
) += chars
;
136 copy_from_user(pipebuf
, buf
, chars
);
140 wake_up_interruptible(&PIPE_WAIT(*inode
));
143 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
144 mark_inode_dirty(inode
);
147 return written
? written
: err
;
150 static long long pipe_lseek(struct file
* file
, long long offset
, int orig
)
155 static ssize_t
bad_pipe_r(struct file
* filp
, char * buf
,
156 size_t count
, loff_t
*ppos
)
161 static ssize_t
bad_pipe_w(struct file
* filp
, const char * buf
,
162 size_t count
, loff_t
*ppos
)
167 static int pipe_ioctl(struct inode
*pino
, struct file
* filp
,
168 unsigned int cmd
, unsigned long arg
)
172 return put_user(PIPE_SIZE(*pino
),(int *) arg
);
178 static unsigned int pipe_poll(struct file
* filp
, poll_table
* wait
)
181 struct inode
* inode
= filp
->f_dentry
->d_inode
;
183 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
184 mask
= POLLIN
| POLLRDNORM
;
185 if (PIPE_EMPTY(*inode
))
186 mask
= POLLOUT
| POLLWRNORM
;
187 if (!PIPE_WRITERS(*inode
))
189 if (!PIPE_READERS(*inode
))
194 #ifdef FIFO_SUNOS_BRAINDAMAGE
196 * Argh! Why does SunOS have to have different select() behaviour
197 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
199 static unsigned int fifo_poll(struct file
* filp
, poll_table
* wait
)
202 struct inode
* inode
= filp
->f_dentry
->d_inode
;
204 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
205 mask
= POLLIN
| POLLRDNORM
;
206 if (PIPE_EMPTY(*inode
))
207 mask
= POLLOUT
| POLLWRNORM
;
208 if (!PIPE_READERS(*inode
))
214 #define fifo_poll pipe_poll
216 #endif /* FIFO_SUNOS_BRAINDAMAGE */
219 * The 'connect_xxx()' functions are needed for named pipes when
220 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
221 * and we need to act differently until we do get a writer..
223 static ssize_t
connect_read(struct file
* filp
, char * buf
,
224 size_t count
, loff_t
*ppos
)
226 struct inode
* inode
= filp
->f_dentry
->d_inode
;
227 if (PIPE_EMPTY(*inode
) && !PIPE_WRITERS(*inode
))
229 filp
->f_op
= &read_fifo_fops
;
230 return pipe_read(filp
,buf
,count
,ppos
);
233 static unsigned int connect_poll(struct file
* filp
, poll_table
* wait
)
235 struct inode
* inode
= filp
->f_dentry
->d_inode
;
237 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
238 if (!PIPE_EMPTY(*inode
)) {
239 filp
->f_op
= &read_fifo_fops
;
240 return POLLIN
| POLLRDNORM
;
242 if (PIPE_WRITERS(*inode
))
243 filp
->f_op
= &read_fifo_fops
;
244 return POLLOUT
| POLLWRNORM
;
247 static int pipe_release(struct inode
* inode
)
249 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
250 struct pipe_inode_info
*info
= inode
->i_pipe
;
251 inode
->i_pipe
= NULL
;
252 free_page((unsigned long) info
->base
);
256 wake_up_interruptible(&PIPE_WAIT(*inode
));
260 static int pipe_read_release(struct inode
* inode
, struct file
* filp
)
262 PIPE_READERS(*inode
)--;
263 return pipe_release(inode
);
266 static int pipe_write_release(struct inode
* inode
, struct file
* filp
)
268 PIPE_WRITERS(*inode
)--;
269 return pipe_release(inode
);
272 static int pipe_rdwr_release(struct inode
* inode
, struct file
* filp
)
274 if (filp
->f_mode
& FMODE_READ
)
275 PIPE_READERS(*inode
)--;
276 if (filp
->f_mode
& FMODE_WRITE
)
277 PIPE_WRITERS(*inode
)--;
278 return pipe_release(inode
);
281 static int pipe_read_open(struct inode
* inode
, struct file
* filp
)
283 PIPE_READERS(*inode
)++;
287 static int pipe_write_open(struct inode
* inode
, struct file
* filp
)
289 PIPE_WRITERS(*inode
)++;
293 static int pipe_rdwr_open(struct inode
* inode
, struct file
* filp
)
295 if (filp
->f_mode
& FMODE_READ
)
296 PIPE_READERS(*inode
)++;
297 if (filp
->f_mode
& FMODE_WRITE
)
298 PIPE_WRITERS(*inode
)++;
303 * The file_operations structs are not static because they
304 * are also used in linux/fs/fifo.c to do operations on FIFOs.
306 struct file_operations connecting_fifo_fops
= {
310 NULL
, /* no readdir */
313 NULL
, /* no mmap on pipes.. surprise */
320 struct file_operations read_fifo_fops
= {
324 NULL
, /* no readdir */
327 NULL
, /* no mmap on pipes.. surprise */
334 struct file_operations write_fifo_fops
= {
338 NULL
, /* no readdir */
348 struct file_operations rdwr_fifo_fops
= {
352 NULL
, /* no readdir */
362 struct file_operations read_pipe_fops
= {
366 NULL
, /* no readdir */
369 NULL
, /* no mmap on pipes.. surprise */
376 struct file_operations write_pipe_fops
= {
380 NULL
, /* no readdir */
390 struct file_operations rdwr_pipe_fops
= {
394 NULL
, /* no readdir */
404 static struct inode
* get_pipe_inode(void)
406 extern struct inode_operations pipe_inode_operations
;
407 struct inode
*inode
= get_empty_inode();
413 page
= __get_free_page(GFP_USER
);
419 inode
->i_pipe
= kmalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
423 PIPE_BASE(*inode
) = (char *) page
;
424 inode
->i_op
= &pipe_inode_operations
;
425 init_waitqueue_head(&PIPE_WAIT(*inode
));
426 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
427 PIPE_RD_OPENERS(*inode
) = PIPE_WR_OPENERS(*inode
) = 0;
428 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
429 PIPE_LOCK(*inode
) = 0;
431 * Mark the inode dirty from the very beginning,
432 * that way it will never be moved to the dirty
433 * list because "mark_inode_dirty()" will think
434 * that it already _is_ on the dirty list.
436 inode
->i_state
= I_DIRTY
;
437 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
438 inode
->i_uid
= current
->fsuid
;
439 inode
->i_gid
= current
->fsgid
;
440 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
441 inode
->i_blksize
= PAGE_SIZE
;
452 struct inode_operations pipe_inode_operations
= {
464 NULL
, /* get_block */
466 NULL
, /* writepage */
467 NULL
, /* flushpage */
469 NULL
, /* permission */
471 NULL
/* revalidate */
476 struct inode
* inode
;
477 struct file
*f1
, *f2
;
482 f1
= get_empty_filp();
486 f2
= get_empty_filp();
490 inode
= get_pipe_inode();
494 error
= get_unused_fd();
496 goto close_f12_inode
;
499 error
= get_unused_fd();
501 goto close_f12_inode_i
;
505 f1
->f_dentry
= f2
->f_dentry
= dget(d_alloc_root(inode
));
507 goto close_f12_inode_i_j
;
510 f1
->f_pos
= f2
->f_pos
= 0;
511 f1
->f_flags
= O_RDONLY
;
512 f1
->f_op
= &read_pipe_fops
;
516 f2
->f_flags
= O_WRONLY
;
517 f2
->f_op
= &write_pipe_fops
;
531 free_page((unsigned long) PIPE_BASE(*inode
));
532 kfree(inode
->i_pipe
);
533 inode
->i_pipe
= NULL
;