Rest of the merge.
[linux-2.6/linux-mips.git] / fs / pipe.c
blob6f8452534f8cdce52959aebb7e50ec41235d12f6
1 /*
2 * linux/fs/pipe.c
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
7 #include <linux/mm.h>
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.
19 #ifdef __sparc__
20 #define FIFO_SUNOS_BRAINDAMAGE
21 #else
22 #undef FIFO_SUNOS_BRAINDAMAGE
23 #endif
26 * We use a start+len construction, which provides full use of the
27 * allocated memory.
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);
40 up(PIPE_SEM(*inode));
41 schedule();
42 remove_wait_queue(PIPE_WAIT(*inode), &wait);
43 current->state = TASK_RUNNING;
46 static ssize_t
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. */
53 ret = -ESPIPE;
54 read = 0;
55 if (ppos != &filp->f_pos)
56 goto out_nolock;
58 /* Always return 0 on null read. */
59 ret = 0;
60 if (count == 0)
61 goto out_nolock;
63 /* Get the pipe semaphore */
64 ret = -ERESTARTSYS;
65 if (down_interruptible(PIPE_SEM(*inode)))
66 goto out_nolock;
68 if (PIPE_EMPTY(*inode)) {
69 do_more_read:
70 ret = 0;
71 if (!PIPE_WRITERS(*inode))
72 goto out;
74 ret = -EAGAIN;
75 if (filp->f_flags & O_NONBLOCK)
76 goto out;
78 for (;;) {
79 PIPE_WAITING_READERS(*inode)++;
80 pipe_wait(inode);
81 PIPE_WAITING_READERS(*inode)--;
82 ret = -ERESTARTSYS;
83 if (signal_pending(current))
84 goto out_nolock;
85 if (down_interruptible(PIPE_SEM(*inode)))
86 goto out_nolock;
87 ret = 0;
88 if (!PIPE_EMPTY(*inode))
89 break;
90 if (!PIPE_WRITERS(*inode))
91 goto out;
95 /* Read what data is available. */
96 ret = -EFAULT;
97 while (count > 0 && (size = PIPE_LEN(*inode))) {
98 char *pipebuf = PIPE_BASE(*inode) + PIPE_START(*inode);
99 ssize_t chars = PIPE_MAX_RCHUNK(*inode);
101 if (chars > count)
102 chars = count;
103 if (chars > size)
104 chars = size;
106 if (copy_to_user(buf, pipebuf, chars))
107 goto out;
109 read += chars;
110 PIPE_START(*inode) += chars;
111 PIPE_START(*inode) &= (PIPE_SIZE - 1);
112 PIPE_LEN(*inode) -= chars;
113 count -= chars;
114 buf += chars;
117 /* Cache behaviour optimization */
118 if (!PIPE_LEN(*inode))
119 PIPE_START(*inode) = 0;
121 if (count && PIPE_WAITING_WRITERS(*inode) && !(filp->f_flags & O_NONBLOCK)) {
123 * We know that we are going to sleep: signal
124 * writers synchronously that there is more
125 * room.
127 wake_up_interruptible_sync(PIPE_WAIT(*inode));
128 if (!PIPE_EMPTY(*inode))
129 BUG();
130 goto do_more_read;
132 /* Signal writers asynchronously that there is more room. */
133 wake_up_interruptible(PIPE_WAIT(*inode));
135 ret = read;
136 out:
137 up(PIPE_SEM(*inode));
138 out_nolock:
139 if (read)
140 ret = read;
141 return ret;
144 static ssize_t
145 pipe_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
147 struct inode *inode = filp->f_dentry->d_inode;
148 ssize_t free, written, ret;
150 /* Seeks are not allowed on pipes. */
151 ret = -ESPIPE;
152 written = 0;
153 if (ppos != &filp->f_pos)
154 goto out_nolock;
156 /* Null write succeeds. */
157 ret = 0;
158 if (count == 0)
159 goto out_nolock;
161 ret = -ERESTARTSYS;
162 if (down_interruptible(PIPE_SEM(*inode)))
163 goto out_nolock;
165 do_more_write:
166 /* No readers yields SIGPIPE. */
167 if (!PIPE_READERS(*inode))
168 goto sigpipe;
170 /* If count <= PIPE_BUF, we have to make it atomic. */
171 free = (count <= PIPE_BUF ? count : 1);
173 /* Wait, or check for, available space. */
174 if (filp->f_flags & O_NONBLOCK) {
175 ret = -EAGAIN;
176 if (PIPE_FREE(*inode) < free)
177 goto out;
178 } else {
179 while (PIPE_FREE(*inode) < free) {
180 PIPE_WAITING_WRITERS(*inode)++;
181 pipe_wait(inode);
182 PIPE_WAITING_WRITERS(*inode)--;
183 ret = -ERESTARTSYS;
184 if (signal_pending(current))
185 goto out_nolock;
186 if (down_interruptible(PIPE_SEM(*inode)))
187 goto out_nolock;
189 if (!PIPE_READERS(*inode))
190 goto sigpipe;
194 /* Copy into available space. */
195 ret = -EFAULT;
196 while (count > 0) {
197 int space;
198 char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode);
199 ssize_t chars = PIPE_MAX_WCHUNK(*inode);
201 if ((space = PIPE_FREE(*inode)) != 0) {
202 if (chars > count)
203 chars = count;
204 if (chars > space)
205 chars = space;
207 if (copy_from_user(pipebuf, buf, chars))
208 goto out;
210 written += chars;
211 PIPE_LEN(*inode) += chars;
212 count -= chars;
213 buf += chars;
214 space = PIPE_FREE(*inode);
215 continue;
218 ret = written;
219 if (filp->f_flags & O_NONBLOCK)
220 break;
222 do {
224 * Synchronous wake-up: it knows that this process
225 * is going to give up this CPU, so it doesnt have
226 * to do idle reschedules.
228 wake_up_interruptible_sync(PIPE_WAIT(*inode));
229 PIPE_WAITING_WRITERS(*inode)++;
230 pipe_wait(inode);
231 PIPE_WAITING_WRITERS(*inode)--;
232 if (signal_pending(current))
233 goto out_nolock;
234 if (down_interruptible(PIPE_SEM(*inode)))
235 goto out_nolock;
236 if (!PIPE_READERS(*inode))
237 goto sigpipe;
238 } while (!PIPE_FREE(*inode));
239 ret = -EFAULT;
242 if (count && PIPE_WAITING_READERS(*inode) &&
243 !(filp->f_flags & O_NONBLOCK)) {
244 wake_up_interruptible_sync(PIPE_WAIT(*inode));
245 goto do_more_write;
247 /* Signal readers asynchronously that there is more data. */
248 wake_up_interruptible(PIPE_WAIT(*inode));
250 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
251 mark_inode_dirty(inode);
253 out:
254 up(PIPE_SEM(*inode));
255 out_nolock:
256 if (written)
257 ret = written;
258 return ret;
260 sigpipe:
261 if (written)
262 goto out;
263 up(PIPE_SEM(*inode));
264 send_sig(SIGPIPE, current, 0);
265 return -EPIPE;
268 static loff_t
269 pipe_lseek(struct file *file, loff_t offset, int orig)
271 return -ESPIPE;
274 static ssize_t
275 bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
277 return -EBADF;
280 static ssize_t
281 bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
283 return -EBADF;
286 static int
287 pipe_ioctl(struct inode *pino, struct file *filp,
288 unsigned int cmd, unsigned long arg)
290 switch (cmd) {
291 case FIONREAD:
292 return put_user(PIPE_LEN(*pino), (int *)arg);
293 default:
294 return -EINVAL;
298 static unsigned int
299 pipe_poll(struct file *filp, poll_table *wait)
301 unsigned int mask;
302 struct inode *inode = filp->f_dentry->d_inode;
304 poll_wait(filp, PIPE_WAIT(*inode), wait);
306 /* Reading only -- no need for aquiring the semaphore. */
307 mask = POLLIN | POLLRDNORM;
308 if (PIPE_EMPTY(*inode))
309 mask = POLLOUT | POLLWRNORM;
310 if (!PIPE_WRITERS(*inode))
311 mask |= POLLHUP;
312 if (!PIPE_READERS(*inode))
313 mask |= POLLERR;
315 return mask;
318 #ifdef FIFO_SUNOS_BRAINDAMAGE
320 * Argh! Why does SunOS have to have different select() behaviour
321 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
323 static unsigned int
324 fifo_poll(struct file *filp, poll_table *wait)
326 unsigned int mask;
327 struct inode *inode = filp->f_dentry->d_inode;
329 poll_wait(filp, PIPE_WAIT(*inode), wait);
331 /* Reading only -- no need for aquiring the semaphore. */
332 mask = POLLIN | POLLRDNORM;
333 if (PIPE_EMPTY(*inode))
334 mask = POLLOUT | POLLWRNORM;
335 if (!PIPE_READERS(*inode))
336 mask |= POLLERR;
338 return mask;
340 #else
342 #define fifo_poll pipe_poll
344 #endif /* FIFO_SUNOS_BRAINDAMAGE */
347 * The 'connect_xxx()' functions are needed for named pipes when
348 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
349 * and we need to act differently until we do get a writer..
351 static ssize_t
352 connect_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
354 struct inode *inode = filp->f_dentry->d_inode;
356 /* Reading only -- no need for aquiring the semaphore. */
357 if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode))
358 return 0;
360 filp->f_op = &read_fifo_fops;
361 return pipe_read(filp, buf, count, ppos);
364 static unsigned int
365 connect_poll(struct file *filp, poll_table *wait)
367 struct inode *inode = filp->f_dentry->d_inode;
368 unsigned int mask = 0;
370 poll_wait(filp, PIPE_WAIT(*inode), wait);
372 /* Reading only -- no need for aquiring the semaphore. */
373 if (!PIPE_EMPTY(*inode)) {
374 filp->f_op = &read_fifo_fops;
375 mask = POLLIN | POLLRDNORM;
376 } else if (PIPE_WRITERS(*inode)) {
377 filp->f_op = &read_fifo_fops;
378 mask = POLLOUT | POLLWRNORM;
381 return mask;
384 static int
385 pipe_release(struct inode *inode, int decr, int decw)
387 down(PIPE_SEM(*inode));
388 PIPE_READERS(*inode) -= decr;
389 PIPE_WRITERS(*inode) -= decw;
390 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
391 struct pipe_inode_info *info = inode->i_pipe;
392 inode->i_pipe = NULL;
393 free_page((unsigned long) info->base);
394 kfree(info);
395 } else {
396 wake_up_interruptible(PIPE_WAIT(*inode));
398 up(PIPE_SEM(*inode));
400 return 0;
403 static int
404 pipe_read_release(struct inode *inode, struct file *filp)
406 return pipe_release(inode, 1, 0);
409 static int
410 pipe_write_release(struct inode *inode, struct file *filp)
412 return pipe_release(inode, 0, 1);
415 static int
416 pipe_rdwr_release(struct inode *inode, struct file *filp)
418 int decr, decw;
420 decr = (filp->f_mode & FMODE_READ) != 0;
421 decw = (filp->f_mode & FMODE_WRITE) != 0;
422 return pipe_release(inode, decr, decw);
425 static int
426 pipe_read_open(struct inode *inode, struct file *filp)
428 /* We could have perhaps used atomic_t, but this and friends
429 below are the only places. So it doesn't seem worthwhile. */
430 down(PIPE_SEM(*inode));
431 PIPE_READERS(*inode)++;
432 up(PIPE_SEM(*inode));
434 return 0;
437 static int
438 pipe_write_open(struct inode *inode, struct file *filp)
440 down(PIPE_SEM(*inode));
441 PIPE_WRITERS(*inode)++;
442 up(PIPE_SEM(*inode));
444 return 0;
447 static int
448 pipe_rdwr_open(struct inode *inode, struct file *filp)
450 down(PIPE_SEM(*inode));
451 if (filp->f_mode & FMODE_READ)
452 PIPE_READERS(*inode)++;
453 if (filp->f_mode & FMODE_WRITE)
454 PIPE_WRITERS(*inode)++;
455 up(PIPE_SEM(*inode));
457 return 0;
461 * The file_operations structs are not static because they
462 * are also used in linux/fs/fifo.c to do operations on FIFOs.
464 struct file_operations connecting_fifo_fops = {
465 llseek: pipe_lseek,
466 read: connect_read,
467 write: bad_pipe_w,
468 poll: connect_poll,
469 ioctl: pipe_ioctl,
470 open: pipe_read_open,
471 release: pipe_read_release,
474 struct file_operations read_fifo_fops = {
475 llseek: pipe_lseek,
476 read: pipe_read,
477 write: bad_pipe_w,
478 poll: fifo_poll,
479 ioctl: pipe_ioctl,
480 open: pipe_read_open,
481 release: pipe_read_release,
484 struct file_operations write_fifo_fops = {
485 llseek: pipe_lseek,
486 read: bad_pipe_r,
487 write: pipe_write,
488 poll: fifo_poll,
489 ioctl: pipe_ioctl,
490 open: pipe_write_open,
491 release: pipe_write_release,
494 struct file_operations rdwr_fifo_fops = {
495 llseek: pipe_lseek,
496 read: pipe_read,
497 write: pipe_write,
498 poll: fifo_poll,
499 ioctl: pipe_ioctl,
500 open: pipe_rdwr_open,
501 release: pipe_rdwr_release,
504 struct file_operations read_pipe_fops = {
505 llseek: pipe_lseek,
506 read: pipe_read,
507 write: bad_pipe_w,
508 poll: pipe_poll,
509 ioctl: pipe_ioctl,
510 open: pipe_read_open,
511 release: pipe_read_release,
514 struct file_operations write_pipe_fops = {
515 llseek: pipe_lseek,
516 read: bad_pipe_r,
517 write: pipe_write,
518 poll: pipe_poll,
519 ioctl: pipe_ioctl,
520 open: pipe_write_open,
521 release: pipe_write_release,
524 struct file_operations rdwr_pipe_fops = {
525 llseek: pipe_lseek,
526 read: pipe_read,
527 write: pipe_write,
528 poll: pipe_poll,
529 ioctl: pipe_ioctl,
530 open: pipe_rdwr_open,
531 release: pipe_rdwr_release,
534 static struct inode * get_pipe_inode(void)
536 struct inode *inode = get_empty_inode();
537 unsigned long page;
539 if (!inode)
540 goto fail_inode;
542 page = __get_free_page(GFP_USER);
543 if (!page)
544 goto fail_iput;
546 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
547 if (!inode->i_pipe)
548 goto fail_page;
550 inode->i_fop = &rdwr_pipe_fops;
552 init_waitqueue_head(PIPE_WAIT(*inode));
553 PIPE_BASE(*inode) = (char *) page;
554 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
555 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
556 PIPE_WAITING_READERS(*inode) = PIPE_WAITING_WRITERS(*inode) = 0;
559 * Mark the inode dirty from the very beginning,
560 * that way it will never be moved to the dirty
561 * list because "mark_inode_dirty()" will think
562 * that it already _is_ on the dirty list.
564 inode->i_state = I_DIRTY;
565 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
566 inode->i_uid = current->fsuid;
567 inode->i_gid = current->fsgid;
568 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
569 inode->i_blksize = PAGE_SIZE;
570 return inode;
572 fail_page:
573 free_page(page);
574 fail_iput:
575 iput(inode);
576 fail_inode:
577 return NULL;
580 int do_pipe(int *fd)
582 struct inode * inode;
583 struct file *f1, *f2;
584 int error;
585 int i,j;
587 error = -ENFILE;
588 f1 = get_empty_filp();
589 if (!f1)
590 goto no_files;
592 f2 = get_empty_filp();
593 if (!f2)
594 goto close_f1;
596 inode = get_pipe_inode();
597 if (!inode)
598 goto close_f12;
600 error = get_unused_fd();
601 if (error < 0)
602 goto close_f12_inode;
603 i = error;
605 error = get_unused_fd();
606 if (error < 0)
607 goto close_f12_inode_i;
608 j = error;
610 error = -ENOMEM;
611 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode));
612 if (!f1->f_dentry)
613 goto close_f12_inode_i_j;
615 /* read file */
616 f1->f_pos = f2->f_pos = 0;
617 f1->f_flags = O_RDONLY;
618 f1->f_op = &read_pipe_fops;
619 f1->f_mode = 1;
621 /* write file */
622 f2->f_flags = O_WRONLY;
623 f2->f_op = &write_pipe_fops;
624 f2->f_mode = 2;
626 fd_install(i, f1);
627 fd_install(j, f2);
628 fd[0] = i;
629 fd[1] = j;
630 return 0;
632 close_f12_inode_i_j:
633 put_unused_fd(j);
634 close_f12_inode_i:
635 put_unused_fd(i);
636 close_f12_inode:
637 free_page((unsigned long) PIPE_BASE(*inode));
638 kfree(inode->i_pipe);
639 inode->i_pipe = NULL;
640 iput(inode);
641 close_f12:
642 put_filp(f2);
643 close_f1:
644 put_filp(f1);
645 no_files:
646 return error;