OSDN Git Service

(split) LDP: Update original to LDP v3.51.
[linuxjm/LDP_man-pages.git] / original / man2 / open.2
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 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Haardt
29 .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified 1996-05-13 by Thomas Koenig
31 .\" Modified 1996-12-20 by Michael Haardt
32 .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
33 .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
34 .\" Modified 1999-06-03 by Michael Haardt
35 .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2004-12-08, mtk, reordered flags list alphabetically
38 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
39 .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
40 .\" 2008-01-03, mtk, with input from Trond Myklebust
41 .\"     <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
42 .\"     Rewrite description of O_EXCL.
43 .\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
44 .\"     on O_DIRECT.
45 .\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
46 .\"
47 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
48 .\" O_TTYINIT.  Eventually these may need to be documented.  --mtk
49 .\" FIXME Linux 2.6.33 has O_DSYNC, and a hidden __O_SYNC.
50 .\" FIXME: Linux 2.6.39 added O_PATH
51 .\"
52 .TH OPEN 2 2013-02-18 "Linux" "Linux Programmer's Manual"
53 .SH NAME
54 open, creat \- open and possibly create a file or device
55 .SH SYNOPSIS
56 .nf
57 .B #include <sys/types.h>
58 .B #include <sys/stat.h>
59 .B #include <fcntl.h>
60 .sp
61 .BI "int open(const char *" pathname ", int " flags );
62 .BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
63
64 .BI "int creat(const char *" pathname ", mode_t " mode );
65 .fi
66 .SH DESCRIPTION
67 Given a
68 .I pathname
69 for a file,
70 .BR open ()
71 returns a file descriptor, a small, nonnegative integer
72 for use in subsequent system calls
73 .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
74 The file descriptor returned by a successful call will be
75 the lowest-numbered file descriptor not currently open for the process.
76 .PP
77 By default, the new file descriptor is set to remain open across an
78 .BR execve (2)
79 (i.e., the
80 .B FD_CLOEXEC
81 file descriptor flag described in
82 .BR fcntl (2)
83 is initially disabled; the
84 .B O_CLOEXEC
85 flag, described below, can be used to change this default).
86 The file offset is set to the beginning of the file (see
87 .BR lseek (2)).
88 .PP
89 A call to
90 .BR open ()
91 creates a new
92 .IR "open file description" ,
93 an entry in the system-wide table of open files.
94 This entry records the file offset and the file status flags
95 (modifiable via the
96 .BR fcntl (2)
97 .B F_SETFL
98 operation).
99 A file descriptor is a reference to one of these entries;
100 this reference is unaffected if
101 .I pathname
102 is subsequently removed or modified to refer to a different file.
103 The new open file description is initially not shared
104 with any other process,
105 but sharing may arise via
106 .BR fork (2).
107 .PP
108 The argument
109 .I flags
110 must include one of the following
111 .IR "access modes" :
112 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
113 These request opening the file read-only, write-only, or read/write,
114 respectively.
115
116 In addition, zero or more file creation flags and file status flags
117 can be
118 .RI bitwise- or 'd
119 in
120 .IR flags .
121 The
122 .I file creation flags
123 are
124 .BR O_CLOEXEC ,
125 .BR O_CREAT ,
126 .BR O_DIRECTORY ,
127 .BR O_EXCL ,
128 .BR O_NOCTTY ,
129 .BR O_NOFOLLOW ,
130 .BR O_TRUNC ,
131 and
132 .BR O_TTY_INIT .
133 The
134 .I file status flags
135 are all of the remaining flags listed below.
136 .\" SUSv4 divides the flags into:
137 .\" * Access mode
138 .\" * File creation
139 .\" * File status
140 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
141 .\" though it's not clear what the difference between "other" and
142 .\" "File creation" flags is.  I raised an Aardvark to see if this
143 .\" can be clarified in SUSv4; 10 Oct 2008.
144 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
145 .\" TC1 (balloted in 2013), resolved this, so that those three constants
146 .\" are also categorized" as file status flags.
147 .\"
148 The distinction between these two groups of flags is that
149 the file status flags can be retrieved and (in some cases)
150 modified using
151 .BR fcntl (2).
152 The full list of file creation flags and file status flags is as follows:
153 .TP
154 .B O_APPEND
155 The file is opened in append mode.
156 Before each
157 .BR write (2),
158 the file offset is positioned at the end of the file,
159 as if with
160 .BR lseek (2).
161 .B O_APPEND
162 may lead to corrupted files on NFS file systems if more than one process
163 appends data to a file at once.
164 .\" For more background, see
165 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
166 .\" http://nfs.sourceforge.net/
167 This is because NFS does not support
168 appending to a file, so the client kernel has to simulate it, which
169 can't be done without a race condition.
170 .TP
171 .B O_ASYNC
172 Enable signal-driven I/O:
173 generate a signal
174 .RB ( SIGIO
175 by default, but this can be changed via
176 .BR fcntl (2))
177 when input or output becomes possible on this file descriptor.
178 This feature is available only for terminals, pseudoterminals,
179 sockets, and (since Linux 2.6) pipes and FIFOs.
180 See
181 .BR fcntl (2)
182 for further details.
183 .TP
184 .BR O_CLOEXEC " (Since Linux 2.6.23)"
185 Enable the close-on-exec flag for the new file descriptor.
186 Specifying this flag permits a program to avoid additional
187 .BR fcntl (2)
188 .B F_SETFD
189 operations to set the
190 .B FD_CLOEXEC
191 flag.
192 Additionally,
193 use of this flag is essential in some multithreaded programs
194 since using a separate
195 .BR fcntl (2)
196 .B F_SETFD
197 operation to set the
198 .B FD_CLOEXEC
199 flag does not suffice to avoid race conditions
200 where one thread opens a file descriptor at the same
201 time as another thread does a
202 .BR fork (2)
203 plus
204 .BR execve (2).
205 .\" This flag fixes only one form of the race condition;
206 .\" The race can also occur with, for example, descriptors
207 .\" returned by accept(), pipe(), etc.
208 .TP
209 .B O_CREAT
210 If the file does not exist it will be created.
211 The owner (user ID) of the file is set to the effective user ID
212 of the process.
213 The group ownership (group ID) is set either to
214 the effective group ID of the process or to the group ID of the
215 parent directory (depending on file system type and mount options,
216 and the mode of the parent directory, see the mount options
217 .I bsdgroups
218 and
219 .I sysvgroups
220 described in
221 .BR mount (8)).
222 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
223 .\" XFS (since 2.6.14).
224 .RS
225 .PP
226 .I mode
227 specifies the permissions to use in case a new file is created.
228 This argument must be supplied when
229 .B O_CREAT
230 is specified in
231 .IR flags ;
232 if
233 .B O_CREAT
234 is not specified, then
235 .I mode
236 is ignored.
237 The effective permissions are modified by
238 the process's
239 .I umask
240 in the usual way: The permissions of the created file are
241 .IR "(mode\ &\ ~umask)" .
242 Note that this mode applies only to future accesses of the
243 newly created file; the
244 .BR open ()
245 call that creates a read-only file may well return a read/write
246 file descriptor.
247 .PP
248 The following symbolic constants are provided for
249 .IR mode :
250 .TP 9
251 .B S_IRWXU
252 00700 user (file owner) has read, write and execute permission
253 .TP
254 .B S_IRUSR
255 00400 user has read permission
256 .TP
257 .B S_IWUSR
258 00200 user has write permission
259 .TP
260 .B S_IXUSR
261 00100 user has execute permission
262 .TP
263 .B S_IRWXG
264 00070 group has read, write and execute permission
265 .TP
266 .B S_IRGRP
267 00040 group has read permission
268 .TP
269 .B S_IWGRP
270 00020 group has write permission
271 .TP
272 .B S_IXGRP
273 00010 group has execute permission
274 .TP
275 .B S_IRWXO
276 00007 others have read, write and execute permission
277 .TP
278 .B S_IROTH
279 00004 others have read permission
280 .TP
281 .B S_IWOTH
282 00002 others have write permission
283 .TP
284 .B S_IXOTH
285 00001 others have execute permission
286 .RE
287 .TP
288 .BR O_DIRECT " (Since Linux 2.4.10)"
289 Try to minimize cache effects of the I/O to and from this file.
290 In general this will degrade performance, but it is useful in
291 special situations, such as when applications do their own caching.
292 File I/O is done directly to/from user-space buffers.
293 The
294 .B O_DIRECT
295 flag on its own makes an effort to transfer data synchronously,
296 but does not give the guarantees of the
297 .B O_SYNC
298 flag that data and necessary metadata are transferred.
299 To guarantee synchronous I/O,
300 .B O_SYNC
301 must be used in addition to
302 .BR O_DIRECT .
303 See NOTES below for further discussion.
304 .sp
305 A semantically similar (but deprecated) interface for block devices
306 is described in
307 .BR raw (8).
308 .TP
309 .B O_DIRECTORY
310 If \fIpathname\fP is not a directory, cause the open to fail.
311 .\" But see the following and its replies:
312 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
313 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
314 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
315 This flag is Linux-specific, and was added in kernel version 2.1.126, to
316 avoid denial-of-service problems if
317 .BR opendir (3)
318 is called on a
319 FIFO or tape device, but should not be used outside of the
320 implementation of
321 .BR opendir (3).
322 .TP
323 .B O_EXCL
324 Ensure that this call creates the file:
325 if this flag is specified in conjunction with
326 .BR O_CREAT ,
327 and
328 .I pathname
329 already exists, then
330 .BR open ()
331 will fail.
332
333 When these two flags are specified, symbolic links are not followed:
334 .\" POSIX.1-2001 explicitly requires this behavior.
335 if
336 .I pathname
337 is a symbolic link, then
338 .BR open ()
339 fails regardless of where the symbolic link points to.
340
341 In general, the behavior of
342 .B O_EXCL
343 is undefined if it is used without
344 .BR O_CREAT .
345 There is one exception: on Linux 2.6 and later,
346 .B O_EXCL
347 can be used without
348 .B O_CREAT
349 if
350 .I pathname
351 refers to a block device.
352 If the block device is in use by the system (e.g., mounted),
353 .BR open ()
354 fails with the error
355 .BR EBUSY .
356
357 On NFS,
358 .B O_EXCL
359 is supported only when using NFSv3 or later on kernel 2.6 or later.
360 In NFS environments where
361 .B O_EXCL
362 support is not provided, programs that rely on it
363 for performing locking tasks will contain a race condition.
364 Portable programs that want to perform atomic file locking using a lockfile,
365 and need to avoid reliance on NFS support for
366 .BR O_EXCL ,
367 can create a unique file on
368 the same file system (e.g., incorporating hostname and PID), and use
369 .BR link (2)
370 to make a link to the lockfile.
371 If
372 .BR link (2)
373 returns 0, the lock is successful.
374 Otherwise, use
375 .BR stat (2)
376 on the unique file to check if its link count has increased to 2,
377 in which case the lock is also successful.
378 .TP
379 .B O_LARGEFILE
380 (LFS)
381 Allow files whose sizes cannot be represented in an
382 .I off_t
383 (but can be represented in an
384 .IR off64_t )
385 to be opened.
386 The
387 .B _LARGEFILE64_SOURCE
388 macro must be defined
389 (before including
390 .I any
391 header files)
392 in order to obtain this definition.
393 Setting the
394 .B _FILE_OFFSET_BITS
395 feature test macro to 64 (rather than using
396 .BR O_LARGEFILE )
397 is the preferred
398 method of accessing large files on 32-bit systems (see
399 .BR feature_test_macros (7)).
400 .TP
401 .BR O_NOATIME " (Since Linux 2.6.8)"
402 Do not update the file last access time
403 .RI ( st_atime
404 in the inode)
405 when the file is
406 .BR read (2).
407 This flag is intended for use by indexing or backup programs,
408 where its use can significantly reduce the amount of disk activity.
409 This flag may not be effective on all file systems.
410 One example is NFS, where the server maintains the access time.
411 .\" The O_NOATIME flag also affects the treatment of st_atime
412 .\" by mmap() and readdir(2), MTK, Dec 04.
413 .TP
414 .B O_NOCTTY
415 If
416 .I pathname
417 refers to a terminal device\(emsee
418 .BR tty (4)\(emit
419 will not become the process's controlling terminal even if the
420 process does not have one.
421 .TP
422 .B O_NOFOLLOW
423 If \fIpathname\fP is a symbolic link, then the open fails.
424 This is a FreeBSD extension, which was added to Linux in version 2.1.126.
425 Symbolic links in earlier components of the pathname will still be
426 followed.
427 .\" The headers from glibc 2.0.100 and later include a
428 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
429 .\" used\fP.
430 .TP
431 .BR O_NONBLOCK " or " O_NDELAY
432 When possible, the file is opened in nonblocking mode.
433 Neither the
434 .BR open ()
435 nor any subsequent operations on the file descriptor which is
436 returned will cause the calling process to wait.
437 For the handling of FIFOs (named pipes), see also
438 .BR fifo (7).
439 For a discussion of the effect of
440 .B O_NONBLOCK
441 in conjunction with mandatory file locks and with file leases, see
442 .BR fcntl (2).
443 .TP
444 .B O_SYNC
445 The file is opened for synchronous I/O.
446 Any
447 .BR write (2)s
448 on the resulting file descriptor will block the calling process until
449 the data has been physically written to the underlying hardware.
450 .IR "But see NOTES below" .
451 .TP
452 .B O_TRUNC
453 If the file already exists and is a regular file and the open mode allows
454 writing (i.e., is
455 .B O_RDWR
456 or
457 .BR O_WRONLY )
458 it will be truncated to length 0.
459 If the file is a FIFO or terminal device file, the
460 .B O_TRUNC
461 flag is ignored.
462 Otherwise the effect of
463 .B O_TRUNC
464 is unspecified.
465 .PP
466 Some of these optional flags can be altered using
467 .BR fcntl (2)
468 after the file has been opened.
469
470 .BR creat ()
471 is equivalent to
472 .BR open ()
473 with
474 .I flags
475 equal to
476 .BR O_CREAT|O_WRONLY|O_TRUNC .
477 .SH RETURN VALUE
478 .BR open ()
479 and
480 .BR creat ()
481 return the new file descriptor, or \-1 if an error occurred
482 (in which case,
483 .I errno
484 is set appropriately).
485 .SH ERRORS
486 .TP
487 .B EACCES
488 The requested access to the file is not allowed, or search permission
489 is denied for one of the directories in the path prefix of
490 .IR pathname ,
491 or the file did not exist yet and write access to the parent directory
492 is not allowed.
493 (See also
494 .BR path_resolution (7).)
495 .TP
496 .B EDQUOT
497 Where
498 .B O_CREAT
499 is specified, the file does not exist, and the user's quota of disk
500 blocks or inodes on the file system has been exhausted.
501 .TP
502 .B EEXIST
503 .I pathname
504 already exists and
505 .BR O_CREAT " and " O_EXCL
506 were used.
507 .TP
508 .B EFAULT
509 .I pathname
510 points outside your accessible address space.
511 .TP
512 .B EFBIG
513 See
514 .BR EOVERFLOW .
515 .TP
516 .B EINTR
517 While blocked waiting to complete an open of a slow device
518 (e.g., a FIFO; see
519 .BR fifo (7)),
520 the call was interrupted by a signal handler; see
521 .BR signal (7).
522 .TP
523 .B EISDIR
524 .I pathname
525 refers to a directory and the access requested involved writing
526 (that is,
527 .B O_WRONLY
528 or
529 .B O_RDWR
530 is set).
531 .TP
532 .B ELOOP
533 Too many symbolic links were encountered in resolving
534 .IR pathname ,
535 or \fBO_NOFOLLOW\fP was specified but
536 .I pathname
537 was a symbolic link.
538 .TP
539 .B EMFILE
540 The process already has the maximum number of files open.
541 .TP
542 .B ENAMETOOLONG
543 .I pathname
544 was too long.
545 .TP
546 .B ENFILE
547 The system limit on the total number of open files has been reached.
548 .TP
549 .B ENODEV
550 .I pathname
551 refers to a device special file and no corresponding device exists.
552 (This is a Linux kernel bug; in this situation
553 .B ENXIO
554 must be returned.)
555 .TP
556 .B ENOENT
557 .B O_CREAT
558 is not set and the named file does not exist.
559 Or, a directory component in
560 .I pathname
561 does not exist or is a dangling symbolic link.
562 .TP
563 .B ENOMEM
564 Insufficient kernel memory was available.
565 .TP
566 .B ENOSPC
567 .I pathname
568 was to be created but the device containing
569 .I pathname
570 has no room for the new file.
571 .TP
572 .B ENOTDIR
573 A component used as a directory in
574 .I pathname
575 is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
576 .I pathname
577 was not a directory.
578 .TP
579 .B ENXIO
580 .BR O_NONBLOCK " | " O_WRONLY
581 is set, the named file is a FIFO and
582 no process has the file open for reading.
583 Or, the file is a device special file and no corresponding device exists.
584 .TP
585 .B EOVERFLOW
586 .I pathname
587 refers to a regular file that is too large to be opened.
588 The usual scenario here is that an application compiled
589 on a 32-bit platform without
590 .I -D_FILE_OFFSET_BITS=64
591 tried to open a file whose size exceeds
592 .I (2<<31)-1
593 bits;
594 see also
595 .B O_LARGEFILE
596 above.
597 This is the error specified by POSIX.1-2001;
598 in kernels before 2.6.24, Linux gave the error
599 .B EFBIG
600 for this case.
601 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
602 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
603 .\" Reported 2006-10-03
604 .TP
605 .B EPERM
606 The
607 .B O_NOATIME
608 flag was specified, but the effective user ID of the caller
609 .\" Strictly speaking, it's the file system UID... (MTK)
610 did not match the owner of the file and the caller was not privileged
611 .RB ( CAP_FOWNER ).
612 .TP
613 .B EROFS
614 .I pathname
615 refers to a file on a read-only file system and write access was
616 requested.
617 .TP
618 .B ETXTBSY
619 .I pathname
620 refers to an executable image which is currently being executed and
621 write access was requested.
622 .TP
623 .B EWOULDBLOCK
624 The
625 .B O_NONBLOCK
626 flag was specified, and an incompatible lease was held on the file
627 (see
628 .BR fcntl (2)).
629 .SH CONFORMING TO
630 SVr4, 4.3BSD, POSIX.1-2001.
631 The
632 .BR O_DIRECTORY ,
633 .BR O_NOATIME ,
634 and
635 .B O_NOFOLLOW
636 flags are Linux-specific, and one may need to define
637 .B _GNU_SOURCE
638 (before including
639 .I any
640 header files)
641 to obtain their definitions.
642
643 The
644 .BR O_CLOEXEC
645 flag is not specified in POSIX.1-2001,
646 but is specified in POSIX.1-2008.
647
648 .B O_DIRECT
649 is not specified in POSIX; one has to define
650 .B _GNU_SOURCE
651 (before including
652 .I any
653 header files)
654 to get its definition.
655 .SH NOTES
656 Under Linux, the
657 .B O_NONBLOCK
658 flag indicates that one wants to open
659 but does not necessarily have the intention to read or write.
660 This is typically used to open devices in order to get a file descriptor
661 for use with
662 .BR ioctl (2).
663
664 Unlike the other values that can be specified in
665 .IR flags ,
666 the
667 .I "access mode"
668 values
669 .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR ,
670 do not specify individual bits.
671 Rather, they define the low order two bits of
672 .IR flags ,
673 and are defined respectively as 0, 1, and 2.
674 In other words, the combination
675 .B "O_RDONLY | O_WRONLY"
676 is a logical error, and certainly does not have the same meaning as
677 .BR O_RDWR .
678 Linux reserves the special, nonstandard access mode 3 (binary 11) in
679 .I flags
680 to mean:
681 check for read and write permission on the file and return a descriptor
682 that can't be used for reading or writing.
683 This nonstandard access mode is used by some Linux drivers to return a
684 descriptor that is to be used only for device-specific
685 .BR ioctl (2)
686 operations.
687 .\" See for example util-linux's disk-utils/setfdprm.c
688 .\" For some background on access mode 3, see
689 .\" http://thread.gmane.org/gmane.linux.kernel/653123
690 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
691 .\" LKML, 12 Mar 2008
692 .LP
693 The (undefined) effect of
694 .B O_RDONLY | O_TRUNC
695 varies among implementations.
696 On many systems the file is actually truncated.
697 .\" Linux 2.0, 2.5: truncate
698 .\" Solaris 5.7, 5.8: truncate
699 .\" Irix 6.5: truncate
700 .\" Tru64 5.1B: truncate
701 .\" HP-UX 11.22: truncate
702 .\" FreeBSD 4.7: truncate
703 .PP
704 There are many infelicities in the protocol underlying NFS, affecting
705 amongst others
706 .BR O_SYNC " and " O_NDELAY .
707
708 POSIX provides for three different variants of synchronized I/O,
709 corresponding to the flags
710 .BR O_SYNC ,
711 .BR O_DSYNC ,
712 and
713 .BR O_RSYNC .
714 Currently (2.6.31), Linux implements only
715 .BR O_SYNC ,
716 but glibc maps
717 .B O_DSYNC
718 and
719 .B O_RSYNC
720 to the same numerical value as
721 .BR O_SYNC .
722 Most Linux file systems don't actually implement the POSIX
723 .B O_SYNC
724 semantics, which require all metadata updates of a write
725 to be on disk on returning to user space, but only the
726 .B O_DSYNC
727 semantics, which require only actual file data and metadata necessary
728 to retrieve it to be on disk by the time the system call returns.
729
730 Note that
731 .BR open ()
732 can open device special files, but
733 .BR creat ()
734 cannot create them; use
735 .BR mknod (2)
736 instead.
737 .LP
738 On NFS file systems with UID mapping enabled,
739 .BR open ()
740 may
741 return a file descriptor but, for example,
742 .BR read (2)
743 requests are denied
744 with \fBEACCES\fP.
745 This is because the client performs
746 .BR open ()
747 by checking the
748 permissions, but UID mapping is performed by the server upon
749 read and write requests.
750
751 If the file is newly created, its
752 .IR st_atime ,
753 .IR st_ctime ,
754 .I st_mtime
755 fields
756 (respectively, time of last access, time of last status change, and
757 time of last modification; see
758 .BR stat (2))
759 are set
760 to the current time, and so are the
761 .I st_ctime
762 and
763 .I st_mtime
764 fields of the
765 parent directory.
766 Otherwise, if the file is modified because of the
767 .B O_TRUNC
768 flag, its st_ctime and st_mtime fields are set to the current time.
769 .SS O_DIRECT
770 .LP
771 The
772 .B O_DIRECT
773 flag may impose alignment restrictions on the length and address
774 of user-space buffers and the file offset of I/Os.
775 In Linux alignment
776 restrictions vary by file system and kernel version and might be
777 absent entirely.
778 However there is currently no file system\-independent
779 interface for an application to discover these restrictions for a given
780 file or file system.
781 Some file systems provide their own interfaces
782 for doing so, for example the
783 .B XFS_IOC_DIOINFO
784 operation in
785 .BR xfsctl (3).
786 .LP
787 Under Linux 2.4, transfer sizes, and the alignment of the user buffer
788 and the file offset must all be multiples of the logical block size
789 of the file system.
790 Under Linux 2.6, alignment to 512-byte boundaries suffices.
791 .LP
792 .B O_DIRECT
793 I/Os should never be run concurrently with the
794 .BR fork (2)
795 system call,
796 if the memory buffer is a private mapping
797 (i.e., any mapping created with the
798 .BR mmap (2)
799 .BR MAP_PRIVATE
800 flag;
801 this includes memory allocated on the heap and statically allocated buffers).
802 Any such I/Os, whether submitted via an asynchronous I/O interface or from
803 another thread in the process,
804 should be completed before
805 .BR fork (2)
806 is called.
807 Failure to do so can result in data corruption and undefined behavior in
808 parent and child processes.
809 This restriction does not apply when the memory buffer for the
810 .B O_DIRECT
811 I/Os was created using
812 .BR shmat (2)
813 or
814 .BR mmap (2)
815 with the
816 .B MAP_SHARED
817 flag.
818 Nor does this restriction apply when the memory buffer has been advised as
819 .B MADV_DONTFORK
820 with
821 .BR madvise (2),
822 ensuring that it will not be available
823 to the child after
824 .BR fork (2).
825 .LP
826 The
827 .B O_DIRECT
828 flag was introduced in SGI IRIX, where it has alignment
829 restrictions similar to those of Linux 2.4.
830 IRIX has also a
831 .BR fcntl (2)
832 call to query appropriate alignments, and sizes.
833 FreeBSD 4.x introduced
834 a flag of the same name, but without alignment restrictions.
835 .LP
836 .B O_DIRECT
837 support was added under Linux in kernel version 2.4.10.
838 Older Linux kernels simply ignore this flag.
839 Some file systems may not implement the flag and
840 .BR open ()
841 will fail with
842 .B EINVAL
843 if it is used.
844 .LP
845 Applications should avoid mixing
846 .B O_DIRECT
847 and normal I/O to the same file,
848 and especially to overlapping byte regions in the same file.
849 Even when the file system correctly handles the coherency issues in
850 this situation, overall I/O throughput is likely to be slower than
851 using either mode alone.
852 Likewise, applications should avoid mixing
853 .BR mmap (2)
854 of files with direct I/O to the same files.
855 .LP
856 The behaviour of
857 .B O_DIRECT
858 with NFS will differ from local file systems.
859 Older kernels, or
860 kernels configured in certain ways, may not support this combination.
861 The NFS protocol does not support passing the flag to the server, so
862 .B O_DIRECT
863 I/O will bypass the page cache only on the client; the server may
864 still cache the I/O.
865 The client asks the server to make the I/O
866 synchronous to preserve the synchronous semantics of
867 .BR O_DIRECT .
868 Some servers will perform poorly under these circumstances, especially
869 if the I/O size is small.
870 Some servers may also be configured to
871 lie to clients about the I/O having reached stable storage; this
872 will avoid the performance penalty at some risk to data integrity
873 in the event of server power failure.
874 The Linux NFS client places no alignment restrictions on
875 .B O_DIRECT
876 I/O.
877 .PP
878 In summary,
879 .B O_DIRECT
880 is a potentially powerful tool that should be used with caution.
881 It is recommended that applications treat use of
882 .B O_DIRECT
883 as a performance option which is disabled by default.
884 .PP
885 .RS
886 "The thing that has always disturbed me about O_DIRECT is that the whole
887 interface is just stupid, and was probably designed by a deranged monkey
888 on some serious mind-controlling substances."\(emLinus
889 .RE
890 .SH BUGS
891 Currently, it is not possible to enable signal-driven
892 I/O by specifying
893 .B O_ASYNC
894 when calling
895 .BR open ();
896 use
897 .BR fcntl (2)
898 to enable this flag.
899 .\" FIXME . Check bugzilla report on open(O_ASYNC)
900 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
901 .SH SEE ALSO
902 .BR chmod (2),
903 .BR chown (2),
904 .BR close (2),
905 .BR dup (2),
906 .BR fcntl (2),
907 .BR link (2),
908 .BR lseek (2),
909 .BR mknod (2),
910 .BR mmap (2),
911 .BR mount (2),
912 .BR openat (2),
913 .BR read (2),
914 .BR socket (2),
915 .BR stat (2),
916 .BR umask (2),
917 .BR unlink (2),
918 .BR write (2),
919 .BR fopen (3),
920 .BR fifo (7),
921 .BR path_resolution (7),
922 .BR symlink (7)