Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35f3d14

Browse files
author
Jens Axboe
committed
pipe: add support for shrinking and growing pipes
This patch adds F_GETPIPE_SZ and F_SETPIPE_SZ fcntl() actions for growing and shrinking the size of a pipe and adjusts pipe.c and splice.c (and relay and network splice) usage to work with these larger (or smaller) pipes. Signed-off-by: Jens Axboe <[email protected]>
1 parent 3d42b36 commit 35f3d14

9 files changed

Lines changed: 292 additions & 108 deletions

File tree

fs/fcntl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/dnotify.h>
1515
#include <linux/slab.h>
1616
#include <linux/module.h>
17+
#include <linux/pipe_fs_i.h>
1718
#include <linux/security.h>
1819
#include <linux/ptrace.h>
1920
#include <linux/signal.h>
@@ -412,6 +413,10 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
412413
case F_NOTIFY:
413414
err = fcntl_dirnotify(fd, filp, arg);
414415
break;
416+
case F_SETPIPE_SZ:
417+
case F_GETPIPE_SZ:
418+
err = pipe_fcntl(filp, cmd, arg);
419+
break;
415420
default:
416421
break;
417422
}

fs/pipe.c

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/module.h>
1212
#include <linux/init.h>
1313
#include <linux/fs.h>
14+
#include <linux/log2.h>
1415
#include <linux/mount.h>
1516
#include <linux/pipe_fs_i.h>
1617
#include <linux/uio.h>
@@ -390,7 +391,7 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov,
390391
if (!buf->len) {
391392
buf->ops = NULL;
392393
ops->release(pipe, buf);
393-
curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
394+
curbuf = (curbuf + 1) & (pipe->buffers - 1);
394395
pipe->curbuf = curbuf;
395396
pipe->nrbufs = --bufs;
396397
do_wakeup = 1;
@@ -472,7 +473,7 @@ pipe_write(struct kiocb *iocb, const struct iovec *_iov,
472473
chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
473474
if (pipe->nrbufs && chars != 0) {
474475
int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
475-
(PIPE_BUFFERS-1);
476+
(pipe->buffers - 1);
476477
struct pipe_buffer *buf = pipe->bufs + lastbuf;
477478
const struct pipe_buf_operations *ops = buf->ops;
478479
int offset = buf->offset + buf->len;
@@ -518,8 +519,8 @@ pipe_write(struct kiocb *iocb, const struct iovec *_iov,
518519
break;
519520
}
520521
bufs = pipe->nrbufs;
521-
if (bufs < PIPE_BUFFERS) {
522-
int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
522+
if (bufs < pipe->buffers) {
523+
int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
523524
struct pipe_buffer *buf = pipe->bufs + newbuf;
524525
struct page *page = pipe->tmp_page;
525526
char *src;
@@ -580,7 +581,7 @@ pipe_write(struct kiocb *iocb, const struct iovec *_iov,
580581
if (!total_len)
581582
break;
582583
}
583-
if (bufs < PIPE_BUFFERS)
584+
if (bufs < pipe->buffers)
584585
continue;
585586
if (filp->f_flags & O_NONBLOCK) {
586587
if (!ret)
@@ -640,7 +641,7 @@ static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
640641
nrbufs = pipe->nrbufs;
641642
while (--nrbufs >= 0) {
642643
count += pipe->bufs[buf].len;
643-
buf = (buf+1) & (PIPE_BUFFERS-1);
644+
buf = (buf+1) & (pipe->buffers - 1);
644645
}
645646
mutex_unlock(&inode->i_mutex);
646647

@@ -671,7 +672,7 @@ pipe_poll(struct file *filp, poll_table *wait)
671672
}
672673

673674
if (filp->f_mode & FMODE_WRITE) {
674-
mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
675+
mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
675676
/*
676677
* Most Unices do not set POLLERR for FIFOs but on Linux they
677678
* behave exactly like pipes for poll().
@@ -877,25 +878,32 @@ struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
877878

878879
pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
879880
if (pipe) {
880-
init_waitqueue_head(&pipe->wait);
881-
pipe->r_counter = pipe->w_counter = 1;
882-
pipe->inode = inode;
881+
pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
882+
if (pipe->bufs) {
883+
init_waitqueue_head(&pipe->wait);
884+
pipe->r_counter = pipe->w_counter = 1;
885+
pipe->inode = inode;
886+
pipe->buffers = PIPE_DEF_BUFFERS;
887+
return pipe;
888+
}
889+
kfree(pipe);
883890
}
884891

885-
return pipe;
892+
return NULL;
886893
}
887894

888895
void __free_pipe_info(struct pipe_inode_info *pipe)
889896
{
890897
int i;
891898

892-
for (i = 0; i < PIPE_BUFFERS; i++) {
899+
for (i = 0; i < pipe->buffers; i++) {
893900
struct pipe_buffer *buf = pipe->bufs + i;
894901
if (buf->ops)
895902
buf->ops->release(pipe, buf);
896903
}
897904
if (pipe->tmp_page)
898905
__free_page(pipe->tmp_page);
906+
kfree(pipe->bufs);
899907
kfree(pipe);
900908
}
901909

@@ -1093,6 +1101,81 @@ SYSCALL_DEFINE1(pipe, int __user *, fildes)
10931101
return sys_pipe2(fildes, 0);
10941102
}
10951103

1104+
/*
1105+
* Allocate a new array of pipe buffers and copy the info over. Returns the
1106+
* pipe size if successful, or return -ERROR on error.
1107+
*/
1108+
static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1109+
{
1110+
struct pipe_buffer *bufs;
1111+
1112+
/*
1113+
* Must be a power-of-2 currently
1114+
*/
1115+
if (!is_power_of_2(arg))
1116+
return -EINVAL;
1117+
1118+
/*
1119+
* We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1120+
* expect a lot of shrink+grow operations, just free and allocate
1121+
* again like we would do for growing. If the pipe currently
1122+
* contains more buffers than arg, then return busy.
1123+
*/
1124+
if (arg < pipe->nrbufs)
1125+
return -EBUSY;
1126+
1127+
bufs = kcalloc(arg, sizeof(struct pipe_buffer), GFP_KERNEL);
1128+
if (unlikely(!bufs))
1129+
return -ENOMEM;
1130+
1131+
/*
1132+
* The pipe array wraps around, so just start the new one at zero
1133+
* and adjust the indexes.
1134+
*/
1135+
if (pipe->nrbufs) {
1136+
const unsigned int tail = pipe->nrbufs & (pipe->buffers - 1);
1137+
const unsigned int head = pipe->nrbufs - tail;
1138+
1139+
if (head)
1140+
memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1141+
if (tail)
1142+
memcpy(bufs + head, pipe->bufs + pipe->curbuf, tail * sizeof(struct pipe_buffer));
1143+
}
1144+
1145+
pipe->curbuf = 0;
1146+
kfree(pipe->bufs);
1147+
pipe->bufs = bufs;
1148+
pipe->buffers = arg;
1149+
return arg;
1150+
}
1151+
1152+
long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1153+
{
1154+
struct pipe_inode_info *pipe;
1155+
long ret;
1156+
1157+
pipe = file->f_path.dentry->d_inode->i_pipe;
1158+
if (!pipe)
1159+
return -EBADF;
1160+
1161+
mutex_lock(&pipe->inode->i_mutex);
1162+
1163+
switch (cmd) {
1164+
case F_SETPIPE_SZ:
1165+
ret = pipe_set_size(pipe, arg);
1166+
break;
1167+
case F_GETPIPE_SZ:
1168+
ret = pipe->buffers;
1169+
break;
1170+
default:
1171+
ret = -EINVAL;
1172+
break;
1173+
}
1174+
1175+
mutex_unlock(&pipe->inode->i_mutex);
1176+
return ret;
1177+
}
1178+
10961179
/*
10971180
* pipefs should _never_ be mounted by userland - too much of security hassle,
10981181
* no real gain from having the whole whorehouse mounted. So we don't need

0 commit comments

Comments
 (0)