mount_setattr.2: Minor wording, grammar, and formatting fixes
[man-pages.git] / man2 / write.2
blob1a2de27228bc35f4f57d95ee9bb6bd43cef20535
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified Sat Jul 24 13:35:59 1993 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified Sun Nov 28 17:19:01 1993 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified Sat Jan 13 12:58:08 1996 by Michael Haardt
30 .\"   <michael@cantor.informatik.rwth-aachen.de>
31 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
32 .\" 2001-12-13 added remark by Zack Weinberg
33 .\" 2007-06-18 mtk:
34 .\"     Added details about seekable files and file offset.
35 .\"     Noted that write() may write less than 'count' bytes, and
36 .\"     gave some examples of why this might occur.
37 .\"     Noted what happens if write() is interrupted by a signal.
38 .\"
39 .TH WRITE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 write \- write to a file descriptor
42 .SH SYNOPSIS
43 .nf
44 .B #include <unistd.h>
45 .PP
46 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
47 .fi
48 .SH DESCRIPTION
49 .BR write ()
50 writes up to
51 .I count
52 bytes from the buffer starting at
53 .I buf
54 to the file referred to by the file descriptor
55 .IR fd .
56 .PP
57 The number of bytes written may be less than
58 .I count
59 if, for example,
60 there is insufficient space on the underlying physical medium, or the
61 .B RLIMIT_FSIZE
62 resource limit is encountered (see
63 .BR setrlimit (2)),
64 or the call was interrupted by a signal
65 handler after having written less than
66 .I count
67 bytes.
68 (See also
69 .BR pipe (7).)
70 .PP
71 For a seekable file (i.e., one to which
72 .BR lseek (2)
73 may be applied, for example, a regular file)
74 writing takes place at the file offset,
75 and the file offset is incremented by
76 the number of bytes actually written.
77 If the file was
78 .BR open (2)ed
79 with
80 .BR O_APPEND ,
81 the file offset is first set to the end of the file before writing.
82 The adjustment of the file offset and the write operation
83 are performed as an atomic step.
84 .PP
85 POSIX requires that a
86 .BR read (2)
87 that can be proved to occur after a
88 .BR write ()
89 has returned will return the new data.
90 Note that not all filesystems are POSIX conforming.
91 .PP
92 According to POSIX.1, if
93 .I count
94 is greater than
95 .BR SSIZE_MAX ,
96 the result is implementation-defined;
97 see NOTES for the upper limit on Linux.
98 .SH RETURN VALUE
99 On success, the number of bytes written is returned.
100 On error, \-1 is returned, and \fIerrno\fP is set
101 to indicate the error.
103 Note that a successful
104 .BR write ()
105 may transfer fewer than
106 .I count
107 bytes.
108 Such partial writes can occur for various reasons;
109 for example, because there was insufficient space on the disk device
110 to write all of the requested bytes, or because a blocked
111 .BR write ()
112 to a socket, pipe, or similar was interrupted by a signal handler
113 after it had transferred some, but before it had transferred all
114 of the requested bytes.
115 In the event of a partial write, the caller can make another
116 .BR write ()
117 call to transfer the remaining bytes.
118 The subsequent call will either transfer further bytes or
119 may result in an error (e.g., if the disk is now full).
121 If \fIcount\fP is zero and
122 .I fd
123 refers to a regular file, then
124 .BR write ()
125 may return a failure status if one of the errors below is detected.
126 If no errors are detected, or error detection is not performed,
127 0 is returned without causing any other effect.
129 \fIcount\fP is zero and
130 .I fd
131 refers to a file other than a regular file,
132 the results are not specified.
133 .SH ERRORS
135 .B EAGAIN
136 The file descriptor
137 .I fd
138 refers to a file other than a socket and has been marked nonblocking
139 .RB ( O_NONBLOCK ),
140 and the write would block.
142 .BR open (2)
143 for further details on the
144 .BR O_NONBLOCK
145 flag.
147 .BR EAGAIN " or " EWOULDBLOCK
148 .\" Actually EAGAIN on Linux
149 The file descriptor
150 .I fd
151 refers to a socket and has been marked nonblocking
152 .RB ( O_NONBLOCK ),
153 and the write would block.
154 POSIX.1-2001 allows either error to be returned for this case,
155 and does not require these constants to have the same value,
156 so a portable application should check for both possibilities.
158 .B EBADF
159 .I fd
160 is not a valid file descriptor or is not open for writing.
162 .B EDESTADDRREQ
163 .I fd
164 refers to a datagram socket for which a peer address has not been set using
165 .BR connect (2).
167 .B EDQUOT
168 The user's quota of disk blocks on the filesystem containing the file
169 referred to by
170 .I fd
171 has been exhausted.
173 .B EFAULT
174 .I buf
175 is outside your accessible address space.
177 .B EFBIG
178 An attempt was made to write a file that exceeds the implementation-defined
179 maximum file size or the process's file size limit,
180 or to write at a position past the maximum allowed offset.
182 .B EINTR
183 The call was interrupted by a signal before any data was written; see
184 .BR signal (7).
186 .B EINVAL
187 .I fd
188 is attached to an object which is unsuitable for writing;
189 or the file was opened with the
190 .B O_DIRECT
191 flag, and either the address specified in
192 .IR buf ,
193 the value specified in
194 .IR count ,
195 or the file offset is not suitably aligned.
197 .B EIO
198 A low-level I/O error occurred while modifying the inode.
199 This error may relate to the write-back of data written by an earlier
200 .BR write (),
201 which may have been issued to a different file descriptor on
202 the same file.
203 Since Linux 4.13, errors from write-back come
204 with a promise that they
205 .I may
206 be reported by subsequent.
207 .BR write ()
208 requests, and
209 .I will
210 be reported by a subsequent
211 .BR fsync (2)
212 (whether or not they were also reported by
213 .BR write ()).
214 .\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
215 An alternate cause of
216 .B EIO
217 on networked filesystems is when an advisory lock had been taken out
218 on the file descriptor and this lock has been lost.
219 See the
220 .I "Lost locks"
221 section of
222 .BR fcntl (2)
223 for further details.
225 .B ENOSPC
226 The device containing the file referred to by
227 .I fd
228 has no room for the data.
230 .B EPERM
231 The operation was prevented by a file seal; see
232 .BR fcntl (2).
234 .B EPIPE
235 .I fd
236 is connected to a pipe or socket whose reading end is closed.
237 When this happens the writing process will also receive a
238 .B SIGPIPE
239 signal.
240 (Thus, the write return value is seen only if the program
241 catches, blocks or ignores this signal.)
243 Other errors may occur, depending on the object connected to
244 .IR fd .
245 .SH CONFORMING TO
246 SVr4, 4.3BSD, POSIX.1-2001.
247 .\" SVr4 documents additional error
248 .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
250 Under SVr4 a write may be interrupted and return
251 .B EINTR
252 at any point,
253 not just before any data is written.
254 .SH NOTES
255 The types
256 .I size_t
258 .I ssize_t
259 are, respectively,
260 unsigned and signed integer data types specified by POSIX.1.
262 A successful return from
263 .BR write ()
264 does not make any guarantee that data has been committed to disk.
265 On some filesystems, including NFS, it does not even guarantee
266 that space has successfully been reserved for the data.
267 In this case,
268 some errors might be delayed until a future
269 .BR write (),
270 .BR fsync (2),
271 or even
272 .BR close (2).
273 The only way to be sure is to call
274 .BR fsync (2)
275 after you are done writing all your data.
277 If a
278 .BR write ()
279 is interrupted by a signal handler before any bytes are written,
280 then the call fails with the error
281 .BR EINTR ;
282 if it is interrupted after at least one byte has been written,
283 the call succeeds, and returns the number of bytes written.
285 On Linux,
286 .BR write ()
287 (and similar system calls) will transfer at most
288 0x7ffff000 (2,147,479,552) bytes,
289 returning the number of bytes actually transferred.
290 .\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
291 (This is true on both 32-bit and 64-bit systems.)
293 An error return value while performing
294 .BR write ()
295 using direct I/O does not mean the
296 entire write has failed.
297 Partial data may be written
298 and the data at the file offset on which the
299 .BR write ()
300 was attempted should be considered inconsistent.
301 .SH BUGS
302 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
303 ("Thread Interactions with Regular File Operations"):
305 .RS 4
306 All of the following functions shall be atomic with respect to
307 each other in the effects specified in POSIX.1-2008 when they
308 operate on regular files or symbolic links: ...
311 Among the APIs subsequently listed are
312 .BR write ()
314 .BR writev (2).
315 And among the effects that should be atomic across threads (and processes)
316 are updates of the file offset.
317 However, on Linux before version 3.14,
318 this was not the case: if two processes that share
319 an open file description (see
320 .BR open (2))
321 perform a
322 .BR write ()
324 .BR writev (2))
325 at the same time, then the I/O operations were not atomic
326 with respect to updating the file offset,
327 with the result that the blocks of data output by the two processes
328 might (incorrectly) overlap.
329 This problem was fixed in Linux 3.14.
330 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
331 .\"    From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
332 .\"    Subject: Update of file offset on write() etc. is non-atomic with I/O
333 .\"    Date: 2014-02-17 15:41:37 GMT
334 .\"    Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
335 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
336 .\"    Author: Linus Torvalds <torvalds@linux-foundation.org>
337 .\"    Date:   Mon Mar 3 09:36:58 2014 -0800
339 .\"        vfs: atomic f_pos accesses as per POSIX
340 .SH SEE ALSO
341 .BR close (2),
342 .BR fcntl (2),
343 .BR fsync (2),
344 .BR ioctl (2),
345 .BR lseek (2),
346 .BR open (2),
347 .BR pwrite (2),
348 .BR read (2),
349 .BR select (2),
350 .BR writev (2),
351 .BR fwrite (3)