[PATCH] splice: add support for sys_tee()
[linux-2.6.git] / fs / pipe.c
1 /*
2  *  linux/fs/pipe.c
3  *
4  *  Copyright (C) 1991, 1992, 1999  Linus Torvalds
5  */
6
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
20 #include <asm/uaccess.h>
21 #include <asm/ioctls.h>
22
23 /*
24  * We use a start+len construction, which provides full use of the 
25  * allocated memory.
26  * -- Florian Coosmann (FGC)
27  * 
28  * Reads with count = 0 should always return 0.
29  * -- Julian Bradfield 1999-06-07.
30  *
31  * FIFOs and Pipes now generate SIGIO for both readers and writers.
32  * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
33  *
34  * pipe_read & write cleanup
35  * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
36  */
37
38 /* Drop the inode semaphore and wait for a pipe event, atomically */
39 void pipe_wait(struct pipe_inode_info *pipe)
40 {
41         DEFINE_WAIT(wait);
42
43         /*
44          * Pipes are system-local resources, so sleeping on them
45          * is considered a noninteractive wait:
46          */
47         prepare_to_wait(&pipe->wait, &wait,
48                         TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
49         if (pipe->inode)
50                 mutex_unlock(&pipe->inode->i_mutex);
51         schedule();
52         finish_wait(&pipe->wait, &wait);
53         if (pipe->inode)
54                 mutex_lock(&pipe->inode->i_mutex);
55 }
56
57 static int
58 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
59 {
60         unsigned long copy;
61
62         while (len > 0) {
63                 while (!iov->iov_len)
64                         iov++;
65                 copy = min_t(unsigned long, len, iov->iov_len);
66
67                 if (copy_from_user(to, iov->iov_base, copy))
68                         return -EFAULT;
69                 to += copy;
70                 len -= copy;
71                 iov->iov_base += copy;
72                 iov->iov_len -= copy;
73         }
74         return 0;
75 }
76
77 static int
78 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
79 {
80         unsigned long copy;
81
82         while (len > 0) {
83                 while (!iov->iov_len)
84                         iov++;
85                 copy = min_t(unsigned long, len, iov->iov_len);
86
87                 if (copy_to_user(iov->iov_base, from, copy))
88                         return -EFAULT;
89                 from += copy;
90                 len -= copy;
91                 iov->iov_base += copy;
92                 iov->iov_len -= copy;
93         }
94         return 0;
95 }
96
97 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
98                                   struct pipe_buffer *buf)
99 {
100         struct page *page = buf->page;
101
102         buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
103
104         /*
105          * If nobody else uses this page, and we don't already have a
106          * temporary page, let's keep track of it as a one-deep
107          * allocation cache. (Otherwise just release our reference to it)
108          */
109         if (page_count(page) == 1 && !pipe->tmp_page)
110                 pipe->tmp_page = page;
111         else
112                 page_cache_release(page);
113 }
114
115 static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe,
116                                 struct pipe_buffer *buf)
117 {
118         return kmap(buf->page);
119 }
120
121 static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe,
122                                 struct pipe_buffer *buf)
123 {
124         kunmap(buf->page);
125 }
126
127 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
128                                struct pipe_buffer *buf)
129 {
130         buf->flags |= PIPE_BUF_FLAG_STOLEN;
131         return 0;
132 }
133
134 static void anon_pipe_buf_get(struct pipe_inode_info *info,
135                               struct pipe_buffer *buf)
136 {
137         page_cache_get(buf->page);
138 }
139
140 static struct pipe_buf_operations anon_pipe_buf_ops = {
141         .can_merge = 1,
142         .map = anon_pipe_buf_map,
143         .unmap = anon_pipe_buf_unmap,
144         .release = anon_pipe_buf_release,
145         .steal = anon_pipe_buf_steal,
146         .get = anon_pipe_buf_get,
147 };
148
149 static ssize_t
150 pipe_readv(struct file *filp, const struct iovec *_iov,
151            unsigned long nr_segs, loff_t *ppos)
152 {
153         struct inode *inode = filp->f_dentry->d_inode;
154         struct pipe_inode_info *pipe;
155         int do_wakeup;
156         ssize_t ret;
157         struct iovec *iov = (struct iovec *)_iov;
158         size_t total_len;
159
160         total_len = iov_length(iov, nr_segs);
161         /* Null read succeeds. */
162         if (unlikely(total_len == 0))
163                 return 0;
164
165         do_wakeup = 0;
166         ret = 0;
167         mutex_lock(&inode->i_mutex);
168         pipe = inode->i_pipe;
169         for (;;) {
170                 int bufs = pipe->nrbufs;
171                 if (bufs) {
172                         int curbuf = pipe->curbuf;
173                         struct pipe_buffer *buf = pipe->bufs + curbuf;
174                         struct pipe_buf_operations *ops = buf->ops;
175                         void *addr;
176                         size_t chars = buf->len;
177                         int error;
178
179                         if (chars > total_len)
180                                 chars = total_len;
181
182                         addr = ops->map(filp, pipe, buf);
183                         if (IS_ERR(addr)) {
184                                 if (!ret)
185                                         ret = PTR_ERR(addr);
186                                 break;
187                         }
188                         error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
189                         ops->unmap(pipe, buf);
190                         if (unlikely(error)) {
191                                 if (!ret)
192                                         ret = -EFAULT;
193                                 break;
194                         }
195                         ret += chars;
196                         buf->offset += chars;
197                         buf->len -= chars;
198                         if (!buf->len) {
199                                 buf->ops = NULL;
200                                 ops->release(pipe, buf);
201                                 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
202                                 pipe->curbuf = curbuf;
203                                 pipe->nrbufs = --bufs;
204                                 do_wakeup = 1;
205                         }
206                         total_len -= chars;
207                         if (!total_len)
208                                 break;  /* common path: read succeeded */
209                 }
210                 if (bufs)       /* More to do? */
211                         continue;
212                 if (!pipe->writers)
213                         break;
214                 if (!pipe->waiting_writers) {
215                         /* syscall merging: Usually we must not sleep
216                          * if O_NONBLOCK is set, or if we got some data.
217                          * But if a writer sleeps in kernel space, then
218                          * we can wait for that data without violating POSIX.
219                          */
220                         if (ret)
221                                 break;
222                         if (filp->f_flags & O_NONBLOCK) {
223                                 ret = -EAGAIN;
224                                 break;
225                         }
226                 }
227                 if (signal_pending(current)) {
228                         if (!ret)
229                                 ret = -ERESTARTSYS;
230                         break;
231                 }
232                 if (do_wakeup) {
233                         wake_up_interruptible_sync(&pipe->wait);
234                         kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
235                 }
236                 pipe_wait(pipe);
237         }
238         mutex_unlock(&inode->i_mutex);
239
240         /* Signal writers asynchronously that there is more room. */
241         if (do_wakeup) {
242                 wake_up_interruptible(&pipe->wait);
243                 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
244         }
245         if (ret > 0)
246                 file_accessed(filp);
247         return ret;
248 }
249
250 static ssize_t
251 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
252 {
253         struct iovec iov = { .iov_base = buf, .iov_len = count };
254
255         return pipe_readv(filp, &iov, 1, ppos);
256 }
257
258 static ssize_t
259 pipe_writev(struct file *filp, const struct iovec *_iov,
260             unsigned long nr_segs, loff_t *ppos)
261 {
262         struct inode *inode = filp->f_dentry->d_inode;
263         struct pipe_inode_info *pipe;
264         ssize_t ret;
265         int do_wakeup;
266         struct iovec *iov = (struct iovec *)_iov;
267         size_t total_len;
268         ssize_t chars;
269
270         total_len = iov_length(iov, nr_segs);
271         /* Null write succeeds. */
272         if (unlikely(total_len == 0))
273                 return 0;
274
275         do_wakeup = 0;
276         ret = 0;
277         mutex_lock(&inode->i_mutex);
278         pipe = inode->i_pipe;
279
280         if (!pipe->readers) {
281                 send_sig(SIGPIPE, current, 0);
282                 ret = -EPIPE;
283                 goto out;
284         }
285
286         /* We try to merge small writes */
287         chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
288         if (pipe->nrbufs && chars != 0) {
289                 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
290                                                         (PIPE_BUFFERS-1);
291                 struct pipe_buffer *buf = pipe->bufs + lastbuf;
292                 struct pipe_buf_operations *ops = buf->ops;
293                 int offset = buf->offset + buf->len;
294
295                 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
296                         void *addr;
297                         int error;
298
299                         addr = ops->map(filp, pipe, buf);
300                         if (IS_ERR(addr)) {
301                                 error = PTR_ERR(addr);
302                                 goto out;
303                         }
304                         error = pipe_iov_copy_from_user(offset + addr, iov,
305                                                         chars);
306                         ops->unmap(pipe, buf);
307                         ret = error;
308                         do_wakeup = 1;
309                         if (error)
310                                 goto out;
311                         buf->len += chars;
312                         total_len -= chars;
313                         ret = chars;
314                         if (!total_len)
315                                 goto out;
316                 }
317         }
318
319         for (;;) {
320                 int bufs;
321
322                 if (!pipe->readers) {
323                         send_sig(SIGPIPE, current, 0);
324                         if (!ret)
325                                 ret = -EPIPE;
326                         break;
327                 }
328                 bufs = pipe->nrbufs;
329                 if (bufs < PIPE_BUFFERS) {
330                         int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
331                         struct pipe_buffer *buf = pipe->bufs + newbuf;
332                         struct page *page = pipe->tmp_page;
333                         int error;
334
335                         if (!page) {
336                                 page = alloc_page(GFP_HIGHUSER);
337                                 if (unlikely(!page)) {
338                                         ret = ret ? : -ENOMEM;
339                                         break;
340                                 }
341                                 pipe->tmp_page = page;
342                         }
343                         /* Always wake up, even if the copy fails. Otherwise
344                          * we lock up (O_NONBLOCK-)readers that sleep due to
345                          * syscall merging.
346                          * FIXME! Is this really true?
347                          */
348                         do_wakeup = 1;
349                         chars = PAGE_SIZE;
350                         if (chars > total_len)
351                                 chars = total_len;
352
353                         error = pipe_iov_copy_from_user(kmap(page), iov, chars);
354                         kunmap(page);
355                         if (unlikely(error)) {
356                                 if (!ret)
357                                         ret = -EFAULT;
358                                 break;
359                         }
360                         ret += chars;
361
362                         /* Insert it into the buffer array */
363                         buf->page = page;
364                         buf->ops = &anon_pipe_buf_ops;
365                         buf->offset = 0;
366                         buf->len = chars;
367                         pipe->nrbufs = ++bufs;
368                         pipe->tmp_page = NULL;
369
370                         total_len -= chars;
371                         if (!total_len)
372                                 break;
373                 }
374                 if (bufs < PIPE_BUFFERS)
375                         continue;
376                 if (filp->f_flags & O_NONBLOCK) {
377                         if (!ret)
378                                 ret = -EAGAIN;
379                         break;
380                 }
381                 if (signal_pending(current)) {
382                         if (!ret)
383                                 ret = -ERESTARTSYS;
384                         break;
385                 }
386                 if (do_wakeup) {
387                         wake_up_interruptible_sync(&pipe->wait);
388                         kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
389                         do_wakeup = 0;
390                 }
391                 pipe->waiting_writers++;
392                 pipe_wait(pipe);
393                 pipe->waiting_writers--;
394         }
395 out:
396         mutex_unlock(&inode->i_mutex);
397         if (do_wakeup) {
398                 wake_up_interruptible(&pipe->wait);
399                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
400         }
401         if (ret > 0)
402                 file_update_time(filp);
403         return ret;
404 }
405
406 static ssize_t
407 pipe_write(struct file *filp, const char __user *buf,
408            size_t count, loff_t *ppos)
409 {
410         struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
411
412         return pipe_writev(filp, &iov, 1, ppos);
413 }
414
415 static ssize_t
416 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
417 {
418         return -EBADF;
419 }
420
421 static ssize_t
422 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
423            loff_t *ppos)
424 {
425         return -EBADF;
426 }
427
428 static int
429 pipe_ioctl(struct inode *pino, struct file *filp,
430            unsigned int cmd, unsigned long arg)
431 {
432         struct inode *inode = filp->f_dentry->d_inode;
433         struct pipe_inode_info *pipe;
434         int count, buf, nrbufs;
435
436         switch (cmd) {
437                 case FIONREAD:
438                         mutex_lock(&inode->i_mutex);
439                         pipe = inode->i_pipe;
440                         count = 0;
441                         buf = pipe->curbuf;
442                         nrbufs = pipe->nrbufs;
443                         while (--nrbufs >= 0) {
444                                 count += pipe->bufs[buf].len;
445                                 buf = (buf+1) & (PIPE_BUFFERS-1);
446                         }
447                         mutex_unlock(&inode->i_mutex);
448
449                         return put_user(count, (int __user *)arg);
450                 default:
451                         return -EINVAL;
452         }
453 }
454
455 /* No kernel lock held - fine */
456 static unsigned int
457 pipe_poll(struct file *filp, poll_table *wait)
458 {
459         unsigned int mask;
460         struct inode *inode = filp->f_dentry->d_inode;
461         struct pipe_inode_info *pipe = inode->i_pipe;
462         int nrbufs;
463
464         poll_wait(filp, &pipe->wait, wait);
465
466         /* Reading only -- no need for acquiring the semaphore.  */
467         nrbufs = pipe->nrbufs;
468         mask = 0;
469         if (filp->f_mode & FMODE_READ) {
470                 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
471                 if (!pipe->writers && filp->f_version != pipe->w_counter)
472                         mask |= POLLHUP;
473         }
474
475         if (filp->f_mode & FMODE_WRITE) {
476                 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
477                 /*
478                  * Most Unices do not set POLLERR for FIFOs but on Linux they
479                  * behave exactly like pipes for poll().
480                  */
481                 if (!pipe->readers)
482                         mask |= POLLERR;
483         }
484
485         return mask;
486 }
487
488 static int
489 pipe_release(struct inode *inode, int decr, int decw)
490 {
491         struct pipe_inode_info *pipe;
492
493         mutex_lock(&inode->i_mutex);
494         pipe = inode->i_pipe;
495         pipe->readers -= decr;
496         pipe->writers -= decw;
497
498         if (!pipe->readers && !pipe->writers) {
499                 free_pipe_info(inode);
500         } else {
501                 wake_up_interruptible(&pipe->wait);
502                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
503                 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
504         }
505         mutex_unlock(&inode->i_mutex);
506
507         return 0;
508 }
509
510 static int
511 pipe_read_fasync(int fd, struct file *filp, int on)
512 {
513         struct inode *inode = filp->f_dentry->d_inode;
514         int retval;
515
516         mutex_lock(&inode->i_mutex);
517         retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
518         mutex_unlock(&inode->i_mutex);
519
520         if (retval < 0)
521                 return retval;
522
523         return 0;
524 }
525
526
527 static int
528 pipe_write_fasync(int fd, struct file *filp, int on)
529 {
530         struct inode *inode = filp->f_dentry->d_inode;
531         int retval;
532
533         mutex_lock(&inode->i_mutex);
534         retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
535         mutex_unlock(&inode->i_mutex);
536
537         if (retval < 0)
538                 return retval;
539
540         return 0;
541 }
542
543
544 static int
545 pipe_rdwr_fasync(int fd, struct file *filp, int on)
546 {
547         struct inode *inode = filp->f_dentry->d_inode;
548         struct pipe_inode_info *pipe = inode->i_pipe;
549         int retval;
550
551         mutex_lock(&inode->i_mutex);
552
553         retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
554
555         if (retval >= 0)
556                 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
557
558         mutex_unlock(&inode->i_mutex);
559
560         if (retval < 0)
561                 return retval;
562
563         return 0;
564 }
565
566
567 static int
568 pipe_read_release(struct inode *inode, struct file *filp)
569 {
570         pipe_read_fasync(-1, filp, 0);
571         return pipe_release(inode, 1, 0);
572 }
573
574 static int
575 pipe_write_release(struct inode *inode, struct file *filp)
576 {
577         pipe_write_fasync(-1, filp, 0);
578         return pipe_release(inode, 0, 1);
579 }
580
581 static int
582 pipe_rdwr_release(struct inode *inode, struct file *filp)
583 {
584         int decr, decw;
585
586         pipe_rdwr_fasync(-1, filp, 0);
587         decr = (filp->f_mode & FMODE_READ) != 0;
588         decw = (filp->f_mode & FMODE_WRITE) != 0;
589         return pipe_release(inode, decr, decw);
590 }
591
592 static int
593 pipe_read_open(struct inode *inode, struct file *filp)
594 {
595         /* We could have perhaps used atomic_t, but this and friends
596            below are the only places.  So it doesn't seem worthwhile.  */
597         mutex_lock(&inode->i_mutex);
598         inode->i_pipe->readers++;
599         mutex_unlock(&inode->i_mutex);
600
601         return 0;
602 }
603
604 static int
605 pipe_write_open(struct inode *inode, struct file *filp)
606 {
607         mutex_lock(&inode->i_mutex);
608         inode->i_pipe->writers++;
609         mutex_unlock(&inode->i_mutex);
610
611         return 0;
612 }
613
614 static int
615 pipe_rdwr_open(struct inode *inode, struct file *filp)
616 {
617         mutex_lock(&inode->i_mutex);
618         if (filp->f_mode & FMODE_READ)
619                 inode->i_pipe->readers++;
620         if (filp->f_mode & FMODE_WRITE)
621                 inode->i_pipe->writers++;
622         mutex_unlock(&inode->i_mutex);
623
624         return 0;
625 }
626
627 /*
628  * The file_operations structs are not static because they
629  * are also used in linux/fs/fifo.c to do operations on FIFOs.
630  */
631 const struct file_operations read_fifo_fops = {
632         .llseek         = no_llseek,
633         .read           = pipe_read,
634         .readv          = pipe_readv,
635         .write          = bad_pipe_w,
636         .poll           = pipe_poll,
637         .ioctl          = pipe_ioctl,
638         .open           = pipe_read_open,
639         .release        = pipe_read_release,
640         .fasync         = pipe_read_fasync,
641 };
642
643 const struct file_operations write_fifo_fops = {
644         .llseek         = no_llseek,
645         .read           = bad_pipe_r,
646         .write          = pipe_write,
647         .writev         = pipe_writev,
648         .poll           = pipe_poll,
649         .ioctl          = pipe_ioctl,
650         .open           = pipe_write_open,
651         .release        = pipe_write_release,
652         .fasync         = pipe_write_fasync,
653 };
654
655 const struct file_operations rdwr_fifo_fops = {
656         .llseek         = no_llseek,
657         .read           = pipe_read,
658         .readv          = pipe_readv,
659         .write          = pipe_write,
660         .writev         = pipe_writev,
661         .poll           = pipe_poll,
662         .ioctl          = pipe_ioctl,
663         .open           = pipe_rdwr_open,
664         .release        = pipe_rdwr_release,
665         .fasync         = pipe_rdwr_fasync,
666 };
667
668 static struct file_operations read_pipe_fops = {
669         .llseek         = no_llseek,
670         .read           = pipe_read,
671         .readv          = pipe_readv,
672         .write          = bad_pipe_w,
673         .poll           = pipe_poll,
674         .ioctl          = pipe_ioctl,
675         .open           = pipe_read_open,
676         .release        = pipe_read_release,
677         .fasync         = pipe_read_fasync,
678 };
679
680 static struct file_operations write_pipe_fops = {
681         .llseek         = no_llseek,
682         .read           = bad_pipe_r,
683         .write          = pipe_write,
684         .writev         = pipe_writev,
685         .poll           = pipe_poll,
686         .ioctl          = pipe_ioctl,
687         .open           = pipe_write_open,
688         .release        = pipe_write_release,
689         .fasync         = pipe_write_fasync,
690 };
691
692 static struct file_operations rdwr_pipe_fops = {
693         .llseek         = no_llseek,
694         .read           = pipe_read,
695         .readv          = pipe_readv,
696         .write          = pipe_write,
697         .writev         = pipe_writev,
698         .poll           = pipe_poll,
699         .ioctl          = pipe_ioctl,
700         .open           = pipe_rdwr_open,
701         .release        = pipe_rdwr_release,
702         .fasync         = pipe_rdwr_fasync,
703 };
704
705 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
706 {
707         struct pipe_inode_info *pipe;
708
709         pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
710         if (pipe) {
711                 init_waitqueue_head(&pipe->wait);
712                 pipe->r_counter = pipe->w_counter = 1;
713                 pipe->inode = inode;
714         }
715
716         return pipe;
717 }
718
719 void __free_pipe_info(struct pipe_inode_info *pipe)
720 {
721         int i;
722
723         for (i = 0; i < PIPE_BUFFERS; i++) {
724                 struct pipe_buffer *buf = pipe->bufs + i;
725                 if (buf->ops)
726                         buf->ops->release(pipe, buf);
727         }
728         if (pipe->tmp_page)
729                 __free_page(pipe->tmp_page);
730         kfree(pipe);
731 }
732
733 void free_pipe_info(struct inode *inode)
734 {
735         __free_pipe_info(inode->i_pipe);
736         inode->i_pipe = NULL;
737 }
738
739 static struct vfsmount *pipe_mnt __read_mostly;
740 static int pipefs_delete_dentry(struct dentry *dentry)
741 {
742         return 1;
743 }
744
745 static struct dentry_operations pipefs_dentry_operations = {
746         .d_delete       = pipefs_delete_dentry,
747 };
748
749 static struct inode * get_pipe_inode(void)
750 {
751         struct inode *inode = new_inode(pipe_mnt->mnt_sb);
752         struct pipe_inode_info *pipe;
753
754         if (!inode)
755                 goto fail_inode;
756
757         pipe = alloc_pipe_info(inode);
758         if (!pipe)
759                 goto fail_iput;
760         inode->i_pipe = pipe;
761
762         pipe->readers = pipe->writers = 1;
763         inode->i_fop = &rdwr_pipe_fops;
764
765         /*
766          * Mark the inode dirty from the very beginning,
767          * that way it will never be moved to the dirty
768          * list because "mark_inode_dirty()" will think
769          * that it already _is_ on the dirty list.
770          */
771         inode->i_state = I_DIRTY;
772         inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
773         inode->i_uid = current->fsuid;
774         inode->i_gid = current->fsgid;
775         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
776         inode->i_blksize = PAGE_SIZE;
777
778         return inode;
779
780 fail_iput:
781         iput(inode);
782
783 fail_inode:
784         return NULL;
785 }
786
787 int do_pipe(int *fd)
788 {
789         struct qstr this;
790         char name[32];
791         struct dentry *dentry;
792         struct inode * inode;
793         struct file *f1, *f2;
794         int error;
795         int i, j;
796
797         error = -ENFILE;
798         f1 = get_empty_filp();
799         if (!f1)
800                 goto no_files;
801
802         f2 = get_empty_filp();
803         if (!f2)
804                 goto close_f1;
805
806         inode = get_pipe_inode();
807         if (!inode)
808                 goto close_f12;
809
810         error = get_unused_fd();
811         if (error < 0)
812                 goto close_f12_inode;
813         i = error;
814
815         error = get_unused_fd();
816         if (error < 0)
817                 goto close_f12_inode_i;
818         j = error;
819
820         error = -ENOMEM;
821         sprintf(name, "[%lu]", inode->i_ino);
822         this.name = name;
823         this.len = strlen(name);
824         this.hash = inode->i_ino; /* will go */
825         dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
826         if (!dentry)
827                 goto close_f12_inode_i_j;
828
829         dentry->d_op = &pipefs_dentry_operations;
830         d_add(dentry, inode);
831         f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
832         f1->f_dentry = f2->f_dentry = dget(dentry);
833         f1->f_mapping = f2->f_mapping = inode->i_mapping;
834
835         /* read file */
836         f1->f_pos = f2->f_pos = 0;
837         f1->f_flags = O_RDONLY;
838         f1->f_op = &read_pipe_fops;
839         f1->f_mode = FMODE_READ;
840         f1->f_version = 0;
841
842         /* write file */
843         f2->f_flags = O_WRONLY;
844         f2->f_op = &write_pipe_fops;
845         f2->f_mode = FMODE_WRITE;
846         f2->f_version = 0;
847
848         fd_install(i, f1);
849         fd_install(j, f2);
850         fd[0] = i;
851         fd[1] = j;
852
853         return 0;
854
855 close_f12_inode_i_j:
856         put_unused_fd(j);
857 close_f12_inode_i:
858         put_unused_fd(i);
859 close_f12_inode:
860         free_pipe_info(inode);
861         iput(inode);
862 close_f12:
863         put_filp(f2);
864 close_f1:
865         put_filp(f1);
866 no_files:
867         return error;   
868 }
869
870 /*
871  * pipefs should _never_ be mounted by userland - too much of security hassle,
872  * no real gain from having the whole whorehouse mounted. So we don't need
873  * any operations on the root directory. However, we need a non-trivial
874  * d_name - pipe: will go nicely and kill the special-casing in procfs.
875  */
876
877 static struct super_block *
878 pipefs_get_sb(struct file_system_type *fs_type, int flags,
879               const char *dev_name, void *data)
880 {
881         return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
882 }
883
884 static struct file_system_type pipe_fs_type = {
885         .name           = "pipefs",
886         .get_sb         = pipefs_get_sb,
887         .kill_sb        = kill_anon_super,
888 };
889
890 static int __init init_pipe_fs(void)
891 {
892         int err = register_filesystem(&pipe_fs_type);
893
894         if (!err) {
895                 pipe_mnt = kern_mount(&pipe_fs_type);
896                 if (IS_ERR(pipe_mnt)) {
897                         err = PTR_ERR(pipe_mnt);
898                         unregister_filesystem(&pipe_fs_type);
899                 }
900         }
901         return err;
902 }
903
904 static void __exit exit_pipe_fs(void)
905 {
906         unregister_filesystem(&pipe_fs_type);
907         mntput(pipe_mnt);
908 }
909
910 fs_initcall(init_pipe_fs);
911 module_exit(exit_pipe_fs);