]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
fts.3, isalpha.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc...
[thirdparty/man-pages.git] / man2 / open.2
CommitLineData
fea681da 1.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
fd185f58
MK
2.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3.\" and Copyright (C) 2008 Greg Banks
7b8ba76c 4.\" and Copyright (C) 2006, 2008, 2013, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
93015253 6.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
7.\" Permission is granted to make and distribute verbatim copies of this
8.\" manual provided the copyright notice and this permission notice are
9.\" preserved on all copies.
10.\"
11.\" Permission is granted to copy and distribute modified versions of this
12.\" manual under the conditions for verbatim copying, provided that the
13.\" entire resulting derived work is distributed under the terms of a
14.\" permission notice identical to this one.
c13182ef 15.\"
fea681da
MK
16.\" Since the Linux kernel and libraries are constantly changing, this
17.\" manual page may be incorrect or out-of-date. The author(s) assume no
18.\" responsibility for errors or omissions, or for damages resulting from
19.\" the use of the information contained herein. The author(s) may not
20.\" have taken the same level of care in the production of this manual,
21.\" which is licensed free of charge, as they might when working
22.\" professionally.
c13182ef 23.\"
fea681da
MK
24.\" Formatted or processed versions of this manual, if unaccompanied by
25.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 26.\" %%%LICENSE_END
fea681da
MK
27.\"
28.\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
29.\" Modified 1994-08-21 by Michael Haardt
30.\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
31.\" Modified 1996-05-13 by Thomas Koenig
32.\" Modified 1996-12-20 by Michael Haardt
33.\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
34.\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
35.\" Modified 1999-06-03 by Michael Haardt
c11b1abf
MK
36.\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
37.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
1c1e15ed
MK
38.\" 2004-12-08, mtk, reordered flags list alphabetically
39.\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
fe75ec04 40.\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
447bb15e 41.\" 2008-01-03, mtk, with input from Trond Myklebust
f4b9d6a5
MK
42.\" <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
43.\" Rewrite description of O_EXCL.
ddc4d339
MK
44.\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
45.\" on O_DIRECT.
d77eb764 46.\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
fea681da 47.\"
61b7c1e1 48.\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
9f91e36c
MK
49.\" O_TTYINIT. Eventually these may need to be documented. --mtk
50.\"
6cf19e62 51.TH OPEN 2 2014-03-16 "Linux" "Linux Programmer's Manual"
fea681da 52.SH NAME
7b8ba76c 53open, openat, creat \- open and possibly create a file
fea681da
MK
54.SH SYNOPSIS
55.nf
56.B #include <sys/types.h>
57.B #include <sys/stat.h>
58.B #include <fcntl.h>
59.sp
60.BI "int open(const char *" pathname ", int " flags );
61.BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
5895e7eb 62
fea681da 63.BI "int creat(const char *" pathname ", mode_t " mode );
7b8ba76c
MK
64.sp
65.BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
66.BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
67", mode_t " mode );
fea681da 68.fi
7b8ba76c
MK
69.sp
70.in -4n
71Feature Test Macro Requirements for glibc (see
72.BR feature_test_macros (7)):
73.in
74.sp
75.BR openat ():
76.PD 0
77.ad l
78.RS 4
79.TP 4
80Since glibc 2.10:
81_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
82.TP
83Before glibc 2.10:
84_ATFILE_SOURCE
85.RE
86.ad
87.PD
fea681da 88.SH DESCRIPTION
e366dbc4 89Given a
0daa9e92 90.I pathname
e366dbc4 91for a file,
1f6ceb40 92.BR open ()
2fda57bd 93returns a file descriptor, a small, nonnegative integer
e366dbc4
MK
94for use in subsequent system calls
95.RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
96The file descriptor returned by a successful call will be
2c4bff36 97the lowest-numbered file descriptor not currently open for the process.
e366dbc4 98.PP
fe75ec04 99By default, the new file descriptor is set to remain open across an
e366dbc4 100.BR execve (2)
1f6ceb40
MK
101(i.e., the
102.B FD_CLOEXEC
103file descriptor flag described in
104.BR fcntl (2)
fd3ac440 105is initially disabled; the
fe75ec04
MK
106.B O_CLOEXEC
107flag, described below, can be used to change this default).
1f6ceb40 108The file offset is set to the beginning of the file (see
c13182ef 109.BR lseek (2)).
e366dbc4
MK
110.PP
111A call to
112.BR open ()
113creates a new
114.IR "open file description" ,
115an entry in the system-wide table of open files.
e366dbc4
MK
116This entry records the file offset and the file status flags
117(modifiable via the
0bfa087b 118.BR fcntl (2)
e366dbc4
MK
119.B F_SETFL
120operation).
2c4bff36
MK
121A file descriptor is a reference to one of these entries;
122this reference is unaffected if
123.I pathname
124is subsequently removed or modified to refer to a different file.
e366dbc4 125The new open file description is initially not shared
2c4bff36
MK
126with any other process,
127but sharing may arise via
128.BR fork (2).
e366dbc4 129.PP
c4bb193f 130The argument
fea681da 131.I flags
e366dbc4
MK
132must include one of the following
133.IR "access modes" :
c7992edc 134.BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
e366dbc4
MK
135These request opening the file read-only, write-only, or read/write,
136respectively.
bfe9ba67
MK
137
138In addition, zero or more file creation flags and file status flags
c13182ef 139can be
fea681da 140.RI bitwise- or 'd
e366dbc4 141in
bfe9ba67 142.IR flags .
c13182ef
MK
143The
144.I file creation flags
145are
0e40804c 146.BR O_CLOEXEC ,
b072a788 147.BR O_CREAT ,
0e40804c
MK
148.BR O_DIRECTORY ,
149.BR O_EXCL ,
150.BR O_NOCTTY ,
151.BR O_NOFOLLOW ,
f2698a42 152.BR O_TMPFILE ,
0e40804c
MK
153.BR O_TRUNC ,
154and
155.BR O_TTY_INIT .
c13182ef
MK
156The
157.I file status flags
bfe9ba67 158are all of the remaining flags listed below.
0e40804c 159.\" SUSv4 divides the flags into:
93ee8f96
MK
160.\" * Access mode
161.\" * File creation
162.\" * File status
163.\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
164.\" though it's not clear what the difference between "other" and
0e40804c
MK
165.\" "File creation" flags is. I raised an Aardvark to see if this
166.\" can be clarified in SUSv4; 10 Oct 2008.
167.\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
168.\" TC1 (balloted in 2013), resolved this, so that those three constants
169.\" are also categorized" as file status flags.
170.\"
bfe9ba67
MK
171The distinction between these two groups of flags is that
172the file status flags can be retrieved and (in some cases)
566b427d
MK
173modified; see
174.BR fcntl (2)
175for details.
176
bfe9ba67 177The full list of file creation flags and file status flags is as follows:
fea681da 178.TP
1c1e15ed 179.B O_APPEND
c13182ef
MK
180The file is opened in append mode.
181Before each
0bfa087b 182.BR write (2),
1e568304 183the file offset is positioned at the end of the file,
1c1e15ed 184as if with
0bfa087b 185.BR lseek (2).
1c1e15ed 186.B O_APPEND
9ee4a2b6 187may lead to corrupted files on NFS filesystems if more than one process
c13182ef 188appends data to a file at once.
a4391429
MK
189.\" For more background, see
190.\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
191.\" http://nfs.sourceforge.net/
c13182ef 192This is because NFS does not support
1c1e15ed
MK
193appending to a file, so the client kernel has to simulate it, which
194can't be done without a race condition.
195.TP
196.B O_ASYNC
b50582eb 197Enable signal-driven I/O:
8bd58774
MK
198generate a signal
199.RB ( SIGIO
200by default, but this can be changed via
1c1e15ed
MK
201.BR fcntl (2))
202when input or output becomes possible on this file descriptor.
33a0ccb2 203This feature is available only for terminals, pseudoterminals,
1f6ceb40
MK
204sockets, and (since Linux 2.6) pipes and FIFOs.
205See
1c1e15ed
MK
206.BR fcntl (2)
207for further details.
9bde4908 208See also BUGS, below.
fe75ec04 209.TP
31c1f2b0 210.BR O_CLOEXEC " (since Linux 2.6.23)"
fe75ec04 211Enable the close-on-exec flag for the new file descriptor.
24ec631f 212Specifying this flag permits a program to avoid additional
fe75ec04
MK
213.BR fcntl (2)
214.B F_SETFD
24ec631f 215operations to set the
0daa9e92 216.B FD_CLOEXEC
fe75ec04
MK
217flag.
218Additionally,
219use of this flag is essential in some multithreaded programs
220since using a separate
221.BR fcntl (2)
222.B F_SETFD
223operation to set the
0daa9e92 224.B FD_CLOEXEC
fe75ec04
MK
225flag does not suffice to avoid race conditions
226where one thread opens a file descriptor at the same
227time as another thread does a
228.BR fork (2)
229plus
230.BR execve (2).
231.\" This flag fixes only one form of the race condition;
232.\" The race can also occur with, for example, descriptors
233.\" returned by accept(), pipe(), etc.
1c1e15ed 234.TP
fea681da
MK
235.B O_CREAT
236If the file does not exist it will be created.
237The owner (user ID) of the file is set to the effective user ID
c13182ef
MK
238of the process.
239The group ownership (group ID) is set either to
fea681da 240the effective group ID of the process or to the group ID of the
9ee4a2b6 241parent directory (depending on filesystem type and mount options,
0fb83d00 242and the mode of the parent directory; see the mount options
fea681da
MK
243.I bsdgroups
244and
245.I sysvgroups
8b39ad66 246described in
fea681da 247.BR mount (8)).
8b39ad66
MK
248.\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
249.\" XFS (since 2.6.14).
4e698277
MK
250.RS
251.PP
252.I mode
253specifies the permissions to use in case a new file is created.
254This argument must be supplied when
255.B O_CREAT
f2698a42
AL
256or
257.B O_TMPFILE
4e698277
MK
258is specified in
259.IR flags ;
f2698a42 260if neither
4e698277 261.B O_CREAT
f2698a42
AL
262nor
263.B O_TMPFILE
264is specified, then
4e698277
MK
265.I mode
266is ignored.
267The effective permissions are modified by
268the process's
269.I umask
270in the usual way: The permissions of the created file are
84a275c4 271.IR "(mode\ &\ ~umask)" .
33a0ccb2 272Note that this mode applies only to future accesses of the
4e698277
MK
273newly created file; the
274.BR open ()
275call that creates a read-only file may well return a read/write
276file descriptor.
277.PP
278The following symbolic constants are provided for
279.IR mode :
280.TP 9
281.B S_IRWXU
28200700 user (file owner) has read, write and execute permission
283.TP
284.B S_IRUSR
28500400 user has read permission
286.TP
287.B S_IWUSR
28800200 user has write permission
289.TP
290.B S_IXUSR
29100100 user has execute permission
292.TP
293.B S_IRWXG
29400070 group has read, write and execute permission
295.TP
296.B S_IRGRP
29700040 group has read permission
298.TP
299.B S_IWGRP
30000020 group has write permission
301.TP
302.B S_IXGRP
30300010 group has execute permission
304.TP
305.B S_IRWXO
30600007 others have read, write and execute permission
307.TP
308.B S_IROTH
30900004 others have read permission
310.TP
311.B S_IWOTH
31200002 others have write permission
313.TP
314.B S_IXOTH
31500001 others have execute permission
316.RE
fea681da 317.TP
31c1f2b0 318.BR O_DIRECT " (since Linux 2.4.10)"
1c1e15ed
MK
319Try to minimize cache effects of the I/O to and from this file.
320In general this will degrade performance, but it is useful in
321special situations, such as when applications do their own caching.
bce0482f 322File I/O is done directly to/from user-space buffers.
015221ef
CH
323The
324.B O_DIRECT
0deb3ce9 325flag on its own makes an effort to transfer data synchronously,
015221ef
CH
326but does not give the guarantees of the
327.B O_SYNC
0deb3ce9
JM
328flag that data and necessary metadata are transferred.
329To guarantee synchronous I/O,
015221ef
CH
330.B O_SYNC
331must be used in addition to
332.BR O_DIRECT .
be02e49f 333See NOTES below for further discussion.
9b54d4fa 334.sp
c13182ef 335A semantically similar (but deprecated) interface for block devices
9b54d4fa 336is described in
1c1e15ed
MK
337.BR raw (8).
338.TP
339.B O_DIRECTORY
a8d55537 340If \fIpathname\fP is not a directory, cause the open to fail.
9f8d688a
MK
341.\" But see the following and its replies:
342.\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
343.\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
344.\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
8382f16d 345This flag is Linux-specific, and was added in kernel version 2.1.126, to
60a90ecd
MK
346avoid denial-of-service problems if
347.BR opendir (3)
348is called on a
a3041a58 349FIFO or tape device.
1c1e15ed 350.TP
6cf19e62
MK
351.B O_DSYNC
352Write operations on the file will complete according to the requirements of
353synchronized I/O
354.I data
355integrity completion.
356
357By the time
358.BR write (2)
359(and similar)
360return, the output data
361has been transferred to the underlying hardware,
362along with any file metadata that would be required to retrieve that data
363(i.e., as though each
364.BR write (2)
365was followed by a call to
366.BR fdatasync (2)).
367.IR "See NOTES below" .
368.TP
fea681da 369.B O_EXCL
f4b9d6a5
MK
370Ensure that this call creates the file:
371if this flag is specified in conjunction with
fea681da 372.BR O_CREAT ,
f4b9d6a5
MK
373and
374.I pathname
375already exists, then
1c1e15ed 376.BR open ()
c13182ef 377will fail.
f4b9d6a5
MK
378
379When these two flags are specified, symbolic links are not followed:
380.\" POSIX.1-2001 explicitly requires this behavior.
381if
382.I pathname
383is a symbolic link, then
384.BR open ()
385fails regardless of where the symbolic link points to.
386
10b7a945
IHV
387In general, the behavior of
388.B O_EXCL
389is undefined if it is used without
390.BR O_CREAT .
391There is one exception: on Linux 2.6 and later,
392.B O_EXCL
393can be used without
394.B O_CREAT
395if
396.I pathname
397refers to a block device.
6303d401
DB
398If the block device is in use by the system (e.g., mounted),
399.BR open ()
10b7a945
IHV
400fails with the error
401.BR EBUSY .
402
efe08656 403On NFS,
f4b9d6a5 404.B O_EXCL
33a0ccb2 405is supported only when using NFSv3 or later on kernel 2.6 or later.
efe08656 406In NFS environments where
fea681da 407.B O_EXCL
f4b9d6a5
MK
408support is not provided, programs that rely on it
409for performing locking tasks will contain a race condition.
410Portable programs that want to perform atomic file locking using a lockfile,
411and need to avoid reliance on NFS support for
412.BR O_EXCL ,
413can create a unique file on
9ee4a2b6 414the same filesystem (e.g., incorporating hostname and PID), and use
fea681da 415.BR link (2)
c13182ef 416to make a link to the lockfile.
60a90ecd
MK
417If
418.BR link (2)
f4b9d6a5 419returns 0, the lock is successful.
c13182ef 420Otherwise, use
fea681da
MK
421.BR stat (2)
422on the unique file to check if its link count has increased to 2,
423in which case the lock is also successful.
424.TP
1c1e15ed
MK
425.B O_LARGEFILE
426(LFS)
427Allow files whose sizes cannot be represented in an
8478ee02 428.I off_t
1c1e15ed 429(but can be represented in an
8478ee02 430.IR off64_t )
1c1e15ed 431to be opened.
c13182ef 432The
bcdd964e 433.B _LARGEFILE64_SOURCE
e417acb0
MK
434macro must be defined
435(before including
436.I any
437header files)
438in order to obtain this definition.
c13182ef 439Setting the
bcdd964e 440.B _FILE_OFFSET_BITS
9f3d8b28
MK
441feature test macro to 64 (rather than using
442.BR O_LARGEFILE )
12e263f1 443is the preferred
9f3d8b28 444method of accessing large files on 32-bit systems (see
2dcbf4f7 445.BR feature_test_macros (7)).
1c1e15ed 446.TP
31c1f2b0 447.BR O_NOATIME " (since Linux 2.6.8)"
1bb72c96
MK
448Do not update the file last access time
449.RI ( st_atime
450in the inode)
310b7919 451when the file is
1c1e15ed
MK
452.BR read (2).
453This flag is intended for use by indexing or backup programs,
454where its use can significantly reduce the amount of disk activity.
9ee4a2b6 455This flag may not be effective on all filesystems.
1c1e15ed 456One example is NFS, where the server maintains the access time.
0e1ad98c 457.\" The O_NOATIME flag also affects the treatment of st_atime
92057f4d 458.\" by mmap() and readdir(2), MTK, Dec 04.
1c1e15ed 459.TP
fea681da
MK
460.B O_NOCTTY
461If
462.I pathname
5503c85e 463refers to a terminal device\(emsee
1bb72c96
MK
464.BR tty (4)\(emit
465will not become the process's controlling terminal even if the
fea681da
MK
466process does not have one.
467.TP
1c1e15ed 468.B O_NOFOLLOW
a8d55537 469If \fIpathname\fP is a symbolic link, then the open fails.
c13182ef 470This is a FreeBSD extension, which was added to Linux in version 2.1.126.
1c1e15ed 471Symbolic links in earlier components of the pathname will still be
e366dbc4 472followed.
1135dbe1 473See also
843068bd 474.BR O_PATH
1135dbe1 475below.
e366dbc4
MK
476.\" The headers from glibc 2.0.100 and later include a
477.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
a8d55537 478.\" used\fP.
fea681da
MK
479.TP
480.BR O_NONBLOCK " or " O_NDELAY
ff40dbb3 481When possible, the file is opened in nonblocking mode.
c13182ef 482Neither the
1c1e15ed 483.BR open ()
fea681da
MK
484nor any subsequent operations on the file descriptor which is
485returned will cause the calling process to wait.
486For the handling of FIFOs (named pipes), see also
af5b2ef2 487.BR fifo (7).
db28bfac 488For a discussion of the effect of
0daa9e92 489.B O_NONBLOCK
db28bfac
MK
490in conjunction with mandatory file locks and with file leases, see
491.BR fcntl (2).
fea681da 492.TP
1135dbe1
MK
493.BR O_PATH " (since Linux 2.6.39)"
494.\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
495.\" commit 326be7b484843988afe57566b627fb7a70beac56
496.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
497.\"
498.\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
499.\" Subject: Re: [PATCH] open(2): document O_PATH
500.\" Newsgroups: gmane.linux.man, gmane.linux.kernel
501.\"
1135dbe1 502Obtain a file descriptor that can be used for two purposes:
9ee4a2b6 503to indicate a location in the filesystem tree and
1135dbe1
MK
504to perform operations that act purely at the file descriptor level.
505The file itself is not opened, and other file operations (e.g.,
506.BR read (2),
507.BR write (2),
508.BR fchmod (2),
509.BR fchown (2),
2510e4e5
RH
510.BR fgetxattr (2),
511.BR mmap (2))
1135dbe1
MK
512fail with the error
513.BR EBADF .
514
515The following operations
516.I can
517be performed on the resulting file descriptor:
518.RS
519.IP * 3
520.BR close (2);
521.BR fchdir (2)
522(since Linux 3.5);
523.\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
524.BR fstat (2)
525(since Linux 3.6).
526.\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
527.IP *
528Duplicating the file descriptor
529.RB ( dup (2),
530.BR fcntl (2)
531.BR F_DUPFD ,
532etc.).
533.IP *
534Getting and setting file descriptor flags
535.RB ( fcntl (2)
536.BR F_GETFD
537and
538.BR F_SETFD ).
09f677a3
MK
539.IP *
540Retrieving open file status flags using the
541.BR fcntl (2)
13a082cb 542.BR F_GETFL
09f677a3
MK
543operation: the returned flags will include the bit
544.BR O_PATH .
545
1135dbe1
MK
546.IP *
547Passing the file descriptor as the
548.IR dirfd
549argument of
550.BR openat (2)
551and the other "*at()" system calls.
552.IP *
553Passing the file descriptor to another process via a UNIX domain socket
554(see
555.BR SCM_RIGHTS
556in
557.BR unix (7)).
558.RE
559.IP
560When
561.B O_PATH
562is specified in
563.IR flags ,
564flag bits other than
565.BR O_DIRECTORY
566and
567.BR O_NOFOLLOW
568are ignored.
569
d30344ab
MK
570If
571.I pathname
572is a symbolic link and the
1135dbe1
MK
573.BR O_NOFOLLOW
574flag is also specified,
575then the call returns a file descriptor referring to the symbolic link.
576This file descriptor can be used as the
577.I dirfd
578argument in calls to
579.BR fchownat (2),
580.BR fstatat (2),
581.BR linkat (2),
582and
583.BR readlinkat (2)
584with an empty pathname to have the calls operate on the symbolic link.
585.TP
fea681da 586.B O_SYNC
6cf19e62
MK
587Write operations on the file will complete according to the requirements of
588synchronized I/O
589.I file
590integrity completion
591(by contrast with contrast with the
592synchronized I/O
593.I data
594integrity completion
595provided by
596.BR O_DSYNC .)
597
598By the time
599.BR write (2)
600(and similar)
601return, the output data and associated file metadata
602have been transferred to the underlying hardware
603(i.e., as though each
604.BR write (2)
605was followed by a call to
606.BR fsync (2)).
607.IR "See NOTES below" .
fea681da 608.TP
40398c1a
MK
609.BR O_TMPFILE " (since Linux 3.11)"
610.\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
611.\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
612.\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
613Create an unnamed temporary file.
614The
615.I pathname
616argument specifies a directory;
617an unnamed inode will be created in that directory's filesystem.
618Anything written to the resulting file will be lost when
619the last file descriptor is closed, unless the file is given a name.
620
621.B O_TMPFILE
622must be specified with one of
623.B O_RDWR
624or
625.B O_WRONLY
626and, optionally,
627.BR O_EXCL .
628If
629.B O_EXCL
630is not specified, then
631.BR linkat (2)
632can be used to link the temporary file into the filesystem, making it
633permanent, using code like the following:
634
635.in +4n
636.nf
637char path[PATH_MAX];
638fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
0fb83d00
MK
639 S_IRUSR | S_IWUSR);
640
40398c1a 641/* File I/O on 'fd'... */
0fb83d00 642
40398c1a 643snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
e1252130 644linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
0fb83d00 645 AT_SYMLINK_FOLLOW);
40398c1a
MK
646.fi
647.in
648
649In this case,
650the
651.BR open ()
652.I mode
653argument determines the file permission mode, as with
654.BR O_CREAT .
655
0115aaed
MK
656Specifying
657.B O_EXCL
658in conjunction with
659.B O_TMPFILE
660prevents a temporary file from being linked into the filesystem
661in the above manner.
662(Note that the meaning of
663.B O_EXCL
664in this case is different from the meaning of
665.B O_EXCL
666otherwise.)
667
668
40398c1a
MK
669There are two main use cases for
670.\" Inspired by http://lwn.net/Articles/559147/
671.BR O_TMPFILE :
672.RS
673.IP * 3
674Improved
675.BR tmpfile (3)
676functionality: race-free creation of temporary files that
677(1) are automatically deleted when closed;
678(2) can never be reached via any pathname;
679(3) are not subject to symlink attacks; and
680(4) do not require the caller to devise unique names.
681.IP *
682Creating a file that is initially invisible, which is then populated
8b04592d 683with data and adjusted to have appropriate filesystem attributes
40398c1a
MK
684.RB ( chown (2),
685.BR chmod (2),
686.BR fsetxattr (2),
687etc.)
688before being atomically linked into the filesystem
689in a fully formed state (using
690.BR linkat (2)
691as described above).
692.RE
693.IP
694.B O_TMPFILE
695requires support by the underlying filesystem;
696.\" As at 3.13, there's support for at least ext2, ext3, ext4
697only a subset of Linux filesystems provide that support.
698.TP
1c1e15ed 699.B O_TRUNC
4d61d36a 700If the file already exists and is a regular file and the access mode allows
682edefb
MK
701writing (i.e., is
702.B O_RDWR
703or
704.BR O_WRONLY )
705it will be truncated to length 0.
706If the file is a FIFO or terminal device file, the
707.B O_TRUNC
c13182ef 708flag is ignored.
682edefb
MK
709Otherwise the effect of
710.B O_TRUNC
711is unspecified.
7b8ba76c 712.SS creat()
1c1e15ed 713.BR creat ()
fea681da 714is equivalent to
1c1e15ed 715.BR open ()
fea681da
MK
716with
717.I flags
718equal to
719.BR O_CREAT|O_WRONLY|O_TRUNC .
7b8ba76c
MK
720.SS openat()
721The
722.BR openat ()
723system call operates in exactly the same way as
cadd38ba 724.BR open (),
7b8ba76c
MK
725except for the differences described here.
726
727If the pathname given in
728.I pathname
729is relative, then it is interpreted relative to the directory
d30344ab 730relative to by the file descriptor
7b8ba76c
MK
731.I dirfd
732(rather than relative to the current working directory of
733the calling process, as is done by
cadd38ba 734.BR open ()
7b8ba76c
MK
735for a relative pathname).
736
737If
738.I pathname
739is relative and
740.I dirfd
741is the special value
742.BR AT_FDCWD ,
743then
744.I pathname
745is interpreted relative to the current working
746directory of the calling process (like
cadd38ba 747.BR open ()).
7b8ba76c
MK
748
749If
750.I pathname
751is absolute, then
752.I dirfd
753is ignored.
47297adb 754.SH RETURN VALUE
7b8ba76c
MK
755.BR open (),
756.BR openat (),
c13182ef 757and
e1d6264d 758.BR creat ()
1c1e15ed
MK
759return the new file descriptor, or \-1 if an error occurred
760(in which case,
fea681da
MK
761.I errno
762is set appropriately).
fea681da 763.SH ERRORS
7b8ba76c
MK
764.BR open (),
765.BR openat (),
766and
767.BR creat ()
768can fail with the following errors:
fea681da
MK
769.TP
770.B EACCES
771The requested access to the file is not allowed, or search permission
772is denied for one of the directories in the path prefix of
773.IR pathname ,
774or the file did not exist yet and write access to the parent directory
775is not allowed.
776(See also
ad7cc990 777.BR path_resolution (7).)
fea681da 778.TP
a1f01685
MH
779.B EDQUOT
780Where
781.B O_CREAT
782is specified, the file does not exist, and the user's quota of disk
9ee4a2b6 783blocks or inodes on the filesystem has been exhausted.
a1f01685 784.TP
fea681da
MK
785.B EEXIST
786.I pathname
787already exists and
788.BR O_CREAT " and " O_EXCL
789were used.
790.TP
791.B EFAULT
0daa9e92 792.I pathname
e1d6264d 793points outside your accessible address space.
fea681da 794.TP
9f5773f7 795.B EFBIG
7c7fb552
MK
796See
797.BR EOVERFLOW .
9f5773f7 798.TP
e51412ea
MK
799.B EINTR
800While blocked waiting to complete an open of a slow device
801(e.g., a FIFO; see
802.BR fifo (7)),
803the call was interrupted by a signal handler; see
804.BR signal (7).
805.TP
ef490193
DG
806.B EINVAL
807The filesystem does not support the
808.BR O_DIRECT
e6f89ed2
MK
809flag.
810See
ef490193
DG
811.BR NOTES
812for more information.
813.TP
8e335391
MK
814.B EINVAL
815Invalid value in
816.\" In particular, __O_TMPFILE instead of O_TMPFILE
817.IR flags .
818.TP
819.B EINVAL
820.B O_TMPFILE
821was specified in
822.IR flags ,
823but neither
824.B O_WRONLY
825nor
826.B O_RDWR
827was specified.
828.TP
fea681da
MK
829.B EISDIR
830.I pathname
831refers to a directory and the access requested involved writing
832(that is,
833.B O_WRONLY
834or
835.B O_RDWR
836is set).
837.TP
8e335391 838.B EISDIR
843068bd
MK
839.I pathname
840refers to an existing directory,
8e335391
MK
841.B O_TMPFILE
842and one of
843.B O_WRONLY
844or
845.B O_RDWR
846were specified in
847.IR flags ,
848but this kernel version does not provide the
849.B O_TMPFILE
850functionality.
851.TP
fea681da
MK
852.B ELOOP
853Too many symbolic links were encountered in resolving
289f7907
MK
854.IR pathname .
855.TP
856.B ELOOP
fea681da 857.I pathname
289f7907
MK
858was a symbolic link, and
859.I flags
860specified
861.BR O_NOFOLLOW
862but not
863.BR O_PATH .
fea681da
MK
864.TP
865.B EMFILE
866The process already has the maximum number of files open.
867.TP
868.B ENAMETOOLONG
0daa9e92 869.I pathname
e1d6264d 870was too long.
fea681da
MK
871.TP
872.B ENFILE
873The system limit on the total number of open files has been reached.
874.TP
875.B ENODEV
876.I pathname
877refers to a device special file and no corresponding device exists.
682edefb
MK
878(This is a Linux kernel bug; in this situation
879.B ENXIO
880must be returned.)
fea681da
MK
881.TP
882.B ENOENT
682edefb
MK
883.B O_CREAT
884is not set and the named file does not exist.
fea681da
MK
885Or, a directory component in
886.I pathname
887does not exist or is a dangling symbolic link.
888.TP
ba03011f
MK
889.B ENOENT
890.I pathname
891refers to a nonexistent directory,
892.B O_TMPFILE
893and one of
894.B O_WRONLY
895or
896.B O_RDWR
897were specified in
898.IR flags ,
899but this kernel version does not provide the
900.B O_TMPFILE
901functionality.
902.TP
fea681da
MK
903.B ENOMEM
904Insufficient kernel memory was available.
905.TP
906.B ENOSPC
907.I pathname
908was to be created but the device containing
909.I pathname
910has no room for the new file.
911.TP
912.B ENOTDIR
913A component used as a directory in
914.I pathname
a8d55537 915is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
fea681da
MK
916.I pathname
917was not a directory.
918.TP
919.B ENXIO
682edefb
MK
920.BR O_NONBLOCK " | " O_WRONLY
921is set, the named file is a FIFO and
fea681da
MK
922no process has the file open for reading.
923Or, the file is a device special file and no corresponding device exists.
924.TP
bbe02b45
MK
925.BR EOPNOTSUPP
926The filesystem containing
927.I pathname
928does not support
929.BR O_TMPFILE .
930.TP
7c7fb552
MK
931.B EOVERFLOW
932.I pathname
933refers to a regular file that is too large to be opened.
934The usual scenario here is that an application compiled
935on a 32-bit platform without
5e4dc269 936.I -D_FILE_OFFSET_BITS=64
7c7fb552
MK
937tried to open a file whose size exceeds
938.I (2<<31)-1
939bits;
940see also
941.B O_LARGEFILE
942above.
943This is the error specified by POSIX.1-2001;
944in kernels before 2.6.24, Linux gave the error
945.B EFBIG
946for this case.
947.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
948.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
949.\" Reported 2006-10-03
950.TP
1c1e15ed
MK
951.B EPERM
952The
953.B O_NOATIME
954flag was specified, but the effective user ID of the caller
9ee4a2b6 955.\" Strictly speaking, it's the filesystem UID... (MTK)
1c1e15ed
MK
956did not match the owner of the file and the caller was not privileged
957.RB ( CAP_FOWNER ).
958.TP
fea681da
MK
959.B EROFS
960.I pathname
9ee4a2b6 961refers to a file on a read-only filesystem and write access was
fea681da
MK
962requested.
963.TP
964.B ETXTBSY
965.I pathname
966refers to an executable image which is currently being executed and
967write access was requested.
d3952311
MK
968.TP
969.B EWOULDBLOCK
970The
971.B O_NONBLOCK
972flag was specified, and an incompatible lease was held on the file
973(see
974.BR fcntl (2)).
7b8ba76c
MK
975.PP
976The following additional errors can occur for
977.BR openat ():
978.TP
979.B EBADF
980.I dirfd
981is not a valid file descriptor.
982.TP
983.B ENOTDIR
984.I pathname
985is relative and
986.I dirfd
987is a file descriptor referring to a file other than a directory.
988.SH VERSIONS
989.BR openat ()
990was added to Linux in kernel 2.6.16;
991library support was added to glibc in version 2.4.
47297adb 992.SH CONFORMING TO
7b8ba76c
MK
993.BR open (),
994.BR creat ()
72ac7268
MK
995SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
996
7b8ba76c
MK
997.BR openat ():
998POSIX.1-2008.
7b8ba76c 999
fea681da 1000The
72ac7268 1001.BR O_DIRECT ,
1c1e15ed 1002.BR O_NOATIME ,
72ac7268 1003.BR O_PATH ,
fea681da 1004and
72ac7268
MK
1005.BR O_TMPFILE
1006flags are Linux-specific.
1007One must define
61b7c1e1
MK
1008.B _GNU_SOURCE
1009to obtain their definitions.
9f91e36c
MK
1010
1011The
72ac7268
MK
1012.BR O_CLOEXEC ,
1013.BR O_DIRECTORY ,
1014and
1015.BR O_NOFOLLOW
1016flags are not specified in POSIX.1-2001,
1017but are specified in POSIX.1-2008.
1018Since glibc 2.12, one can obtain their definitions by defining either
1019.B _POSIX_C_SOURCE
1020with a value greater than or equal to 200809L or
1021.BR _XOPEN_SOURCE
1022with a value greater than or equal to 700.
1023In glibc 2.11 and earlier, one obtains the definitions by defining
1024.BR _GNU_SOURCE .
9f91e36c 1025
72ac7268
MK
1026As noted in
1027.BR feature_test_macros (7),
84fc2a6e 1028feature test macros such as
72ac7268
MK
1029.BR _POSIX_C_SOURCE ,
1030.BR _XOPEN_SOURCE ,
1031and
fe75ec04 1032.B _GNU_SOURCE
72ac7268 1033must be defined before including
e417acb0 1034.I any
72ac7268 1035header files.
a1d5f77c 1036.SH NOTES
988db661 1037Under Linux, the
a1d5f77c
MK
1038.B O_NONBLOCK
1039flag indicates that one wants to open
1040but does not necessarily have the intention to read or write.
1041This is typically used to open devices in order to get a file descriptor
1042for use with
1043.BR ioctl (2).
c734b9f2 1044
fea681da
MK
1045.LP
1046The (undefined) effect of
1047.B O_RDONLY | O_TRUNC
c13182ef 1048varies among implementations.
bcdd964e 1049On many systems the file is actually truncated.
fea681da
MK
1050.\" Linux 2.0, 2.5: truncate
1051.\" Solaris 5.7, 5.8: truncate
1052.\" Irix 6.5: truncate
1053.\" Tru64 5.1B: truncate
1054.\" HP-UX 11.22: truncate
1055.\" FreeBSD 4.7: truncate
a1d5f77c 1056
5dc8986d
MK
1057Note that
1058.BR open ()
1059can open device special files, but
1060.BR creat ()
1061cannot create them; use
1062.BR mknod (2)
1063instead.
1064
1065If the file is newly created, its
1066.IR st_atime ,
1067.IR st_ctime ,
1068.I st_mtime
1069fields
1070(respectively, time of last access, time of last status change, and
1071time of last modification; see
1072.BR stat (2))
1073are set
1074to the current time, and so are the
1075.I st_ctime
1076and
1077.I st_mtime
1078fields of the
1079parent directory.
1080Otherwise, if the file is modified because of the
1081.B O_TRUNC
1082flag, its st_ctime and st_mtime fields are set to the current time.
1083.\"
1084.\"
1085.SS Synchronized I/O
6cf19e62
MK
1086The POSIX.1-2008 "synchronized I/O" option
1087specifies different variants of synchronized I/O,
1088and specifies the
1089.BR open ()
1090flags
015221ef
CH
1091.BR O_SYNC ,
1092.BR O_DSYNC ,
1093and
6cf19e62
MK
1094.BR O_RSYNC
1095for controlling the behavior.
1096Regardless of whether an implementation supports this option,
1097it must at least support the use of
1098.BR O_SYNC
1099for regular files.
1100
1101Linux implements
1102.BR O_SYNC
1103and
1104.BR O_DSYNC ,
1105but not
015221ef 1106.BR O_RSYNC .
6cf19e62
MK
1107(Somewhat incorrectly, glibc defines
1108.BR O_RSYNC
1109to have the same value as
1110.BR O_SYNC .)
1111
1112.BR O_SYNC
1113provides synchronized I/O
1114.I file
1115integrity completion,
1116meaning write operations will flush data and all associated metadata
1117to the underlying hardware.
1118.BR O_DSYNC
1119provides synchronized I/O
1120.I data
1121integrity completion,
1122meaning write operations will flush data
1123to the underlying hardware,
1124but will only flush metadata updates that are required
1125to allow a subsequent read operation to complete successfully.
1126Data integrity completion can reduce the number of disk operations
1127that are required for applications that don't need the guarantees
1128of file integrity completion.
1129
1130To understand the difference between the the two types of completion,
1131consider two pieces of file metadata:
1132the file last modification timestamp
1133.RI ( st_mtime )
1134and the file length.
1135All write operations will update the last file modification timestamp,
1136but only writes that add data to the end of the
1137file will change the file length.
1138The last modification timestamp is not needed to ensure that
1139a read completes successfully, but the file length is.
1140Thus,
1141.BR O_DSYNC
1142would only guarantee to flush updates to the file length metadata
1143(whereas
1144.BR O_SYNC
1145would also always flush the last modification timestamp metadata).
1146
1147Before Linux 2.6.33, Linux implemented only the
1148.BR O_SYNC
1149flag for
1150.BR open ().
1151However, when that flag was specified,
1152most filesystems actually provided the equivalent of synchronized I/O
1153.I data
1154integrity completion (i.e.,
1155.BR O_SYNC
1156was actually implemented as the equivalent of
1157.BR O_DSYNC ).
1158
1159Since Linux 2.6.33, proper
1160.BR O_SYNC
1161support is provided.
1162However, to ensure backward binary compatibility,
1163.BR O_DSYNC
1164was defined with the same value as the historical
015221ef 1165.BR O_SYNC ,
015221ef 1166and
6cf19e62
MK
1167.BR O_SYNC
1168was defined as a new (two-bit) flag value that includes the
1169.BR O_DSYNC
1170flag value.
1171This ensures that applications compiled against
1172new headers get at least
1173.BR O_DSYNC
1174semantics on pre-2.6.33 kernels.
5dc8986d
MK
1175.\"
1176.\"
1177.SS NFS
1178There are many infelicities in the protocol underlying NFS, affecting
1179amongst others
1180.BR O_SYNC " and " O_NDELAY .
a1d5f77c 1181
9ee4a2b6 1182On NFS filesystems with UID mapping enabled,
a1d5f77c
MK
1183.BR open ()
1184may
75b94dc3 1185return a file descriptor but, for example,
a1d5f77c
MK
1186.BR read (2)
1187requests are denied
1188with \fBEACCES\fP.
1189This is because the client performs
1190.BR open ()
1191by checking the
1192permissions, but UID mapping is performed by the server upon
1193read and write requests.
5dc8986d
MK
1194.\"
1195.\"
1196.SS File access mode
1197Unlike the other values that can be specified in
1198.IR flags ,
1199the
1200.I "access mode"
1201values
1202.BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1203do not specify individual bits.
1204Rather, they define the low order two bits of
1205.IR flags ,
1206and are defined respectively as 0, 1, and 2.
1207In other words, the combination
1208.B "O_RDONLY | O_WRONLY"
1209is a logical error, and certainly does not have the same meaning as
1210.BR O_RDWR .
a1d5f77c 1211
5dc8986d
MK
1212Linux reserves the special, nonstandard access mode 3 (binary 11) in
1213.I flags
1214to mean:
1215check for read and write permission on the file and return a descriptor
1216that can't be used for reading or writing.
1217This nonstandard access mode is used by some Linux drivers to return a
1218descriptor that is to be used only for device-specific
1219.BR ioctl (2)
1220operations.
1221.\" See for example util-linux's disk-utils/setfdprm.c
1222.\" For some background on access mode 3, see
1223.\" http://thread.gmane.org/gmane.linux.kernel/653123
1224.\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1225.\" LKML, 12 Mar 2008
7b8ba76c
MK
1226.\"
1227.\"
80d250b4 1228.SS Rationale for openat() and other "directory file descriptor" APIs
7b8ba76c 1229.BR openat ()
80d250b4
MK
1230and the other system calls and library functions that take
1231a directory file descriptor argument
7b8ba76c
MK
1232(i.e.,
1233.BR faccessat (2),
80d250b4 1234.BR fanotify_mark (2),
7b8ba76c
MK
1235.BR fchmodat (2),
1236.BR fchownat (2),
1237.BR fstatat (2),
1238.BR futimesat (2),
1239.BR linkat (2),
1240.BR mkdirat (2),
1241.BR mknodat (2),
80d250b4 1242.BR name_to_handle_at (2),
7b8ba76c
MK
1243.BR readlinkat (2),
1244.BR renameat (2),
1245.BR symlinkat (2),
1246.BR unlinkat (2),
1247.BR utimensat (2)
80d250b4 1248.BR mkfifoat (3),
7b8ba76c 1249and
80d250b4 1250.BR scandirat (3))
7b8ba76c
MK
1251are supported
1252for two reasons.
92692952 1253Here, the explanation is in terms of the
7b8ba76c 1254.BR openat ()
d26f8a31 1255call, but the rationale is analogous for the other interfaces.
7b8ba76c
MK
1256
1257First,
1258.BR openat ()
1259allows an application to avoid race conditions that could
1260occur when using
cadd38ba 1261.BR open ()
7b8ba76c
MK
1262to open files in directories other than the current working directory.
1263These race conditions result from the fact that some component
1264of the directory prefix given to
cadd38ba 1265.BR open ()
7b8ba76c 1266could be changed in parallel with the call to
cadd38ba 1267.BR open ().
7b8ba76c
MK
1268Such races can be avoided by
1269opening a file descriptor for the target directory,
1270and then specifying that file descriptor as the
1271.I dirfd
1272argument of
1273.BR openat ().
1274
1275Second,
1276.BR openat ()
1277allows the implementation of a per-thread "current working
1278directory", via file descriptor(s) maintained by the application.
1279(This functionality can also be obtained by tricks based
1280on the use of
1281.IR /proc/self/fd/ dirfd,
1282but less efficiently.)
1283.\"
1284.\"
ddc4d339
MK
1285.SS O_DIRECT
1286.LP
1287The
1288.B O_DIRECT
1289flag may impose alignment restrictions on the length and address
7fac88a9 1290of user-space buffers and the file offset of I/Os.
ddc4d339 1291In Linux alignment
9ee4a2b6 1292restrictions vary by filesystem and kernel version and might be
ddc4d339 1293absent entirely.
9ee4a2b6 1294However there is currently no filesystem\-independent
ddc4d339 1295interface for an application to discover these restrictions for a given
9ee4a2b6
MK
1296file or filesystem.
1297Some filesystems provide their own interfaces
ddc4d339
MK
1298for doing so, for example the
1299.B XFS_IOC_DIOINFO
1300operation in
1301.BR xfsctl (3).
1302.LP
85c2bdba
MK
1303Under Linux 2.4, transfer sizes, and the alignment of the user buffer
1304and the file offset must all be multiples of the logical block size
9ee4a2b6 1305of the filesystem.
04cd7f64 1306Under Linux 2.6, alignment to 512-byte boundaries suffices.
1847167b
NP
1307.LP
1308.B O_DIRECT
1309I/Os should never be run concurrently with the
04cd7f64 1310.BR fork (2)
1847167b
NP
1311system call,
1312if the memory buffer is a private mapping
1313(i.e., any mapping created with the
02ace852 1314.BR mmap (2)
1847167b 1315.BR MAP_PRIVATE
0ab8aeec 1316flag;
1847167b
NP
1317this includes memory allocated on the heap and statically allocated buffers).
1318Any such I/Os, whether submitted via an asynchronous I/O interface or from
1319another thread in the process,
1320should be completed before
1321.BR fork (2)
1322is called.
1323Failure to do so can result in data corruption and undefined behavior in
1324parent and child processes.
1325This restriction does not apply when the memory buffer for the
1326.B O_DIRECT
1327I/Os was created using
1328.BR shmat (2)
1329or
1330.BR mmap (2)
1331with the
1332.B MAP_SHARED
1333flag.
1334Nor does this restriction apply when the memory buffer has been advised as
1335.B MADV_DONTFORK
0ab8aeec 1336with
02ace852 1337.BR madvise (2),
1847167b
NP
1338ensuring that it will not be available
1339to the child after
1340.BR fork (2).
ddc4d339
MK
1341.LP
1342The
1343.B O_DIRECT
1344flag was introduced in SGI IRIX, where it has alignment
1345restrictions similar to those of Linux 2.4.
1346IRIX has also a
1347.BR fcntl (2)
1348call to query appropriate alignments, and sizes.
1349FreeBSD 4.x introduced
1350a flag of the same name, but without alignment restrictions.
1351.LP
1352.B O_DIRECT
1353support was added under Linux in kernel version 2.4.10.
1354Older Linux kernels simply ignore this flag.
9ee4a2b6 1355Some filesystems may not implement the flag and
ddc4d339
MK
1356.BR open ()
1357will fail with
1358.B EINVAL
1359if it is used.
1360.LP
1361Applications should avoid mixing
1362.B O_DIRECT
1363and normal I/O to the same file,
1364and especially to overlapping byte regions in the same file.
9ee4a2b6 1365Even when the filesystem correctly handles the coherency issues in
ddc4d339
MK
1366this situation, overall I/O throughput is likely to be slower than
1367using either mode alone.
1368Likewise, applications should avoid mixing
1369.BR mmap (2)
1370of files with direct I/O to the same files.
1371.LP
1372The behaviour of
1373.B O_DIRECT
9ee4a2b6 1374with NFS will differ from local filesystems.
ddc4d339
MK
1375Older kernels, or
1376kernels configured in certain ways, may not support this combination.
1377The NFS protocol does not support passing the flag to the server, so
1378.B O_DIRECT
33a0ccb2 1379I/O will bypass the page cache only on the client; the server may
ddc4d339
MK
1380still cache the I/O.
1381The client asks the server to make the I/O
1382synchronous to preserve the synchronous semantics of
1383.BR O_DIRECT .
1384Some servers will perform poorly under these circumstances, especially
1385if the I/O size is small.
1386Some servers may also be configured to
1387lie to clients about the I/O having reached stable storage; this
1388will avoid the performance penalty at some risk to data integrity
1389in the event of server power failure.
1390The Linux NFS client places no alignment restrictions on
1391.B O_DIRECT
1392I/O.
1393.PP
1394In summary,
1395.B O_DIRECT
1396is a potentially powerful tool that should be used with caution.
1397It is recommended that applications treat use of
1398.B O_DIRECT
1399as a performance option which is disabled by default.
1400.PP
1401.RS
fea681da
MK
1402"The thing that has always disturbed me about O_DIRECT is that the whole
1403interface is just stupid, and was probably designed by a deranged monkey
5503c85e 1404on some serious mind-controlling substances."\(emLinus
ddc4d339
MK
1405.RE
1406.SH BUGS
b50582eb
MK
1407Currently, it is not possible to enable signal-driven
1408I/O by specifying
1409.B O_ASYNC
c13182ef 1410when calling
b50582eb
MK
1411.BR open ();
1412use
1413.BR fcntl (2)
1414to enable this flag.
0e1ad98c 1415.\" FIXME . Check bugzilla report on open(O_ASYNC)
92057f4d 1416.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
0d730fcc
MK
1417
1418One must check for two different error codes,
1419.B EISDIR
1420and
1421.BR ENOENT ,
1422when trying to determine whether the kernel supports
0d55b37f 1423.B O_TMPFILE
0d730fcc 1424functionality.
47297adb 1425.SH SEE ALSO
a3bf8022
MK
1426.BR chmod (2),
1427.BR chown (2),
fea681da 1428.BR close (2),
e366dbc4 1429.BR dup (2),
fea681da
MK
1430.BR fcntl (2),
1431.BR link (2),
1f6ceb40 1432.BR lseek (2),
fea681da 1433.BR mknod (2),
e366dbc4 1434.BR mmap (2),
f0c34053 1435.BR mount (2),
fea681da
MK
1436.BR read (2),
1437.BR socket (2),
1438.BR stat (2),
1439.BR umask (2),
1440.BR unlink (2),
1441.BR write (2),
1442.BR fopen (3),
f0c34053 1443.BR fifo (7),
a9cfde1d
MK
1444.BR path_resolution (7),
1445.BR symlink (7)