Import 2.3.18pre1
[davej-history.git] / fs / pipe.c
blob7cdd2b3946b0cb7bd3a14310b2e956520e31906a
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 if (ppos != &filp->f_pos)
55 goto out_nolock;
57 /* Always return 0 on null read. */
58 ret = 0;
59 if (count == 0)
60 goto out_nolock;
62 /* Get the pipe semaphore */
63 ret = -ERESTARTSYS;
64 if (down_interruptible(PIPE_SEM(*inode)))
65 goto out_nolock;
67 if (PIPE_EMPTY(*inode)) {
68 ret = 0;
69 if (!PIPE_WRITERS(*inode))
70 goto out;
72 ret = -EAGAIN;
73 if (filp->f_flags & O_NONBLOCK)
74 goto out;
76 for (;;) {
77 pipe_wait(inode);
78 ret = -ERESTARTSYS;
79 if (signal_pending(current))
80 goto out_nolock;
81 if (down_interruptible(PIPE_SEM(*inode)))
82 goto out_nolock;
83 ret = 0;
84 if (!PIPE_EMPTY(*inode))
85 break;
86 if (!PIPE_WRITERS(*inode))
87 goto out;
91 /* Read what data is available. */
92 ret = -EFAULT;
93 read = 0;
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);
98 if (chars > count)
99 chars = count;
100 if (chars > size)
101 chars = size;
103 if (copy_to_user(buf, pipebuf, chars))
104 goto out;
106 read += chars;
107 PIPE_START(*inode) += chars;
108 PIPE_START(*inode) &= (PIPE_SIZE - 1);
109 PIPE_LEN(*inode) -= chars;
110 count -= chars;
111 buf += 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));
121 if (read)
122 UPDATE_ATIME(inode);
123 ret = read;
125 out:
126 up(PIPE_SEM(*inode));
127 out_nolock:
128 return ret;
131 static ssize_t
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. */
138 ret = -ESPIPE;
139 if (ppos != &filp->f_pos)
140 goto out_nolock;
142 /* Null write succeeds. */
143 ret = 0;
144 if (count == 0)
145 goto out_nolock;
147 ret = -ERESTARTSYS;
148 if (down_interruptible(PIPE_SEM(*inode)))
149 goto out_nolock;
151 /* No readers yields SIGPIPE. */
152 if (!PIPE_READERS(*inode))
153 goto sigpipe;
155 /* If count <= PIPE_BUF, we have to make it atomic. */
156 free = (count <= PIPE_BUF ? count : 1);
157 written = 0;
159 /* Wait, or check for, available space. */
160 if (filp->f_flags & O_NONBLOCK) {
161 ret = -EAGAIN;
162 if (PIPE_FREE(*inode) < free)
163 goto out;
164 } else {
165 while (PIPE_FREE(*inode) < free) {
166 pipe_wait(inode);
167 ret = -ERESTARTSYS;
168 if (signal_pending(current))
169 goto out_nolock;
170 if (down_interruptible(PIPE_SEM(*inode)))
171 goto out_nolock;
173 if (!PIPE_READERS(*inode))
174 goto sigpipe;
178 /* Copy into available space. */
179 ret = -EFAULT;
180 while (count > 0) {
181 int 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) {
186 if (chars > count)
187 chars = count;
188 if (chars > space)
189 chars = space;
191 if (copy_from_user(pipebuf, buf, chars))
192 goto out;
194 written += chars;
195 PIPE_LEN(*inode) += chars;
196 count -= chars;
197 buf += chars;
198 space = PIPE_FREE(*inode);
199 continue;
202 ret = written;
203 if (filp->f_flags & O_NONBLOCK)
204 break;
206 do {
207 /* This should be a synchronous wake-up: don't do idle reschedules! */
208 wake_up_interruptible(PIPE_WAIT(*inode));
209 pipe_wait(inode);
210 if (signal_pending(current))
211 goto out_nolock;
212 if (down_interruptible(PIPE_SEM(*inode)))
213 goto out_nolock;
214 if (!PIPE_READERS(*inode))
215 goto sigpipe;
216 } while (!PIPE_FREE(*inode));
217 ret = -EFAULT;
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);
227 out:
228 up(PIPE_SEM(*inode));
229 out_nolock:
230 return ret;
232 sigpipe:
233 up(PIPE_SEM(*inode));
234 send_sig(SIGPIPE, current, 0);
235 return -EPIPE;
238 static loff_t
239 pipe_lseek(struct file *file, loff_t offset, int orig)
241 return -ESPIPE;
244 static ssize_t
245 bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
247 return -EBADF;
250 static ssize_t
251 bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
253 return -EBADF;
256 static int
257 pipe_ioctl(struct inode *pino, struct file *filp,
258 unsigned int cmd, unsigned long arg)
260 switch (cmd) {
261 case FIONREAD:
262 return put_user(PIPE_LEN(*pino), (int *)arg);
263 default:
264 return -EINVAL;
268 static unsigned int
269 pipe_poll(struct file *filp, poll_table *wait)
271 unsigned int mask;
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))
281 mask |= POLLHUP;
282 if (!PIPE_READERS(*inode))
283 mask |= POLLERR;
285 return mask;
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.
293 static unsigned int
294 fifo_poll(struct file *filp, poll_table *wait)
296 unsigned int mask;
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))
306 mask |= POLLERR;
308 return mask;
310 #else
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..
321 static ssize_t
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))
328 return 0;
330 filp->f_op = &read_fifo_fops;
331 return pipe_read(filp, buf, count, ppos);
334 static unsigned int
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;
351 return mask;
354 static int
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);
364 kfree(info);
365 } else {
366 wake_up_interruptible(PIPE_WAIT(*inode));
368 up(PIPE_SEM(*inode));
370 return 0;
373 static int
374 pipe_read_release(struct inode *inode, struct file *filp)
376 return pipe_release(inode, 1, 0);
379 static int
380 pipe_write_release(struct inode *inode, struct file *filp)
382 return pipe_release(inode, 0, 1);
385 static int
386 pipe_rdwr_release(struct inode *inode, struct file *filp)
388 int decr, decw;
390 decr = (filp->f_mode & FMODE_READ) != 0;
391 decw = (filp->f_mode & FMODE_WRITE) != 0;
392 return pipe_release(inode, decr, decw);
395 static int
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));
404 return 0;
407 static int
408 pipe_write_open(struct inode *inode, struct file *filp)
410 down(PIPE_SEM(*inode));
411 PIPE_WRITERS(*inode)++;
412 up(PIPE_SEM(*inode));
414 return 0;
417 static int
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));
427 return 0;
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 = {
435 pipe_lseek,
436 connect_read,
437 bad_pipe_w,
438 NULL, /* no readdir */
439 connect_poll,
440 pipe_ioctl,
441 NULL, /* no mmap on pipes.. surprise */
442 pipe_read_open,
443 NULL, /* flush */
444 pipe_read_release,
445 NULL
448 struct file_operations read_fifo_fops = {
449 pipe_lseek,
450 pipe_read,
451 bad_pipe_w,
452 NULL, /* no readdir */
453 fifo_poll,
454 pipe_ioctl,
455 NULL, /* no mmap on pipes.. surprise */
456 pipe_read_open,
457 NULL, /* flush */
458 pipe_read_release,
459 NULL
462 struct file_operations write_fifo_fops = {
463 pipe_lseek,
464 bad_pipe_r,
465 pipe_write,
466 NULL, /* no readdir */
467 fifo_poll,
468 pipe_ioctl,
469 NULL, /* mmap */
470 pipe_write_open,
471 NULL, /* flush */
472 pipe_write_release,
473 NULL
476 struct file_operations rdwr_fifo_fops = {
477 pipe_lseek,
478 pipe_read,
479 pipe_write,
480 NULL, /* no readdir */
481 fifo_poll,
482 pipe_ioctl,
483 NULL, /* mmap */
484 pipe_rdwr_open,
485 NULL, /* flush */
486 pipe_rdwr_release,
487 NULL
490 struct file_operations read_pipe_fops = {
491 pipe_lseek,
492 pipe_read,
493 bad_pipe_w,
494 NULL, /* no readdir */
495 pipe_poll,
496 pipe_ioctl,
497 NULL, /* no mmap on pipes.. surprise */
498 pipe_read_open,
499 NULL, /* flush */
500 pipe_read_release,
501 NULL
504 struct file_operations write_pipe_fops = {
505 pipe_lseek,
506 bad_pipe_r,
507 pipe_write,
508 NULL, /* no readdir */
509 pipe_poll,
510 pipe_ioctl,
511 NULL, /* mmap */
512 pipe_write_open,
513 NULL, /* flush */
514 pipe_write_release,
515 NULL
518 struct file_operations rdwr_pipe_fops = {
519 pipe_lseek,
520 pipe_read,
521 pipe_write,
522 NULL, /* no readdir */
523 pipe_poll,
524 pipe_ioctl,
525 NULL, /* mmap */
526 pipe_rdwr_open,
527 NULL, /* flush */
528 pipe_rdwr_release,
529 NULL
532 static struct inode * get_pipe_inode(void)
534 extern struct inode_operations pipe_inode_operations;
535 struct inode *inode = get_empty_inode();
536 unsigned long page;
538 if (!inode)
539 goto fail_inode;
541 page = __get_free_page(GFP_USER);
542 if (!page)
543 goto fail_iput;
545 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
546 if (!inode->i_pipe)
547 goto fail_page;
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;
568 return inode;
570 fail_page:
571 free_page(page);
572 fail_iput:
573 iput(inode);
574 fail_inode:
575 return NULL;
578 struct inode_operations pipe_inode_operations = {
579 &rdwr_pipe_fops,
580 NULL, /* create */
581 NULL, /* lookup */
582 NULL, /* link */
583 NULL, /* unlink */
584 NULL, /* symlink */
585 NULL, /* mkdir */
586 NULL, /* rmdir */
587 NULL, /* mknod */
588 NULL, /* rename */
589 NULL, /* readlink */
590 NULL, /* follow_link */
591 NULL, /* get_block */
592 NULL, /* readpage */
593 NULL, /* writepage */
594 NULL, /* flushpage */
595 NULL, /* truncate */
596 NULL, /* permission */
597 NULL, /* smap */
598 NULL /* revalidate */
601 int do_pipe(int *fd)
603 struct inode * inode;
604 struct file *f1, *f2;
605 int error;
606 int i,j;
608 error = -ENFILE;
609 f1 = get_empty_filp();
610 if (!f1)
611 goto no_files;
613 f2 = get_empty_filp();
614 if (!f2)
615 goto close_f1;
617 inode = get_pipe_inode();
618 if (!inode)
619 goto close_f12;
621 error = get_unused_fd();
622 if (error < 0)
623 goto close_f12_inode;
624 i = error;
626 error = get_unused_fd();
627 if (error < 0)
628 goto close_f12_inode_i;
629 j = error;
631 error = -ENOMEM;
632 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode));
633 if (!f1->f_dentry)
634 goto close_f12_inode_i_j;
636 /* read file */
637 f1->f_pos = f2->f_pos = 0;
638 f1->f_flags = O_RDONLY;
639 f1->f_op = &read_pipe_fops;
640 f1->f_mode = 1;
642 /* write file */
643 f2->f_flags = O_WRONLY;
644 f2->f_op = &write_pipe_fops;
645 f2->f_mode = 2;
647 fd_install(i, f1);
648 fd_install(j, f2);
649 fd[0] = i;
650 fd[1] = j;
651 return 0;
653 close_f12_inode_i_j:
654 put_unused_fd(j);
655 close_f12_inode_i:
656 put_unused_fd(i);
657 close_f12_inode:
658 free_page((unsigned long) PIPE_BASE(*inode));
659 kfree(inode->i_pipe);
660 inode->i_pipe = NULL;
661 iput(inode);
662 close_f12:
663 put_filp(f2);
664 close_f1:
665 put_filp(f1);
666 no_files:
667 return error;