]> err.no Git - linux-2.6/blobdiff - fs/pipe.c
[PATCH] introduce a "kernel-internal pipe object" abstraction
[linux-2.6] / fs / pipe.c
index d976866a115b8e0b975e3cea3c7ccdecb74a6d12..705b4869262765c17124b2c361359686dce6e202 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -15,6 +15,7 @@
 #include <linux/pipe_fs_i.h>
 #include <linux/uio.h>
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include <asm/uaccess.h>
 #include <asm/ioctls.h>
@@ -35,7 +36,7 @@
  */
 
 /* Drop the inode semaphore and wait for a pipe event, atomically */
-void pipe_wait(struct inode * inode)
+void pipe_wait(struct pipe_inode_info *pipe)
 {
        DEFINE_WAIT(wait);
 
@@ -43,11 +44,13 @@ void pipe_wait(struct inode * inode)
         * Pipes are system-local resources, so sleeping on them
         * is considered a noninteractive wait:
         */
-       prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
-       mutex_unlock(PIPE_MUTEX(*inode));
+       prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
+       if (pipe->inode)
+               mutex_unlock(&pipe->inode->i_mutex);
        schedule();
-       finish_wait(PIPE_WAIT(*inode), &wait);
-       mutex_lock(PIPE_MUTEX(*inode));
+       finish_wait(&pipe->wait, &wait);
+       if (pipe->inode)
+               mutex_lock(&pipe->inode->i_mutex);
 }
 
 static int
@@ -94,11 +97,22 @@ static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buff
 {
        struct page *page = buf->page;
 
-       if (info->tmp_page) {
-               __free_page(page);
+       buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
+
+       /*
+        * If nobody else uses this page, and we don't already have a
+        * temporary page, let's keep track of it as a one-deep
+        * allocation cache
+        */
+       if (page_count(page) == 1 && !info->tmp_page) {
+               info->tmp_page = page;
                return;
        }
-       info->tmp_page = page;
+
+       /*
+        * Otherwise just release our reference to it
+        */
+       page_cache_release(page);
 }
 
 static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf)
@@ -111,11 +125,19 @@ static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer
        kunmap(buf->page);
 }
 
+static int anon_pipe_buf_steal(struct pipe_inode_info *info,
+                              struct pipe_buffer *buf)
+{
+       buf->flags |= PIPE_BUF_FLAG_STOLEN;
+       return 0;
+}
+
 static struct pipe_buf_operations anon_pipe_buf_ops = {
        .can_merge = 1,
        .map = anon_pipe_buf_map,
        .unmap = anon_pipe_buf_unmap,
        .release = anon_pipe_buf_release,
+       .steal = anon_pipe_buf_steal,
 };
 
 static ssize_t
@@ -152,6 +174,11 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
                                chars = total_len;
 
                        addr = ops->map(filp, info, buf);
+                       if (IS_ERR(addr)) {
+                               if (!ret)
+                                       ret = PTR_ERR(addr);
+                               break;
+                       }
                        error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
                        ops->unmap(info, buf);
                        if (unlikely(error)) {
@@ -198,7 +225,7 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
                        wake_up_interruptible_sync(PIPE_WAIT(*inode));
                        kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
                }
-               pipe_wait(inode);
+               pipe_wait(inode->i_pipe);
        }
        mutex_unlock(PIPE_MUTEX(*inode));
        /* Signal writers asynchronously that there is more room.  */
@@ -254,8 +281,16 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
                struct pipe_buf_operations *ops = buf->ops;
                int offset = buf->offset + buf->len;
                if (ops->can_merge && offset + chars <= PAGE_SIZE) {
-                       void *addr = ops->map(filp, info, buf);
-                       int error = pipe_iov_copy_from_user(offset + addr, iov, chars);
+                       void *addr;
+                       int error;
+
+                       addr = ops->map(filp, info, buf);
+                       if (IS_ERR(addr)) {
+                               error = PTR_ERR(addr);
+                               goto out;
+                       }
+                       error = pipe_iov_copy_from_user(offset + addr, iov,
+                                                       chars);
                        ops->unmap(info, buf);
                        ret = error;
                        do_wakeup = 1;
@@ -337,7 +372,7 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
                        do_wakeup = 0;
                }
                PIPE_WAITING_WRITERS(*inode)++;
-               pipe_wait(inode);
+               pipe_wait(inode->i_pipe);
                PIPE_WAITING_WRITERS(*inode)--;
        }
 out:
@@ -568,7 +603,7 @@ pipe_rdwr_open(struct inode *inode, struct file *filp)
  * The file_operations structs are not static because they
  * are also used in linux/fs/fifo.c to do operations on FIFOs.
  */
-struct file_operations read_fifo_fops = {
+const struct file_operations read_fifo_fops = {
        .llseek         = no_llseek,
        .read           = pipe_read,
        .readv          = pipe_readv,
@@ -580,7 +615,7 @@ struct file_operations read_fifo_fops = {
        .fasync         = pipe_read_fasync,
 };
 
-struct file_operations write_fifo_fops = {
+const struct file_operations write_fifo_fops = {
        .llseek         = no_llseek,
        .read           = bad_pipe_r,
        .write          = pipe_write,
@@ -592,7 +627,7 @@ struct file_operations write_fifo_fops = {
        .fasync         = pipe_write_fasync,
 };
 
-struct file_operations rdwr_fifo_fops = {
+const struct file_operations rdwr_fifo_fops = {
        .llseek         = no_llseek,
        .read           = pipe_read,
        .readv          = pipe_readv,
@@ -642,6 +677,20 @@ static struct file_operations rdwr_pipe_fops = {
        .fasync         = pipe_rdwr_fasync,
 };
 
+struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
+{
+       struct pipe_inode_info *info;
+
+       info = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
+       if (info) {
+               init_waitqueue_head(&info->wait);
+               info->r_counter = info->w_counter = 1;
+               info->inode = inode;
+       }
+
+       return info;
+}
+
 void free_pipe_info(struct inode *inode)
 {
        int i;
@@ -658,24 +707,7 @@ void free_pipe_info(struct inode *inode)
        kfree(info);
 }
 
-struct inode* pipe_new(struct inode* inode)
-{
-       struct pipe_inode_info *info;
-
-       info = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
-       if (!info)
-               goto fail_page;
-       inode->i_pipe = info;
-
-       init_waitqueue_head(PIPE_WAIT(*inode));
-       PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
-
-       return inode;
-fail_page:
-       return NULL;
-}
-
-static struct vfsmount *pipe_mnt;
+static struct vfsmount *pipe_mnt __read_mostly;
 static int pipefs_delete_dentry(struct dentry *dentry)
 {
        return 1;
@@ -691,8 +723,10 @@ static struct inode * get_pipe_inode(void)
        if (!inode)
                goto fail_inode;
 
-       if(!pipe_new(inode))
+       inode->i_pipe = alloc_pipe_info(inode);
+       if (!inode->i_pipe)
                goto fail_iput;
+
        PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
        inode->i_fop = &rdwr_pipe_fops;