OSDN Git Service

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