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