2005-04-17 02:20:36 +04:00
/*
* 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>
2010-05-20 12:43:18 +04:00
# include <linux/log2.h>
2005-04-17 02:20:36 +04:00
# include <linux/mount.h>
2012-03-24 02:01:50 +04:00
# include <linux/magic.h>
2005-04-17 02:20:36 +04:00
# include <linux/pipe_fs_i.h>
# include <linux/uio.h>
# include <linux/highmem.h>
2006-03-30 17:15:30 +04:00
# include <linux/pagemap.h>
2007-02-07 09:48:00 +03:00
# include <linux/audit.h>
2008-05-07 07:42:38 +04:00
# include <linux/syscalls.h>
2010-05-19 23:03:16 +04:00
# include <linux/fcntl.h>
2005-04-17 02:20:36 +04:00
# include <asm/uaccess.h>
# include <asm/ioctls.h>
2013-03-12 17:58:10 +04:00
# include "internal.h"
2010-05-19 23:03:16 +04:00
/*
* The max size that a non - root user is allowed to grow the pipe . Can
2010-06-03 16:54:39 +04:00
* be set by root in / proc / sys / fs / pipe - max - size
2010-05-19 23:03:16 +04:00
*/
2010-06-03 16:54:39 +04:00
unsigned int pipe_max_size = 1048576 ;
/*
* Minimum pipe size , as required by POSIX
*/
unsigned int pipe_min_size = PAGE_SIZE ;
2010-05-19 23:03:16 +04:00
2005-04-17 02:20:36 +04:00
/*
* We use a start + len construction , which provides full use of the
* allocated memory .
* - - Florian Coosmann ( FGC )
*
* Reads with count = 0 should always return 0.
* - - Julian Bradfield 1999 - 06 - 07.
*
* FIFOs and Pipes now generate SIGIO for both readers and writers .
* - - Jeremy Elson < jelson @ circlemud . org > 2001 - 08 - 16
*
* pipe_read & write cleanup
* - - Manfred Spraul < manfred @ colorfullife . com > 2002 - 05 - 09
*/
2009-04-14 21:48:41 +04:00
static void pipe_lock_nested ( struct pipe_inode_info * pipe , int subclass )
{
if ( pipe - > inode )
mutex_lock_nested ( & pipe - > inode - > i_mutex , subclass ) ;
}
void pipe_lock ( struct pipe_inode_info * pipe )
{
/*
* pipe_lock ( ) nests non - pipe inode locks ( for writing to a file )
*/
pipe_lock_nested ( pipe , I_MUTEX_PARENT ) ;
}
EXPORT_SYMBOL ( pipe_lock ) ;
void pipe_unlock ( struct pipe_inode_info * pipe )
{
if ( pipe - > inode )
mutex_unlock ( & pipe - > inode - > i_mutex ) ;
}
EXPORT_SYMBOL ( pipe_unlock ) ;
void pipe_double_lock ( struct pipe_inode_info * pipe1 ,
struct pipe_inode_info * pipe2 )
{
BUG_ON ( pipe1 = = pipe2 ) ;
if ( pipe1 < pipe2 ) {
pipe_lock_nested ( pipe1 , I_MUTEX_PARENT ) ;
pipe_lock_nested ( pipe2 , I_MUTEX_CHILD ) ;
} else {
2009-07-21 12:09:23 +04:00
pipe_lock_nested ( pipe2 , I_MUTEX_PARENT ) ;
pipe_lock_nested ( pipe1 , I_MUTEX_CHILD ) ;
2009-04-14 21:48:41 +04:00
}
}
2005-04-17 02:20:36 +04:00
/* Drop the inode semaphore and wait for a pipe event, atomically */
2006-04-10 17:18:35 +04:00
void pipe_wait ( struct pipe_inode_info * pipe )
2005-04-17 02:20:36 +04:00
{
DEFINE_WAIT ( wait ) ;
2005-09-10 11:26:12 +04:00
/*
* Pipes are system - local resources , so sleeping on them
* is considered a noninteractive wait :
*/
2007-10-15 19:00:13 +04:00
prepare_to_wait ( & pipe - > wait , & wait , TASK_INTERRUPTIBLE ) ;
2009-04-14 21:48:41 +04:00
pipe_unlock ( pipe ) ;
2005-04-17 02:20:36 +04:00
schedule ( ) ;
2006-04-10 17:18:35 +04:00
finish_wait ( & pipe - > wait , & wait ) ;
2009-04-14 21:48:41 +04:00
pipe_lock ( pipe ) ;
2005-04-17 02:20:36 +04:00
}
2006-01-15 00:20:43 +03:00
static int
2006-05-01 22:02:05 +04:00
pipe_iov_copy_from_user ( void * to , struct iovec * iov , unsigned long len ,
int atomic )
2005-04-17 02:20:36 +04:00
{
unsigned long copy ;
while ( len > 0 ) {
while ( ! iov - > iov_len )
iov + + ;
copy = min_t ( unsigned long , len , iov - > iov_len ) ;
2006-05-01 22:02:05 +04:00
if ( atomic ) {
if ( __copy_from_user_inatomic ( to , iov - > iov_base , copy ) )
return - EFAULT ;
} else {
if ( copy_from_user ( to , iov - > iov_base , copy ) )
return - EFAULT ;
}
2005-04-17 02:20:36 +04:00
to + = copy ;
len - = copy ;
iov - > iov_base + = copy ;
iov - > iov_len - = copy ;
}
return 0 ;
}
2006-01-15 00:20:43 +03:00
static int
2006-05-01 22:02:05 +04:00
pipe_iov_copy_to_user ( struct iovec * iov , const void * from , unsigned long len ,
int atomic )
2005-04-17 02:20:36 +04:00
{
unsigned long copy ;
while ( len > 0 ) {
while ( ! iov - > iov_len )
iov + + ;
copy = min_t ( unsigned long , len , iov - > iov_len ) ;
2006-05-01 22:02:05 +04:00
if ( atomic ) {
if ( __copy_to_user_inatomic ( iov - > iov_base , from , copy ) )
return - EFAULT ;
} else {
if ( copy_to_user ( iov - > iov_base , from , copy ) )
return - EFAULT ;
}
2005-04-17 02:20:36 +04:00
from + = copy ;
len - = copy ;
iov - > iov_base + = copy ;
iov - > iov_len - = copy ;
}
return 0 ;
}
2006-05-01 22:02:05 +04:00
/*
* Attempt to pre - fault in the user memory , so we can use atomic copies .
* Returns the number of bytes not faulted in .
*/
static int iov_fault_in_pages_write ( struct iovec * iov , unsigned long len )
{
while ( ! iov - > iov_len )
iov + + ;
while ( len > 0 ) {
unsigned long this_len ;
this_len = min_t ( unsigned long , len , iov - > iov_len ) ;
if ( fault_in_pages_writeable ( iov - > iov_base , this_len ) )
break ;
len - = this_len ;
iov + + ;
}
return len ;
}
/*
* Pre - fault in the user memory , so we can use atomic copies .
*/
static void iov_fault_in_pages_read ( struct iovec * iov , unsigned long len )
{
while ( ! iov - > iov_len )
iov + + ;
while ( len > 0 ) {
unsigned long this_len ;
this_len = min_t ( unsigned long , len , iov - > iov_len ) ;
fault_in_pages_readable ( iov - > iov_base , this_len ) ;
len - = this_len ;
iov + + ;
}
}
2006-04-11 15:57:45 +04:00
static void anon_pipe_buf_release ( struct pipe_inode_info * pipe ,
struct pipe_buffer * buf )
2005-04-17 02:20:36 +04:00
{
struct page * page = buf - > page ;
2006-03-30 17:15:30 +04:00
/*
* 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
2006-04-11 15:57:45 +04:00
* allocation cache . ( Otherwise just release our reference to it )
2006-03-30 17:15:30 +04:00
*/
2006-04-11 15:57:45 +04:00
if ( page_count ( page ) = = 1 & & ! pipe - > tmp_page )
2006-04-11 15:53:33 +04:00
pipe - > tmp_page = page ;
2006-04-11 15:57:45 +04:00
else
page_cache_release ( page ) ;
2005-04-17 02:20:36 +04:00
}
2007-06-12 22:51:32 +04:00
/**
* generic_pipe_buf_map - virtually map a pipe buffer
* @ pipe : the pipe that the buffer belongs to
* @ buf : the buffer that should be mapped
* @ atomic : whether to use an atomic map
*
* Description :
* This function returns a kernel virtual address mapping for the
2008-02-14 02:03:22 +03:00
* pipe_buffer passed in @ buf . If @ atomic is set , an atomic map is provided
2007-06-12 22:51:32 +04:00
* and the caller has to be careful not to fault before calling
* the unmap function .
*
2012-06-23 07:33:51 +04:00
* Note that this function calls kmap_atomic ( ) if @ atomic ! = 0.
2007-06-12 22:51:32 +04:00
*/
2006-05-01 21:59:03 +04:00
void * generic_pipe_buf_map ( struct pipe_inode_info * pipe ,
2006-05-01 22:02:05 +04:00
struct pipe_buffer * buf , int atomic )
2005-04-17 02:20:36 +04:00
{
2006-05-01 22:02:05 +04:00
if ( atomic ) {
buf - > flags | = PIPE_BUF_FLAG_ATOMIC ;
2011-11-25 19:14:27 +04:00
return kmap_atomic ( buf - > page ) ;
2006-05-01 22:02:05 +04:00
}
2005-04-17 02:20:36 +04:00
return kmap ( buf - > page ) ;
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_map ) ;
2005-04-17 02:20:36 +04:00
2007-06-12 22:51:32 +04:00
/**
* generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
* @ pipe : the pipe that the buffer belongs to
* @ buf : the buffer that should be unmapped
* @ map_data : the data that the mapping function returned
*
* Description :
* This function undoes the mapping that - > map ( ) provided .
*/
2006-05-01 21:59:03 +04:00
void generic_pipe_buf_unmap ( struct pipe_inode_info * pipe ,
2006-05-01 22:02:05 +04:00
struct pipe_buffer * buf , void * map_data )
2005-04-17 02:20:36 +04:00
{
2006-05-01 22:02:05 +04:00
if ( buf - > flags & PIPE_BUF_FLAG_ATOMIC ) {
buf - > flags & = ~ PIPE_BUF_FLAG_ATOMIC ;
2011-11-25 19:14:27 +04:00
kunmap_atomic ( map_data ) ;
2006-05-01 22:02:05 +04:00
} else
kunmap ( buf - > page ) ;
2005-04-17 02:20:36 +04:00
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_unmap ) ;
2005-04-17 02:20:36 +04:00
2007-06-12 22:51:32 +04:00
/**
2008-02-14 02:03:22 +03:00
* generic_pipe_buf_steal - attempt to take ownership of a & pipe_buffer
2007-06-12 22:51:32 +04:00
* @ pipe : the pipe that the buffer belongs to
* @ buf : the buffer to attempt to steal
*
* Description :
2008-02-14 02:03:22 +03:00
* This function attempts to steal the & struct page attached to
2007-06-12 22:51:32 +04:00
* @ buf . If successful , this function returns 0 and returns with
* the page locked . The caller may then reuse the page for whatever
2008-02-14 02:03:22 +03:00
* he wishes ; the typical use is insertion into a different file
2007-06-12 22:51:32 +04:00
* page cache .
*/
2006-05-02 17:29:57 +04:00
int generic_pipe_buf_steal ( struct pipe_inode_info * pipe ,
struct pipe_buffer * buf )
2006-03-30 17:16:46 +04:00
{
2006-04-30 18:36:32 +04:00
struct page * page = buf - > page ;
2007-06-12 22:51:32 +04:00
/*
* A reference of one is golden , that means that the owner of this
* page is the only one holding a reference to it . lock the page
* and return OK .
*/
2006-04-30 18:36:32 +04:00
if ( page_count ( page ) = = 1 ) {
lock_page ( page ) ;
return 0 ;
}
return 1 ;
2006-03-30 17:16:46 +04:00
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_steal ) ;
2006-03-30 17:16:46 +04:00
2007-06-12 22:51:32 +04:00
/**
2008-02-14 02:03:22 +03:00
* generic_pipe_buf_get - get a reference to a & struct pipe_buffer
2007-06-12 22:51:32 +04:00
* @ pipe : the pipe that the buffer belongs to
* @ buf : the buffer to get a reference to
*
* Description :
* This function grabs an extra reference to @ buf . It ' s used in
* in the tee ( ) system call , when we duplicate the buffers in one
* pipe into another .
*/
void generic_pipe_buf_get ( struct pipe_inode_info * pipe , struct pipe_buffer * buf )
2006-04-11 17:51:17 +04:00
{
page_cache_get ( buf - > page ) ;
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_get ) ;
2006-04-11 17:51:17 +04:00
2007-06-12 22:51:32 +04:00
/**
* generic_pipe_buf_confirm - verify contents of the pipe buffer
2007-07-27 10:08:51 +04:00
* @ info : the pipe that the buffer belongs to
2007-06-12 22:51:32 +04:00
* @ buf : the buffer to confirm
*
* Description :
* This function does nothing , because the generic pipe code uses
* pages that are always good when inserted into the pipe .
*/
2007-06-14 15:10:48 +04:00
int generic_pipe_buf_confirm ( struct pipe_inode_info * info ,
struct pipe_buffer * buf )
2006-05-01 21:59:03 +04:00
{
return 0 ;
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_confirm ) ;
2006-05-01 21:59:03 +04:00
2009-05-07 17:37:36 +04:00
/**
* generic_pipe_buf_release - put a reference to a & struct pipe_buffer
* @ pipe : the pipe that the buffer belongs to
* @ buf : the buffer to put a reference to
*
* Description :
* This function releases a reference to @ buf .
*/
void generic_pipe_buf_release ( struct pipe_inode_info * pipe ,
struct pipe_buffer * buf )
{
page_cache_release ( buf - > page ) ;
}
2010-05-26 10:44:22 +04:00
EXPORT_SYMBOL ( generic_pipe_buf_release ) ;
2009-05-07 17:37:36 +04:00
2006-12-13 11:34:04 +03:00
static const struct pipe_buf_operations anon_pipe_buf_ops = {
2005-04-17 02:20:36 +04:00
. can_merge = 1 ,
2006-05-01 21:59:03 +04:00
. map = generic_pipe_buf_map ,
. unmap = generic_pipe_buf_unmap ,
2007-06-14 15:10:48 +04:00
. confirm = generic_pipe_buf_confirm ,
2005-04-17 02:20:36 +04:00
. release = anon_pipe_buf_release ,
2006-05-02 17:29:57 +04:00
. steal = generic_pipe_buf_steal ,
2006-05-01 21:59:03 +04:00
. get = generic_pipe_buf_get ,
2005-04-17 02:20:36 +04:00
} ;
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>
2012-04-30 00:12:42 +04:00
static const struct pipe_buf_operations packet_pipe_buf_ops = {
. can_merge = 0 ,
. map = generic_pipe_buf_map ,
. unmap = generic_pipe_buf_unmap ,
. confirm = generic_pipe_buf_confirm ,
. release = anon_pipe_buf_release ,
. steal = generic_pipe_buf_steal ,
. get = generic_pipe_buf_get ,
} ;
2005-04-17 02:20:36 +04:00
static ssize_t
2006-10-01 10:28:47 +04:00
pipe_read ( struct kiocb * iocb , const struct iovec * _iov ,
unsigned long nr_segs , loff_t pos )
2005-04-17 02:20:36 +04:00
{
2006-10-01 10:28:47 +04:00
struct file * filp = iocb - > ki_filp ;
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2005-04-17 02:20:36 +04:00
int do_wakeup ;
ssize_t ret ;
struct iovec * iov = ( struct iovec * ) _iov ;
size_t total_len ;
total_len = iov_length ( iov , nr_segs ) ;
/* Null read succeeds. */
if ( unlikely ( total_len = = 0 ) )
return 0 ;
do_wakeup = 0 ;
ret = 0 ;
2006-04-11 15:53:10 +04:00
mutex_lock ( & inode - > i_mutex ) ;
2006-04-11 15:53:33 +04:00
pipe = inode - > i_pipe ;
2005-04-17 02:20:36 +04:00
for ( ; ; ) {
2006-04-11 15:53:33 +04:00
int bufs = pipe - > nrbufs ;
2005-04-17 02:20:36 +04:00
if ( bufs ) {
2006-04-11 15:53:33 +04:00
int curbuf = pipe - > curbuf ;
struct pipe_buffer * buf = pipe - > bufs + curbuf ;
2006-12-13 11:34:04 +03:00
const struct pipe_buf_operations * ops = buf - > ops ;
2005-04-17 02:20:36 +04:00
void * addr ;
size_t chars = buf - > len ;
2006-05-01 22:02:05 +04:00
int error , atomic ;
2005-04-17 02:20:36 +04:00
if ( chars > total_len )
chars = total_len ;
2007-06-14 15:10:48 +04:00
error = ops - > confirm ( pipe , buf ) ;
2006-05-01 21:59:03 +04:00
if ( error ) {
2006-03-30 17:15:30 +04:00
if ( ! ret )
2010-10-21 16:56:00 +04:00
ret = error ;
2006-03-30 17:15:30 +04:00
break ;
}
2006-05-01 21:59:03 +04:00
2006-05-01 22:02:05 +04:00
atomic = ! iov_fault_in_pages_write ( iov , chars ) ;
redo :
addr = ops - > map ( pipe , buf , atomic ) ;
error = pipe_iov_copy_to_user ( iov , addr + buf - > offset , chars , atomic ) ;
ops - > unmap ( pipe , buf , addr ) ;
2005-04-17 02:20:36 +04:00
if ( unlikely ( error ) ) {
2006-05-01 22:02:05 +04:00
/*
* Just retry with the slow path if we failed .
*/
if ( atomic ) {
atomic = 0 ;
goto redo ;
}
2006-04-11 15:57:45 +04:00
if ( ! ret )
2006-05-01 22:02:05 +04:00
ret = error ;
2005-04-17 02:20:36 +04:00
break ;
}
ret + = chars ;
buf - > offset + = chars ;
buf - > len - = chars ;
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>
2012-04-30 00:12:42 +04:00
/* Was it a packet buffer? Clean up and exit */
if ( buf - > flags & PIPE_BUF_FLAG_PACKET ) {
total_len = chars ;
buf - > len = 0 ;
}
2005-04-17 02:20:36 +04:00
if ( ! buf - > len ) {
buf - > ops = NULL ;
2006-04-11 15:53:33 +04:00
ops - > release ( pipe , buf ) ;
2010-05-20 12:43:18 +04:00
curbuf = ( curbuf + 1 ) & ( pipe - > buffers - 1 ) ;
2006-04-11 15:53:33 +04:00
pipe - > curbuf = curbuf ;
pipe - > nrbufs = - - bufs ;
2005-04-17 02:20:36 +04:00
do_wakeup = 1 ;
}
total_len - = chars ;
if ( ! total_len )
break ; /* common path: read succeeded */
}
if ( bufs ) /* More to do? */
continue ;
2006-04-11 15:53:33 +04:00
if ( ! pipe - > writers )
2005-04-17 02:20:36 +04:00
break ;
2006-04-11 15:53:33 +04:00
if ( ! pipe - > waiting_writers ) {
2005-04-17 02:20:36 +04:00
/* syscall merging: Usually we must not sleep
* if O_NONBLOCK is set , or if we got some data .
* But if a writer sleeps in kernel space , then
* we can wait for that data without violating POSIX .
*/
if ( ret )
break ;
if ( filp - > f_flags & O_NONBLOCK ) {
ret = - EAGAIN ;
break ;
}
}
if ( signal_pending ( current ) ) {
2006-04-11 15:57:45 +04:00
if ( ! ret )
ret = - ERESTARTSYS ;
2005-04-17 02:20:36 +04:00
break ;
}
if ( do_wakeup ) {
2011-01-21 03:21:59 +03:00
wake_up_interruptible_sync_poll ( & pipe - > wait , POLLOUT | POLLWRNORM ) ;
2006-04-11 15:53:33 +04:00
kill_fasync ( & pipe - > fasync_writers , SIGIO , POLL_OUT ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:53:33 +04:00
pipe_wait ( pipe ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:53:10 +04:00
mutex_unlock ( & inode - > i_mutex ) ;
2006-04-11 15:57:45 +04:00
/* Signal writers asynchronously that there is more room. */
2005-04-17 02:20:36 +04:00
if ( do_wakeup ) {
2011-01-21 03:21:59 +03:00
wake_up_interruptible_sync_poll ( & pipe - > wait , POLLOUT | POLLWRNORM ) ;
2006-04-11 15:53:33 +04:00
kill_fasync ( & pipe - > fasync_writers , SIGIO , POLL_OUT ) ;
2005-04-17 02:20:36 +04:00
}
if ( ret > 0 )
file_accessed ( filp ) ;
return ret ;
}
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>
2012-04-30 00:12:42 +04:00
static inline int is_packetized ( struct file * file )
{
return ( file - > f_flags & O_DIRECT ) ! = 0 ;
}
2005-04-17 02:20:36 +04:00
static ssize_t
2006-10-01 10:28:47 +04:00
pipe_write ( struct kiocb * iocb , const struct iovec * _iov ,
unsigned long nr_segs , loff_t ppos )
2005-04-17 02:20:36 +04:00
{
2006-10-01 10:28:47 +04:00
struct file * filp = iocb - > ki_filp ;
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2005-04-17 02:20:36 +04:00
ssize_t ret ;
int do_wakeup ;
struct iovec * iov = ( struct iovec * ) _iov ;
size_t total_len ;
ssize_t chars ;
total_len = iov_length ( iov , nr_segs ) ;
/* Null write succeeds. */
if ( unlikely ( total_len = = 0 ) )
return 0 ;
do_wakeup = 0 ;
ret = 0 ;
2006-04-11 15:53:10 +04:00
mutex_lock ( & inode - > i_mutex ) ;
2006-04-11 15:53:33 +04:00
pipe = inode - > i_pipe ;
2005-04-17 02:20:36 +04:00
2006-04-11 15:53:33 +04:00
if ( ! pipe - > readers ) {
2005-04-17 02:20:36 +04:00
send_sig ( SIGPIPE , current , 0 ) ;
ret = - EPIPE ;
goto out ;
}
/* We try to merge small writes */
chars = total_len & ( PAGE_SIZE - 1 ) ; /* size of the last buffer */
2006-04-11 15:53:33 +04:00
if ( pipe - > nrbufs & & chars ! = 0 ) {
2006-04-11 15:57:45 +04:00
int lastbuf = ( pipe - > curbuf + pipe - > nrbufs - 1 ) &
2010-05-20 12:43:18 +04:00
( pipe - > buffers - 1 ) ;
2006-04-11 15:53:33 +04:00
struct pipe_buffer * buf = pipe - > bufs + lastbuf ;
2006-12-13 11:34:04 +03:00
const struct pipe_buf_operations * ops = buf - > ops ;
2005-04-17 02:20:36 +04:00
int offset = buf - > offset + buf - > len ;
2006-04-11 15:57:45 +04:00
2005-04-17 02:20:36 +04:00
if ( ops - > can_merge & & offset + chars < = PAGE_SIZE ) {
2006-05-01 22:02:05 +04:00
int error , atomic = 1 ;
2006-03-30 17:15:30 +04:00
void * addr ;
2007-06-14 15:10:48 +04:00
error = ops - > confirm ( pipe , buf ) ;
2006-05-01 21:59:03 +04:00
if ( error )
2006-03-30 17:15:30 +04:00
goto out ;
2006-05-01 21:59:03 +04:00
2006-05-01 22:02:05 +04:00
iov_fault_in_pages_read ( iov , chars ) ;
redo1 :
addr = ops - > map ( pipe , buf , atomic ) ;
2006-03-30 17:15:30 +04:00
error = pipe_iov_copy_from_user ( offset + addr , iov ,
2006-05-01 22:02:05 +04:00
chars , atomic ) ;
ops - > unmap ( pipe , buf , addr ) ;
2005-04-17 02:20:36 +04:00
ret = error ;
do_wakeup = 1 ;
2006-05-01 22:02:05 +04:00
if ( error ) {
if ( atomic ) {
atomic = 0 ;
goto redo1 ;
}
2005-04-17 02:20:36 +04:00
goto out ;
2006-05-01 22:02:05 +04:00
}
2005-04-17 02:20:36 +04:00
buf - > len + = chars ;
total_len - = chars ;
ret = chars ;
if ( ! total_len )
goto out ;
}
}
for ( ; ; ) {
int bufs ;
2006-04-11 15:57:45 +04:00
2006-04-11 15:53:33 +04:00
if ( ! pipe - > readers ) {
2005-04-17 02:20:36 +04:00
send_sig ( SIGPIPE , current , 0 ) ;
2006-04-11 15:57:45 +04:00
if ( ! ret )
ret = - EPIPE ;
2005-04-17 02:20:36 +04:00
break ;
}
2006-04-11 15:53:33 +04:00
bufs = pipe - > nrbufs ;
2010-05-20 12:43:18 +04:00
if ( bufs < pipe - > buffers ) {
int newbuf = ( pipe - > curbuf + bufs ) & ( pipe - > buffers - 1 ) ;
2006-04-11 15:53:33 +04:00
struct pipe_buffer * buf = pipe - > bufs + newbuf ;
struct page * page = pipe - > tmp_page ;
2006-05-01 22:02:05 +04:00
char * src ;
int error , atomic = 1 ;
2005-04-17 02:20:36 +04:00
if ( ! page ) {
page = alloc_page ( GFP_HIGHUSER ) ;
if ( unlikely ( ! page ) ) {
ret = ret ? : - ENOMEM ;
break ;
}
2006-04-11 15:53:33 +04:00
pipe - > tmp_page = page ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:57:45 +04:00
/* Always wake up, even if the copy fails. Otherwise
2005-04-17 02:20:36 +04:00
* we lock up ( O_NONBLOCK - ) readers that sleep due to
* syscall merging .
* FIXME ! Is this really true ?
*/
do_wakeup = 1 ;
chars = PAGE_SIZE ;
if ( chars > total_len )
chars = total_len ;
2006-05-01 22:02:05 +04:00
iov_fault_in_pages_read ( iov , chars ) ;
redo2 :
if ( atomic )
2011-11-25 19:14:27 +04:00
src = kmap_atomic ( page ) ;
2006-05-01 22:02:05 +04:00
else
src = kmap ( page ) ;
error = pipe_iov_copy_from_user ( src , iov , chars ,
atomic ) ;
if ( atomic )
2011-11-25 19:14:27 +04:00
kunmap_atomic ( src ) ;
2006-05-01 22:02:05 +04:00
else
kunmap ( page ) ;
2005-04-17 02:20:36 +04:00
if ( unlikely ( error ) ) {
2006-05-01 22:02:05 +04:00
if ( atomic ) {
atomic = 0 ;
goto redo2 ;
}
2006-04-11 15:57:45 +04:00
if ( ! ret )
2006-05-01 22:02:05 +04:00
ret = error ;
2005-04-17 02:20:36 +04:00
break ;
}
ret + = chars ;
/* Insert it into the buffer array */
buf - > page = page ;
buf - > ops = & anon_pipe_buf_ops ;
buf - > offset = 0 ;
buf - > len = chars ;
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>
2012-04-30 00:12:42 +04:00
buf - > flags = 0 ;
if ( is_packetized ( filp ) ) {
buf - > ops = & packet_pipe_buf_ops ;
buf - > flags = PIPE_BUF_FLAG_PACKET ;
}
2006-04-11 15:53:33 +04:00
pipe - > nrbufs = + + bufs ;
pipe - > tmp_page = NULL ;
2005-04-17 02:20:36 +04:00
total_len - = chars ;
if ( ! total_len )
break ;
}
2010-05-20 12:43:18 +04:00
if ( bufs < pipe - > buffers )
2005-04-17 02:20:36 +04:00
continue ;
if ( filp - > f_flags & O_NONBLOCK ) {
2006-04-11 15:57:45 +04:00
if ( ! ret )
ret = - EAGAIN ;
2005-04-17 02:20:36 +04:00
break ;
}
if ( signal_pending ( current ) ) {
2006-04-11 15:57:45 +04:00
if ( ! ret )
ret = - ERESTARTSYS ;
2005-04-17 02:20:36 +04:00
break ;
}
if ( do_wakeup ) {
2011-01-21 03:21:59 +03:00
wake_up_interruptible_sync_poll ( & pipe - > wait , POLLIN | POLLRDNORM ) ;
2006-04-11 15:53:33 +04:00
kill_fasync ( & pipe - > fasync_readers , SIGIO , POLL_IN ) ;
2005-04-17 02:20:36 +04:00
do_wakeup = 0 ;
}
2006-04-11 15:53:33 +04:00
pipe - > waiting_writers + + ;
pipe_wait ( pipe ) ;
pipe - > waiting_writers - - ;
2005-04-17 02:20:36 +04:00
}
out :
2006-04-11 15:53:10 +04:00
mutex_unlock ( & inode - > i_mutex ) ;
2005-04-17 02:20:36 +04:00
if ( do_wakeup ) {
2011-01-21 03:21:59 +03:00
wake_up_interruptible_sync_poll ( & pipe - > wait , POLLIN | POLLRDNORM ) ;
2006-04-11 15:53:33 +04:00
kill_fasync ( & pipe - > fasync_readers , SIGIO , POLL_IN ) ;
2005-04-17 02:20:36 +04:00
}
2012-03-26 17:59:21 +04:00
if ( ret > 0 ) {
int err = file_update_time ( filp ) ;
if ( err )
ret = err ;
}
2005-04-17 02:20:36 +04:00
return ret ;
}
2008-02-08 15:21:23 +03:00
static long pipe_ioctl ( struct file * filp , unsigned int cmd , unsigned long arg )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2005-04-17 02:20:36 +04:00
int count , buf , nrbufs ;
switch ( cmd ) {
case FIONREAD :
2006-04-11 15:53:10 +04:00
mutex_lock ( & inode - > i_mutex ) ;
2006-04-11 15:53:33 +04:00
pipe = inode - > i_pipe ;
2005-04-17 02:20:36 +04:00
count = 0 ;
2006-04-11 15:53:33 +04:00
buf = pipe - > curbuf ;
nrbufs = pipe - > nrbufs ;
2005-04-17 02:20:36 +04:00
while ( - - nrbufs > = 0 ) {
2006-04-11 15:53:33 +04:00
count + = pipe - > bufs [ buf ] . len ;
2010-05-20 12:43:18 +04:00
buf = ( buf + 1 ) & ( pipe - > buffers - 1 ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:53:10 +04:00
mutex_unlock ( & inode - > i_mutex ) ;
2006-04-11 15:53:33 +04:00
2005-04-17 02:20:36 +04:00
return put_user ( count , ( int __user * ) arg ) ;
default :
2012-05-25 14:39:13 +04:00
return - ENOIOCTLCMD ;
2005-04-17 02:20:36 +04:00
}
}
/* No kernel lock held - fine */
static unsigned int
pipe_poll ( struct file * filp , poll_table * wait )
{
unsigned int mask ;
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe = inode - > i_pipe ;
2005-04-17 02:20:36 +04:00
int nrbufs ;
2006-04-11 15:53:33 +04:00
poll_wait ( filp , & pipe - > wait , wait ) ;
2005-04-17 02:20:36 +04:00
/* Reading only -- no need for acquiring the semaphore. */
2006-04-11 15:53:33 +04:00
nrbufs = pipe - > nrbufs ;
2005-04-17 02:20:36 +04:00
mask = 0 ;
if ( filp - > f_mode & FMODE_READ ) {
mask = ( nrbufs > 0 ) ? POLLIN | POLLRDNORM : 0 ;
2006-04-11 15:53:33 +04:00
if ( ! pipe - > writers & & filp - > f_version ! = pipe - > w_counter )
2005-04-17 02:20:36 +04:00
mask | = POLLHUP ;
}
if ( filp - > f_mode & FMODE_WRITE ) {
2010-05-20 12:43:18 +04:00
mask | = ( nrbufs < pipe - > buffers ) ? POLLOUT | POLLWRNORM : 0 ;
2005-09-07 02:17:48 +04:00
/*
* Most Unices do not set POLLERR for FIFOs but on Linux they
* behave exactly like pipes for poll ( ) .
*/
2006-04-11 15:53:33 +04:00
if ( ! pipe - > readers )
2005-04-17 02:20:36 +04:00
mask | = POLLERR ;
}
return mask ;
}
static int
2013-03-12 17:58:10 +04:00
pipe_release ( struct inode * inode , struct file * file )
2005-04-17 02:20:36 +04:00
{
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2006-04-11 15:53:10 +04:00
mutex_lock ( & inode - > i_mutex ) ;
2006-04-11 15:53:33 +04:00
pipe = inode - > i_pipe ;
2013-03-12 17:58:10 +04:00
if ( file - > f_mode & FMODE_READ )
pipe - > readers - - ;
if ( file - > f_mode & FMODE_WRITE )
pipe - > writers - - ;
2006-04-11 15:57:45 +04:00
2006-04-11 15:53:33 +04:00
if ( ! pipe - > readers & & ! pipe - > writers ) {
2005-04-17 02:20:36 +04:00
free_pipe_info ( inode ) ;
} else {
2011-01-21 03:21:59 +03:00
wake_up_interruptible_sync_poll ( & pipe - > wait , POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP ) ;
2006-04-11 15:53:33 +04:00
kill_fasync ( & pipe - > fasync_readers , SIGIO , POLL_IN ) ;
kill_fasync ( & pipe - > fasync_writers , SIGIO , POLL_OUT ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:53:10 +04:00
mutex_unlock ( & inode - > i_mutex ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
static int
2013-03-12 17:58:10 +04:00
pipe_fasync ( int fd , struct file * filp , int on )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2006-04-11 15:57:45 +04:00
struct pipe_inode_info * pipe = inode - > i_pipe ;
2013-03-12 17:58:10 +04:00
int retval = 0 ;
2005-04-17 02:20:36 +04:00
2006-04-11 15:53:10 +04:00
mutex_lock ( & inode - > i_mutex ) ;
2013-03-12 17:58:10 +04:00
if ( filp - > f_mode & FMODE_READ )
retval = fasync_helper ( fd , filp , on , & pipe - > fasync_readers ) ;
if ( ( filp - > f_mode & FMODE_WRITE ) & & retval > = 0 ) {
2006-04-11 15:57:45 +04:00
retval = fasync_helper ( fd , filp , on , & pipe - > fasync_writers ) ;
2013-03-12 17:58:10 +04:00
if ( retval < 0 & & ( filp - > f_mode & FMODE_READ ) )
/* this can happen only if on == T */
2009-03-13 00:31:28 +03:00
fasync_helper ( - 1 , filp , 0 , & pipe - > fasync_readers ) ;
}
2006-04-11 15:53:10 +04:00
mutex_unlock ( & inode - > i_mutex ) ;
2009-02-02 00:52:56 +03:00
return retval ;
2005-04-17 02:20:36 +04:00
}
2006-04-10 17:18:35 +04:00
struct pipe_inode_info * alloc_pipe_info ( struct inode * inode )
{
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2006-04-10 17:18:35 +04:00
2006-04-11 15:53:33 +04:00
pipe = kzalloc ( sizeof ( struct pipe_inode_info ) , GFP_KERNEL ) ;
if ( pipe ) {
2010-05-20 12:43:18 +04:00
pipe - > bufs = kzalloc ( sizeof ( struct pipe_buffer ) * PIPE_DEF_BUFFERS , GFP_KERNEL ) ;
if ( pipe - > bufs ) {
init_waitqueue_head ( & pipe - > wait ) ;
pipe - > r_counter = pipe - > w_counter = 1 ;
pipe - > inode = inode ;
pipe - > buffers = PIPE_DEF_BUFFERS ;
return pipe ;
}
kfree ( pipe ) ;
2006-04-10 17:18:35 +04:00
}
2010-05-20 12:43:18 +04:00
return NULL ;
2006-04-10 17:18:35 +04:00
}
2006-04-11 15:53:33 +04:00
void __free_pipe_info ( struct pipe_inode_info * pipe )
2005-04-17 02:20:36 +04:00
{
int i ;
2010-05-20 12:43:18 +04:00
for ( i = 0 ; i < pipe - > buffers ; i + + ) {
2006-04-11 15:53:33 +04:00
struct pipe_buffer * buf = pipe - > bufs + i ;
2005-04-17 02:20:36 +04:00
if ( buf - > ops )
2006-04-11 15:53:33 +04:00
buf - > ops - > release ( pipe , buf ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:53:33 +04:00
if ( pipe - > tmp_page )
__free_page ( pipe - > tmp_page ) ;
2010-05-20 12:43:18 +04:00
kfree ( pipe - > bufs ) ;
2006-04-11 15:53:33 +04:00
kfree ( pipe ) ;
2005-04-17 02:20:36 +04:00
}
2006-04-11 15:52:07 +04:00
void free_pipe_info ( struct inode * inode )
{
__free_pipe_info ( inode - > i_pipe ) ;
inode - > i_pipe = NULL ;
}
2006-03-26 13:37:24 +04:00
static struct vfsmount * pipe_mnt __read_mostly ;
2006-04-11 15:57:45 +04:00
2007-05-08 11:26:18 +04:00
/*
* pipefs_dname ( ) is called from d_path ( ) .
*/
static char * pipefs_dname ( struct dentry * dentry , char * buffer , int buflen )
{
return dynamic_dname ( dentry , buffer , buflen , " pipe:[%lu] " ,
dentry - > d_inode - > i_ino ) ;
}
2009-02-20 09:02:22 +03:00
static const struct dentry_operations pipefs_dentry_operations = {
2007-05-08 11:26:18 +04:00
. d_dname = pipefs_dname ,
2005-04-17 02:20:36 +04:00
} ;
static struct inode * get_pipe_inode ( void )
{
2011-07-26 13:36:34 +04:00
struct inode * inode = new_inode_pseudo ( pipe_mnt - > mnt_sb ) ;
2006-04-11 15:53:33 +04:00
struct pipe_inode_info * pipe ;
2005-04-17 02:20:36 +04:00
if ( ! inode )
goto fail_inode ;
2010-10-23 19:19:54 +04:00
inode - > i_ino = get_next_ino ( ) ;
2006-04-11 15:53:33 +04:00
pipe = alloc_pipe_info ( inode ) ;
if ( ! pipe )
2005-04-17 02:20:36 +04:00
goto fail_iput ;
2006-04-11 15:53:33 +04:00
inode - > i_pipe = pipe ;
2006-04-10 17:18:35 +04:00
2006-04-11 15:53:33 +04:00
pipe - > readers = pipe - > writers = 1 ;
2013-03-12 17:58:10 +04:00
inode - > i_fop = & pipefifo_fops ;
2005-04-17 02:20:36 +04:00
/*
* 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 - > i_state = I_DIRTY ;
inode - > i_mode = S_IFIFO | S_IRUSR | S_IWUSR ;
2008-11-14 02:39:05 +03:00
inode - > i_uid = current_fsuid ( ) ;
inode - > i_gid = current_fsgid ( ) ;
2005-04-17 02:20:36 +04:00
inode - > i_atime = inode - > i_mtime = inode - > i_ctime = CURRENT_TIME ;
2006-04-11 15:53:33 +04:00
2005-04-17 02:20:36 +04:00
return inode ;
fail_iput :
iput ( inode ) ;
2006-04-11 15:57:45 +04:00
2005-04-17 02:20:36 +04:00
fail_inode :
return NULL ;
}
2012-07-21 15:33:25 +04:00
int create_pipe_files ( struct file * * res , int flags )
2005-04-17 02:20:36 +04:00
{
2006-10-01 10:29:26 +04:00
int err ;
2012-07-21 15:33:25 +04:00
struct inode * inode = get_pipe_inode ( ) ;
2006-10-01 10:29:26 +04:00
struct file * f ;
2009-08-09 00:52:35 +04:00
struct path path ;
2012-07-21 15:33:25 +04:00
static struct qstr name = { . name = " " } ;
2005-04-17 02:20:36 +04:00
if ( ! inode )
2012-07-21 15:33:25 +04:00
return - ENFILE ;
2005-04-17 02:20:36 +04:00
2006-10-01 10:29:26 +04:00
err = - ENOMEM ;
2011-01-07 09:50:07 +03:00
path . dentry = d_alloc_pseudo ( pipe_mnt - > mnt_sb , & name ) ;
2009-08-09 00:52:35 +04:00
if ( ! path . dentry )
2006-10-01 10:29:26 +04:00
goto err_inode ;
2009-08-09 00:52:35 +04:00
path . mnt = mntget ( pipe_mnt ) ;
2006-04-11 15:57:45 +04:00
2009-08-09 00:52:35 +04:00
d_instantiate ( path . dentry , inode ) ;
2008-02-16 01:37:26 +03:00
err = - ENFILE ;
2013-03-12 17:58:10 +04:00
f = alloc_file ( & path , FMODE_WRITE , & pipefifo_fops ) ;
2012-09-13 07:11:55 +04:00
if ( IS_ERR ( f ) )
2008-02-16 01:37:26 +03:00
goto err_dentry ;
2006-04-11 15:57:45 +04:00
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>
2012-04-30 00:12:42 +04:00
f - > f_flags = O_WRONLY | ( flags & ( O_NONBLOCK | O_DIRECT ) ) ;
2006-10-01 10:29:26 +04:00
2013-03-12 17:58:10 +04:00
res [ 0 ] = alloc_file ( & path , FMODE_READ , & pipefifo_fops ) ;
2012-09-13 07:11:55 +04:00
if ( IS_ERR ( res [ 0 ] ) )
2012-07-21 15:33:25 +04:00
goto err_file ;
path_get ( & path ) ;
res [ 0 ] - > f_flags = O_RDONLY | ( flags & O_NONBLOCK ) ;
res [ 1 ] = f ;
return 0 ;
2005-04-17 02:20:36 +04:00
2012-07-21 15:33:25 +04:00
err_file :
put_filp ( f ) ;
err_dentry :
2008-04-23 03:51:27 +04:00
free_pipe_info ( inode ) ;
2009-08-09 00:52:35 +04:00
path_put ( & path ) ;
2012-07-21 15:33:25 +04:00
return err ;
2008-04-23 03:51:27 +04:00
2012-07-21 15:33:25 +04:00
err_inode :
2005-04-17 02:20:36 +04:00
free_pipe_info ( inode ) ;
iput ( inode ) ;
2012-07-21 15:33:25 +04:00
return err ;
2006-10-01 10:29:26 +04:00
}
2012-08-19 20:17:29 +04:00
static int __do_pipe_flags ( int * fd , struct file * * files , int flags )
2006-10-01 10:29:26 +04:00
{
int error ;
int fdw , fdr ;
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>
2012-04-30 00:12:42 +04:00
if ( flags & ~ ( O_CLOEXEC | O_NONBLOCK | O_DIRECT ) )
2008-07-24 08:29:30 +04:00
return - EINVAL ;
2012-07-21 15:33:25 +04:00
error = create_pipe_files ( files , flags ) ;
if ( error )
return error ;
2006-10-01 10:29:26 +04:00
2008-07-24 08:29:30 +04:00
error = get_unused_fd_flags ( flags ) ;
2006-10-01 10:29:26 +04:00
if ( error < 0 )
goto err_read_pipe ;
fdr = error ;
2008-07-24 08:29:30 +04:00
error = get_unused_fd_flags ( flags ) ;
2006-10-01 10:29:26 +04:00
if ( error < 0 )
goto err_fdr ;
fdw = error ;
2008-12-14 12:57:47 +03:00
audit_fd_pair ( fdr , fdw ) ;
2006-10-01 10:29:26 +04:00
fd [ 0 ] = fdr ;
fd [ 1 ] = fdw ;
return 0 ;
err_fdr :
put_unused_fd ( fdr ) ;
err_read_pipe :
2012-07-21 15:33:25 +04:00
fput ( files [ 0 ] ) ;
fput ( files [ 1 ] ) ;
2006-10-01 10:29:26 +04:00
return error ;
2005-04-17 02:20:36 +04:00
}
2012-08-19 20:17:29 +04:00
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 ;
}
2008-05-03 23:10:37 +04:00
/*
* sys_pipe ( ) is the normal C calling standard for creating
* a pipe . It ' s not the way Unix traditionally does this , though .
*/
2009-01-14 16:14:34 +03:00
SYSCALL_DEFINE2 ( pipe2 , int __user * , fildes , int , flags )
2008-05-03 23:10:37 +04:00
{
2012-08-19 20:17:29 +04:00
struct file * files [ 2 ] ;
2008-05-03 23:10:37 +04:00
int fd [ 2 ] ;
int error ;
2012-08-19 20:17:29 +04:00
error = __do_pipe_flags ( fd , files , flags ) ;
2008-05-03 23:10:37 +04:00
if ( ! error ) {
2012-08-19 20:17:29 +04:00
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 ] ) ;
2008-05-03 23:10:37 +04:00
error = - EFAULT ;
2012-08-19 20:17:29 +04:00
} else {
fd_install ( fd [ 0 ] , files [ 0 ] ) ;
fd_install ( fd [ 1 ] , files [ 1 ] ) ;
2008-05-07 07:42:38 +04:00
}
2008-05-03 23:10:37 +04:00
}
return error ;
}
2009-01-14 16:14:35 +03:00
SYSCALL_DEFINE1 ( pipe , int __user * , fildes )
2008-07-24 08:29:30 +04:00
{
return sys_pipe2 ( fildes , 0 ) ;
}
2013-03-12 17:46:27 +04:00
static int wait_for_partner ( struct inode * inode , unsigned int * cnt )
{
int cur = * cnt ;
while ( cur = = * cnt ) {
pipe_wait ( inode - > i_pipe ) ;
if ( signal_pending ( current ) )
break ;
}
return cur = = * cnt ? - ERESTARTSYS : 0 ;
}
static void wake_up_partner ( struct inode * inode )
{
wake_up_interruptible ( & inode - > i_pipe - > wait ) ;
}
static int fifo_open ( struct inode * inode , struct file * filp )
{
struct pipe_inode_info * pipe ;
2013-03-12 17:58:10 +04:00
bool is_pipe = inode - > i_sb - > s_magic = = PIPEFS_MAGIC ;
2013-03-12 17:46:27 +04:00
int ret ;
mutex_lock ( & inode - > i_mutex ) ;
pipe = inode - > i_pipe ;
if ( ! pipe ) {
ret = - ENOMEM ;
pipe = alloc_pipe_info ( inode ) ;
if ( ! pipe )
goto err_nocleanup ;
inode - > i_pipe = pipe ;
}
filp - > f_version = 0 ;
/* We can only do regular read/write on fifos */
filp - > f_mode & = ( FMODE_READ | FMODE_WRITE ) ;
switch ( filp - > f_mode ) {
case FMODE_READ :
/*
* O_RDONLY
* POSIX .1 says that O_NONBLOCK means return with the FIFO
* opened , even when there is no process writing the FIFO .
*/
pipe - > r_counter + + ;
if ( pipe - > readers + + = = 0 )
wake_up_partner ( inode ) ;
2013-03-12 17:58:10 +04:00
if ( ! is_pipe & & ! pipe - > writers ) {
2013-03-12 17:46:27 +04:00
if ( ( filp - > f_flags & O_NONBLOCK ) ) {
/* suppress POLLHUP until we have
* seen a writer */
filp - > f_version = pipe - > w_counter ;
} else {
if ( wait_for_partner ( inode , & pipe - > w_counter ) )
goto err_rd ;
}
}
break ;
case FMODE_WRITE :
/*
* O_WRONLY
* POSIX .1 says that O_NONBLOCK means return - 1 with
* errno = ENXIO when there is no process reading the FIFO .
*/
ret = - ENXIO ;
2013-03-12 17:58:10 +04:00
if ( ! is_pipe & & ( filp - > f_flags & O_NONBLOCK ) & & ! pipe - > readers )
2013-03-12 17:46:27 +04:00
goto err ;
pipe - > w_counter + + ;
if ( ! pipe - > writers + + )
wake_up_partner ( inode ) ;
2013-03-12 17:58:10 +04:00
if ( ! is_pipe & & ! pipe - > readers ) {
2013-03-12 17:46:27 +04:00
if ( wait_for_partner ( inode , & pipe - > r_counter ) )
goto err_wr ;
}
break ;
case FMODE_READ | FMODE_WRITE :
/*
* O_RDWR
* POSIX .1 leaves this case " undefined " when O_NONBLOCK is set .
* This implementation will NEVER block on a O_RDWR open , since
* the process can at least talk to itself .
*/
pipe - > readers + + ;
pipe - > writers + + ;
pipe - > r_counter + + ;
pipe - > w_counter + + ;
if ( pipe - > readers = = 1 | | pipe - > writers = = 1 )
wake_up_partner ( inode ) ;
break ;
default :
ret = - EINVAL ;
goto err ;
}
/* Ok! */
mutex_unlock ( & inode - > i_mutex ) ;
return 0 ;
err_rd :
if ( ! - - pipe - > readers )
wake_up_interruptible ( & pipe - > wait ) ;
ret = - ERESTARTSYS ;
goto err ;
err_wr :
if ( ! - - pipe - > writers )
wake_up_interruptible ( & pipe - > wait ) ;
ret = - ERESTARTSYS ;
goto err ;
err :
if ( ! pipe - > readers & & ! pipe - > writers )
free_pipe_info ( inode ) ;
err_nocleanup :
mutex_unlock ( & inode - > i_mutex ) ;
return ret ;
}
2013-03-12 17:58:10 +04:00
const struct file_operations pipefifo_fops = {
. open = fifo_open ,
. llseek = no_llseek ,
. read = do_sync_read ,
. aio_read = pipe_read ,
. write = do_sync_write ,
. aio_write = pipe_write ,
. poll = pipe_poll ,
. unlocked_ioctl = pipe_ioctl ,
. release = pipe_release ,
. fasync = pipe_fasync ,
2013-03-12 17:46:27 +04:00
} ;
2010-05-20 12:43:18 +04:00
/*
* Allocate a new array of pipe buffers and copy the info over . Returns the
* pipe size if successful , or return - ERROR on error .
*/
2010-05-24 21:34:43 +04:00
static long pipe_set_size ( struct pipe_inode_info * pipe , unsigned long nr_pages )
2010-05-20 12:43:18 +04:00
{
struct pipe_buffer * bufs ;
/*
* We can shrink the pipe , if arg > = pipe - > nrbufs . Since we don ' t
* expect a lot of shrink + grow operations , just free and allocate
* again like we would do for growing . If the pipe currently
* contains more buffers than arg , then return busy .
*/
2010-05-24 21:34:43 +04:00
if ( nr_pages < pipe - > nrbufs )
2010-05-20 12:43:18 +04:00
return - EBUSY ;
2012-01-13 05:17:40 +04:00
bufs = kcalloc ( nr_pages , sizeof ( * bufs ) , GFP_KERNEL | __GFP_NOWARN ) ;
2010-05-20 12:43:18 +04:00
if ( unlikely ( ! bufs ) )
return - ENOMEM ;
/*
* The pipe array wraps around , so just start the new one at zero
* and adjust the indexes .
*/
if ( pipe - > nrbufs ) {
2010-06-08 18:28:45 +04:00
unsigned int tail ;
unsigned int head ;
2010-05-20 12:43:18 +04:00
2010-06-08 18:28:45 +04:00
tail = pipe - > curbuf + pipe - > nrbufs ;
if ( tail < pipe - > buffers )
tail = 0 ;
else
tail & = ( pipe - > buffers - 1 ) ;
head = pipe - > nrbufs - tail ;
2010-05-20 12:43:18 +04:00
if ( head )
memcpy ( bufs , pipe - > bufs + pipe - > curbuf , head * sizeof ( struct pipe_buffer ) ) ;
if ( tail )
2010-06-08 18:28:45 +04:00
memcpy ( bufs + head , pipe - > bufs , tail * sizeof ( struct pipe_buffer ) ) ;
2010-05-20 12:43:18 +04:00
}
pipe - > curbuf = 0 ;
kfree ( pipe - > bufs ) ;
pipe - > bufs = bufs ;
2010-05-24 21:34:43 +04:00
pipe - > buffers = nr_pages ;
return nr_pages * PAGE_SIZE ;
2010-05-20 12:43:18 +04:00
}
2010-06-03 16:54:39 +04:00
/*
* Currently we rely on the pipe array holding a power - of - 2 number
* of pages .
*/
static inline unsigned int round_pipe_size ( unsigned int size )
{
unsigned long nr_pages ;
nr_pages = ( size + PAGE_SIZE - 1 ) > > PAGE_SHIFT ;
return roundup_pow_of_two ( nr_pages ) < < PAGE_SHIFT ;
}
/*
* This should work even if CONFIG_PROC_FS isn ' t set , as proc_dointvec_minmax
* will return an error .
*/
int pipe_proc_fn ( struct ctl_table * table , int write , void __user * buf ,
size_t * lenp , loff_t * ppos )
{
int ret ;
ret = proc_dointvec_minmax ( table , write , buf , lenp , ppos ) ;
if ( ret < 0 | | ! write )
return ret ;
pipe_max_size = round_pipe_size ( pipe_max_size ) ;
return ret ;
}
2010-11-29 03:27:19 +03:00
/*
* After the inode slimming patch , i_pipe / i_bdev / i_cdev share the same
* location , so checking - > i_pipe is not enough to verify that this is a
* pipe .
*/
struct pipe_inode_info * get_pipe_info ( struct file * file )
{
2013-01-24 02:07:38 +04:00
struct inode * i = file_inode ( file ) ;
2010-11-29 03:27:19 +03:00
return S_ISFIFO ( i - > i_mode ) ? i - > i_pipe : NULL ;
}
2010-05-20 12:43:18 +04:00
long pipe_fcntl ( struct file * file , unsigned int cmd , unsigned long arg )
{
struct pipe_inode_info * pipe ;
long ret ;
2010-11-29 01:09:57 +03:00
pipe = get_pipe_info ( file ) ;
2010-05-20 12:43:18 +04:00
if ( ! pipe )
return - EBADF ;
mutex_lock ( & pipe - > inode - > i_mutex ) ;
switch ( cmd ) {
2010-05-24 21:34:43 +04:00
case F_SETPIPE_SZ : {
2010-06-03 16:54:39 +04:00
unsigned int size , nr_pages ;
2010-05-24 21:34:43 +04:00
2010-06-03 16:54:39 +04:00
size = round_pipe_size ( arg ) ;
nr_pages = size > > PAGE_SHIFT ;
2010-05-24 21:34:43 +04:00
2010-06-09 11:27:57 +04:00
ret = - EINVAL ;
if ( ! nr_pages )
goto out ;
2010-06-03 16:54:39 +04:00
if ( ! capable ( CAP_SYS_RESOURCE ) & & size > pipe_max_size ) {
2010-06-01 14:42:12 +04:00
ret = - EPERM ;
2010-05-26 19:54:39 +04:00
goto out ;
}
2010-06-03 16:54:39 +04:00
ret = pipe_set_size ( pipe , nr_pages ) ;
2010-05-20 12:43:18 +04:00
break ;
2010-05-24 21:34:43 +04:00
}
2010-05-20 12:43:18 +04:00
case F_GETPIPE_SZ :
2010-05-24 21:34:43 +04:00
ret = pipe - > buffers * PAGE_SIZE ;
2010-05-20 12:43:18 +04:00
break ;
default :
ret = - EINVAL ;
break ;
}
2010-05-26 19:54:39 +04:00
out :
2010-05-20 12:43:18 +04:00
mutex_unlock ( & pipe - > inode - > i_mutex ) ;
return ret ;
}
2011-01-07 09:49:50 +03:00
static const struct super_operations pipefs_ops = {
. destroy_inode = free_inode_nonrcu ,
2011-11-01 04:10:04 +04:00
. statfs = simple_statfs ,
2011-01-07 09:49:50 +03:00
} ;
2005-04-17 02:20:36 +04:00
/*
* pipefs should _never_ be mounted by userland - too much of security hassle ,
* no real gain from having the whole whorehouse 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 .
*/
2010-07-25 23:47:46 +04:00
static struct dentry * pipefs_mount ( struct file_system_type * fs_type ,
int flags , const char * dev_name , void * data )
2005-04-17 02:20:36 +04:00
{
2011-01-13 00:59:34 +03:00
return mount_pseudo ( fs_type , " pipe: " , & pipefs_ops ,
& pipefs_dentry_operations , PIPEFS_MAGIC ) ;
2005-04-17 02:20:36 +04:00
}
static struct file_system_type pipe_fs_type = {
. name = " pipefs " ,
2010-07-25 23:47:46 +04:00
. mount = pipefs_mount ,
2005-04-17 02:20:36 +04:00
. kill_sb = kill_anon_super ,
} ;
static int __init init_pipe_fs ( void )
{
int err = register_filesystem ( & pipe_fs_type ) ;
2006-04-11 15:57:45 +04:00
2005-04-17 02:20:36 +04:00
if ( ! err ) {
pipe_mnt = kern_mount ( & pipe_fs_type ) ;
if ( IS_ERR ( pipe_mnt ) ) {
err = PTR_ERR ( pipe_mnt ) ;
unregister_filesystem ( & pipe_fs_type ) ;
}
}
return err ;
}
fs_initcall ( init_pipe_fs ) ;