diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-04-29 16:12:42 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-04-29 16:12:42 -0400 |
commit | 9883035ae7edef3ec62ad215611cb8e17d6a1a5d (patch) | |
tree | ab4afff1603e0f1e85e349b8a1fdb8415cc457cf /fs | |
parent | de9e24eda331bbefb9195a4d646c786bdcbba7d4 (diff) |
pipes: add a "packetized pipe" mode for writing
The actual internal pipe implementation is already really about
individual packets (called "pipe buffers"), and this simply exposes that
as a special packetized mode.
When we are in the packetized mode (marked by O_DIRECT as suggested by
Alan Cox), a write() on a pipe will not merge the new data with previous
writes, so each write will get a pipe buffer of its own. The pipe
buffer is then marked with the PIPE_BUF_FLAG_PACKET flag, which in turn
will tell the reader side to break the read at that boundary (and throw
away any partial packet contents that do not fit in the read buffer).
End result: as long as you do writes less than PIPE_BUF in size (so that
the pipe doesn't have to split them up), you can now treat the pipe as a
packet interface, where each read() system call will read one packet at
a time. You can just use a sufficiently big read buffer (PIPE_BUF is
sufficient, since bigger than that doesn't guarantee atomicity anyway),
and the return value of the read() will naturally give you the size of
the packet.
NOTE! We do not support zero-sized packets, and zero-sized reads and
writes to a pipe continue to be no-ops. Also note that big packets will
currently be split at write time, but that the size at which that
happens is not really specified (except that it's bigger than PIPE_BUF).
Currently that limit is the system page size, but we might want to
explicitly support bigger packets some day.
The main user for this is going to be the autofs packet interface,
allowing us to stop having to care so deeply about exact packet sizes
(which have had bugs with 32/64-bit compatibility modes). But user
space can create packetized pipes with "pipe2(fd, O_DIRECT)", which will
fail with an EINVAL on kernels that do not support this interface.
Tested-by: Michael Tokarev <mjt@tls.msk.ru>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: David Miller <davem@davemloft.net>
Cc: Ian Kent <raven@themaw.net>
Cc: Thomas Meyer <thomas@m3y3r.de>
Cc: stable@kernel.org # needed for systemd/autofs interaction fix
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/pipe.c | 31 |
1 files changed, 29 insertions, 2 deletions
@@ -346,6 +346,16 @@ static const struct pipe_buf_operations anon_pipe_buf_ops = { | |||
346 | .get = generic_pipe_buf_get, | 346 | .get = generic_pipe_buf_get, |
347 | }; | 347 | }; |
348 | 348 | ||
349 | static const struct pipe_buf_operations packet_pipe_buf_ops = { | ||
350 | .can_merge = 0, | ||
351 | .map = generic_pipe_buf_map, | ||
352 | .unmap = generic_pipe_buf_unmap, | ||
353 | .confirm = generic_pipe_buf_confirm, | ||
354 | .release = anon_pipe_buf_release, | ||
355 | .steal = generic_pipe_buf_steal, | ||
356 | .get = generic_pipe_buf_get, | ||
357 | }; | ||
358 | |||
349 | static ssize_t | 359 | static ssize_t |
350 | pipe_read(struct kiocb *iocb, const struct iovec *_iov, | 360 | pipe_read(struct kiocb *iocb, const struct iovec *_iov, |
351 | unsigned long nr_segs, loff_t pos) | 361 | unsigned long nr_segs, loff_t pos) |
@@ -407,6 +417,13 @@ redo: | |||
407 | ret += chars; | 417 | ret += chars; |
408 | buf->offset += chars; | 418 | buf->offset += chars; |
409 | buf->len -= chars; | 419 | buf->len -= chars; |
420 | |||
421 | /* Was it a packet buffer? Clean up and exit */ | ||
422 | if (buf->flags & PIPE_BUF_FLAG_PACKET) { | ||
423 | total_len = chars; | ||
424 | buf->len = 0; | ||
425 | } | ||
426 | |||
410 | if (!buf->len) { | 427 | if (!buf->len) { |
411 | buf->ops = NULL; | 428 | buf->ops = NULL; |
412 | ops->release(pipe, buf); | 429 | ops->release(pipe, buf); |
@@ -459,6 +476,11 @@ redo: | |||
459 | return ret; | 476 | return ret; |
460 | } | 477 | } |
461 | 478 | ||
479 | static inline int is_packetized(struct file *file) | ||
480 | { | ||
481 | return (file->f_flags & O_DIRECT) != 0; | ||
482 | } | ||
483 | |||
462 | static ssize_t | 484 | static ssize_t |
463 | pipe_write(struct kiocb *iocb, const struct iovec *_iov, | 485 | pipe_write(struct kiocb *iocb, const struct iovec *_iov, |
464 | unsigned long nr_segs, loff_t ppos) | 486 | unsigned long nr_segs, loff_t ppos) |
@@ -593,6 +615,11 @@ redo2: | |||
593 | buf->ops = &anon_pipe_buf_ops; | 615 | buf->ops = &anon_pipe_buf_ops; |
594 | buf->offset = 0; | 616 | buf->offset = 0; |
595 | buf->len = chars; | 617 | buf->len = chars; |
618 | buf->flags = 0; | ||
619 | if (is_packetized(filp)) { | ||
620 | buf->ops = &packet_pipe_buf_ops; | ||
621 | buf->flags = PIPE_BUF_FLAG_PACKET; | ||
622 | } | ||
596 | pipe->nrbufs = ++bufs; | 623 | pipe->nrbufs = ++bufs; |
597 | pipe->tmp_page = NULL; | 624 | pipe->tmp_page = NULL; |
598 | 625 | ||
@@ -1013,7 +1040,7 @@ struct file *create_write_pipe(int flags) | |||
1013 | goto err_dentry; | 1040 | goto err_dentry; |
1014 | f->f_mapping = inode->i_mapping; | 1041 | f->f_mapping = inode->i_mapping; |
1015 | 1042 | ||
1016 | f->f_flags = O_WRONLY | (flags & O_NONBLOCK); | 1043 | f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)); |
1017 | f->f_version = 0; | 1044 | f->f_version = 0; |
1018 | 1045 | ||
1019 | return f; | 1046 | return f; |
@@ -1057,7 +1084,7 @@ int do_pipe_flags(int *fd, int flags) | |||
1057 | int error; | 1084 | int error; |
1058 | int fdw, fdr; | 1085 | int fdw, fdr; |
1059 | 1086 | ||
1060 | if (flags & ~(O_CLOEXEC | O_NONBLOCK)) | 1087 | if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
1061 | return -EINVAL; | 1088 | return -EINVAL; |
1062 | 1089 | ||
1063 | fw = create_write_pipe(flags); | 1090 | fw = create_write_pipe(flags); |