open.2: Improve O_PATH documentation
[man-pages.git] / man2 / open.2
blob38d532397b0200296ee2e2ee49050d2dbcf3442a
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2008 Greg Banks
4 .\" and Copyright (C) 2006, 2008, 2013, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
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.
15 .\"
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.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
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
36 .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
38 .\" 2004-12-08, mtk, reordered flags list alphabetically
39 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
40 .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
41 .\" 2008-01-03, mtk, with input from Trond Myklebust
42 .\"     <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
43 .\"     Rewrite description of O_EXCL.
44 .\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
45 .\"     on O_DIRECT.
46 .\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
47 .\"
48 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
49 .\" O_TTYINIT.  Eventually these may need to be documented.  --mtk
50 .\"
51 .TH OPEN 2 2017-05-03 "Linux" "Linux Programmer's Manual"
52 .SH NAME
53 open, openat, creat \- open and possibly create a file
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 );
63 .BI "int creat(const char *" pathname ", mode_t " mode );
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 );
68 .fi
69 .sp
70 .in -4n
71 Feature 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
80 Since glibc 2.10:
81 _POSIX_C_SOURCE\ >=\ 200809L
82 .TP
83 Before glibc 2.10:
84 _ATFILE_SOURCE
85 .RE
86 .ad
87 .PD
88 .SH DESCRIPTION
89 Given a
90 .I pathname
91 for a file,
92 .BR open ()
93 returns a file descriptor, a small, nonnegative integer
94 for use in subsequent system calls
95 .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
96 The file descriptor returned by a successful call will be
97 the lowest-numbered file descriptor not currently open for the process.
98 .PP
99 By default, the new file descriptor is set to remain open across an
100 .BR execve (2)
101 (i.e., the
102 .B FD_CLOEXEC
103 file descriptor flag described in
104 .BR fcntl (2)
105 is initially disabled); the
106 .B O_CLOEXEC
107 flag, described below, can be used to change this default.
108 The file offset is set to the beginning of the file (see
109 .BR lseek (2)).
111 A call to
112 .BR open ()
113 creates a new
114 .IR "open file description" ,
115 an entry in the system-wide table of open files.
116 The open file description records the file offset and the file status flags
117 (see below).
118 A file descriptor is a reference to an open file description;
119 this reference is unaffected if
120 .I pathname
121 is subsequently removed or modified to refer to a different file.
122 For further details on open file descriptions, see NOTES.
124 The argument
125 .I flags
126 must include one of the following
127 .IR "access modes" :
128 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
129 These request opening the file read-only, write-only, or read/write,
130 respectively.
132 In addition, zero or more file creation flags and file status flags
133 can be
134 .RI bitwise- or 'd
136 .IR flags .
138 .I file creation flags
140 .BR O_CLOEXEC ,
141 .BR O_CREAT ,
142 .BR O_DIRECTORY ,
143 .BR O_EXCL ,
144 .BR O_NOCTTY ,
145 .BR O_NOFOLLOW ,
146 .BR O_TMPFILE ,
148 .BR O_TRUNC .
150 .I file status flags
151 are all of the remaining flags listed below.
152 .\" SUSv4 divides the flags into:
153 .\" * Access mode
154 .\" * File creation
155 .\" * File status
156 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
157 .\" though it's not clear what the difference between "other" and
158 .\" "File creation" flags is.  I raised an Aardvark to see if this
159 .\" can be clarified in SUSv4; 10 Oct 2008.
160 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
161 .\" TC1 (balloted in 2013), resolved this, so that those three constants
162 .\" are also categorized" as file status flags.
164 The distinction between these two groups of flags is that
165 the file creation flags affect the semantics of the open operation itself,
166 while the file status flags affect the semantics of subsequent I/O operations.
167 The file status flags can be retrieved and (in some cases)
168 modified; see
169 .BR fcntl (2)
170 for details.
172 The full list of file creation flags and file status flags is as follows:
174 .B O_APPEND
175 The file is opened in append mode.
176 Before each
177 .BR write (2),
178 the file offset is positioned at the end of the file,
179 as if with
180 .BR lseek (2).
181 The modification of the file offset and the write operation
182 are performed as a single atomic step.
184 .B O_APPEND
185 may lead to corrupted files on NFS filesystems if more than one process
186 appends data to a file at once.
187 .\" For more background, see
188 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
189 .\" http://nfs.sourceforge.net/
190 This is because NFS does not support
191 appending to a file, so the client kernel has to simulate it, which
192 can't be done without a race condition.
194 .B O_ASYNC
195 Enable signal-driven I/O:
196 generate a signal
197 .RB ( SIGIO
198 by default, but this can be changed via
199 .BR fcntl (2))
200 when input or output becomes possible on this file descriptor.
201 This feature is available only for terminals, pseudoterminals,
202 sockets, and (since Linux 2.6) pipes and FIFOs.
204 .BR fcntl (2)
205 for further details.
206 See also BUGS, below.
208 .BR O_CLOEXEC " (since Linux 2.6.23)"
209 .\" NOTE! several other man pages refer to this text
210 Enable the close-on-exec flag for the new file descriptor.
211 .\" FIXME . for later review when Issue 8 is one day released...
212 .\" POSIX proposes to fix many APIs that provide hidden FDs
213 .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
214 .\" http://austingroupbugs.net/view.php?id=368
215 Specifying this flag permits a program to avoid additional
216 .BR fcntl (2)
217 .B F_SETFD
218 operations to set the
219 .B FD_CLOEXEC
220 flag.
222 Note that the use of this flag is essential in some multithreaded programs,
223 because using a separate
224 .BR fcntl (2)
225 .B F_SETFD
226 operation to set the
227 .B FD_CLOEXEC
228 flag does not suffice to avoid race conditions
229 where one thread opens a file descriptor and
230 attempts to set its close-on-exec flag using
231 .BR fcntl (2)
232 at the same time as another thread does a
233 .BR fork (2)
234 plus
235 .BR execve (2).
236 Depending on the order of execution,
237 the race may lead to the file descriptor returned by
238 .BR open ()
239 being unintentionally leaked to the program executed by the child process
240 created by
241 .BR fork (2).
242 (This kind of race is in principle possible for any system call
243 that creates a file descriptor whose close-on-exec flag should be set,
244 and various other Linux system calls provide an equivalent of the
245 .BR O_CLOEXEC
246 flag to deal with this problem.)
247 .\" This flag fixes only one form of the race condition;
248 .\" The race can also occur with, for example, file descriptors
249 .\" returned by accept(), pipe(), etc.
251 .B O_CREAT
252 If the file does not exist, it will be created.
254 The owner (user ID) of the new file is set to the effective user ID
255 of the process.
257 The group ownership (group ID) of the new file is set either to
258 the effective group ID of the process (System V semantics)
259 or to the group ID of the parent directory (BSD semantics).
260 On Linux, the behavior depends on whether the
261 set-group-ID mode bit is set on the parent directory:
262 if that bit is set, then BSD semantics apply;
263 otherwise, System V semantics apply.
264 For some filesystems, the behavior also depends on the
265 .I bsdgroups
267 .I sysvgroups
268 mount options described in
269 .BR mount (8)).
270 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
271 .\" XFS (since 2.6.14).
275 .I mode
276 argument specifies the file mode bits be applied when a new file is created.
277 This argument must be supplied when
278 .B O_CREAT
280 .B O_TMPFILE
281 is specified in
282 .IR flags ;
283 if neither
284 .B O_CREAT
286 .B O_TMPFILE
287 is specified, then
288 .I mode
289 is ignored.
290 The effective mode is modified by the process's
291 .I umask
292 in the usual way: in the absence of a default ACL, the mode of the
293 created file is
294 .IR "(mode\ &\ ~umask)" .
295 Note that this mode applies only to future accesses of the
296 newly created file; the
297 .BR open ()
298 call that creates a read-only file may well return a read/write
299 file descriptor.
301 The following symbolic constants are provided for
302 .IR mode :
303 .TP 9
304 .B S_IRWXU
305 00700 user (file owner) has read, write, and execute permission
307 .B S_IRUSR
308 00400 user has read permission
310 .B S_IWUSR
311 00200 user has write permission
313 .B S_IXUSR
314 00100 user has execute permission
316 .B S_IRWXG
317 00070 group has read, write, and execute permission
319 .B S_IRGRP
320 00040 group has read permission
322 .B S_IWGRP
323 00020 group has write permission
325 .B S_IXGRP
326 00010 group has execute permission
328 .B S_IRWXO
329 00007 others have read, write, and execute permission
331 .B S_IROTH
332 00004 others have read permission
334 .B S_IWOTH
335 00002 others have write permission
337 .B S_IXOTH
338 00001 others have execute permission
341 According to POSIX, the effect when other bits are set in
342 .I mode
343 is unspecified.
344 On Linux, the following bits are also honored in
345 .IR mode :
347 .TP 9
348 .B S_ISUID
349 0004000 set-user-ID bit
351 .B S_ISGID
352 0002000 set-group-ID bit (see
353 .BR inode (7)).
355 .B S_ISVTX
356 0001000 sticky bit (see
357 .BR inode (7)).
360 .BR O_DIRECT " (since Linux 2.4.10)"
361 Try to minimize cache effects of the I/O to and from this file.
362 In general this will degrade performance, but it is useful in
363 special situations, such as when applications do their own caching.
364 File I/O is done directly to/from user-space buffers.
366 .B O_DIRECT
367 flag on its own makes an effort to transfer data synchronously,
368 but does not give the guarantees of the
369 .B O_SYNC
370 flag that data and necessary metadata are transferred.
371 To guarantee synchronous I/O,
372 .B O_SYNC
373 must be used in addition to
374 .BR O_DIRECT .
375 See NOTES below for further discussion.
377 A semantically similar (but deprecated) interface for block devices
378 is described in
379 .BR raw (8).
381 .B O_DIRECTORY
382 If \fIpathname\fP is not a directory, cause the open to fail.
383 .\" But see the following and its replies:
384 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
385 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
386 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
387 This flag was added in kernel version 2.1.126, to
388 avoid denial-of-service problems if
389 .BR opendir (3)
390 is called on a
391 FIFO or tape device.
393 .B O_DSYNC
394 Write operations on the file will complete according to the requirements of
395 synchronized I/O
396 .I data
397 integrity completion.
399 By the time
400 .BR write (2)
401 (and similar)
402 return, the output data
403 has been transferred to the underlying hardware,
404 along with any file metadata that would be required to retrieve that data
405 (i.e., as though each
406 .BR write (2)
407 was followed by a call to
408 .BR fdatasync (2)).
409 .IR "See NOTES below" .
411 .B O_EXCL
412 Ensure that this call creates the file:
413 if this flag is specified in conjunction with
414 .BR O_CREAT ,
416 .I pathname
417 already exists, then
418 .BR open ()
419 will fail.
421 When these two flags are specified, symbolic links are not followed:
422 .\" POSIX.1-2001 explicitly requires this behavior.
424 .I pathname
425 is a symbolic link, then
426 .BR open ()
427 fails regardless of where the symbolic link points to.
429 In general, the behavior of
430 .B O_EXCL
431 is undefined if it is used without
432 .BR O_CREAT .
433 There is one exception: on Linux 2.6 and later,
434 .B O_EXCL
435 can be used without
436 .B O_CREAT
438 .I pathname
439 refers to a block device.
440 If the block device is in use by the system (e.g., mounted),
441 .BR open ()
442 fails with the error
443 .BR EBUSY .
445 On NFS,
446 .B O_EXCL
447 is supported only when using NFSv3 or later on kernel 2.6 or later.
448 In NFS environments where
449 .B O_EXCL
450 support is not provided, programs that rely on it
451 for performing locking tasks will contain a race condition.
452 Portable programs that want to perform atomic file locking using a lockfile,
453 and need to avoid reliance on NFS support for
454 .BR O_EXCL ,
455 can create a unique file on
456 the same filesystem (e.g., incorporating hostname and PID), and use
457 .BR link (2)
458 to make a link to the lockfile.
460 .BR link (2)
461 returns 0, the lock is successful.
462 Otherwise, use
463 .BR stat (2)
464 on the unique file to check if its link count has increased to 2,
465 in which case the lock is also successful.
467 .B O_LARGEFILE
468 (LFS)
469 Allow files whose sizes cannot be represented in an
470 .I off_t
471 (but can be represented in an
472 .IR off64_t )
473 to be opened.
475 .B _LARGEFILE64_SOURCE
476 macro must be defined
477 (before including
478 .I any
479 header files)
480 in order to obtain this definition.
481 Setting the
482 .B _FILE_OFFSET_BITS
483 feature test macro to 64 (rather than using
484 .BR O_LARGEFILE )
485 is the preferred
486 method of accessing large files on 32-bit systems (see
487 .BR feature_test_macros (7)).
489 .BR O_NOATIME " (since Linux 2.6.8)"
490 Do not update the file last access time
491 .RI ( st_atime
492 in the inode)
493 when the file is
494 .BR read (2).
496 This flag can be employed only if one of the following conditions is true:
498 .IP * 3
499 The effective UID of the process
500 .\" Strictly speaking: the filesystem UID
501 matches the owner UID of the file.
502 .IP *
503 The calling process has the
504 .BR CAP_FOWNER
505 capability in its user namespace and
506 the owner UID of the file has a mapping in the namespace.
509 This flag is intended for use by indexing or backup programs,
510 where its use can significantly reduce the amount of disk activity.
511 This flag may not be effective on all filesystems.
512 One example is NFS, where the server maintains the access time.
513 .\" The O_NOATIME flag also affects the treatment of st_atime
514 .\" by mmap() and readdir(2), MTK, Dec 04.
516 .B O_NOCTTY
518 .I pathname
519 refers to a terminal device\(emsee
520 .BR tty (4)\(emit
521 will not become the process's controlling terminal even if the
522 process does not have one.
524 .B O_NOFOLLOW
525 If \fIpathname\fP is a symbolic link, then the open fails, with the error
526 .BR ELOOP .
527 Symbolic links in earlier components of the pathname will still be
528 followed.
529 (Note that the
530 .B ELOOP
531 error that can occur in this case is indistinguishable from the case where
532 an open fails because there are too many symbolic links found
533 while resolving components in the prefix part of the pathname.)
535 This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
536 and has subsequently been standardized in POSIX.1-2008.
538 See also
539 .BR O_PATH
540 below.
541 .\" The headers from glibc 2.0.100 and later include a
542 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
543 .\" used\fP.
545 .BR O_NONBLOCK " or " O_NDELAY
546 When possible, the file is opened in nonblocking mode.
547 Neither the
548 .BR open ()
549 nor any subsequent operations on the file descriptor which is
550 returned will cause the calling process to wait.
552 Note that this flag has no effect for regular files and block devices;
553 that is, I/O operations will (briefly) block when device activity
554 is required, regardless of whether
555 .B O_NONBLOCK
556 is set.
557 Since
558 .B O_NONBLOCK
559 semantics might eventually be implemented,
560 applications should not depend upon blocking behavior
561 when specifying this flag for regular files and block devices.
563 For the handling of FIFOs (named pipes), see also
564 .BR fifo (7).
565 For a discussion of the effect of
566 .B O_NONBLOCK
567 in conjunction with mandatory file locks and with file leases, see
568 .BR fcntl (2).
570 .BR O_PATH " (since Linux 2.6.39)"
571 .\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
572 .\" commit 326be7b484843988afe57566b627fb7a70beac56
573 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
575 .\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
576 .\"     Subject: Re: [PATCH] open(2): document O_PATH
577 .\"     Newsgroups: gmane.linux.man, gmane.linux.kernel
579 Obtain a file descriptor that can be used for two purposes:
580 to indicate a location in the filesystem tree and
581 to perform operations that act purely at the file descriptor level.
582 The file itself is not opened, and other file operations (e.g.,
583 .BR read (2),
584 .BR write (2),
585 .BR fchmod (2),
586 .BR fchown (2),
587 .BR fgetxattr (2),
588 .BR ioctl (2),
589 .BR mmap (2))
590 fail with the error
591 .BR EBADF .
593 The following operations
594 .I can
595 be performed on the resulting file descriptor:
597 .IP * 3
598 .BR close (2);
599 .BR fchdir (2)
600 (since Linux 3.5);
601 .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
602 .BR fstat (2)
603 (since Linux 3.6);
604 .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
605 .BR fstatfs (2)
606 (since Linux 3.12).
607 .\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf
608 .IP *
609 Duplicating the file descriptor
610 .RB ( dup (2),
611 .BR fcntl (2)
612 .BR F_DUPFD ,
613 etc.).
614 .IP *
615 Getting and setting file descriptor flags
616 .RB ( fcntl (2)
617 .BR F_GETFD
619 .BR F_SETFD ).
620 .IP *
621 Retrieving open file status flags using the
622 .BR fcntl (2)
623 .BR F_GETFL
624 operation: the returned flags will include the bit
625 .BR O_PATH .
626 .IP *
627 Passing the file descriptor as the
628 .IR dirfd
629 argument of
630 .BR openat ()
631 and the other "*at()" system calls.
632 This includes
633 .BR linkat (2)
634 with
635 .BR AT_EMPTY_PATH
636 (or via procfs using
637 .BR AT_SYMLINK_FOLLOW )
638 even if the file is not a directory.
639 .IP *
640 Passing the file descriptor to another process via a UNIX domain socket
641 (see
642 .BR SCM_RIGHTS
644 .BR unix (7)).
647 When
648 .B O_PATH
649 is specified in
650 .IR flags ,
651 flag bits other than
652 .BR O_CLOEXEC ,
653 .BR O_DIRECTORY ,
655 .BR O_NOFOLLOW
656 are ignored.
659 .I pathname
660 is a symbolic link and the
661 .BR O_NOFOLLOW
662 flag is also specified,
663 then the call returns a file descriptor referring to the symbolic link.
664 This file descriptor can be used as the
665 .I dirfd
666 argument in calls to
667 .BR fchownat (2),
668 .BR fstatat (2),
669 .BR linkat (2),
671 .BR readlinkat (2)
672 with an empty pathname to have the calls operate on the symbolic link.
675 .I pathname
676 refers to an automount point that has not yet been triggered, so no
677 other filesystem is mounted on it, then the call returns a file
678 descriptor referring to the automount directory without triggering a mount.
679 .BR fstatfs (2)
680 can then be used to determine if it is, in fact, an untriggered
681 automount point
682 .RB ( ".f_type == AUTOFS_SUPER_MAGIC" ).
684 .B O_SYNC
685 Write operations on the file will complete according to the requirements of
686 synchronized I/O
687 .I file
688 integrity completion
689 (by contrast with the
690 synchronized I/O
691 .I data
692 integrity completion
693 provided by
694 .BR O_DSYNC .)
696 By the time
697 .BR write (2)
698 (and similar)
699 return, the output data and associated file metadata
700 have been transferred to the underlying hardware
701 (i.e., as though each
702 .BR write (2)
703 was followed by a call to
704 .BR fsync (2)).
705 .IR "See NOTES below" .
707 .BR O_TMPFILE " (since Linux 3.11)"
708 .\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
709 .\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
710 .\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
711 Create an unnamed temporary file.
713 .I pathname
714 argument specifies a directory;
715 an unnamed inode will be created in that directory's filesystem.
716 Anything written to the resulting file will be lost when
717 the last file descriptor is closed, unless the file is given a name.
719 .B O_TMPFILE
720 must be specified with one of
721 .B O_RDWR
723 .B O_WRONLY
724 and, optionally,
725 .BR O_EXCL .
727 .B O_EXCL
728 is not specified, then
729 .BR linkat (2)
730 can be used to link the temporary file into the filesystem, making it
731 permanent, using code like the following:
733 .in +4n
735 char path[PATH_MAX];
736 fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
737                         S_IRUSR | S_IWUSR);
739 /* File I/O on 'fd'... */
741 snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
742 linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
743                         AT_SYMLINK_FOLLOW);
747 In this case,
749 .BR open ()
750 .I mode
751 argument determines the file permission mode, as with
752 .BR O_CREAT .
754 Specifying
755 .B O_EXCL
756 in conjunction with
757 .B O_TMPFILE
758 prevents a temporary file from being linked into the filesystem
759 in the above manner.
760 (Note that the meaning of
761 .B O_EXCL
762 in this case is different from the meaning of
763 .B O_EXCL
764 otherwise.)
766 There are two main use cases for
767 .\" Inspired by http://lwn.net/Articles/559147/
768 .BR O_TMPFILE :
770 .IP * 3
771 Improved
772 .BR tmpfile (3)
773 functionality: race-free creation of temporary files that
774 (1) are automatically deleted when closed;
775 (2) can never be reached via any pathname;
776 (3) are not subject to symlink attacks; and
777 (4) do not require the caller to devise unique names.
778 .IP *
779 Creating a file that is initially invisible, which is then populated
780 with data and adjusted to have appropriate filesystem attributes
781 .RB ( fchown (2),
782 .BR fchmod (2),
783 .BR fsetxattr (2),
784 etc.)
785 before being atomically linked into the filesystem
786 in a fully formed state (using
787 .BR linkat (2)
788 as described above).
791 .B O_TMPFILE
792 requires support by the underlying filesystem;
793 only a subset of Linux filesystems provide that support.
794 In the initial implementation, support was provided in
795 the ext2, ext3, ext4, UDF, Minix, and shmem filesystems.
796 .\" To check for support, grep for "tmpfile" in kernel sources
797 Support for other filesystems has subsequently been added as follows:
798 XFS (Linux 3.15);
799 .\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
800 .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
801 Btrfs (Linux 3.16);
802 .\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
803 F2FS (Linux 3.16);
804 .\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
805 and ubifs (Linux 4.9)
807 .B O_TRUNC
808 If the file already exists and is a regular file and the access mode allows
809 writing (i.e., is
810 .B O_RDWR
812 .BR O_WRONLY )
813 it will be truncated to length 0.
814 If the file is a FIFO or terminal device file, the
815 .B O_TRUNC
816 flag is ignored.
817 Otherwise, the effect of
818 .B O_TRUNC
819 is unspecified.
820 .SS creat()
821 A call to
822 .BR creat ()
823 is equivalent to calling
824 .BR open ()
825 with
826 .I flags
827 equal to
828 .BR O_CREAT|O_WRONLY|O_TRUNC .
829 .SS openat()
831 .BR openat ()
832 system call operates in exactly the same way as
833 .BR open (),
834 except for the differences described here.
836 If the pathname given in
837 .I pathname
838 is relative, then it is interpreted relative to the directory
839 referred to by the file descriptor
840 .I dirfd
841 (rather than relative to the current working directory of
842 the calling process, as is done by
843 .BR open ()
844 for a relative pathname).
847 .I pathname
848 is relative and
849 .I dirfd
850 is the special value
851 .BR AT_FDCWD ,
852 then
853 .I pathname
854 is interpreted relative to the current working
855 directory of the calling process (like
856 .BR open ()).
859 .I pathname
860 is absolute, then
861 .I dirfd
862 is ignored.
863 .SH RETURN VALUE
864 .BR open (),
865 .BR openat (),
867 .BR creat ()
868 return the new file descriptor, or \-1 if an error occurred
869 (in which case,
870 .I errno
871 is set appropriately).
872 .SH ERRORS
873 .BR open (),
874 .BR openat (),
876 .BR creat ()
877 can fail with the following errors:
879 .B EACCES
880 The requested access to the file is not allowed, or search permission
881 is denied for one of the directories in the path prefix of
882 .IR pathname ,
883 or the file did not exist yet and write access to the parent directory
884 is not allowed.
885 (See also
886 .BR path_resolution (7).)
888 .B EDQUOT
889 Where
890 .B O_CREAT
891 is specified, the file does not exist, and the user's quota of disk
892 blocks or inodes on the filesystem has been exhausted.
894 .B EEXIST
895 .I pathname
896 already exists and
897 .BR O_CREAT " and " O_EXCL
898 were used.
900 .B EFAULT
901 .I pathname
902 points outside your accessible address space.
904 .B EFBIG
906 .BR EOVERFLOW .
908 .B EINTR
909 While blocked waiting to complete an open of a slow device
910 (e.g., a FIFO; see
911 .BR fifo (7)),
912 the call was interrupted by a signal handler; see
913 .BR signal (7).
915 .B EINVAL
916 The filesystem does not support the
917 .BR O_DIRECT
918 flag.
920 .BR NOTES
921 for more information.
923 .B EINVAL
924 Invalid value in
925 .\" In particular, __O_TMPFILE instead of O_TMPFILE
926 .IR flags .
928 .B EINVAL
929 .B O_TMPFILE
930 was specified in
931 .IR flags ,
932 but neither
933 .B O_WRONLY
935 .B O_RDWR
936 was specified.
938 .B EISDIR
939 .I pathname
940 refers to a directory and the access requested involved writing
941 (that is,
942 .B O_WRONLY
944 .B O_RDWR
945 is set).
947 .B EISDIR
948 .I pathname
949 refers to an existing directory,
950 .B O_TMPFILE
951 and one of
952 .B O_WRONLY
954 .B O_RDWR
955 were specified in
956 .IR flags ,
957 but this kernel version does not provide the
958 .B O_TMPFILE
959 functionality.
961 .B ELOOP
962 Too many symbolic links were encountered in resolving
963 .IR pathname .
965 .B ELOOP
966 .I pathname
967 was a symbolic link, and
968 .I flags
969 specified
970 .BR O_NOFOLLOW
971 but not
972 .BR O_PATH .
974 .B EMFILE
975 The per-process limit on the number of open file descriptors has been reached
976 (see the description of
977 .BR RLIMIT_NOFILE
979 .BR getrlimit (2)).
981 .B ENAMETOOLONG
982 .I pathname
983 was too long.
985 .B ENFILE
986 The system-wide limit on the total number of open files has been reached.
988 .B ENODEV
989 .I pathname
990 refers to a device special file and no corresponding device exists.
991 (This is a Linux kernel bug; in this situation
992 .B ENXIO
993 must be returned.)
995 .B ENOENT
996 .B O_CREAT
997 is not set and the named file does not exist.
998 Or, a directory component in
999 .I pathname
1000 does not exist or is a dangling symbolic link.
1002 .B ENOENT
1003 .I pathname
1004 refers to a nonexistent directory,
1005 .B O_TMPFILE
1006 and one of
1007 .B O_WRONLY
1009 .B O_RDWR
1010 were specified in
1011 .IR flags ,
1012 but this kernel version does not provide the
1013 .B O_TMPFILE
1014 functionality.
1016 .B ENOMEM
1017 The named file is a FIFO,
1018 but memory for the FIFO buffer can't be allocated because
1019 the per-user hard limit on memory allocation for pipes has been reached
1020 and the caller is not privileged; see
1021 .BR pipe (7).
1023 .B ENOMEM
1024 Insufficient kernel memory was available.
1026 .B ENOSPC
1027 .I pathname
1028 was to be created but the device containing
1029 .I pathname
1030 has no room for the new file.
1032 .B ENOTDIR
1033 A component used as a directory in
1034 .I pathname
1035 is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
1036 .I pathname
1037 was not a directory.
1039 .B ENXIO
1040 .BR O_NONBLOCK " | " O_WRONLY
1041 is set, the named file is a FIFO, and
1042 no process has the FIFO open for reading.
1044 .B ENXIO
1045 The file is a device special file and no corresponding device exists.
1047 .BR EOPNOTSUPP
1048 The filesystem containing
1049 .I pathname
1050 does not support
1051 .BR O_TMPFILE .
1053 .B EOVERFLOW
1054 .I pathname
1055 refers to a regular file that is too large to be opened.
1056 The usual scenario here is that an application compiled
1057 on a 32-bit platform without
1058 .I -D_FILE_OFFSET_BITS=64
1059 tried to open a file whose size exceeds
1060 .I (1<<31)-1
1061 bytes;
1062 see also
1063 .B O_LARGEFILE
1064 above.
1065 This is the error specified by POSIX.1;
1066 in kernels before 2.6.24, Linux gave the error
1067 .B EFBIG
1068 for this case.
1069 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
1070 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
1071 .\" Reported 2006-10-03
1073 .B EPERM
1075 .B O_NOATIME
1076 flag was specified, but the effective user ID of the caller
1077 .\" Strictly speaking, it's the filesystem UID... (MTK)
1078 did not match the owner of the file and the caller was not privileged.
1080 .B EPERM
1081 The operation was prevented by a file seal; see
1082 .BR fcntl (2).
1084 .B EROFS
1085 .I pathname
1086 refers to a file on a read-only filesystem and write access was
1087 requested.
1089 .B ETXTBSY
1090 .I pathname
1091 refers to an executable image which is currently being executed and
1092 write access was requested.
1094 .B EWOULDBLOCK
1096 .B O_NONBLOCK
1097 flag was specified, and an incompatible lease was held on the file
1098 (see
1099 .BR fcntl (2)).
1101 The following additional errors can occur for
1102 .BR openat ():
1104 .B EBADF
1105 .I dirfd
1106 is not a valid file descriptor.
1108 .B ENOTDIR
1109 .I pathname
1110 is a relative pathname and
1111 .I dirfd
1112 is a file descriptor referring to a file other than a directory.
1113 .SH VERSIONS
1114 .BR openat ()
1115 was added to Linux in kernel 2.6.16;
1116 library support was added to glibc in version 2.4.
1117 .SH CONFORMING TO
1118 .BR open (),
1119 .BR creat ()
1120 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
1122 .BR openat ():
1123 POSIX.1-2008.
1126 .BR O_DIRECT ,
1127 .BR O_NOATIME ,
1128 .BR O_PATH ,
1130 .BR O_TMPFILE
1131 flags are Linux-specific.
1132 One must define
1133 .B _GNU_SOURCE
1134 to obtain their definitions.
1137 .BR O_CLOEXEC ,
1138 .BR O_DIRECTORY ,
1140 .BR O_NOFOLLOW
1141 flags are not specified in POSIX.1-2001,
1142 but are specified in POSIX.1-2008.
1143 Since glibc 2.12, one can obtain their definitions by defining either
1144 .B _POSIX_C_SOURCE
1145 with a value greater than or equal to 200809L or
1146 .BR _XOPEN_SOURCE
1147 with a value greater than or equal to 700.
1148 In glibc 2.11 and earlier, one obtains the definitions by defining
1149 .BR _GNU_SOURCE .
1151 As noted in
1152 .BR feature_test_macros (7),
1153 feature test macros such as
1154 .BR _POSIX_C_SOURCE ,
1155 .BR _XOPEN_SOURCE ,
1157 .B _GNU_SOURCE
1158 must be defined before including
1159 .I any
1160 header files.
1161 .SH NOTES
1162 Under Linux, the
1163 .B O_NONBLOCK
1164 flag indicates that one wants to open
1165 but does not necessarily have the intention to read or write.
1166 This is typically used to open devices in order to get a file descriptor
1167 for use with
1168 .BR ioctl (2).
1170 The (undefined) effect of
1171 .B O_RDONLY | O_TRUNC
1172 varies among implementations.
1173 On many systems the file is actually truncated.
1174 .\" Linux 2.0, 2.5: truncate
1175 .\" Solaris 5.7, 5.8: truncate
1176 .\" Irix 6.5: truncate
1177 .\" Tru64 5.1B: truncate
1178 .\" HP-UX 11.22: truncate
1179 .\" FreeBSD 4.7: truncate
1181 Note that
1182 .BR open ()
1183 can open device special files, but
1184 .BR creat ()
1185 cannot create them; use
1186 .BR mknod (2)
1187 instead.
1189 If the file is newly created, its
1190 .IR st_atime ,
1191 .IR st_ctime ,
1192 .I st_mtime
1193 fields
1194 (respectively, time of last access, time of last status change, and
1195 time of last modification; see
1196 .BR stat (2))
1197 are set
1198 to the current time, and so are the
1199 .I st_ctime
1201 .I st_mtime
1202 fields of the
1203 parent directory.
1204 Otherwise, if the file is modified because of the
1205 .B O_TRUNC
1206 flag, its
1207 .I st_ctime
1209 .I st_mtime
1210 fields are set to the current time.
1212 The files in the
1213 .I /proc/[pid]/fd
1214 directory show the open file descriptors of the process with the PID
1215 .IR pid .
1216 The files in the
1217 .I /proc/[pid]/fdinfo
1218 directory show even more information about these files descriptors.
1220 .BR proc (5)
1221 for further details of both of these directories.
1224 .SS Open file descriptions
1225 The term open file description is the one used by POSIX to refer to the
1226 entries in the system-wide table of open files.
1227 In other contexts, this object is
1228 variously also called an "open file object",
1229 a "file handle", an "open file table entry",
1230 or\(emin kernel-developer parlance\(ema
1231 .IR "struct file" .
1233 When a file descriptor is duplicated (using
1234 .BR dup (2)
1235 or similar),
1236 the duplicate refers to the same open file description
1237 as the original file descriptor,
1238 and the two file descriptors consequently share
1239 the file offset and file status flags.
1240 Such sharing can also occur between processes:
1241 a child process created via
1242 .BR fork (2)
1243 inherits duplicates of its parent's file descriptors,
1244 and those duplicates refer to the same open file descriptions.
1246 Each
1247 .BR open ()
1248 of a file creates a new open file description;
1249 thus, there may be multiple open file descriptions
1250 corresponding to a file inode.
1252 On Linux, one can use the
1253 .BR kcmp (2)
1254 .B KCMP_FILE
1255 operation to test whether two file descriptors
1256 (in the same process or in two different processes)
1257 refer to the same open file description.
1260 .SS Synchronized I/O
1261 The POSIX.1-2008 "synchronized I/O" option
1262 specifies different variants of synchronized I/O,
1263 and specifies the
1264 .BR open ()
1265 flags
1266 .BR O_SYNC ,
1267 .BR O_DSYNC ,
1269 .BR O_RSYNC
1270 for controlling the behavior.
1271 Regardless of whether an implementation supports this option,
1272 it must at least support the use of
1273 .BR O_SYNC
1274 for regular files.
1276 Linux implements
1277 .BR O_SYNC
1279 .BR O_DSYNC ,
1280 but not
1281 .BR O_RSYNC .
1282 (Somewhat incorrectly, glibc defines
1283 .BR O_RSYNC
1284 to have the same value as
1285 .BR O_SYNC .)
1287 .BR O_SYNC
1288 provides synchronized I/O
1289 .I file
1290 integrity completion,
1291 meaning write operations will flush data and all associated metadata
1292 to the underlying hardware.
1293 .BR O_DSYNC
1294 provides synchronized I/O
1295 .I data
1296 integrity completion,
1297 meaning write operations will flush data
1298 to the underlying hardware,
1299 but will only flush metadata updates that are required
1300 to allow a subsequent read operation to complete successfully.
1301 Data integrity completion can reduce the number of disk operations
1302 that are required for applications that don't need the guarantees
1303 of file integrity completion.
1305 To understand the difference between the two types of completion,
1306 consider two pieces of file metadata:
1307 the file last modification timestamp
1308 .RI ( st_mtime )
1309 and the file length.
1310 All write operations will update the last file modification timestamp,
1311 but only writes that add data to the end of the
1312 file will change the file length.
1313 The last modification timestamp is not needed to ensure that
1314 a read completes successfully, but the file length is.
1315 Thus,
1316 .BR O_DSYNC
1317 would only guarantee to flush updates to the file length metadata
1318 (whereas
1319 .BR O_SYNC
1320 would also always flush the last modification timestamp metadata).
1322 Before Linux 2.6.33, Linux implemented only the
1323 .BR O_SYNC
1324 flag for
1325 .BR open ().
1326 However, when that flag was specified,
1327 most filesystems actually provided the equivalent of synchronized I/O
1328 .I data
1329 integrity completion (i.e.,
1330 .BR O_SYNC
1331 was actually implemented as the equivalent of
1332 .BR O_DSYNC ).
1334 Since Linux 2.6.33, proper
1335 .BR O_SYNC
1336 support is provided.
1337 However, to ensure backward binary compatibility,
1338 .BR O_DSYNC
1339 was defined with the same value as the historical
1340 .BR O_SYNC ,
1342 .BR O_SYNC
1343 was defined as a new (two-bit) flag value that includes the
1344 .BR O_DSYNC
1345 flag value.
1346 This ensures that applications compiled against
1347 new headers get at least
1348 .BR O_DSYNC
1349 semantics on pre-2.6.33 kernels.
1352 .SS NFS
1353 There are many infelicities in the protocol underlying NFS, affecting
1354 amongst others
1355 .BR O_SYNC " and " O_NDELAY .
1357 On NFS filesystems with UID mapping enabled,
1358 .BR open ()
1360 return a file descriptor but, for example,
1361 .BR read (2)
1362 requests are denied
1363 with \fBEACCES\fP.
1364 This is because the client performs
1365 .BR open ()
1366 by checking the
1367 permissions, but UID mapping is performed by the server upon
1368 read and write requests.
1371 .SS FIFOs
1372 Opening the read or write end of a FIFO blocks until the other
1373 end is also opened (by another process or thread).
1375 .BR fifo (7)
1376 for further details.
1379 .SS File access mode
1380 Unlike the other values that can be specified in
1381 .IR flags ,
1383 .I "access mode"
1384 values
1385 .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1386 do not specify individual bits.
1387 Rather, they define the low order two bits of
1388 .IR flags ,
1389 and are defined respectively as 0, 1, and 2.
1390 In other words, the combination
1391 .B "O_RDONLY | O_WRONLY"
1392 is a logical error, and certainly does not have the same meaning as
1393 .BR O_RDWR .
1395 Linux reserves the special, nonstandard access mode 3 (binary 11) in
1396 .I flags
1397 to mean:
1398 check for read and write permission on the file and return a file descriptor
1399 that can't be used for reading or writing.
1400 This nonstandard access mode is used by some Linux drivers to return a
1401 file descriptor that is to be used only for device-specific
1402 .BR ioctl (2)
1403 operations.
1404 .\" See for example util-linux's disk-utils/setfdprm.c
1405 .\" For some background on access mode 3, see
1406 .\" http://thread.gmane.org/gmane.linux.kernel/653123
1407 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1408 .\" LKML, 12 Mar 2008
1411 .SS Rationale for openat() and other "directory file descriptor" APIs
1412 .BR openat ()
1413 and the other system calls and library functions that take
1414 a directory file descriptor argument
1415 (i.e.,
1416 .BR execveat (2),
1417 .BR faccessat (2),
1418 .BR fanotify_mark (2),
1419 .BR fchmodat (2),
1420 .BR fchownat (2),
1421 .BR fstatat (2),
1422 .BR futimesat (2),
1423 .BR linkat (2),
1424 .BR mkdirat (2),
1425 .BR mknodat (2),
1426 .BR name_to_handle_at (2),
1427 .BR readlinkat (2),
1428 .BR renameat (2),
1429 .BR statx (2),
1430 .BR symlinkat (2),
1431 .BR unlinkat (2),
1432 .BR utimensat (2),
1433 .BR mkfifoat (3),
1435 .BR scandirat (3))
1436 address two problems with the older interfaces that preceded them.
1437 Here, the explanation is in terms of the
1438 .BR openat ()
1439 call, but the rationale is analogous for the other interfaces.
1441 First,
1442 .BR openat ()
1443 allows an application to avoid race conditions that could
1444 occur when using
1445 .BR open ()
1446 to open files in directories other than the current working directory.
1447 These race conditions result from the fact that some component
1448 of the directory prefix given to
1449 .BR open ()
1450 could be changed in parallel with the call to
1451 .BR open ().
1452 Suppose, for example, that we wish to create the file
1453 .I path/to/xxx.dep
1454 if the file
1455 .I path/to/xxx
1456 exists.
1457 The problem is that between the existence check and the file creation step,
1458 .I path
1460 .I to
1461 (which might be symbolic links)
1462 could be modified to point to a different location.
1463 Such races can be avoided by
1464 opening a file descriptor for the target directory,
1465 and then specifying that file descriptor as the
1466 .I dirfd
1467 argument of (say)
1468 .BR fstatat (2)
1470 .BR openat ().
1471 The use of the
1472 .I dirfd
1473 file descriptor also has other benefits:
1474 .IP * 3
1475 the file descriptor is a stable reference to the directory,
1476 even if the directory is renamed; and
1477 .IP *
1478 the open file descriptor prevents the underlying filesystem from
1479 being dismounted,
1480 just as when a process has a current working directory on a filesystem.
1482 Second,
1483 .BR openat ()
1484 allows the implementation of a per-thread "current working
1485 directory", via file descriptor(s) maintained by the application.
1486 (This functionality can also be obtained by tricks based
1487 on the use of
1488 .IR /proc/self/fd/ dirfd,
1489 but less efficiently.)
1492 .SS O_DIRECT
1495 .B O_DIRECT
1496 flag may impose alignment restrictions on the length and address
1497 of user-space buffers and the file offset of I/Os.
1498 In Linux alignment
1499 restrictions vary by filesystem and kernel version and might be
1500 absent entirely.
1501 However there is currently no filesystem\-independent
1502 interface for an application to discover these restrictions for a given
1503 file or filesystem.
1504 Some filesystems provide their own interfaces
1505 for doing so, for example the
1506 .B XFS_IOC_DIOINFO
1507 operation in
1508 .BR xfsctl (3).
1510 Under Linux 2.4, transfer sizes, and the alignment of the user buffer
1511 and the file offset must all be multiples of the logical block size
1512 of the filesystem.
1513 Since Linux 2.6.0, alignment to the logical block size of the
1514 underlying storage (typically 512 bytes) suffices.
1515 The logical block size can be determined using the
1516 .BR ioctl (2)
1517 .B BLKSSZGET
1518 operation or from the shell using the command:
1520     blockdev \-\-getss
1522 .B O_DIRECT
1523 I/Os should never be run concurrently with the
1524 .BR fork (2)
1525 system call,
1526 if the memory buffer is a private mapping
1527 (i.e., any mapping created with the
1528 .BR mmap (2)
1529 .BR MAP_PRIVATE
1530 flag;
1531 this includes memory allocated on the heap and statically allocated buffers).
1532 Any such I/Os, whether submitted via an asynchronous I/O interface or from
1533 another thread in the process,
1534 should be completed before
1535 .BR fork (2)
1536 is called.
1537 Failure to do so can result in data corruption and undefined behavior in
1538 parent and child processes.
1539 This restriction does not apply when the memory buffer for the
1540 .B O_DIRECT
1541 I/Os was created using
1542 .BR shmat (2)
1544 .BR mmap (2)
1545 with the
1546 .B MAP_SHARED
1547 flag.
1548 Nor does this restriction apply when the memory buffer has been advised as
1549 .B MADV_DONTFORK
1550 with
1551 .BR madvise (2),
1552 ensuring that it will not be available
1553 to the child after
1554 .BR fork (2).
1557 .B O_DIRECT
1558 flag was introduced in SGI IRIX, where it has alignment
1559 restrictions similar to those of Linux 2.4.
1560 IRIX has also a
1561 .BR fcntl (2)
1562 call to query appropriate alignments, and sizes.
1563 FreeBSD 4.x introduced
1564 a flag of the same name, but without alignment restrictions.
1566 .B O_DIRECT
1567 support was added under Linux in kernel version 2.4.10.
1568 Older Linux kernels simply ignore this flag.
1569 Some filesystems may not implement the flag and
1570 .BR open ()
1571 will fail with
1572 .B EINVAL
1573 if it is used.
1575 Applications should avoid mixing
1576 .B O_DIRECT
1577 and normal I/O to the same file,
1578 and especially to overlapping byte regions in the same file.
1579 Even when the filesystem correctly handles the coherency issues in
1580 this situation, overall I/O throughput is likely to be slower than
1581 using either mode alone.
1582 Likewise, applications should avoid mixing
1583 .BR mmap (2)
1584 of files with direct I/O to the same files.
1586 The behavior of
1587 .B O_DIRECT
1588 with NFS will differ from local filesystems.
1589 Older kernels, or
1590 kernels configured in certain ways, may not support this combination.
1591 The NFS protocol does not support passing the flag to the server, so
1592 .B O_DIRECT
1593 I/O will bypass the page cache only on the client; the server may
1594 still cache the I/O.
1595 The client asks the server to make the I/O
1596 synchronous to preserve the synchronous semantics of
1597 .BR O_DIRECT .
1598 Some servers will perform poorly under these circumstances, especially
1599 if the I/O size is small.
1600 Some servers may also be configured to
1601 lie to clients about the I/O having reached stable storage; this
1602 will avoid the performance penalty at some risk to data integrity
1603 in the event of server power failure.
1604 The Linux NFS client places no alignment restrictions on
1605 .B O_DIRECT
1606 I/O.
1608 In summary,
1609 .B O_DIRECT
1610 is a potentially powerful tool that should be used with caution.
1611 It is recommended that applications treat use of
1612 .B O_DIRECT
1613 as a performance option which is disabled by default.
1616 "The thing that has always disturbed me about O_DIRECT is that the whole
1617 interface is just stupid, and was probably designed by a deranged monkey
1618 on some serious mind-controlling substances."\(emLinus
1620 .SH BUGS
1621 Currently, it is not possible to enable signal-driven
1622 I/O by specifying
1623 .B O_ASYNC
1624 when calling
1625 .BR open ();
1627 .BR fcntl (2)
1628 to enable this flag.
1629 .\" FIXME . Check bugzilla report on open(O_ASYNC)
1630 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
1632 One must check for two different error codes,
1633 .B EISDIR
1635 .BR ENOENT ,
1636 when trying to determine whether the kernel supports
1637 .B O_TMPFILE
1638 functionality.
1640 When both
1641 .B O_CREAT
1643 .B O_DIRECTORY
1644 are specified in
1645 .IR flags
1646 and the file specified by
1647 .I pathname
1648 does not exist,
1649 .BR open ()
1650 will create a regular file (i.e.,
1651 .B O_DIRECTORY
1652 is ignored).
1653 .SH SEE ALSO
1654 .BR chmod (2),
1655 .BR chown (2),
1656 .BR close (2),
1657 .BR dup (2),
1658 .BR fcntl (2),
1659 .BR link (2),
1660 .BR lseek (2),
1661 .BR mknod (2),
1662 .BR mmap (2),
1663 .BR mount (2),
1664 .BR open_by_handle_at (2),
1665 .BR read (2),
1666 .BR socket (2),
1667 .BR stat (2),
1668 .BR umask (2),
1669 .BR unlink (2),
1670 .BR write (2),
1671 .BR fopen (3),
1672 .BR acl (5),
1673 .BR fifo (7),
1674 .BR inode (7),
1675 .BR path_resolution (7),
1676 .BR symlink (7)