allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / pipe.c
blobe5bd2ecaf59dbb05311ab5cbfda3bb06012b6e60
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/slab.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/pipe_fs_i.h>
16 #include <linux/uio.h>
17 #include <linux/highmem.h>
18 #include <linux/pagemap.h>
19 #include <linux/audit.h>
21 #include <asm/uaccess.h>
22 #include <asm/ioctls.h>
25 * We use a start+len construction, which provides full use of the
26 * allocated memory.
27 * -- Florian Coosmann (FGC)
29 * Reads with count = 0 should always return 0.
30 * -- Julian Bradfield 1999-06-07.
32 * FIFOs and Pipes now generate SIGIO for both readers and writers.
33 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
35 * pipe_read & write cleanup
36 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
39 /* Drop the inode semaphore and wait for a pipe event, atomically */
40 void pipe_wait(struct pipe_inode_info *pipe)
42 DEFINE_WAIT(wait);
45 * Pipes are system-local resources, so sleeping on them
46 * is considered a noninteractive wait:
48 prepare_to_wait(&pipe->wait, &wait,
49 TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
50 if (pipe->inode)
51 mutex_unlock(&pipe->inode->i_mutex);
52 schedule();
53 finish_wait(&pipe->wait, &wait);
54 if (pipe->inode)
55 mutex_lock(&pipe->inode->i_mutex);
58 static int
59 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
60 int atomic)
62 unsigned long copy;
64 while (len > 0) {
65 while (!iov->iov_len)
66 iov++;
67 copy = min_t(unsigned long, len, iov->iov_len);
69 if (atomic) {
70 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
71 return -EFAULT;
72 } else {
73 if (copy_from_user(to, iov->iov_base, copy))
74 return -EFAULT;
76 to += copy;
77 len -= copy;
78 iov->iov_base += copy;
79 iov->iov_len -= copy;
81 return 0;
84 static int
85 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
86 int atomic)
88 unsigned long copy;
90 while (len > 0) {
91 while (!iov->iov_len)
92 iov++;
93 copy = min_t(unsigned long, len, iov->iov_len);
95 if (atomic) {
96 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
97 return -EFAULT;
98 } else {
99 if (copy_to_user(iov->iov_base, from, copy))
100 return -EFAULT;
102 from += copy;
103 len -= copy;
104 iov->iov_base += copy;
105 iov->iov_len -= copy;
107 return 0;
111 * Attempt to pre-fault in the user memory, so we can use atomic copies.
112 * Returns the number of bytes not faulted in.
114 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
116 while (!iov->iov_len)
117 iov++;
119 while (len > 0) {
120 unsigned long this_len;
122 this_len = min_t(unsigned long, len, iov->iov_len);
123 if (fault_in_pages_writeable(iov->iov_base, this_len))
124 break;
126 len -= this_len;
127 iov++;
130 return len;
134 * Pre-fault in the user memory, so we can use atomic copies.
136 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
138 while (!iov->iov_len)
139 iov++;
141 while (len > 0) {
142 unsigned long this_len;
144 this_len = min_t(unsigned long, len, iov->iov_len);
145 fault_in_pages_readable(iov->iov_base, this_len);
146 len -= this_len;
147 iov++;
151 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
152 struct pipe_buffer *buf)
154 struct page *page = buf->page;
157 * If nobody else uses this page, and we don't already have a
158 * temporary page, let's keep track of it as a one-deep
159 * allocation cache. (Otherwise just release our reference to it)
161 if (page_count(page) == 1 && !pipe->tmp_page)
162 pipe->tmp_page = page;
163 else
164 page_cache_release(page);
168 * generic_pipe_buf_map - virtually map a pipe buffer
169 * @pipe: the pipe that the buffer belongs to
170 * @buf: the buffer that should be mapped
171 * @atomic: whether to use an atomic map
173 * Description:
174 * This function returns a kernel virtual address mapping for the
175 * passed in @pipe_buffer. If @atomic is set, an atomic map is provided
176 * and the caller has to be careful not to fault before calling
177 * the unmap function.
179 * Note that this function occupies KM_USER0 if @atomic != 0.
181 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
182 struct pipe_buffer *buf, int atomic)
184 if (atomic) {
185 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
186 return kmap_atomic(buf->page, KM_USER0);
189 return kmap(buf->page);
193 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
194 * @pipe: the pipe that the buffer belongs to
195 * @buf: the buffer that should be unmapped
196 * @map_data: the data that the mapping function returned
198 * Description:
199 * This function undoes the mapping that ->map() provided.
201 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
202 struct pipe_buffer *buf, void *map_data)
204 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
205 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
206 kunmap_atomic(map_data, KM_USER0);
207 } else
208 kunmap(buf->page);
212 * generic_pipe_buf_steal - attempt to take ownership of a @pipe_buffer
213 * @pipe: the pipe that the buffer belongs to
214 * @buf: the buffer to attempt to steal
216 * Description:
217 * This function attempts to steal the @struct page attached to
218 * @buf. If successful, this function returns 0 and returns with
219 * the page locked. The caller may then reuse the page for whatever
220 * he wishes, the typical use is insertion into a different file
221 * page cache.
223 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
224 struct pipe_buffer *buf)
226 struct page *page = buf->page;
229 * A reference of one is golden, that means that the owner of this
230 * page is the only one holding a reference to it. lock the page
231 * and return OK.
233 if (page_count(page) == 1) {
234 lock_page(page);
235 return 0;
238 return 1;
242 * generic_pipe_buf_get - get a reference to a @struct pipe_buffer
243 * @pipe: the pipe that the buffer belongs to
244 * @buf: the buffer to get a reference to
246 * Description:
247 * This function grabs an extra reference to @buf. It's used in
248 * in the tee() system call, when we duplicate the buffers in one
249 * pipe into another.
251 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
253 page_cache_get(buf->page);
257 * generic_pipe_buf_confirm - verify contents of the pipe buffer
258 * @pipe: the pipe that the buffer belongs to
259 * @buf: the buffer to confirm
261 * Description:
262 * This function does nothing, because the generic pipe code uses
263 * pages that are always good when inserted into the pipe.
265 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
266 struct pipe_buffer *buf)
268 return 0;
271 static const struct pipe_buf_operations anon_pipe_buf_ops = {
272 .can_merge = 1,
273 .map = generic_pipe_buf_map,
274 .unmap = generic_pipe_buf_unmap,
275 .confirm = generic_pipe_buf_confirm,
276 .release = anon_pipe_buf_release,
277 .steal = generic_pipe_buf_steal,
278 .get = generic_pipe_buf_get,
281 static ssize_t
282 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
283 unsigned long nr_segs, loff_t pos)
285 struct file *filp = iocb->ki_filp;
286 struct inode *inode = filp->f_path.dentry->d_inode;
287 struct pipe_inode_info *pipe;
288 int do_wakeup;
289 ssize_t ret;
290 struct iovec *iov = (struct iovec *)_iov;
291 size_t total_len;
293 total_len = iov_length(iov, nr_segs);
294 /* Null read succeeds. */
295 if (unlikely(total_len == 0))
296 return 0;
298 do_wakeup = 0;
299 ret = 0;
300 mutex_lock(&inode->i_mutex);
301 pipe = inode->i_pipe;
302 for (;;) {
303 int bufs = pipe->nrbufs;
304 if (bufs) {
305 int curbuf = pipe->curbuf;
306 struct pipe_buffer *buf = pipe->bufs + curbuf;
307 const struct pipe_buf_operations *ops = buf->ops;
308 void *addr;
309 size_t chars = buf->len;
310 int error, atomic;
312 if (chars > total_len)
313 chars = total_len;
315 error = ops->confirm(pipe, buf);
316 if (error) {
317 if (!ret)
318 error = ret;
319 break;
322 atomic = !iov_fault_in_pages_write(iov, chars);
323 redo:
324 addr = ops->map(pipe, buf, atomic);
325 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
326 ops->unmap(pipe, buf, addr);
327 if (unlikely(error)) {
329 * Just retry with the slow path if we failed.
331 if (atomic) {
332 atomic = 0;
333 goto redo;
335 if (!ret)
336 ret = error;
337 break;
339 ret += chars;
340 buf->offset += chars;
341 buf->len -= chars;
342 if (!buf->len) {
343 buf->ops = NULL;
344 ops->release(pipe, buf);
345 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
346 pipe->curbuf = curbuf;
347 pipe->nrbufs = --bufs;
348 do_wakeup = 1;
350 total_len -= chars;
351 if (!total_len)
352 break; /* common path: read succeeded */
354 if (bufs) /* More to do? */
355 continue;
356 if (!pipe->writers)
357 break;
358 if (!pipe->waiting_writers) {
359 /* syscall merging: Usually we must not sleep
360 * if O_NONBLOCK is set, or if we got some data.
361 * But if a writer sleeps in kernel space, then
362 * we can wait for that data without violating POSIX.
364 if (ret)
365 break;
366 if (filp->f_flags & O_NONBLOCK) {
367 ret = -EAGAIN;
368 break;
371 if (signal_pending(current)) {
372 if (!ret)
373 ret = -ERESTARTSYS;
374 break;
376 if (do_wakeup) {
377 wake_up_interruptible_sync(&pipe->wait);
378 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
380 pipe_wait(pipe);
382 mutex_unlock(&inode->i_mutex);
384 /* Signal writers asynchronously that there is more room. */
385 if (do_wakeup) {
386 wake_up_interruptible(&pipe->wait);
387 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
389 if (ret > 0)
390 file_accessed(filp);
391 return ret;
394 static ssize_t
395 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
396 unsigned long nr_segs, loff_t ppos)
398 struct file *filp = iocb->ki_filp;
399 struct inode *inode = filp->f_path.dentry->d_inode;
400 struct pipe_inode_info *pipe;
401 ssize_t ret;
402 int do_wakeup;
403 struct iovec *iov = (struct iovec *)_iov;
404 size_t total_len;
405 ssize_t chars;
407 total_len = iov_length(iov, nr_segs);
408 /* Null write succeeds. */
409 if (unlikely(total_len == 0))
410 return 0;
412 do_wakeup = 0;
413 ret = 0;
414 mutex_lock(&inode->i_mutex);
415 pipe = inode->i_pipe;
417 if (!pipe->readers) {
418 send_sig(SIGPIPE, current, 0);
419 ret = -EPIPE;
420 goto out;
423 /* We try to merge small writes */
424 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
425 if (pipe->nrbufs && chars != 0) {
426 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
427 (PIPE_BUFFERS-1);
428 struct pipe_buffer *buf = pipe->bufs + lastbuf;
429 const struct pipe_buf_operations *ops = buf->ops;
430 int offset = buf->offset + buf->len;
432 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
433 int error, atomic = 1;
434 void *addr;
436 error = ops->confirm(pipe, buf);
437 if (error)
438 goto out;
440 iov_fault_in_pages_read(iov, chars);
441 redo1:
442 addr = ops->map(pipe, buf, atomic);
443 error = pipe_iov_copy_from_user(offset + addr, iov,
444 chars, atomic);
445 ops->unmap(pipe, buf, addr);
446 ret = error;
447 do_wakeup = 1;
448 if (error) {
449 if (atomic) {
450 atomic = 0;
451 goto redo1;
453 goto out;
455 buf->len += chars;
456 total_len -= chars;
457 ret = chars;
458 if (!total_len)
459 goto out;
463 for (;;) {
464 int bufs;
466 if (!pipe->readers) {
467 send_sig(SIGPIPE, current, 0);
468 if (!ret)
469 ret = -EPIPE;
470 break;
472 bufs = pipe->nrbufs;
473 if (bufs < PIPE_BUFFERS) {
474 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
475 struct pipe_buffer *buf = pipe->bufs + newbuf;
476 struct page *page = pipe->tmp_page;
477 char *src;
478 int error, atomic = 1;
480 if (!page) {
481 page = alloc_page(GFP_HIGHUSER);
482 if (unlikely(!page)) {
483 ret = ret ? : -ENOMEM;
484 break;
486 pipe->tmp_page = page;
488 /* Always wake up, even if the copy fails. Otherwise
489 * we lock up (O_NONBLOCK-)readers that sleep due to
490 * syscall merging.
491 * FIXME! Is this really true?
493 do_wakeup = 1;
494 chars = PAGE_SIZE;
495 if (chars > total_len)
496 chars = total_len;
498 iov_fault_in_pages_read(iov, chars);
499 redo2:
500 if (atomic)
501 src = kmap_atomic(page, KM_USER0);
502 else
503 src = kmap(page);
505 error = pipe_iov_copy_from_user(src, iov, chars,
506 atomic);
507 if (atomic)
508 kunmap_atomic(src, KM_USER0);
509 else
510 kunmap(page);
512 if (unlikely(error)) {
513 if (atomic) {
514 atomic = 0;
515 goto redo2;
517 if (!ret)
518 ret = error;
519 break;
521 ret += chars;
523 /* Insert it into the buffer array */
524 buf->page = page;
525 buf->ops = &anon_pipe_buf_ops;
526 buf->offset = 0;
527 buf->len = chars;
528 pipe->nrbufs = ++bufs;
529 pipe->tmp_page = NULL;
531 total_len -= chars;
532 if (!total_len)
533 break;
535 if (bufs < PIPE_BUFFERS)
536 continue;
537 if (filp->f_flags & O_NONBLOCK) {
538 if (!ret)
539 ret = -EAGAIN;
540 break;
542 if (signal_pending(current)) {
543 if (!ret)
544 ret = -ERESTARTSYS;
545 break;
547 if (do_wakeup) {
548 wake_up_interruptible_sync(&pipe->wait);
549 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
550 do_wakeup = 0;
552 pipe->waiting_writers++;
553 pipe_wait(pipe);
554 pipe->waiting_writers--;
556 out:
557 mutex_unlock(&inode->i_mutex);
558 if (do_wakeup) {
559 wake_up_interruptible(&pipe->wait);
560 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
562 if (ret > 0)
563 file_update_time(filp);
564 return ret;
567 static ssize_t
568 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
570 return -EBADF;
573 static ssize_t
574 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
575 loff_t *ppos)
577 return -EBADF;
580 static int
581 pipe_ioctl(struct inode *pino, struct file *filp,
582 unsigned int cmd, unsigned long arg)
584 struct inode *inode = filp->f_path.dentry->d_inode;
585 struct pipe_inode_info *pipe;
586 int count, buf, nrbufs;
588 switch (cmd) {
589 case FIONREAD:
590 mutex_lock(&inode->i_mutex);
591 pipe = inode->i_pipe;
592 count = 0;
593 buf = pipe->curbuf;
594 nrbufs = pipe->nrbufs;
595 while (--nrbufs >= 0) {
596 count += pipe->bufs[buf].len;
597 buf = (buf+1) & (PIPE_BUFFERS-1);
599 mutex_unlock(&inode->i_mutex);
601 return put_user(count, (int __user *)arg);
602 default:
603 return -EINVAL;
607 /* No kernel lock held - fine */
608 static unsigned int
609 pipe_poll(struct file *filp, poll_table *wait)
611 unsigned int mask;
612 struct inode *inode = filp->f_path.dentry->d_inode;
613 struct pipe_inode_info *pipe = inode->i_pipe;
614 int nrbufs;
616 poll_wait(filp, &pipe->wait, wait);
618 /* Reading only -- no need for acquiring the semaphore. */
619 nrbufs = pipe->nrbufs;
620 mask = 0;
621 if (filp->f_mode & FMODE_READ) {
622 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
623 if (!pipe->writers && filp->f_version != pipe->w_counter)
624 mask |= POLLHUP;
627 if (filp->f_mode & FMODE_WRITE) {
628 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
630 * Most Unices do not set POLLERR for FIFOs but on Linux they
631 * behave exactly like pipes for poll().
633 if (!pipe->readers)
634 mask |= POLLERR;
637 return mask;
640 static int
641 pipe_release(struct inode *inode, int decr, int decw)
643 struct pipe_inode_info *pipe;
645 mutex_lock(&inode->i_mutex);
646 pipe = inode->i_pipe;
647 pipe->readers -= decr;
648 pipe->writers -= decw;
650 if (!pipe->readers && !pipe->writers) {
651 free_pipe_info(inode);
652 } else {
653 wake_up_interruptible(&pipe->wait);
654 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
655 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
657 mutex_unlock(&inode->i_mutex);
659 return 0;
662 static int
663 pipe_read_fasync(int fd, struct file *filp, int on)
665 struct inode *inode = filp->f_path.dentry->d_inode;
666 int retval;
668 mutex_lock(&inode->i_mutex);
669 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
670 mutex_unlock(&inode->i_mutex);
672 if (retval < 0)
673 return retval;
675 return 0;
679 static int
680 pipe_write_fasync(int fd, struct file *filp, int on)
682 struct inode *inode = filp->f_path.dentry->d_inode;
683 int retval;
685 mutex_lock(&inode->i_mutex);
686 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
687 mutex_unlock(&inode->i_mutex);
689 if (retval < 0)
690 return retval;
692 return 0;
696 static int
697 pipe_rdwr_fasync(int fd, struct file *filp, int on)
699 struct inode *inode = filp->f_path.dentry->d_inode;
700 struct pipe_inode_info *pipe = inode->i_pipe;
701 int retval;
703 mutex_lock(&inode->i_mutex);
705 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
707 if (retval >= 0)
708 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
710 mutex_unlock(&inode->i_mutex);
712 if (retval < 0)
713 return retval;
715 return 0;
719 static int
720 pipe_read_release(struct inode *inode, struct file *filp)
722 pipe_read_fasync(-1, filp, 0);
723 return pipe_release(inode, 1, 0);
726 static int
727 pipe_write_release(struct inode *inode, struct file *filp)
729 pipe_write_fasync(-1, filp, 0);
730 return pipe_release(inode, 0, 1);
733 static int
734 pipe_rdwr_release(struct inode *inode, struct file *filp)
736 int decr, decw;
738 pipe_rdwr_fasync(-1, filp, 0);
739 decr = (filp->f_mode & FMODE_READ) != 0;
740 decw = (filp->f_mode & FMODE_WRITE) != 0;
741 return pipe_release(inode, decr, decw);
744 static int
745 pipe_read_open(struct inode *inode, struct file *filp)
747 int ret = -ENOENT;
749 mutex_lock(&inode->i_mutex);
751 if (inode->i_pipe) {
752 ret = 0;
753 inode->i_pipe->readers++;
756 mutex_unlock(&inode->i_mutex);
758 return ret;
761 static int
762 pipe_write_open(struct inode *inode, struct file *filp)
764 int ret = -ENOENT;
766 mutex_lock(&inode->i_mutex);
768 if (inode->i_pipe) {
769 ret = 0;
770 inode->i_pipe->writers++;
773 mutex_unlock(&inode->i_mutex);
775 return ret;
778 static int
779 pipe_rdwr_open(struct inode *inode, struct file *filp)
781 int ret = -ENOENT;
783 mutex_lock(&inode->i_mutex);
785 if (inode->i_pipe) {
786 ret = 0;
787 if (filp->f_mode & FMODE_READ)
788 inode->i_pipe->readers++;
789 if (filp->f_mode & FMODE_WRITE)
790 inode->i_pipe->writers++;
793 mutex_unlock(&inode->i_mutex);
795 return ret;
799 * The file_operations structs are not static because they
800 * are also used in linux/fs/fifo.c to do operations on FIFOs.
802 const struct file_operations read_fifo_fops = {
803 .llseek = no_llseek,
804 .read = do_sync_read,
805 .aio_read = pipe_read,
806 .write = bad_pipe_w,
807 .poll = pipe_poll,
808 .ioctl = pipe_ioctl,
809 .open = pipe_read_open,
810 .release = pipe_read_release,
811 .fasync = pipe_read_fasync,
814 const struct file_operations write_fifo_fops = {
815 .llseek = no_llseek,
816 .read = bad_pipe_r,
817 .write = do_sync_write,
818 .aio_write = pipe_write,
819 .poll = pipe_poll,
820 .ioctl = pipe_ioctl,
821 .open = pipe_write_open,
822 .release = pipe_write_release,
823 .fasync = pipe_write_fasync,
826 const struct file_operations rdwr_fifo_fops = {
827 .llseek = no_llseek,
828 .read = do_sync_read,
829 .aio_read = pipe_read,
830 .write = do_sync_write,
831 .aio_write = pipe_write,
832 .poll = pipe_poll,
833 .ioctl = pipe_ioctl,
834 .open = pipe_rdwr_open,
835 .release = pipe_rdwr_release,
836 .fasync = pipe_rdwr_fasync,
839 static const struct file_operations read_pipe_fops = {
840 .llseek = no_llseek,
841 .read = do_sync_read,
842 .aio_read = pipe_read,
843 .write = bad_pipe_w,
844 .poll = pipe_poll,
845 .ioctl = pipe_ioctl,
846 .open = pipe_read_open,
847 .release = pipe_read_release,
848 .fasync = pipe_read_fasync,
851 static const struct file_operations write_pipe_fops = {
852 .llseek = no_llseek,
853 .read = bad_pipe_r,
854 .write = do_sync_write,
855 .aio_write = pipe_write,
856 .poll = pipe_poll,
857 .ioctl = pipe_ioctl,
858 .open = pipe_write_open,
859 .release = pipe_write_release,
860 .fasync = pipe_write_fasync,
863 static const struct file_operations rdwr_pipe_fops = {
864 .llseek = no_llseek,
865 .read = do_sync_read,
866 .aio_read = pipe_read,
867 .write = do_sync_write,
868 .aio_write = pipe_write,
869 .poll = pipe_poll,
870 .ioctl = pipe_ioctl,
871 .open = pipe_rdwr_open,
872 .release = pipe_rdwr_release,
873 .fasync = pipe_rdwr_fasync,
876 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
878 struct pipe_inode_info *pipe;
880 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
881 if (pipe) {
882 init_waitqueue_head(&pipe->wait);
883 pipe->r_counter = pipe->w_counter = 1;
884 pipe->inode = inode;
887 return pipe;
890 void __free_pipe_info(struct pipe_inode_info *pipe)
892 int i;
894 for (i = 0; i < PIPE_BUFFERS; i++) {
895 struct pipe_buffer *buf = pipe->bufs + i;
896 if (buf->ops)
897 buf->ops->release(pipe, buf);
899 if (pipe->tmp_page)
900 __free_page(pipe->tmp_page);
901 kfree(pipe);
904 void free_pipe_info(struct inode *inode)
906 __free_pipe_info(inode->i_pipe);
907 inode->i_pipe = NULL;
910 static struct vfsmount *pipe_mnt __read_mostly;
911 static int pipefs_delete_dentry(struct dentry *dentry)
914 * At creation time, we pretended this dentry was hashed
915 * (by clearing DCACHE_UNHASHED bit in d_flags)
916 * At delete time, we restore the truth : not hashed.
917 * (so that dput() can proceed correctly)
919 dentry->d_flags |= DCACHE_UNHASHED;
920 return 0;
924 * pipefs_dname() is called from d_path().
926 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
928 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
929 dentry->d_inode->i_ino);
932 static struct dentry_operations pipefs_dentry_operations = {
933 .d_delete = pipefs_delete_dentry,
934 .d_dname = pipefs_dname,
937 static struct inode * get_pipe_inode(void)
939 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
940 struct pipe_inode_info *pipe;
942 if (!inode)
943 goto fail_inode;
945 pipe = alloc_pipe_info(inode);
946 if (!pipe)
947 goto fail_iput;
948 inode->i_pipe = pipe;
950 pipe->readers = pipe->writers = 1;
951 inode->i_fop = &rdwr_pipe_fops;
954 * Mark the inode dirty from the very beginning,
955 * that way it will never be moved to the dirty
956 * list because "mark_inode_dirty()" will think
957 * that it already _is_ on the dirty list.
959 inode->i_state = I_DIRTY;
960 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
961 inode->i_uid = current->fsuid;
962 inode->i_gid = current->fsgid;
963 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
965 return inode;
967 fail_iput:
968 iput(inode);
970 fail_inode:
971 return NULL;
974 struct file *create_write_pipe(void)
976 int err;
977 struct inode *inode;
978 struct file *f;
979 struct dentry *dentry;
980 struct qstr name = { .name = "" };
982 f = get_empty_filp();
983 if (!f)
984 return ERR_PTR(-ENFILE);
985 err = -ENFILE;
986 inode = get_pipe_inode();
987 if (!inode)
988 goto err_file;
990 err = -ENOMEM;
991 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
992 if (!dentry)
993 goto err_inode;
995 dentry->d_op = &pipefs_dentry_operations;
997 * We dont want to publish this dentry into global dentry hash table.
998 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
999 * This permits a working /proc/$pid/fd/XXX on pipes
1001 dentry->d_flags &= ~DCACHE_UNHASHED;
1002 d_instantiate(dentry, inode);
1003 f->f_path.mnt = mntget(pipe_mnt);
1004 f->f_path.dentry = dentry;
1005 f->f_mapping = inode->i_mapping;
1007 f->f_flags = O_WRONLY;
1008 f->f_op = &write_pipe_fops;
1009 f->f_mode = FMODE_WRITE;
1010 f->f_version = 0;
1012 return f;
1014 err_inode:
1015 free_pipe_info(inode);
1016 iput(inode);
1017 err_file:
1018 put_filp(f);
1019 return ERR_PTR(err);
1022 void free_write_pipe(struct file *f)
1024 free_pipe_info(f->f_dentry->d_inode);
1025 dput(f->f_path.dentry);
1026 mntput(f->f_path.mnt);
1027 put_filp(f);
1030 struct file *create_read_pipe(struct file *wrf)
1032 struct file *f = get_empty_filp();
1033 if (!f)
1034 return ERR_PTR(-ENFILE);
1036 /* Grab pipe from the writer */
1037 f->f_path.mnt = mntget(wrf->f_path.mnt);
1038 f->f_path.dentry = dget(wrf->f_path.dentry);
1039 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
1041 f->f_pos = 0;
1042 f->f_flags = O_RDONLY;
1043 f->f_op = &read_pipe_fops;
1044 f->f_mode = FMODE_READ;
1045 f->f_version = 0;
1047 return f;
1050 int do_pipe(int *fd)
1052 struct file *fw, *fr;
1053 int error;
1054 int fdw, fdr;
1056 fw = create_write_pipe();
1057 if (IS_ERR(fw))
1058 return PTR_ERR(fw);
1059 fr = create_read_pipe(fw);
1060 error = PTR_ERR(fr);
1061 if (IS_ERR(fr))
1062 goto err_write_pipe;
1064 error = get_unused_fd();
1065 if (error < 0)
1066 goto err_read_pipe;
1067 fdr = error;
1069 error = get_unused_fd();
1070 if (error < 0)
1071 goto err_fdr;
1072 fdw = error;
1074 error = audit_fd_pair(fdr, fdw);
1075 if (error < 0)
1076 goto err_fdw;
1078 fd_install(fdr, fr);
1079 fd_install(fdw, fw);
1080 fd[0] = fdr;
1081 fd[1] = fdw;
1083 return 0;
1085 err_fdw:
1086 put_unused_fd(fdw);
1087 err_fdr:
1088 put_unused_fd(fdr);
1089 err_read_pipe:
1090 dput(fr->f_dentry);
1091 mntput(fr->f_vfsmnt);
1092 put_filp(fr);
1093 err_write_pipe:
1094 free_write_pipe(fw);
1095 return error;
1099 * pipefs should _never_ be mounted by userland - too much of security hassle,
1100 * no real gain from having the whole whorehouse mounted. So we don't need
1101 * any operations on the root directory. However, we need a non-trivial
1102 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1104 static int pipefs_get_sb(struct file_system_type *fs_type,
1105 int flags, const char *dev_name, void *data,
1106 struct vfsmount *mnt)
1108 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
1111 static struct file_system_type pipe_fs_type = {
1112 .name = "pipefs",
1113 .get_sb = pipefs_get_sb,
1114 .kill_sb = kill_anon_super,
1117 static int __init init_pipe_fs(void)
1119 int err = register_filesystem(&pipe_fs_type);
1121 if (!err) {
1122 pipe_mnt = kern_mount(&pipe_fs_type);
1123 if (IS_ERR(pipe_mnt)) {
1124 err = PTR_ERR(pipe_mnt);
1125 unregister_filesystem(&pipe_fs_type);
1128 return err;
1131 static void __exit exit_pipe_fs(void)
1133 unregister_filesystem(&pipe_fs_type);
1134 mntput(pipe_mnt);
1137 fs_initcall(init_pipe_fs);
1138 module_exit(exit_pipe_fs);