779 lines
19 KiB
Diff
779 lines
19 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Wed, 4 Nov 2026 01:24:02 -0500
|
|
Subject: [PATCH] refactor(pipe): select original FIFO and special inode
|
|
operations
|
|
|
|
---
|
|
fs/Makefile | 2 +-
|
|
fs/inode.c | 27 ----
|
|
fs/inode_cache_init.c | 27 ++++
|
|
fs/internal.h | 3 +
|
|
fs/pipe.c | 299 +-------------------------------------
|
|
fs/pipe_create.c | 328 ++++++++++++++++++++++++++++++++++++++++++
|
|
6 files changed, 361 insertions(+), 325 deletions(-)
|
|
create mode 100644 fs/pipe_create.c
|
|
|
|
diff --git a/fs/Makefile b/fs/Makefile
|
|
index 3e8dffadc3e2e1d88c1ce15ae3131f58cc70bf03..0d2b2aef99f9eadc52d86a99081910c8d1553826 100644
|
|
--- a/fs/Makefile
|
|
+++ b/fs/Makefile
|
|
@@ -8,7 +8,7 @@
|
|
|
|
|
|
obj-y := open.o path_open.o filp_close.o read_write.o file_position.o file_table.o file_table_init.o super.o super_anon_dev.o super_lifetime.o \
|
|
- char_dev.o stat.o exec.o task_comm.o pipe.o pipe_buffers.o namei.o path_get.o name_hash.o filename.o fcntl.o file_async.o \
|
|
+ char_dev.o stat.o exec.o task_comm.o pipe.o pipe_buffers.o pipe_create.o namei.o path_get.o name_hash.o filename.o fcntl.o file_async.o \
|
|
ioctl.o readdir.o select.o dcache.o dcache_init.o dentry_ancestry.o inode.o inode_cache_init.o inode_timestamps.o inode_evict.o \
|
|
attr.o bad_inode.o bad_inode_ops.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o namespace_tree.o mount_cache.o \
|
|
seq_file.o seq_lifetime.o seq_output.o seq_path.o xattr.o xattr_access.o simple_xattr.o libfs.o pseudo_metadata.o stashed_path.o fs-writeback.o \
|
|
diff --git a/fs/inode.c b/fs/inode.c
|
|
index cc2c12736a78f091e182f7a75e7a1b9d6adb4893..8e54af9fce627b1be41e55fd5d6b7e2b9ec5c338 100644
|
|
--- a/fs/inode.c
|
|
+++ b/fs/inode.c
|
|
@@ -1010,33 +1010,6 @@ static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked, bool
|
|
rcu_read_lock();
|
|
}
|
|
|
|
-void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
|
|
-{
|
|
- inode->i_mode = mode;
|
|
- switch (inode->i_mode & S_IFMT) {
|
|
- case S_IFCHR:
|
|
- inode->i_fop = &def_chr_fops;
|
|
- inode->i_rdev = rdev;
|
|
- break;
|
|
- case S_IFBLK:
|
|
- if (IS_ENABLED(CONFIG_BLOCK))
|
|
- inode->i_fop = &def_blk_fops;
|
|
- inode->i_rdev = rdev;
|
|
- break;
|
|
- case S_IFIFO:
|
|
- inode->i_fop = &pipefifo_fops;
|
|
- break;
|
|
- case S_IFSOCK:
|
|
- /* leave it no_open_fops */
|
|
- break;
|
|
- default:
|
|
- pr_debug("init_special_inode: bogus i_mode (%o) for inode %s:%llu\n",
|
|
- mode, inode->i_sb->s_id, inode->i_ino);
|
|
- break;
|
|
- }
|
|
-}
|
|
-EXPORT_SYMBOL(init_special_inode);
|
|
-
|
|
/*
|
|
* Direct i/o helper functions
|
|
*/
|
|
diff --git a/fs/inode_cache_init.c b/fs/inode_cache_init.c
|
|
index 6d4a3d8bd347eaae56511c62c7ff82e0eb2d8139..faae02803e26bb4f34836b243eb72b666084ac07 100644
|
|
--- a/fs/inode_cache_init.c
|
|
+++ b/fs/inode_cache_init.c
|
|
@@ -537,3 +537,30 @@ void inode_nohighmem(struct inode *inode)
|
|
mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
|
|
}
|
|
EXPORT_SYMBOL(inode_nohighmem);
|
|
+
|
|
+void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
|
|
+{
|
|
+ inode->i_mode = mode;
|
|
+ switch (inode->i_mode & S_IFMT) {
|
|
+ case S_IFCHR:
|
|
+ inode->i_fop = &def_chr_fops;
|
|
+ inode->i_rdev = rdev;
|
|
+ break;
|
|
+ case S_IFBLK:
|
|
+ if (IS_ENABLED(CONFIG_BLOCK))
|
|
+ inode->i_fop = &def_blk_fops;
|
|
+ inode->i_rdev = rdev;
|
|
+ break;
|
|
+ case S_IFIFO:
|
|
+ inode->i_fop = &pipefifo_fops;
|
|
+ break;
|
|
+ case S_IFSOCK:
|
|
+ /* leave it no_open_fops */
|
|
+ break;
|
|
+ default:
|
|
+ pr_debug("init_special_inode: bogus i_mode (%o) for inode %s:%llu\n",
|
|
+ mode, inode->i_sb->s_id, inode->i_ino);
|
|
+ break;
|
|
+ }
|
|
+}
|
|
+EXPORT_SYMBOL(init_special_inode);
|
|
diff --git a/fs/internal.h b/fs/internal.h
|
|
index 7aceabafc867512ee691540cab32577995a4bf39..3af80786e7bcbccce5a96a61df5f43a970effe32 100644
|
|
--- a/fs/internal.h
|
|
+++ b/fs/internal.h
|
|
@@ -274,6 +274,9 @@ extern struct dentry *__d_lookup_rcu(const struct dentry *parent,
|
|
* pipe.c
|
|
*/
|
|
extern const struct file_operations pipefifo_fops;
|
|
+extern const struct file_operations pipeanon_fops;
|
|
+struct pipe_inode_info;
|
|
+void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe);
|
|
|
|
/*
|
|
* fs_pin.c
|
|
diff --git a/fs/pipe.c b/fs/pipe.c
|
|
index c408ab91231da261458bdb3215434d34bb19d4ef..3f6845d90976d9a18e9ee9cb280ebcfbb49de6a6 100644
|
|
--- a/fs/pipe.c
|
|
+++ b/fs/pipe.c
|
|
@@ -664,7 +664,7 @@ pipe_poll(struct file *filp, poll_table *wait)
|
|
return mask;
|
|
}
|
|
|
|
-static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
|
|
+void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
|
|
{
|
|
int kill = 0;
|
|
|
|
@@ -722,201 +722,6 @@ pipe_fasync(int fd, struct file *filp, int on)
|
|
return retval;
|
|
}
|
|
|
|
-static struct vfsmount *pipe_mnt __ro_after_init;
|
|
-
|
|
-/*
|
|
- * pipefs_dname() is called from d_path().
|
|
- */
|
|
-static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
|
|
-{
|
|
- return dynamic_dname(buffer, buflen, "pipe:[%llu]",
|
|
- d_inode(dentry)->i_ino);
|
|
-}
|
|
-
|
|
-static const struct dentry_operations pipefs_dentry_operations = {
|
|
- .d_dname = pipefs_dname,
|
|
-};
|
|
-
|
|
-static const struct file_operations pipeanon_fops;
|
|
-
|
|
-static struct inode * get_pipe_inode(void)
|
|
-{
|
|
- struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
|
|
- struct pipe_inode_info *pipe;
|
|
-
|
|
- if (!inode)
|
|
- goto fail_inode;
|
|
-
|
|
- inode->i_ino = get_next_ino();
|
|
-
|
|
- pipe = alloc_pipe_info();
|
|
- if (!pipe)
|
|
- goto fail_iput;
|
|
-
|
|
- inode->i_pipe = pipe;
|
|
- pipe->files = 2;
|
|
- pipe->readers = pipe->writers = 1;
|
|
- inode->i_fop = &pipeanon_fops;
|
|
-
|
|
- /*
|
|
- * Mark the inode dirty from the very beginning,
|
|
- * that way it will never be moved to the dirty
|
|
- * list because "mark_inode_dirty()" will think
|
|
- * that it already _is_ on the dirty list.
|
|
- */
|
|
- inode_state_assign_raw(inode, I_DIRTY);
|
|
- inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
|
|
- inode->i_uid = current_fsuid();
|
|
- inode->i_gid = current_fsgid();
|
|
- simple_inode_init_ts(inode);
|
|
-
|
|
- return inode;
|
|
-
|
|
-fail_iput:
|
|
- iput(inode);
|
|
-
|
|
-fail_inode:
|
|
- return NULL;
|
|
-}
|
|
-
|
|
-int create_pipe_files(struct file **res, int flags)
|
|
-{
|
|
- struct inode *inode = get_pipe_inode();
|
|
- struct file *f;
|
|
- int error;
|
|
-
|
|
- if (!inode)
|
|
- return -ENFILE;
|
|
-
|
|
- if (flags & O_NOTIFICATION_PIPE) {
|
|
- error = watch_queue_init(inode->i_pipe);
|
|
- if (error) {
|
|
- free_pipe_info(inode->i_pipe);
|
|
- iput(inode);
|
|
- return error;
|
|
- }
|
|
- }
|
|
-
|
|
- f = alloc_file_pseudo(inode, pipe_mnt, "",
|
|
- O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
|
|
- &pipeanon_fops);
|
|
- if (IS_ERR(f)) {
|
|
- free_pipe_info(inode->i_pipe);
|
|
- iput(inode);
|
|
- return PTR_ERR(f);
|
|
- }
|
|
-
|
|
- f->private_data = inode->i_pipe;
|
|
- f->f_pipe = 0;
|
|
-
|
|
- res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
|
|
- &pipeanon_fops);
|
|
- if (IS_ERR(res[0])) {
|
|
- put_pipe_info(inode, inode->i_pipe);
|
|
- fput(f);
|
|
- return PTR_ERR(res[0]);
|
|
- }
|
|
- res[0]->private_data = inode->i_pipe;
|
|
- res[0]->f_pipe = 0;
|
|
- res[1] = f;
|
|
- stream_open(inode, res[0]);
|
|
- stream_open(inode, res[1]);
|
|
-
|
|
- /* pipe groks IOCB_NOWAIT */
|
|
- res[0]->f_mode |= FMODE_NOWAIT;
|
|
- res[1]->f_mode |= FMODE_NOWAIT;
|
|
-
|
|
- /*
|
|
- * Disable permission and pre-content events, but enable legacy
|
|
- * inotify events for legacy users.
|
|
- */
|
|
- file_set_fsnotify_mode(res[0], FMODE_NONOTIFY_PERM);
|
|
- file_set_fsnotify_mode(res[1], FMODE_NONOTIFY_PERM);
|
|
- return 0;
|
|
-}
|
|
-
|
|
-static int __do_pipe_flags(int *fd, struct file **files, int flags)
|
|
-{
|
|
- int error;
|
|
- int fdw, fdr;
|
|
-
|
|
- if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
|
|
- return -EINVAL;
|
|
-
|
|
- error = create_pipe_files(files, flags);
|
|
- if (error)
|
|
- return error;
|
|
-
|
|
- 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[0] = fdr;
|
|
- fd[1] = fdw;
|
|
- return 0;
|
|
-
|
|
- err_fdr:
|
|
- put_unused_fd(fdr);
|
|
- err_read_pipe:
|
|
- fput(files[0]);
|
|
- fput(files[1]);
|
|
- return error;
|
|
-}
|
|
-
|
|
-int do_pipe_flags(int *fd, int flags)
|
|
-{
|
|
- struct file *files[2];
|
|
- int error = __do_pipe_flags(fd, files, flags);
|
|
- if (!error) {
|
|
- fd_install(fd[0], files[0]);
|
|
- fd_install(fd[1], files[1]);
|
|
- }
|
|
- return error;
|
|
-}
|
|
-
|
|
-/*
|
|
- * sys_pipe() is the normal C calling standard for creating
|
|
- * a pipe. It's not the way Unix traditionally does this, though.
|
|
- */
|
|
-static int do_pipe2(int __user *fildes, int flags)
|
|
-{
|
|
- struct file *files[2];
|
|
- int fd[2];
|
|
- int error;
|
|
-
|
|
- error = __do_pipe_flags(fd, files, flags);
|
|
- if (!error) {
|
|
- if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
|
|
- fput(files[0]);
|
|
- fput(files[1]);
|
|
- put_unused_fd(fd[0]);
|
|
- put_unused_fd(fd[1]);
|
|
- error = -EFAULT;
|
|
- } else {
|
|
- fd_install(fd[0], files[0]);
|
|
- fd_install(fd[1], files[1]);
|
|
- }
|
|
- }
|
|
- return error;
|
|
-}
|
|
-
|
|
-SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
|
|
-{
|
|
- return do_pipe2(fildes, flags);
|
|
-}
|
|
-
|
|
-SYSCALL_DEFINE1(pipe, int __user *, fildes)
|
|
-{
|
|
- return do_pipe2(fildes, 0);
|
|
-}
|
|
-
|
|
/*
|
|
* This depends on both the wait (here) and the wakeup (wake_up_partner)
|
|
* holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
|
|
@@ -1087,7 +892,7 @@ const struct file_operations pipefifo_fops = {
|
|
.splice_write = iter_file_splice_write,
|
|
};
|
|
|
|
-static const struct file_operations pipeanon_fops = {
|
|
+const struct file_operations pipeanon_fops = {
|
|
.open = fifo_open,
|
|
.read_iter = anon_pipe_read,
|
|
.write_iter = anon_pipe_write,
|
|
@@ -1283,103 +1088,3 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg)
|
|
mutex_unlock(&pipe->mutex);
|
|
return ret;
|
|
}
|
|
-
|
|
-static const struct super_operations pipefs_ops = {
|
|
- .destroy_inode = free_inode_nonrcu,
|
|
- .statfs = simple_statfs,
|
|
-};
|
|
-
|
|
-/*
|
|
- * pipefs should _never_ be mounted by userland - too much of security hassle,
|
|
- * no real gain from having the whole file system mounted. So we don't need
|
|
- * any operations on the root directory. However, we need a non-trivial
|
|
- * d_name - pipe: will go nicely and kill the special-casing in procfs.
|
|
- */
|
|
-
|
|
-static int pipefs_init_fs_context(struct fs_context *fc)
|
|
-{
|
|
- struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
|
|
- if (!ctx)
|
|
- return -ENOMEM;
|
|
- ctx->ops = &pipefs_ops;
|
|
- ctx->dops = &pipefs_dentry_operations;
|
|
- return 0;
|
|
-}
|
|
-
|
|
-static struct file_system_type pipe_fs_type = {
|
|
- .name = "pipefs",
|
|
- .init_fs_context = pipefs_init_fs_context,
|
|
- .kill_sb = kill_anon_super,
|
|
-};
|
|
-
|
|
-#ifdef CONFIG_SYSCTL
|
|
-
|
|
-static ulong round_pipe_size_ul(ulong size)
|
|
-{
|
|
- return round_pipe_size(size);
|
|
-}
|
|
-
|
|
-static int u2k_pipe_maxsz(const ulong *u_ptr, uint *k_ptr)
|
|
-{
|
|
- return proc_uint_u2k_conv_uop(u_ptr, k_ptr, round_pipe_size_ul);
|
|
-}
|
|
-
|
|
-static int do_proc_uint_conv_pipe_maxsz(ulong *u_ptr, uint *k_ptr,
|
|
- int dir, const struct ctl_table *table)
|
|
-{
|
|
- return proc_uint_conv(u_ptr, k_ptr, dir, table, true,
|
|
- u2k_pipe_maxsz,
|
|
- proc_uint_k2u_conv);
|
|
-}
|
|
-
|
|
-static int proc_dopipe_max_size(const struct ctl_table *table, int write,
|
|
- void *buffer, size_t *lenp, loff_t *ppos)
|
|
-{
|
|
- return proc_douintvec_conv(table, write, buffer, lenp, ppos,
|
|
- do_proc_uint_conv_pipe_maxsz);
|
|
-}
|
|
-
|
|
-static const struct ctl_table fs_pipe_sysctls[] = {
|
|
- {
|
|
- .procname = "pipe-max-size",
|
|
- .data = &pipe_max_size,
|
|
- .maxlen = sizeof(pipe_max_size),
|
|
- .mode = 0644,
|
|
- .proc_handler = proc_dopipe_max_size,
|
|
- .extra1 = SYSCTL_ONE,
|
|
- },
|
|
- {
|
|
- .procname = "pipe-user-pages-hard",
|
|
- .data = &pipe_user_pages_hard,
|
|
- .maxlen = sizeof(pipe_user_pages_hard),
|
|
- .mode = 0644,
|
|
- .proc_handler = proc_doulongvec_minmax,
|
|
- },
|
|
- {
|
|
- .procname = "pipe-user-pages-soft",
|
|
- .data = &pipe_user_pages_soft,
|
|
- .maxlen = sizeof(pipe_user_pages_soft),
|
|
- .mode = 0644,
|
|
- .proc_handler = proc_doulongvec_minmax,
|
|
- },
|
|
-};
|
|
-#endif
|
|
-
|
|
-static int __init init_pipe_fs(void)
|
|
-{
|
|
- int err = register_filesystem(&pipe_fs_type);
|
|
-
|
|
- if (!err) {
|
|
- pipe_mnt = kern_mount(&pipe_fs_type);
|
|
- if (IS_ERR(pipe_mnt)) {
|
|
- err = PTR_ERR(pipe_mnt);
|
|
- unregister_filesystem(&pipe_fs_type);
|
|
- }
|
|
- }
|
|
-#ifdef CONFIG_SYSCTL
|
|
- register_sysctl_init("fs", fs_pipe_sysctls);
|
|
-#endif
|
|
- return err;
|
|
-}
|
|
-
|
|
-fs_initcall(init_pipe_fs);
|
|
diff --git a/fs/pipe_create.c b/fs/pipe_create.c
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..5cbe6c80607a6f73519c9fae3719f5201dd9ceb2
|
|
--- /dev/null
|
|
+++ b/fs/pipe_create.c
|
|
@@ -0,0 +1,328 @@
|
|
+// SPDX-License-Identifier: GPL-2.0
|
|
+/*
|
|
+ * linux/fs/pipe.c
|
|
+ *
|
|
+ * Copyright (C) 1991, 1992, 1999 Linus Torvalds
|
|
+ */
|
|
+
|
|
+#include <linux/mm.h>
|
|
+#include <linux/file.h>
|
|
+#include <linux/poll.h>
|
|
+#include <linux/slab.h>
|
|
+#include <linux/module.h>
|
|
+#include <linux/init.h>
|
|
+#include <linux/fs.h>
|
|
+#include <linux/log2.h>
|
|
+#include <linux/mount.h>
|
|
+#include <linux/pseudo_fs.h>
|
|
+#include <linux/magic.h>
|
|
+#include <linux/pipe_fs_i.h>
|
|
+#include <linux/uio.h>
|
|
+#include <linux/highmem.h>
|
|
+#include <linux/pagemap.h>
|
|
+#include <linux/audit.h>
|
|
+#include <linux/syscalls.h>
|
|
+#include <linux/fcntl.h>
|
|
+#include <linux/memcontrol.h>
|
|
+#include <linux/watch_queue.h>
|
|
+#include <linux/sysctl.h>
|
|
+#include <linux/sort.h>
|
|
+
|
|
+#include <linux/uaccess.h>
|
|
+#include <asm/ioctls.h>
|
|
+
|
|
+#include "internal.h"
|
|
+#include "pipe_buffers.h"
|
|
+
|
|
+static struct vfsmount *pipe_mnt __ro_after_init;
|
|
+
|
|
+/*
|
|
+ * pipefs_dname() is called from d_path().
|
|
+ */
|
|
+static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
|
|
+{
|
|
+ return dynamic_dname(buffer, buflen, "pipe:[%llu]",
|
|
+ d_inode(dentry)->i_ino);
|
|
+}
|
|
+
|
|
+static const struct dentry_operations pipefs_dentry_operations = {
|
|
+ .d_dname = pipefs_dname,
|
|
+};
|
|
+
|
|
+static struct inode * get_pipe_inode(void)
|
|
+{
|
|
+ struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
|
|
+ struct pipe_inode_info *pipe;
|
|
+
|
|
+ if (!inode)
|
|
+ goto fail_inode;
|
|
+
|
|
+ inode->i_ino = get_next_ino();
|
|
+
|
|
+ pipe = alloc_pipe_info();
|
|
+ if (!pipe)
|
|
+ goto fail_iput;
|
|
+
|
|
+ inode->i_pipe = pipe;
|
|
+ pipe->files = 2;
|
|
+ pipe->readers = pipe->writers = 1;
|
|
+ inode->i_fop = &pipeanon_fops;
|
|
+
|
|
+ /*
|
|
+ * Mark the inode dirty from the very beginning,
|
|
+ * that way it will never be moved to the dirty
|
|
+ * list because "mark_inode_dirty()" will think
|
|
+ * that it already _is_ on the dirty list.
|
|
+ */
|
|
+ inode_state_assign_raw(inode, I_DIRTY);
|
|
+ inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
|
|
+ inode->i_uid = current_fsuid();
|
|
+ inode->i_gid = current_fsgid();
|
|
+ simple_inode_init_ts(inode);
|
|
+
|
|
+ return inode;
|
|
+
|
|
+fail_iput:
|
|
+ iput(inode);
|
|
+
|
|
+fail_inode:
|
|
+ return NULL;
|
|
+}
|
|
+
|
|
+int create_pipe_files(struct file **res, int flags)
|
|
+{
|
|
+ struct inode *inode = get_pipe_inode();
|
|
+ struct file *f;
|
|
+ int error;
|
|
+
|
|
+ if (!inode)
|
|
+ return -ENFILE;
|
|
+
|
|
+ if (flags & O_NOTIFICATION_PIPE) {
|
|
+ error = watch_queue_init(inode->i_pipe);
|
|
+ if (error) {
|
|
+ free_pipe_info(inode->i_pipe);
|
|
+ iput(inode);
|
|
+ return error;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ f = alloc_file_pseudo(inode, pipe_mnt, "",
|
|
+ O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
|
|
+ &pipeanon_fops);
|
|
+ if (IS_ERR(f)) {
|
|
+ free_pipe_info(inode->i_pipe);
|
|
+ iput(inode);
|
|
+ return PTR_ERR(f);
|
|
+ }
|
|
+
|
|
+ f->private_data = inode->i_pipe;
|
|
+ f->f_pipe = 0;
|
|
+
|
|
+ res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
|
|
+ &pipeanon_fops);
|
|
+ if (IS_ERR(res[0])) {
|
|
+ put_pipe_info(inode, inode->i_pipe);
|
|
+ fput(f);
|
|
+ return PTR_ERR(res[0]);
|
|
+ }
|
|
+ res[0]->private_data = inode->i_pipe;
|
|
+ res[0]->f_pipe = 0;
|
|
+ res[1] = f;
|
|
+ stream_open(inode, res[0]);
|
|
+ stream_open(inode, res[1]);
|
|
+
|
|
+ /* pipe groks IOCB_NOWAIT */
|
|
+ res[0]->f_mode |= FMODE_NOWAIT;
|
|
+ res[1]->f_mode |= FMODE_NOWAIT;
|
|
+
|
|
+ /*
|
|
+ * Disable permission and pre-content events, but enable legacy
|
|
+ * inotify events for legacy users.
|
|
+ */
|
|
+ file_set_fsnotify_mode(res[0], FMODE_NONOTIFY_PERM);
|
|
+ file_set_fsnotify_mode(res[1], FMODE_NONOTIFY_PERM);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int __do_pipe_flags(int *fd, struct file **files, int flags)
|
|
+{
|
|
+ int error;
|
|
+ int fdw, fdr;
|
|
+
|
|
+ if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
|
|
+ return -EINVAL;
|
|
+
|
|
+ error = create_pipe_files(files, flags);
|
|
+ if (error)
|
|
+ return error;
|
|
+
|
|
+ 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[0] = fdr;
|
|
+ fd[1] = fdw;
|
|
+ return 0;
|
|
+
|
|
+ err_fdr:
|
|
+ put_unused_fd(fdr);
|
|
+ err_read_pipe:
|
|
+ fput(files[0]);
|
|
+ fput(files[1]);
|
|
+ return error;
|
|
+}
|
|
+
|
|
+int do_pipe_flags(int *fd, int flags)
|
|
+{
|
|
+ struct file *files[2];
|
|
+ int error = __do_pipe_flags(fd, files, flags);
|
|
+ if (!error) {
|
|
+ fd_install(fd[0], files[0]);
|
|
+ fd_install(fd[1], files[1]);
|
|
+ }
|
|
+ return error;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * sys_pipe() is the normal C calling standard for creating
|
|
+ * a pipe. It's not the way Unix traditionally does this, though.
|
|
+ */
|
|
+static int do_pipe2(int __user *fildes, int flags)
|
|
+{
|
|
+ struct file *files[2];
|
|
+ int fd[2];
|
|
+ int error;
|
|
+
|
|
+ error = __do_pipe_flags(fd, files, flags);
|
|
+ if (!error) {
|
|
+ if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
|
|
+ fput(files[0]);
|
|
+ fput(files[1]);
|
|
+ put_unused_fd(fd[0]);
|
|
+ put_unused_fd(fd[1]);
|
|
+ error = -EFAULT;
|
|
+ } else {
|
|
+ fd_install(fd[0], files[0]);
|
|
+ fd_install(fd[1], files[1]);
|
|
+ }
|
|
+ }
|
|
+ return error;
|
|
+}
|
|
+
|
|
+SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
|
|
+{
|
|
+ return do_pipe2(fildes, flags);
|
|
+}
|
|
+
|
|
+SYSCALL_DEFINE1(pipe, int __user *, fildes)
|
|
+{
|
|
+ return do_pipe2(fildes, 0);
|
|
+}
|
|
+
|
|
+static const struct super_operations pipefs_ops = {
|
|
+ .destroy_inode = free_inode_nonrcu,
|
|
+ .statfs = simple_statfs,
|
|
+};
|
|
+
|
|
+/*
|
|
+ * pipefs should _never_ be mounted by userland - too much of security hassle,
|
|
+ * no real gain from having the whole file system mounted. So we don't need
|
|
+ * any operations on the root directory. However, we need a non-trivial
|
|
+ * d_name - pipe: will go nicely and kill the special-casing in procfs.
|
|
+ */
|
|
+
|
|
+static int pipefs_init_fs_context(struct fs_context *fc)
|
|
+{
|
|
+ struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
|
|
+ if (!ctx)
|
|
+ return -ENOMEM;
|
|
+ ctx->ops = &pipefs_ops;
|
|
+ ctx->dops = &pipefs_dentry_operations;
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static struct file_system_type pipe_fs_type = {
|
|
+ .name = "pipefs",
|
|
+ .init_fs_context = pipefs_init_fs_context,
|
|
+ .kill_sb = kill_anon_super,
|
|
+};
|
|
+
|
|
+#ifdef CONFIG_SYSCTL
|
|
+
|
|
+static ulong round_pipe_size_ul(ulong size)
|
|
+{
|
|
+ return round_pipe_size(size);
|
|
+}
|
|
+
|
|
+static int u2k_pipe_maxsz(const ulong *u_ptr, uint *k_ptr)
|
|
+{
|
|
+ return proc_uint_u2k_conv_uop(u_ptr, k_ptr, round_pipe_size_ul);
|
|
+}
|
|
+
|
|
+static int do_proc_uint_conv_pipe_maxsz(ulong *u_ptr, uint *k_ptr,
|
|
+ int dir, const struct ctl_table *table)
|
|
+{
|
|
+ return proc_uint_conv(u_ptr, k_ptr, dir, table, true,
|
|
+ u2k_pipe_maxsz,
|
|
+ proc_uint_k2u_conv);
|
|
+}
|
|
+
|
|
+static int proc_dopipe_max_size(const struct ctl_table *table, int write,
|
|
+ void *buffer, size_t *lenp, loff_t *ppos)
|
|
+{
|
|
+ return proc_douintvec_conv(table, write, buffer, lenp, ppos,
|
|
+ do_proc_uint_conv_pipe_maxsz);
|
|
+}
|
|
+
|
|
+static const struct ctl_table fs_pipe_sysctls[] = {
|
|
+ {
|
|
+ .procname = "pipe-max-size",
|
|
+ .data = &pipe_max_size,
|
|
+ .maxlen = sizeof(pipe_max_size),
|
|
+ .mode = 0644,
|
|
+ .proc_handler = proc_dopipe_max_size,
|
|
+ .extra1 = SYSCTL_ONE,
|
|
+ },
|
|
+ {
|
|
+ .procname = "pipe-user-pages-hard",
|
|
+ .data = &pipe_user_pages_hard,
|
|
+ .maxlen = sizeof(pipe_user_pages_hard),
|
|
+ .mode = 0644,
|
|
+ .proc_handler = proc_doulongvec_minmax,
|
|
+ },
|
|
+ {
|
|
+ .procname = "pipe-user-pages-soft",
|
|
+ .data = &pipe_user_pages_soft,
|
|
+ .maxlen = sizeof(pipe_user_pages_soft),
|
|
+ .mode = 0644,
|
|
+ .proc_handler = proc_doulongvec_minmax,
|
|
+ },
|
|
+};
|
|
+#endif
|
|
+
|
|
+static int __init init_pipe_fs(void)
|
|
+{
|
|
+ int err = register_filesystem(&pipe_fs_type);
|
|
+
|
|
+ if (!err) {
|
|
+ pipe_mnt = kern_mount(&pipe_fs_type);
|
|
+ if (IS_ERR(pipe_mnt)) {
|
|
+ err = PTR_ERR(pipe_mnt);
|
|
+ unregister_filesystem(&pipe_fs_type);
|
|
+ }
|
|
+ }
|
|
+#ifdef CONFIG_SYSCTL
|
|
+ register_sysctl_init("fs", fs_pipe_sysctls);
|
|
+#endif
|
|
+ return err;
|
|
+}
|
|
+
|
|
+fs_initcall(init_pipe_fs);
|