+ err:
+ return ERR_PTR(err);
+}
+
+void free_write_pipe(struct file *f)
+{
+ free_pipe_info(f->f_dentry->d_inode);
+ path_put(&f->f_path);
+ put_filp(f);
+}
+
+struct file *create_read_pipe(struct file *wrf, int flags)
+{
+ struct file *f = get_empty_filp();
+ if (!f)
+ return ERR_PTR(-ENFILE);
+
+ /* Grab pipe from the writer */
+ f->f_path = wrf->f_path;
+ path_get(&wrf->f_path);
+ f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
+
+ f->f_pos = 0;
+ f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
+ f->f_op = &read_pipefifo_fops;
+ f->f_mode = FMODE_READ;
+ f->f_version = 0;
+
+ return f;
+}
+
+int do_pipe_flags(int *fd, int flags)
+{
+ struct file *fw, *fr;
+ int error;
+ int fdw, fdr;
+
+ if (flags & ~(O_CLOEXEC | O_NONBLOCK))
+ return -EINVAL;
+
+ fw = create_write_pipe(flags);
+ if (IS_ERR(fw))
+ return PTR_ERR(fw);
+ fr = create_read_pipe(fw, flags);
+ error = PTR_ERR(fr);
+ if (IS_ERR(fr))
+ goto err_write_pipe;
+
+ error = get_unused_fd_flags(flags);
+ if (error < 0)
+ goto err_read_pipe;
+ fdr = error;
+
+ error = get_unused_fd_flags(flags);
+ if (error < 0)
+ goto err_fdr;
+ fdw = error;
+
+ audit_fd_pair(fdr, fdw);
+ fd_install(fdr, fr);
+ fd_install(fdw, fw);
+ fd[0] = fdr;
+ fd[1] = fdw;
+
+ return 0;
+
+ err_fdr:
+ put_unused_fd(fdr);
+ err_read_pipe:
+ path_put(&fr->f_path);
+ put_filp(fr);
+ err_write_pipe:
+ free_write_pipe(fw);
+ return error;
+}
+
+int do_pipe(int *fd)
+{
+ return do_pipe_flags(fd, 0);
+}
+
+/*
+ * sys_pipe() is the normal C calling standard for creating
+ * a pipe. It's not the way Unix traditionally does this, though.
+ */
+SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
+{
+ int fd[2];
+ int error;
+
+ error = do_pipe_flags(fd, flags);
+ if (!error) {
+ if (copy_to_user(fildes, fd, sizeof(fd))) {
+ sys_close(fd[0]);
+ sys_close(fd[1]);
+ error = -EFAULT;
+ }
+ }
+ return error;
+}
+
+SYSCALL_DEFINE1(pipe, int __user *, fildes)
+{
+ return sys_pipe2(fildes, 0);