1 .\" Copyright (C) 2007, 2010 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Sat Jul 24 18:34:44 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" Merged readv.[23], 2002-10-17, aeb
28 .\" 2007-04-30 mtk, A fairly major rewrite to fix errors and
30 .\" 2010-11-16, mtk, Added documentation of preadv() and pwritev()
32 .TH READV 2 2021-03-22 "Linux" "Linux Programmer's Manual"
34 readv, writev, preadv, pwritev, preadv2, pwritev2 \- read or write data into multiple buffers
37 .B #include <sys/uio.h>
39 .BI "ssize_t readv(int " fd ", const struct iovec *" iov ", int " iovcnt );
40 .BI "ssize_t writev(int " fd ", const struct iovec *" iov ", int " iovcnt );
42 .BI "ssize_t preadv(int " fd ", const struct iovec *" iov ", int " iovcnt ,
43 .BI " off_t " offset );
44 .BI "ssize_t pwritev(int " fd ", const struct iovec *" iov ", int " iovcnt ,
45 .BI " off_t " offset );
47 .BI "ssize_t preadv2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
48 .BI " off_t " offset ", int " flags );
49 .BI "ssize_t pwritev2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
50 .BI " off_t " offset ", int " flags );
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
63 Glibc 2.19 and earlier:
71 buffers from the file associated with the file descriptor
73 into the buffers described by
81 buffers of data described by
83 to the file associated with the file descriptor
99 void *iov_base; /* Starting address */
100 size_t iov_len; /* Number of bytes to transfer */
107 system call works just like
109 except that multiple buffers are filled.
113 system call works just like
115 except that multiple buffers are written out.
117 Buffers are processed in array order.
125 (If there is insufficient data, then not all buffers pointed to by
130 writes out the entire contents of
136 The data transfers performed by
140 are atomic: the data written by
141 .\" Regarding atomicity, see https://bugzilla.kernel.org/show_bug.cgi?id=10596
143 is written as a single block that is not intermingled with output
144 from writes in other processes;
147 is guaranteed to read a contiguous block of data from the file,
148 regardless of read operations performed in other threads or processes
149 that have file descriptors referring to the same open file description
152 .SS preadv() and pwritev()
155 system call combines the functionality of
159 It performs the same task as
161 but adds a fourth argument,
163 which specifies the file offset at which the input operation
168 system call combines the functionality of
172 It performs the same task as
174 but adds a fourth argument,
176 which specifies the file offset at which the output operation
179 The file offset is not changed by these system calls.
180 The file referred to by
182 must be capable of seeking.
183 .SS preadv2() and pwritev2()
184 These system calls are similar to
188 calls, but add a fifth argument,
190 which modifies the behavior on a per-call basis.
198 argument is \-1, then the current file offset is used and updated.
202 argument contains a bitwise OR of zero or more of the following flags:
204 .BR RWF_DSYNC " (since Linux 4.7)"
205 .\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
206 Provide a per-write equivalent of the
210 This flag is meaningful only for
212 and its effect applies only to the data range written by the system call.
214 .BR RWF_HIPRI " (since Linux 4.6)"
215 High priority read/write.
216 Allows block-based filesystems to use polling of the device,
217 which provides lower latency, but may use additional resources.
218 (Currently, this feature is usable only on a file descriptor opened using the
222 .BR RWF_SYNC " (since Linux 4.7)"
223 .\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
224 Provide a per-write equivalent of the
228 This flag is meaningful only for
230 and its effect applies only to the data range written by the system call.
232 .BR RWF_NOWAIT " (since Linux 4.14)"
233 .\" commit 3239d834847627b6634a4139cf1dc58f6f137a46
234 .\" commit 91f9943e1c7b6638f27312d03fe71fcc67b23571
235 Do not wait for data which is not immediately available.
236 If this flag is specified, the
238 system call will return instantly if it would have to read data from
239 the backing storage or wait for a lock.
240 If some data was successfully read, it will return the number of bytes read.
241 If no bytes were read, it will return \-1 and set
247 Currently, this flag is meaningful only for
250 .BR RWF_APPEND " (since Linux 4.16)"
251 .\" commit e1fc742e14e01d84d9693c4aca4ab23da65811fb
252 Provide a per-write equivalent of the
256 This flag is meaningful only for
258 and its effect applies only to the data range written by the system call.
261 argument does not affect the write operation;
262 the data is always appended to the end of the file.
265 argument is \-1, the current file offset is updated.
272 return the number of bytes read;
277 return the number of bytes written.
279 Note that it is not an error for a successful call to transfer fewer bytes
285 On error, \-1 is returned, and \fIerrno\fP is set to indicate the error.
287 The errors are as given for
297 can also fail for the same reasons as
299 Additionally, the following errors are defined:
311 is less than zero or greater than the permitted maximum.
314 An unknown flag is specified in \fIflags\fP.
319 first appeared in Linux 2.6.30; library support was added in glibc 2.10.
324 first appeared in Linux 4.6.
325 Library support was added in glibc 2.26.
329 POSIX.1-2001, POSIX.1-2008,
330 4.4BSD (these system calls first appeared in 4.2BSD).
331 .\" Linux libc5 used \fIsize_t\fP as the type of the \fIiovcnt\fP argument,
332 .\" and \fIint\fP as the return type.
333 .\" The readv/writev system calls were buggy before Linux 1.3.40.
334 .\" (Says release.libc.)
338 nonstandard, but present also on the modern BSDs.
342 nonstandard Linux extension.
344 POSIX.1 allows an implementation to place a limit on
345 the number of items that can be passed in
347 An implementation can advertise its limit by defining
351 or at run time via the return value from
352 .IR sysconf(_SC_IOV_MAX) .
353 On modern Linux systems, the limit is 1024.
354 Back in Linux 2.0 days, this limit was 16.
357 .SS C library/kernel differences
362 system calls have call signatures that differ slightly from that of the
363 corresponding GNU C library wrapper functions shown in the SYNOPSIS.
366 is unpacked by the wrapper functions into two arguments in the system calls:
368 .BI " unsigned long " pos_l ", unsigned long " pos
370 These arguments contain, respectively, the low order and high order 32 bits of
372 .SS Historical C library/kernel differences
373 To deal with the fact that
375 was so low on early versions of Linux,
376 the glibc wrapper functions for
380 did some extra work if they detected that the underlying kernel
381 system call failed because this limit was exceeded.
384 the wrapper function allocated a temporary buffer large enough
385 for all of the items specified by
387 passed that buffer in a call to
389 copied data from the buffer to the locations specified by the
391 fields of the elements of
393 and then freed the buffer.
394 The wrapper function for
396 performed the analogous task using a temporary buffer and a call to
399 The need for this extra effort in the glibc wrapper functions
400 went away with Linux 2.2 and later.
401 However, glibc continued to provide this behavior until version 2.10.
402 Starting with glibc version 2.9,
403 the wrapper functions provide this behavior only if the library detects
404 that the system is running a Linux kernel older than version 2.6.18
405 (an arbitrarily selected kernel version).
407 (which requires a minimum Linux kernel version of 2.6.32),
408 the glibc wrapper functions always just directly invoke the system calls.
410 The following code sample demonstrates the use of
415 char *str0 = "hello ";
416 char *str1 = "world\en";
420 iov[0].iov_base = str0;
421 iov[0].iov_len = strlen(str0);
422 iov[1].iov_base = str1;
423 iov[1].iov_len = strlen(str1);
425 nwritten = writev(STDOUT_FILENO, iov, 2);
429 Linux 5.9 and 5.10 have a bug where
433 flag may return 0 even when not at end of file.
435 .\" <https://lore.kernel.org/linux-fsdevel/fea8b16d-5a69-40f9-b123-e84dcd6e8f2e@www.fastmail.com/T/#u>
436 .\" The bug was introduced in
437 .\" efa8480a831 fs: RWF_NOWAIT should imply IOCB_NOIO
439 .\" 06c0444290 mm/filemap.c: generic_file_buffered_read() now uses find_get_pages_contig