4 * Copyright (C) 1991, 1992 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
25 /* We don't use the head/tail construction any more. Now we use the start/len*/
26 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
27 /* Florian Coosmann (FGC) ^ current = 1 */
28 /* Additionally, we now use locking technique. This prevents race condition */
29 /* in case of paging and multiple read/write on the same pipe. (FGC) */
30 /* Reads with count = 0 should always return 0. Julian Bradfield 1999-06-07. */
32 static ssize_t
do_pipe_read(struct file
* filp
, char * buf
, size_t count
)
34 struct inode
* inode
= filp
->f_dentry
->d_inode
;
35 ssize_t chars
= 0, size
= 0, read
= 0;
38 if (filp
->f_flags
& O_NONBLOCK
) {
39 if (PIPE_LOCK(*inode
))
41 if (PIPE_EMPTY(*inode
)) {
42 if (PIPE_WRITERS(*inode
))
47 } else while (PIPE_EMPTY(*inode
) || PIPE_LOCK(*inode
)) {
48 if (PIPE_EMPTY(*inode
)) {
49 if (!PIPE_WRITERS(*inode
) || !count
)
52 if (signal_pending(current
))
54 interruptible_sleep_on(&PIPE_WAIT(*inode
));
57 while (count
>0 && (size
= PIPE_SIZE(*inode
))) {
58 chars
= PIPE_MAX_RCHUNK(*inode
);
64 pipebuf
= PIPE_BASE(*inode
)+PIPE_START(*inode
);
65 PIPE_START(*inode
) += chars
;
66 PIPE_START(*inode
) &= (PIPE_BUF
-1);
67 PIPE_LEN(*inode
) -= chars
;
69 copy_to_user(buf
, pipebuf
, chars
);
73 wake_up_interruptible(&PIPE_WAIT(*inode
));
78 if (PIPE_WRITERS(*inode
))
83 static ssize_t
do_pipe_write(struct file
* filp
, const char * buf
, size_t count
)
85 struct inode
* inode
= filp
->f_dentry
->d_inode
;
86 ssize_t chars
= 0, free
= 0, written
= 0, err
=0;
89 if (!PIPE_READERS(*inode
)) { /* no readers */
90 send_sig(SIGPIPE
,current
,0);
93 /* if count <= PIPE_BUF, we have to make it atomic */
94 if (count
<= PIPE_BUF
)
97 free
= 1; /* can't do it atomically, wait for any free space */
98 if (down_interruptible(&inode
->i_sem
)) {
102 while ((PIPE_FREE(*inode
) < free
) || PIPE_LOCK(*inode
)) {
103 if (!PIPE_READERS(*inode
)) { /* no readers */
104 send_sig(SIGPIPE
,current
,0);
108 if (signal_pending(current
)) {
112 if (filp
->f_flags
& O_NONBLOCK
) {
116 interruptible_sleep_on(&PIPE_WAIT(*inode
));
119 while (count
>0 && (free
= PIPE_FREE(*inode
))) {
120 chars
= PIPE_MAX_WCHUNK(*inode
);
125 pipebuf
= PIPE_BASE(*inode
)+PIPE_END(*inode
);
127 PIPE_LEN(*inode
) += chars
;
129 copy_from_user(pipebuf
, buf
, chars
);
133 wake_up_interruptible(&PIPE_WAIT(*inode
));
136 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
137 mark_inode_dirty(inode
);
140 return written
? written
: err
;
143 static ssize_t
pipe_read(struct file
* filp
, char * buf
, size_t count
, loff_t
*ppos
)
148 if (ppos
!= &filp
->f_pos
)
151 if ( !count
) return 0;
154 retval
= do_pipe_read(filp
, buf
, count
);
159 static ssize_t
pipe_write(struct file
* filp
, const char * buf
, size_t count
, loff_t
*ppos
)
163 if (ppos
!= &filp
->f_pos
)
167 retval
= do_pipe_write(filp
, buf
, count
);
172 static long long pipe_lseek(struct file
* file
, long long offset
, int orig
)
177 static ssize_t
bad_pipe_r(struct file
* filp
, char * buf
,
178 size_t count
, loff_t
*ppos
)
183 static ssize_t
bad_pipe_w(struct file
* filp
, const char * buf
,
184 size_t count
, loff_t
*ppos
)
189 static int pipe_ioctl(struct inode
*pino
, struct file
* filp
,
190 unsigned int cmd
, unsigned long arg
)
194 return put_user(PIPE_SIZE(*pino
),(int *) arg
);
200 static unsigned int pipe_poll(struct file
* filp
, poll_table
* wait
)
203 struct inode
* inode
= filp
->f_dentry
->d_inode
;
205 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
206 mask
= POLLIN
| POLLRDNORM
;
207 if (PIPE_EMPTY(*inode
))
208 mask
= POLLOUT
| POLLWRNORM
;
209 if (!PIPE_WRITERS(*inode
))
211 if (!PIPE_READERS(*inode
))
216 #ifdef FIFO_SUNOS_BRAINDAMAGE
218 * Argh! Why does SunOS have to have different select() behaviour
219 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
221 static unsigned int fifo_poll(struct file
* filp
, poll_table
* wait
)
224 struct inode
* inode
= filp
->f_dentry
->d_inode
;
226 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
227 mask
= POLLIN
| POLLRDNORM
;
228 if (PIPE_EMPTY(*inode
))
229 mask
= POLLOUT
| POLLWRNORM
;
230 if (!PIPE_READERS(*inode
))
236 #define fifo_poll pipe_poll
238 #endif /* FIFO_SUNOS_BRAINDAMAGE */
241 * The 'connect_xxx()' functions are needed for named pipes when
242 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
243 * and we need to act differently until we do get a writer..
245 static ssize_t
connect_read(struct file
* filp
, char * buf
,
246 size_t count
, loff_t
*ppos
)
248 struct inode
* inode
= filp
->f_dentry
->d_inode
;
249 if (PIPE_EMPTY(*inode
) && !PIPE_WRITERS(*inode
))
251 filp
->f_op
= &read_fifo_fops
;
252 return pipe_read(filp
,buf
,count
,ppos
);
255 static unsigned int connect_poll(struct file
* filp
, poll_table
* wait
)
257 struct inode
* inode
= filp
->f_dentry
->d_inode
;
259 poll_wait(filp
, &PIPE_WAIT(*inode
), wait
);
260 if (!PIPE_EMPTY(*inode
)) {
261 filp
->f_op
= &read_fifo_fops
;
262 return POLLIN
| POLLRDNORM
;
264 if (PIPE_WRITERS(*inode
))
265 filp
->f_op
= &read_fifo_fops
;
266 return POLLOUT
| POLLWRNORM
;
269 static int pipe_release(struct inode
* inode
)
271 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
272 struct pipe_inode_info
*info
= inode
->i_pipe
;
273 inode
->i_pipe
= NULL
;
274 free_page((unsigned long) info
->base
);
278 wake_up_interruptible(&PIPE_WAIT(*inode
));
282 static int pipe_read_release(struct inode
* inode
, struct file
* filp
)
284 PIPE_READERS(*inode
)--;
285 return pipe_release(inode
);
288 static int pipe_write_release(struct inode
* inode
, struct file
* filp
)
290 PIPE_WRITERS(*inode
)--;
291 return pipe_release(inode
);
294 static int pipe_rdwr_release(struct inode
* inode
, struct file
* filp
)
296 if (filp
->f_mode
& FMODE_READ
)
297 PIPE_READERS(*inode
)--;
298 if (filp
->f_mode
& FMODE_WRITE
)
299 PIPE_WRITERS(*inode
)--;
300 return pipe_release(inode
);
303 static int pipe_read_open(struct inode
* inode
, struct file
* filp
)
305 PIPE_READERS(*inode
)++;
309 static int pipe_write_open(struct inode
* inode
, struct file
* filp
)
311 PIPE_WRITERS(*inode
)++;
315 static int pipe_rdwr_open(struct inode
* inode
, struct file
* filp
)
317 if (filp
->f_mode
& FMODE_READ
)
318 PIPE_READERS(*inode
)++;
319 if (filp
->f_mode
& FMODE_WRITE
)
320 PIPE_WRITERS(*inode
)++;
325 * The file_operations structs are not static because they
326 * are also used in linux/fs/fifo.c to do operations on FIFOs.
328 struct file_operations connecting_fifo_fops
= {
332 NULL
, /* no readdir */
335 NULL
, /* no mmap on pipes.. surprise */
342 struct file_operations read_fifo_fops
= {
346 NULL
, /* no readdir */
349 NULL
, /* no mmap on pipes.. surprise */
356 struct file_operations write_fifo_fops
= {
360 NULL
, /* no readdir */
370 struct file_operations rdwr_fifo_fops
= {
374 NULL
, /* no readdir */
384 struct file_operations read_pipe_fops
= {
388 NULL
, /* no readdir */
391 NULL
, /* no mmap on pipes.. surprise */
398 struct file_operations write_pipe_fops
= {
402 NULL
, /* no readdir */
412 struct file_operations rdwr_pipe_fops
= {
416 NULL
, /* no readdir */
426 static struct inode
* get_pipe_inode(void)
428 extern struct inode_operations pipe_inode_operations
;
429 struct inode
*inode
= get_empty_inode();
435 page
= __get_free_page(GFP_USER
);
441 inode
->i_pipe
= kmalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
445 PIPE_BASE(*inode
) = (char *) page
;
446 inode
->i_op
= &pipe_inode_operations
;
447 init_waitqueue_head(&PIPE_WAIT(*inode
));
448 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
449 PIPE_RD_OPENERS(*inode
) = PIPE_WR_OPENERS(*inode
) = 0;
450 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
451 PIPE_LOCK(*inode
) = 0;
453 * Mark the inode dirty from the very beginning,
454 * that way it will never be moved to the dirty
455 * list because "mark_inode_dirty()" will think
456 * that it already _is_ on the dirty list.
458 inode
->i_state
= I_DIRTY
;
459 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
460 inode
->i_uid
= current
->fsuid
;
461 inode
->i_gid
= current
->fsgid
;
462 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
463 inode
->i_blksize
= PAGE_SIZE
;
474 struct inode_operations pipe_inode_operations
= {
486 NULL
, /* follow_link */
487 NULL
, /* get_block */
489 NULL
, /* writepage */
490 NULL
, /* flushpage */
492 NULL
, /* permission */
494 NULL
/* revalidate */
499 struct inode
* inode
;
500 struct file
*f1
, *f2
;
505 f1
= get_empty_filp();
509 f2
= get_empty_filp();
513 inode
= get_pipe_inode();
517 error
= get_unused_fd();
519 goto close_f12_inode
;
522 error
= get_unused_fd();
524 goto close_f12_inode_i
;
528 f1
->f_dentry
= f2
->f_dentry
= dget(d_alloc_root(inode
));
530 goto close_f12_inode_i_j
;
533 f1
->f_pos
= f2
->f_pos
= 0;
534 f1
->f_flags
= O_RDONLY
;
535 f1
->f_op
= &read_pipe_fops
;
539 f2
->f_flags
= O_WRONLY
;
540 f2
->f_op
= &write_pipe_fops
;
554 free_page((unsigned long) PIPE_BASE(*inode
));
555 kfree(inode
->i_pipe
);
556 inode
->i_pipe
= NULL
;