OSDN Git Service

3ab35b5acbaf3a041a32c5ca96bae4b2edde956f
[linuxjm/LDP_man-pages.git] / draft / 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 .\" 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 .\"*******************************************************************
52 .\"
53 .\" This file was generated with po4a. Translate the source file.
54 .\"
55 .\"*******************************************************************
56 .\"
57 .\" Japanese Version Copyright (c) 1997-1999 HANATAKA Shinya
58 .\"         all rights reserved.
59 .\" Translated 1999-08-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
60 .\" Updated 2001-05-25, Yuichi SATO <ysato444@yahoo.co.jp>
61 .\" Updated & Modified 2002-01-02, Yuichi SATO
62 .\" Updated & Modified 2002-07-07, Yuichi SATO
63 .\" Updated & Modified 2002-09-19, Yuichi SATO
64 .\" Updated & Modified 2003-07-30, Yuichi SATO
65 .\" Updated & Modified 2003-11-27, Yuichi SATO
66 .\" Updated & Modified 2005-01-01, Yuichi SATO
67 .\" Updated & Modified 2005-09-14, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
68 .\" Updated & Modified 2005-10-14, Akihiro MOTOKI
69 .\" Updated & Modified 2006-01-18, Akihiro MOTOKI
70 .\" Updated & Modified 2006-04-15, Akihiro MOTOKI, LDP v2.29
71 .\" Updated 2007-01-07, Akihiro MOTOKI, LDP v2.43
72 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
73 .\" Updated 2007-10-12, Akihiro MOTOKI, LDP v2.66
74 .\" Updated 2008-02-12, Akihiro MOTOKI, LDP v2.77
75 .\" Updated 2008-04-04, Akihiro MOTOKI, LDP v2.79
76 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05
77 .\" Updated 2010-04-23, Akihiro MOTOKI, LDP v3.24
78 .\" Updated 2012-05-08, Akihiro MOTOKI <amotoki@gmail.com>
79 .\" Updated 2012-05-30, Akihiro MOTOKI <amotoki@gmail.com>
80 .\" Updated 2013-05-06, Akihiro MOTOKI <amotoki@gmail.com>
81 .\" Updated 2013-08-16, Akihiro MOTOKI <amotoki@gmail.com>
82 .\"
83 .TH OPEN 2 2014\-04\-20 Linux "Linux Programmer's Manual"
84 .SH 名前
85 open, openat, creat \- ファイルのオープン、作成を行う
86 .SH 書式
87 .nf
88 \fB#include <sys/types.h>\fP
89 \fB#include <sys/stat.h>\fP
90 \fB#include <fcntl.h>\fP
91 .sp
92 \fBint open(const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB);\fP
93 \fBint open(const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB, mode_t \fP\fImode\fP\fB);\fP
94
95 \fBint creat(const char *\fP\fIpathname\fP\fB, mode_t \fP\fImode\fP\fB);\fP
96 .sp
97 \fBint openat(int \fP\fIdirfd\fP\fB, const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB);\fP
98 \fBint openat(int \fP\fIdirfd\fP\fB, const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB, mode_t \fP\fImode\fP\fB);\fP
99 .fi
100 .sp
101 .in -4n
102 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
103 .in
104 .sp
105 \fBopenat\fP():
106 .PD 0
107 .ad l
108 .RS 4
109 .TP  4
110 glibc 2.10 以降:
111 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
112 .TP 
113 glibc 2.10 より前:
114 _ATFILE_SOURCE
115 .RE
116 .ad
117 .PD
118 .SH 説明
119 ファイルの \fIpathname\fP を与えると、 \fBopen\fP()  はファイルディスクリプタを返す。
120 ファイルディスクリプタは、この後に続くシステムコール (\fBread\fP(2), \fBwrite\fP(2), \fBlseek\fP(2),
121 \fBfcntl\fP(2) など)  で使用される小さな非負の整数である。 このシステムコールが成功した場合に返されるファイルディスクリプタは
122 そのプロセスがその時点でオープンしていないファイルディスクリプタの うち最小の数字のものとなる。
123 .PP
124 デフォルトでは、新しいファイルディスクリプタは \fBexecve\fP(2) を実行した後も
125 オープンされたままとなる (つまり、 \fBfcntl\fP(2) に説明がある \fBFD_CLOEXEC\fP
126 ファイルディスクリプタフラグは最初は無効である; 後述の \fBO_CLOEXEC\fP フラグ
127 を使うとこのデフォルトを変更することができる)。 ファイルオフセット
128 (file offset) はファイルの先頭に設定される (\fBlseek\fP(2) 参照)。
129 .PP
130 \fBopen\fP()  を呼び出すと、「オープンファイル記述」 \fI(open file description)\fP
131 が作成される。ファイル記述とは、システム全体の オープン中のファイルのテーブルのエントリである。 このエントリは、ファイルオフセットとファイル状態フラグ
132 (\fBfcntl\fP(2)  \fBF_SETFL\fP 操作により変更可能) が保持する。 ファイルディスクリプタはこれらのエントリの一つへの参照である。
133 この後で \fIpathname\fP が削除されたり、他のファイルを参照するように変更されたりしても、 この参照は影響を受けない。
134 新しいオープンファイル記述は最初は他のどのプロセスとも 共有されていないが、 \fBfork\fP(2)  で共有が起こる場合がある。
135 .PP
136 引き数 \fIflags\fP には、アクセスモード \fBO_RDONLY\fP, \fBO_WRONLY\fP, \fBO_RDWR\fP
137 のどれかひとつが入っていなければならない。 これらはそれぞれ読み込み専用、書き込み専用、読み書き用に ファイルをオープンすることを要求するものである。
138
139 .\" SUSv4 divides the flags into:
140 .\" * Access mode
141 .\" * File creation
142 .\" * File status
143 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
144 .\" though it's not clear what the difference between "other" and
145 .\" "File creation" flags is.  I raised an Aardvark to see if this
146 .\" can be clarified in SUSv4; 10 Oct 2008.
147 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
148 .\" TC1 (balloted in 2013), resolved this, so that those three constants
149 .\" are also categorized" as file status flags.
150 .\"
151 さらに、 \fIflags\fP には、ファイル作成フラグ (file creation flag) とファイル状態フラグ (file status
152 flag) を 0 個以上「ビット単位の OR (bitwise\-or)」で 指定することができる。 \fIファイル作成フラグ\fP は
153 \fBO_CLOEXEC\fP, \fBO_CREAT\fP, \fBO_DIRECTORY\fP, \fBO_EXCL\fP, \fBO_NOCTTY\fP,
154 \fBO_NOFOLLOW\fP, \fBO_TMPFILE\fP, \fBO_TRUNC\fP, \fBO_TTY_INIT\fP である。 \fIファイル状態フラグ\fP
155 は以下のリストのうち上記以外の残りのものである。 二種類のフラグの違いは、ファイル状態フラグの方はその内容を取得したり (場合によっては)
156 変更したりできる点にある。詳細は \fBfcntl\fP(2) を参照。
157
158 The full list of file creation flags and file status flags is as follows:
159 .TP 
160 \fBO_APPEND\fP
161 .\" For more background, see
162 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
163 .\" http://nfs.sourceforge.net/
164 ファイルを追加 (append) モードでオープンする。 毎回の \fBwrite\fP(2)  の前に \fBlseek\fP(2)
165 を行ったかのように、ファイルポインタをファイルの最後に移動する。 NFS ファイルシステムで、 \fBO_APPEND\fP
166 を使用すると、複数のプロセスがひとつのファイルに同時にデータを追加した場合、 ファイルが壊れてしまうことがある。 これは NFS
167 が追加モードをサポートしていないため、 クライアントのカーネル (kernel) がそれをシミュレートしなければならないのだが、
168 競合状態を避けることはできないからである。
169 .TP 
170 \fBO_ASYNC\fP
171 シグナル駆動 I/O (signal\-driven I/O) を有効にする: このファイルディスクリプタへの
172 入力または出力が可能になった場合に、シグナルを生成する (デフォルトは \fBSIGIO\fP であるが、 \fBfcntl\fP(2)
173 によって変更可能である)。 この機能が使用可能なのは端末、疑似端末、ソケットのみであり、 (Linux 2.6 以降では) パイプと FIFO
174 に対しても使用できる。 さらに詳しい説明は \fBfcntl\fP(2)  を参照すること。 下記の「バグ」も参照。
175 .TP 
176 \fBO_CLOEXEC\fP (Linux 2.6.23 以降)
177 .\" NOTE! several otehr man pages refer to this text
178 Enable the close\-on\-exec flag for the new file descriptor.  Specifying this
179 flag permits a program to avoid additional \fBfcntl\fP(2)  \fBF_SETFD\fP
180 operations to set the \fBFD_CLOEXEC\fP flag.
181
182 .\" This flag fixes only one form of the race condition;
183 .\" The race can also occur with, for example, descriptors
184 .\" returned by accept(), pipe(), etc.
185 Note that the use of this flag is essential in some multithreaded programs,
186 because using a separate \fBfcntl\fP(2)  \fBF_SETFD\fP operation to set the
187 \fBFD_CLOEXEC\fP flag does not suffice to avoid race conditions where one
188 thread opens a file descriptor and attempts to set its close\-on\-exec flag
189 using \fBfcntl\fP(2)  at the same time as another thread does a \fBfork\fP(2)
190 plus \fBexecve\fP(2).  Depending on the order of execution, the race may lead
191 to the file desriptor returned by \fBopen\fP()  being unintentionally leaked to
192 the program executed by the child process created by \fBfork\fP(2).  (This kind
193 of race is in principle possible for any system call that creates a file
194 descriptor whose close\-on\-exec flag should be set, and various other Linux
195 system calls provide an equivalent of the \fBO_CLOEXEC\fP flag to deal with
196 this problem.)
197 .TP 
198 \fBO_CREAT\fP
199 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
200 .\" XFS (since 2.6.14).
201 ファイルが存在しなかった場合は作成 (create) する。 ファイルの所有者 (ユーザー ID) は、プロセスの実効ユーザー ID に設定される。
202 グループ所有権 (グループ ID) は、プロセスの実効グループ ID または親ディレクトリのグループ ID に設定される
203 (これは、ファイルシステムタイプ、マウントオプション、 親ディレクトリのモードに依存する。 \fBmount\fP(8)  で説明されているマウントオプション
204 \fIbsdgroups\fP と \fIsysvgroups\fP を参照)。
205 .RS
206 .PP
207 \fImode\fP は新しいファイルを作成する場合に使用するアクセス許可 (permission) を指定する。 \fIflags\fP に \fBO_CREAT\fP
208 か \fBO_TMPFILE\fP が指定されている場合、 \fImode\fP を指定しなければならない。 \fBO_CREAT\fP も \fBO_TMPFILE\fP
209 も指定されていない場合、 \fImode\fP は無視される。 有効なアクセス許可は、普段と同じようにプロセスの \fIumask\fP
210 によって修正され、作成されたファイルの許可は \fI(mode\ &\ ~umask)\fP となる。
211 このモードは、新しく作成されたファイルに対するそれ以降のアクセス にのみ適用される点に注意すること。 読み取り専用のファイルを作成する
212 \fBopen\fP()  コールであっても、 読み書き可能なファイルディスクリプタを返すことがありうる。
213 .PP
214 \fImode\fP のために以下のシンボル定数が提供されている :
215 .TP  9
216 \fBS_IRWXU\fP
217 00700 ユーザー (ファイルの所有者) に読み込み、書き込み、 実行の許可がある。
218 .TP 
219 \fBS_IRUSR\fP
220 00400 ユーザーに読み込みの許可がある。
221 .TP 
222 \fBS_IWUSR\fP
223 00200 ユーザーに書き込みの許可がある。
224 .TP 
225 \fBS_IXUSR\fP
226 00100 ユーザーに実行の許可がある。
227 .TP 
228 \fBS_IRWXG\fP
229 00070 グループに読み込み、書き込み、実行の許可がある。
230 .TP 
231 \fBS_IRGRP\fP
232 00040 グループに読み込みの許可がある。
233 .TP 
234 \fBS_IWGRP\fP
235 00020 グループに書き込みの許可がある。
236 .TP 
237 \fBS_IXGRP\fP
238 00010 グループに実行の許可がある。
239 .TP 
240 \fBS_IRWXO\fP
241 00007 他人 (others) に読み込み、書き込み、実行の許可がある。
242 .TP 
243 \fBS_IROTH\fP
244 00004 他人に読み込みの許可がある。
245 .TP 
246 \fBS_IWOTH\fP
247 00002 他人に書き込みの許可がある。
248 .TP 
249 \fBS_IXOTH\fP
250 00001 他人に実行の許可がある。
251 .RE
252 .TP 
253 \fBO_DIRECT\fP (Linux 2.4.10 以降)
254 このファイルに対する I/O のキャッシュの効果を最小化しようとする。このフラグを使うと、一般的に性能が低下する。
255 しかしアプリケーションが独自にキャッシングを行っているような 特別な場合には役に立つ。 ファイルの I/O はユーザー空間バッファに対して直接行われる。
256 \fBO_DIRECT\fP フラグ自身はデータを同期で転送しようとはするが、 \fBO_SYNC\fP
257 フラグのようにデータと必要なメタデータの転送が保証されるわけではない。同期 I/O を保証するためには、 \fBO_DIRECT\fP に加えて
258 \fBO_SYNC\fP を使用しなければならない。下記の「注意」の節の議論も参照。
259 .sp
260 ブロックデバイスに対する似通った意味のインターフェースが \fBraw\fP(8)  で説明されている (但し、このインタフェースは非推奨である)。
261 .TP 
262 \fBO_DIRECTORY\fP
263 .\" But see the following and its replies:
264 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
265 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
266 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
267 If \fIpathname\fP is not a directory, cause the open to fail.  This flag was
268 added in kernel version 2.1.126, to avoid denial\-of\-service problems if
269 \fBopendir\fP(3)  is called on a FIFO or tape device.
270 .TP 
271 \fBO_DSYNC\fP
272 Write operations on the file will complete according to the requirements of
273 synchronized I/O \fIdata\fP integrity completion.
274
275 By the time \fBwrite\fP(2)  (and similar)  return, the output data has been
276 transferred to the underlying hardware, along with any file metadata that
277 would be required to retrieve that data (i.e., as though each \fBwrite\fP(2)
278 was followed by a call to \fBfdatasync\fP(2)).  \fISee NOTES below\fP.
279 .TP 
280 \fBO_EXCL\fP
281 この呼び出しでファイルが作成されることを保証する。このフラグが \fBO_CREAT\fP と
282 一緒に指定され、 \fIpathname\fP のファイルが既に存在した場合、 \fBopen\fP() は失敗
283 する。
284
285 .\" POSIX.1-2001 explicitly requires this behavior.
286 これら二つのフラグが指定された際、シンボリックリンクは辿られない。 \fIpathname\fP がシンボリックリンクの場合、
287 シンボリックリンクがどこを指しているかに関わらず \fBopen\fP()  は失敗する。
288
289 一般的には、 \fBO_CREAT\fP を指定せずに \fBO_EXCL\fP を使用した場合の
290 \fBO_EXCL\fP の動作は規定されていない。
291 これには一つ例外があり、Linux 2.6 以降では、
292 \fIpathname\fP がブロックデバイスを参照している場合、
293 \fBO_CREAT\fP なしで \fBO_EXCL\fP を使用することができる。
294 システムがそのブロックデバイスを使用中の場合 (例えば、
295 マウントされているなど)、 \fBopen\fP() はエラー \fBEBUSY\fP で失敗する。
296
297 NFS では、 \fBO_EXCL\fP は、Linux 2.6 以降で NFSv3 以降を使っている場合でのみサポートされる。 \fBO_EXCL\fP
298 サポートが提供されていない NFS 環境では、このフラグに頼って ロック処理を実行するプログラムは競合状態 (race condition) に出会う
299 可能性がある。 ロックファイルを使用して不可分 (atomic) なファイルロックを実現し、 NFS が \fBO_EXCL\fP
300 をサポートしているかに依存しないようにしたい場合、 移植性のある方法は、同じファイルシステム上に他と名前の重ならない ファイル (例えばホスト名と
301 PID を組み合わせた名前) を作成し、 \fBlink\fP(2)  を使用してそのロックファイルへのリンクを作成することである。 \fBlink\fP(2)
302 コールの返り値が 0 ならばロックに成功している。 あるいは、そのファイルに \fBstat\fP(2)  を使用してリンク数 (link count) が
303 2 になっているかをチェックする。 そうなっていれば、同じくロックに成功しているということである。
304 .TP 
305 \fBO_LARGEFILE\fP
306 (LFS) \fIoff_t\fP ではサイズを表せない (だだし \fIoff64_t\fP ではサイズを表せる)ファ
307 イルをオープン可能にする。この定義を有効にするためには、(\fIどの\fPヘッダファイ
308 ルをインクルードするよりも前に) \fB_LARGEFILE64_SOURCE\fP マクロを定義しなければ
309 ならない。
310 32 ビットシステムにおいて大きなファイルにアクセスしたい場合、
311 (\fBO_LARGEFILE\fP を使うよりも) \fB_FILE_OFFSET_BITS\fP 機能検査マクロを 64 に
312 セットする方が望ましい方法である (\fBfeature_test_macros\fP(7) を参照)。
313 .TP 
314 \fBO_NOATIME\fP (Linux 2.6.8 以降)
315 .\" The O_NOATIME flag also affects the treatment of st_atime
316 .\" by mmap() and readdir(2), MTK, Dec 04.
317 ファイルに対して \fBread\fP(2)  が実行されたときに、最終アクセス時刻 (inode の \fIst_atime\fP) を更新しない。
318 このフラグはインデックス作成やバックアッププログラムで使うことを意図している。 これを使うとディスクに対する操作を大幅に減らすことができる。
319 このフラグは全てのファイルシステムに対して有効であるわけではない。 その一例が NFS であり、サーバがアクセス時刻を管理している。
320 .TP 
321 \fBO_NOCTTY\fP
322 \fIpathname\fP が端末 (terminal) デバイス \(em \fBtty\fP(4) 参照 \(em を指している
323 場合に、たとえそのプロセスが制御端末を持っていなくても、オープンしたファイル
324 は制御端末にはならない。
325 .TP 
326 \fBO_NOFOLLOW\fP
327 .\" The headers from glibc 2.0.100 and later include a
328 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
329 .\" used\fP.
330 \fIpathname\fP がシンボリックリンクだった場合、オープンは失敗する。 これは FreeBSD の拡張で、Linux には バージョン
331 2.1.126 で追加された。 このフラグが指定された場合でも \fIpathname\fP の前の方の要素 (最後のディレクトリセパレータより前の部分)
332 にあるシンボリックリンクについてはリンクが辿られる。 下記の \fBO_PATH\fP も参照のこと。
333 .TP 
334 \fBO_NONBLOCK\fP または \fBO_NDELAY\fP
335 可能ならば、ファイルは非停止 (nonblocking) モードでオープンされる。
336 \fBopen\fP() も、返したファイルディスクリプタに対する以後のすべての操作も呼び出
337 したプロセスを待たせることはない。 FIFO (名前付きパイプ) を扱う場合には
338 \fBfifo\fP(7) も参照すること。 強制ファイルロック (mandatory file lock) やファイ
339 ルリース (file lease) と組み合わせた場合の、 \fBO_NONBLOCK\fP の効果についての
340 議論は、 \fBfcntl\fP(2) を参照すること。
341 .TP 
342 \fBO_PATH\fP (Linux 2.6.39 以降)
343 .\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
344 .\" commit 326be7b484843988afe57566b627fb7a70beac56
345 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
346 .\"
347 .\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
348 .\"     Subject: Re: [PATCH] open(2): document O_PATH
349 .\"     Newsgroups: gmane.linux.man, gmane.linux.kernel
350 .\"
351 このフラグを指定して取得したファイルディスクリプタは、 ファイルシステムツリー内での場所を示すため、
352 純粋にファイルディスクリプタレベルでの作用する操作を実行するため、 の二つの目的で使用することができる。 ファイル自身はオープンされず、
353 他のファイル操作 (例えば \fBread\fP(2), \fBwrite\fP(2), \fBfchmod\fP(2), \fBfchown\fP(2),
354 \fBfgetxattr\fP(2), \fBmmap\fP(2)) はエラー \fBEBADF\fP で失敗する。
355
356 取得したファイルディスクリプタに対して以下の操作を行うことが「できる」。
357 .RS
358 .IP * 3
359 .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
360 .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
361 \fBclose\fP(2); \fBfchdir\fP(2)  (Linux 3.5 以降); \fBfstat\fP(2)  (Linux 3.6 以降)
362 .IP *
363 ファイルディスクリプタの複製 (\fBdup\fP(2), \fBfcntl\fP(2)  \fBF_DUPFD\fP など)
364 .IP *
365 ファイルディスクリプタフラグの取得と設定 (\fBfcntl\fP(2) の \fBF_GETFD\fP と \fBF_SETFD\fP)
366 .IP *
367 \fBfcntl\fP(2) の \fBF_GETFL\fP 操作を使ったオープンされたファイルの状態フラグの取得。 返されるフラグには \fBO_PATH\fP
368 ビットが含まれる。
369
370 .IP *
371 \fBopenat\fP(2) や他の "*at()" 系のシステムコールの \fIdirfd\fP 引数としてそのファイルディスクリプタを渡す。
372 .IP *
373 そのファイルディスクリプタを別のプロセスに UNIX ドメインソケット経由で渡す。 (\fBunix\fP(7) の \fBSCM_RIGHTS\fP を参照)
374 .RE
375 .IP
376 \fIflags\fP に \fBO_PATH\fP が指定された場合、 \fBO_DIRECTORY\fP と \fBO_NOFOLLOW\fP
377 以外のフラグビットは無視される。
378
379 \fIpathname\fP がシンボリックリンクで \fBO_NOFOLLOW\fP フラグも合わせて指定された場合、
380 この呼び出しではシンボリックリンクを参照するファイルディスクリプタを返す。 このファイルディスクリプタは、 空のパス名を指定した
381 \fBfchownat\fP(2), \fBfstatat\fP(2), \fBlinkat\fP(2), \fBreadlinkat\fP(2) の呼び出しで
382 \fIdirfd\fP 引数として使うことで、 そのシンボリックリンクに対して操作を行うことができる。
383 .TP 
384 \fBO_SYNC\fP
385 Write operations on the file will complete according to the requirements of
386 synchronized I/O \fIfile\fP integrity completion (by contrast with contrast
387 with the synchronized I/O \fIdata\fP integrity completion provided by
388 \fBO_DSYNC\fP.)
389
390 By the time \fBwrite\fP(2)  (and similar)  return, the output data and
391 associated file metadata have been transferred to the underlying hardware
392 (i.e., as though each \fBwrite\fP(2)  was followed by a call to \fBfsync\fP(2)).
393 \fISee NOTES below\fP.
394 .TP 
395 \fBO_TMPFILE\fP (Linux 3.11 以降)
396 .\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
397 .\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
398 .\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
399 Create an unnamed temporary file.  The \fIpathname\fP argument specifies a
400 directory; an unnamed inode will be created in that directory's filesystem.
401 Anything written to the resulting file will be lost when the last file
402 descriptor is closed, unless the file is given a name.
403
404 \fBO_TMPFILE\fP must be specified with one of \fBO_RDWR\fP or \fBO_WRONLY\fP and,
405 optionally, \fBO_EXCL\fP.  If \fBO_EXCL\fP is not specified, then \fBlinkat\fP(2)
406 can be used to link the temporary file into the filesystem, making it
407 permanent, using code like the following:
408
409 .in +4n
410 .nf
411 char path[PATH_MAX];
412 fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
413                         S_IRUSR | S_IWUSR);
414
415 /* File I/O on 'fd'... */
416
417 snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
418 linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
419                         AT_SYMLINK_FOLLOW);
420 .fi
421 .in
422
423 In this case, the \fBopen\fP()  \fImode\fP argument determines the file permission
424 mode, as with \fBO_CREAT\fP.
425
426 Specifying \fBO_EXCL\fP in conjunction with \fBO_TMPFILE\fP prevents a temporary
427 file from being linked into the filesystem in the above manner.  (Note that
428 the meaning of \fBO_EXCL\fP in this case is different from the meaning of
429 \fBO_EXCL\fP otherwise.)
430
431
432 .\" Inspired by http://lwn.net/Articles/559147/
433 There are two main use cases for \fBO_TMPFILE\fP:
434 .RS
435 .IP * 3
436 Improved \fBtmpfile\fP(3)  functionality: race\-free creation of temporary files
437 that (1) are automatically deleted when closed; (2) can never be reached via
438 any pathname; (3) are not subject to symlink attacks; and (4) do not require
439 the caller to devise unique names.
440 .IP *
441 Creating a file that is initially invisible, which is then populated with
442 data and adjusted to have appropriate filesystem attributes (\fBchown\fP(2),
443 \fBchmod\fP(2), \fBfsetxattr\fP(2), etc.)  before being atomically linked into the
444 filesystem in a fully formed state (using \fBlinkat\fP(2)  as described above).
445 .RE
446 .IP
447 .\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
448 .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
449 \fBO_TMPFILE\fP requires support by the underlying filesystem; only a subset of
450 Linux filesystems provide that support.  In the initial implementation,
451 support was provided in the ex2, ext3, ext4, UDF, Minix, and shmem
452 filesystems.  XFS support was added in Linux 3.15.
453 .TP 
454 \fBO_TRUNC\fP
455 ファイルが既に存在し、通常ファイルであり、 アクセスモードで書き込みが許可されている (つまり、 \fBO_RDWR\fP または \fBO_WRONLY\fP
456 の) 場合、長さ 0 に切り詰め (truncate) られる。 ファイルが FIFO または端末デバイスファイルの場合、 \fBO_TRUNC\fP
457 フラグは無視される。 それ以外の場合、 \fBO_TRUNC\fP の効果は未定義である。
458 .SS creat()
459 \fBcreat\fP()  は \fIflags\fP に \fBO_CREAT|O_WRONLY|O_TRUNC\fP を指定して \fBopen\fP()
460 を行うのと等価である。
461 .SS openat()
462 \fBopenat\fP() システムコールは \fBopen\fP() と全く同様に動作するが、以下で説明する点が異なる。
463
464 \fIpathname\fP で指定されたパス名が相対パスの場合、このパス名はファイルディスクリプター \fIdirfd\fP
465 が参照するディレクトリに対する相対パスと解釈される (\fBopen\fP()
466 に相対パス名を渡した場合のように、呼び出したプロセスのカレントワーキングディレクトリに対する相対パスではない)。
467
468 \fIpathname\fP で指定されたパス名が相対パスで、 \fIdirfd\fP が特別な値 \fBAT_FDCWD\fP の場合、 (\fBopen\fP()
469 と同様に) \fIpathname\fP は呼び出したプロセスのカレントワーキングディレクトリに対する相対パスと解釈される。
470
471 \fIpathname\fP で指定されたパス名が絶対パスの場合、 \fIdirfd\fP は無視される。
472 .SH 返り値
473 \fBopen\fP(), \fBopenat\fP(), \fBcreat\fP() は新しいファイルディスクリプタを返す。 エラーが発生した場合は \-1 を返す
474 (その場合は \fIerrno\fP が適切に設定される)。
475 .SH エラー
476 \fBopen\fP(), \fBopenat\fP(), and \fBcreat\fP()  can fail with the following errors:
477 .TP 
478 \fBEACCES\fP
479 ファイルに対する要求されたアクセスが許されていないか、 \fIpathname\fP のディレクトリ部分の何れかのディレクトリに検索許可がなかった。
480 またはファイルが存在せず、親ディレクトリへの書き込み許可がなかった。 (\fBpath_resolution\fP(7)  も参照すること。)
481 .TP 
482 \fBEDQUOT\fP
483 \fBO_CREAT\fP が指定された場合で、そのファイルが存在せず、ディスクブロックか inode がそのファイルシステムのユーザクォータに達していた。
484 .TP 
485 \fBEEXIST\fP
486 \fIpathname\fP は既に存在し、 \fBO_CREAT\fP と \fBO_EXCL\fP が使用された。
487 .TP 
488 \fBEFAULT\fP
489 \fIpathname\fP がアクセス可能なアドレス空間の外を指している。
490 .TP 
491 \fBEFBIG\fP
492 \fBEOVERFLOW\fP 参照。
493 .TP 
494 \fBEINTR\fP
495 遅いデバイス (例えば FIFO、 \fBfifo\fP(7)  参照) のオープンが完了するのを待って停止している間に
496 システムコールがシグナルハンドラにより割り込まれた。 \fBsignal\fP(7)  参照。
497 .TP 
498 \fBEINVAL\fP
499 ファイルシステムが \fBO_DIRECT\fP フラグをサポートしていない。 詳細は\fB注意\fPを参照。
500 .TP 
501 \fBEINVAL\fP
502 .\" In particular, __O_TMPFILE instead of O_TMPFILE
503 \fIflags\fP に無効な値が入っている。
504 .TP 
505 \fBEINVAL\fP
506 \fIflags\fP に \fBO_TMPFILE\fP が指定されたが、 \fBO_WRONLY\fP も \fBO_RDWR\fP も指定されていなかった。
507 .TP 
508 \fBEISDIR\fP
509 \fIpathname\fP はディレクトリを参照しており、書き込み要求が含まれていた (つまり \fBO_WRONLY\fP または \fBO_RDWR\fP
510 が設定されている)。
511 .TP 
512 \fBEISDIR\fP
513 \fIpathname\fP refers to an existing directory, \fBO_TMPFILE\fP and one of
514 \fBO_WRONLY\fP or \fBO_RDWR\fP were specified in \fIflags\fP, but this kernel version
515 does not provide the \fBO_TMPFILE\fP functionality.
516 .TP 
517 \fBELOOP\fP
518 \fIpathname\fP を解決する際に遭遇したシンボリックリンクが多過ぎる。
519 .TP 
520 \fBELOOP\fP
521 \fIpathname\fP was a symbolic link, and \fIflags\fP specified \fBO_NOFOLLOW\fP but
522 not \fBO_PATH\fP.
523 .TP 
524 \fBEMFILE\fP
525 プロセスがオープンしているファイル数がすでに最大数に達している。
526 .TP 
527 \fBENAMETOOLONG\fP
528 \fIpathname\fP が長過ぎる。
529 .TP 
530 \fBENFILE\fP
531 オープンされているファイルの総数がシステムの制限に達している。
532 .TP 
533 \fBENODEV\fP
534 \fIpathname\fP がデバイススペシャルファイルを参照しており、対応するデバイスが存在しない。 (これは Linux
535 カーネルのバグであり、この場合には \fBENXIO\fP が返されるべきである)
536 .TP 
537 \fBENOENT\fP
538 \fBO_CREAT\fP が設定されておらず、かつ指定されたファイルが存在しない。 または、 \fIpathname\fP のディレクトリ部分が存在しないか壊れた
539 (dangling) シンボリックリンクである。
540 .TP 
541 \fBENOENT\fP
542 \fIpathname\fP refers to a nonexistent directory, \fBO_TMPFILE\fP and one of
543 \fBO_WRONLY\fP or \fBO_RDWR\fP were specified in \fIflags\fP, but this kernel version
544 does not provide the \fBO_TMPFILE\fP functionality.
545 .TP 
546 \fBENOMEM\fP
547 十分なカーネルメモリーがない。
548 .TP 
549 \fBENOSPC\fP
550 \fIpathname\fP を作成する必要があるが、 \fIpathname\fP を含んでいるデバイスに新しいファイルのための空き容量がない。
551 .TP 
552 \fBENOTDIR\fP
553 \fIpathname\fP に含まれるディレクトリ部分のどれかが実際にはディレクトリでない。 または \fBO_DIRECTORY\fP が指定されており、
554 \fIpathname\fP がディレクトリでない。
555 .TP 
556 \fBENXIO\fP
557 \fBO_NONBLOCK\fP | \fBO_WRONLY\fP が設定されており、指定したファイルが FIFO で
558 そのファイルを読み込みのためにオープンしているプロセスが存在しない。 または、ファイルがデバイススペシャルファイルで 対応するデバイスが存在しない。
559 .TP 
560 \fBEOPNOTSUPP\fP
561 \fIpathname\fP を含んでいるファイルシステムが \fBO_TMPFILE\fP をサポートしていない。
562 .TP 
563 \fBEOVERFLOW\fP
564 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
565 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
566 .\" Reported 2006-10-03
567 \fIpathname\fP が参照しているのが、大き過ぎてオープンできない通常のファイルである。 通常、このエラーが発生するは、32
568 ビットプラットフォーム上で \fI\-D_FILE_OFFSET_BITS=64\fP を指定せずにコンパイルされたアプリケーションが、ファイルサイズが
569 \fI(2<31)\-1\fP ビットを超えるファイルを開こうとした場合である。 上記の \fBO_LARGEFILE\fP も参照。 これは
570 POSIX.1\-2001 で規定されているエラーである。 2.6.24 より前のカーネルでは、Linux はこの場合にエラー \fBEFBIG\fP
571 を返していた。
572 .TP 
573 \fBEPERM\fP
574 .\" Strictly speaking, it's the filesystem UID... (MTK)
575 \fBO_NOATIME\fP フラグが指定されたが、呼び出し元の実効ユーザー ID が ファイルの所有者と一致せず、かつ呼び出し元に特権
576 (\fBCAP_FOWNER\fP)  がない。
577 .TP 
578 \fBEROFS\fP
579 \fIpathname\fP が読み込み専用のファイルシステム上のファイルを参照しており、 書き込みアクセスが要求された。
580 .TP 
581 \fBETXTBSY\fP
582 \fIpathname\fP が現在実行中の実行イメージを参照しており、書き込みが要求された。
583 .TP 
584 \fBEWOULDBLOCK\fP
585 \fBO_NONBLOCK\fP フラグが指定されたが、そのファイルには矛盾するリースが設定されていた (\fBfcntl\fP(2)  参照)。
586 .PP
587 \fBopenat\fP() では以下のエラーも発生する。
588 .TP 
589 \fBEBADF\fP
590 \fIdirfd\fP が有効なファイルディスクリプタではない。
591 .TP 
592 \fBENOTDIR\fP
593 \fIpathname\fP が相対パスで、 \fIdirfd\fP がディレクトリ以外のファイルを参照しているファイルディスクリプタである。
594 .SH バージョン
595 \fBopenat\fP()  はカーネル 2.6.16 で Linux に追加された。 ライブラリによるサポートはバージョン 2.4 で glibc
596 に追加された。
597 .SH 準拠
598 \fBopen\fP(), \fBcreat\fP()  SVr4, 4.3BSD, POSIX.1\-2001, POSIX.1\-2008.
599
600 \fBopenat\fP(): POSIX.1\-2008.
601
602 フラグ \fBO_DIRECT\fP, \fBO_NOATIME\fP, \fBO_PATH\fP, \fBO_TMPFILE\fP は Linux 特有のものである。
603 これらのフラグの定義を得るためには \fB_GNU_SOURCE\fP を定義しなければならない。
604
605 The \fBO_CLOEXEC\fP, \fBO_DIRECTORY\fP, and \fBO_NOFOLLOW\fP flags are not specified
606 in POSIX.1\-2001, but are specified in POSIX.1\-2008.  Since glibc 2.12, one
607 can obtain their definitions by defining either \fB_POSIX_C_SOURCE\fP with a
608 value greater than or equal to 200809L or \fB_XOPEN_SOURCE\fP with a value
609 greater than or equal to 700.  In glibc 2.11 and earlier, one obtains the
610 definitions by defining \fB_GNU_SOURCE\fP.
611
612 As noted in \fBfeature_test_macros\fP(7), feature test macros such as
613 \fB_POSIX_C_SOURCE\fP, \fB_XOPEN_SOURCE\fP, and \fB_GNU_SOURCE\fP must be defined
614 before including \fIany\fP header files.
615 .SH 注意
616 Linux では、 \fBO_NONBLOCK\fP フラグは、 open を実行したいが read または write を実行する意図は
617 必ずしもないことを意味する。 これは \fBioctl\fP(2)  のためのファイルディスクリプタを取得するために、
618 デバイスをオープンするときによく用いられる。
619
620 .LP
621 .\" Linux 2.0, 2.5: truncate
622 .\" Solaris 5.7, 5.8: truncate
623 .\" Irix 6.5: truncate
624 .\" Tru64 5.1B: truncate
625 .\" HP-UX 11.22: truncate
626 .\" FreeBSD 4.7: truncate
627 \fBO_RDONLY | O_TRUNC\fP の影響は未定義であり、その動作は実装によって異なる。 多くのシステムではファイルは実際に切り詰められる。
628
629 \fBopen\fP()  はスペシャルファイルをオープンすることができるが、 \fBcreat\fP()  でスペシャルファイルを作成できない点に注意すること。
630 代わりに \fBmknod\fP(2)  を使用する。
631
632 .\"
633 .\"
634 ファイルが新しく作成されると、 ファイルの \fIst_atime\fP, \fIst_ctime\fP, \fIst_mtime\fP フィールド
635 (それぞれ最終アクセス時刻、最終状態変更時刻、最終修正時刻である。 \fBstat\fP(2)  参照) が現在時刻に設定される。 さらに親ディレクトリの
636 \fIst_ctime\fP と \fIst_mtime\fP も現在時刻に設定される。 それ以外の場合で、O_TRUNC フラグでファイルが修正されたときは、
637 ファイルの \fIst_ctime\fP と \fIst_mtime\fP フィールドが現在時刻に設定される。
638 .SS "Synchronized I/O"
639 The POSIX.1\-2008 "synchronized I/O" option specifies different variants of
640 synchronized I/O, and specifies the \fBopen\fP()  flags \fBO_SYNC\fP, \fBO_DSYNC\fP,
641 and \fBO_RSYNC\fP for controlling the behavior.  Regardless of whether an
642 implementation supports this option, it must at least support the use of
643 \fBO_SYNC\fP for regular files.
644
645 Linux implements \fBO_SYNC\fP and \fBO_DSYNC\fP, but not \fBO_RSYNC\fP.  (Somewhat
646 incorrectly, glibc defines \fBO_RSYNC\fP to have the same value as \fBO_SYNC\fP.)
647
648 \fBO_SYNC\fP provides synchronized I/O \fIfile\fP integrity completion, meaning
649 write operations will flush data and all associated metadata to the
650 underlying hardware.  \fBO_DSYNC\fP provides synchronized I/O \fIdata\fP integrity
651 completion, meaning write operations will flush data to the underlying
652 hardware, but will only flush metadata updates that are required to allow a
653 subsequent read operation to complete successfully.  Data integrity
654 completion can reduce the number of disk operations that are required for
655 applications that don't need the guarantees of file integrity completion.
656
657 To understand the difference between the the two types of completion,
658 consider two pieces of file metadata: the file last modification timestamp
659 (\fIst_mtime\fP)  and the file length.  All write operations will update the
660 last file modification timestamp, but only writes that add data to the end
661 of the file will change the file length.  The last modification timestamp is
662 not needed to ensure that a read completes successfully, but the file length
663 is.  Thus, \fBO_DSYNC\fP would only guarantee to flush updates to the file
664 length metadata (whereas \fBO_SYNC\fP would also always flush the last
665 modification timestamp metadata).
666
667 Before Linux 2.6.33, Linux implemented only the \fBO_SYNC\fP flag for
668 \fBopen\fP().  However, when that flag was specified, most filesystems actually
669 provided the equivalent of synchronized I/O \fIdata\fP integrity completion
670 (i.e., \fBO_SYNC\fP was actually implemented as the equivalent of \fBO_DSYNC\fP).
671
672 .\"
673 .\"
674 Since Linux 2.6.33, proper \fBO_SYNC\fP support is provided.  However, to
675 ensure backward binary compatibility, \fBO_DSYNC\fP was defined with the same
676 value as the historical \fBO_SYNC\fP, and \fBO_SYNC\fP was defined as a new
677 (two\-bit) flag value that includes the \fBO_DSYNC\fP flag value.  This ensures
678 that applications compiled against new headers get at least \fBO_DSYNC\fP
679 semantics on pre\-2.6.33 kernels.
680 .SS NFS
681 NFS を実現しているプロトコルには多くの不備があり、特に \fBO_SYNC\fP と \fBO_NDELAY\fP に影響する。
682
683 .\"
684 .\"
685 UID マッピングを使用している NFS ファイルシステムでは、 \fBopen\fP()  がファイルディスクリプタを返した場合でも \fBread\fP(2)
686 が \fBEACCES\fP で拒否される場合がある。 これはクライアントがアクセス許可のチェックを行って \fBopen\fP()
687 を実行するが、読み込みや書き込みの際には サーバーで UID マッピングが行われるためである。
688 .SS "File access mode"
689 「アクセスモード」の値 \fBO_RDONLY\fP, \fBO_WRONLY\fP, \fBO_RDWR\fP は、 \fIflags\fP
690 に指定できる他の値と違い、個々のビットを指定するものではなく、 これらの値は \fIflags\fP の下位 2 ビットを定義する。 \fBO_RDONLY\fP,
691 \fBO_WRONLY\fP, \fBO_RDWR\fP はそれぞれ 0, 1, 2 に定義されている。 言い換えると、 \fBO_RDONLY |
692 O_WRONLY\fP の組み合わせは論理的に間違いであり、確かに \fBO_RDWR\fP と同じ意味ではない。
693
694 .\" See for example util-linux's disk-utils/setfdprm.c
695 .\" For some background on access mode 3, see
696 .\" http://thread.gmane.org/gmane.linux.kernel/653123
697 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
698 .\" LKML, 12 Mar 2008
699 .\"
700 .\"
701 Linux では、特別な、非標準なアクセスモードとして 3 (バイナリでは 11) が 予約されており \fIflags\fP に指定できる。
702 このアクセスモードを指定すると、ファイルの読み出し/書き込み許可をチェックし、 読み出しにも書き込みにも使用できないディスクリプタを返す。
703 この非標準のアクセスモードはいくつかの Linux ドライバで、デバイス固有の \fBioctl\fP(2)
704 操作にのみ使用されるディスクリプタを返すために使われている。
705 .SS "Rationale for openat() and other directory file descriptor APIs"
706 \fBopenat\fP()  and the other system calls and library functions that take a
707 directory file descriptor argument (i.e., \fBfaccessat\fP(2),
708 \fBfanotify_mark\fP(2), \fBfchmodat\fP(2), \fBfchownat\fP(2), \fBfstatat\fP(2),
709 \fBfutimesat\fP(2), \fBlinkat\fP(2), \fBmkdirat\fP(2), \fBmknodat\fP(2),
710 \fBname_to_handle_at\fP(2), \fBreadlinkat\fP(2), \fBrenameat\fP(2), \fBsymlinkat\fP(2),
711 \fBunlinkat\fP(2), \fButimensat\fP(2)  \fBmkfifoat\fP(3), and \fBscandirat\fP(3))  are
712 supported for two reasons.  Here, the explanation is in terms of the
713 \fBopenat\fP()  call, but the rationale is analogous for the other interfaces.
714
715 First, \fBopenat\fP()  allows an application to avoid race conditions that
716 could occur when using \fBopen\fP()  to open files in directories other than
717 the current working directory.  These race conditions result from the fact
718 that some component of the directory prefix given to \fBopen\fP()  could be
719 changed in parallel with the call to \fBopen\fP().  Such races can be avoided
720 by opening a file descriptor for the target directory, and then specifying
721 that file descriptor as the \fIdirfd\fP argument of \fBopenat\fP().
722
723 .\"
724 .\"
725 Second, \fBopenat\fP()  allows the implementation of a per\-thread "current
726 working directory", via file descriptor(s) maintained by the application.
727 (This functionality can also be obtained by tricks based on the use of
728 \fI/proc/self/fd/\fPdirfd, but less efficiently.)
729 .SS O_DIRECT
730 .LP
731 \fBO_DIRECT\fP フラグを使用する場合、ユーザ空間バッファの長さやアドレス、 I/O
732 のファイルオフセットに関してアラインメントの制限が課されることがある。 Linux では、アラインメントの制限はファイルシステムやカーネルのバージョンに
733 よって異なり、全く制限が存在しない場合もある。 しかしながら、現在のところ、指定されたファイルやファイルシステムに対して
734 こうした制限があるかを見つけるための、アプリケーション向けのインタフェースで ファイルシステム非依存のものは存在しない。
735 いくつかのファイルシステムでは、制限を確認するための独自のインタフェースが 提供されている。例えば、 \fBxfsctl\fP(3)  の
736 \fBXFS_IOC_DIOINFO\fP 命令である。
737 .LP
738 Linux 2.4 では、転送サイズ、 ユーザーバッファのアラインメント、ファイルオフセットは、
739 ファイルシステムの論理ブロックサイズの倍数でなければならない。 Linux 2.6 では、512 バイトごとの境界に配置されていれば充分である。
740 .LP
741 メモリバッファがプライベートマッピング (\fBmmap\fP(2) の \fBMAP_PRIVATE\fP
742 フラグで作成されたマッピング) の場合には、\fBO_DIRECT\fP I/O は
743 \fBfork\fP(2) システムコールと同時に決して実行すべきではない
744 (プライベートマッピングには、ヒープ領域に割り当てられたメモリや静的に
745 割り当てたバッファも含まれる)。非同期 I/O インターフェース (AIO) 経由
746 やプロセス内の他のスレッドから発行された、このような I/O は、
747 \fBfork\fP(2) が呼び出される前に完了されるべきである。
748 そうしなかった場合、データ破壊や、親プロセスや子プロセスでの予期しない
749 動作が起こる可能性がある。
750 \fBO_DIRECT\fP I/O 用のメモリバッファが \fBshmat\fP(2) や\fBMAP_SHARED\fP フラグ
751 付きの \fBmmap\fP(2) で作成された場合には、この制限はあてはまらない。
752 \fBmadvise\fP(2) でメモリバッファにアドバイス \fBMADV_DONTFORK\fP が設定され
753 ている場合にも、この制限はあてはまらない(\fBMADV_DONTFORK\fP はそのメモリ
754 バッファが \fBfork\fP(2) 後に子プロセスからは利用できないことを保証するも
755 のである)。
756 .LP
757 \fBO_DIRECT\fP フラグは SGI IRIX で導入された。SGI IRIX にも Linux 2.4 と同様の (ユーザーバッファの)
758 アラインメントの制限がある。 また、IRIX には適切な配置とサイズを取得するための \fBfcntl\fP(2)  コールがある。 FreeBSD 4.x
759 も同じ名前のフラグを導入したが、アラインメントの制限はない。
760 .LP
761 \fBO_DIRECT\fP が Linux でサポートされたのは、カーネルバージョン 2.4.10 である。 古い Linux
762 カーネルは、このフラグを単に無視する。 \fBO_DIRECT\fP フラグをサポートしていないファイルシステムもあり、その場合は、 \fBO_DIRECT\fP
763 を使用すると \fBopen\fP()  は \fBEINVAL\fP で失敗する。
764 .LP
765 アプリケーションは、同じファイル、 特に同じファイルの重複するバイト領域に対して、 \fBO_DIRECT\fP と通常の I/O
766 を混ぜて使うのは避けるべきである。 ファイルシステムがこのような状況において一貫性の問題を正しく 扱うことができる場合であっても、全体の I/O
767 スループットは どちらか一方を使用するときと比べて低速になるであろう。 同様に、アプリケーションは、同じファイルに対して \fBmmap\fP(2)  と直接
768 I/O (\fBO_DIRECT\fP)  を混ぜて使うのも避けるべきである。
769 .LP
770 NFS で \fBO_DIRECT\fP を使った場合の動作はローカルのファイルシステムの場合と違う。
771 古いカーネルや、ある種の設定でコンパイルされたカーネルは、 \fBO_DIRECT\fP と NFS の組み合わせをサポートしていないかもしれない。 NFS
772 プロトコル自体はサーバにフラグを渡す機能は持っていないので、 \fBO_DIRECT\fP I/O
773 はクライアント上のページキャッシュをバイパスするだけになり、 サーバは I/O をキャッシュしているかもしれない。 クライアントは、
774 \fBO_DIRECT\fP の同期機構を保持するため、サーバに対して I/O を同期して行うように依頼する。 サーバによっては、こうした状況下、特に I/O
775 サイズが小さい場合に 性能が大きく劣化する。 また、サーバによっては、I/O が安定したストレージにまで行われたと、
776 クライアントに対して嘘をつくものもある。 これは、サーバの電源故障が起こった際にデータの完全性が保たれない
777 危険は少しあるが、性能面での不利な条件を回避するために行われている。 Linux の NFS クライアントでは \fBO_DIRECT\fP I/O
778 でのアラインメントの制限はない。
779 .PP
780 まとめると、 \fBO_DIRECT\fP は、注意して使うべきであるが、強力なツールとなる可能性を持っている。 アプリケーションは \fBO_DIRECT\fP
781 をデフォルトでは無効になっている性能向上のためのオプションと 考えておくのがよいであろう。
782 .PP
783 .RS
784 「O_DIRECT でいつも困るのは、インタフェース全部が本当にお馬鹿な点だ。 たぶん危ないマインドコントロール剤で
785 頭がおかしくなったサルが設計したんじゃないかな」 \(em Linus
786 .RE
787 .SH バグ
788 .\" FIXME . Check bugzilla report on open(O_ASYNC)
789 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
790 現在のところ、 \fBopen\fP()  の呼び出し時に \fBO_ASYNC\fP を指定してシグナル駆動 I/O を有効にすることはできない。
791 このフラグを有効にするには \fBfcntl\fP(2)  を使用すること。
792
793 One must check for two different error codes, \fBEISDIR\fP and \fBENOENT\fP, when
794 trying to determine whether the kernel supports \fBO_TMPFILE\fP functionality.
795 .SH 関連項目
796 \fBchmod\fP(2), \fBchown\fP(2), \fBclose\fP(2), \fBdup\fP(2), \fBfcntl\fP(2), \fBlink\fP(2),
797 \fBlseek\fP(2), \fBmknod\fP(2), \fBmmap\fP(2), \fBmount\fP(2), \fBopen_by_name_at\fP(2),
798 \fBread\fP(2), \fBsocket\fP(2), \fBstat\fP(2), \fBumask\fP(2), \fBunlink\fP(2),
799 \fBwrite\fP(2), \fBfopen\fP(3), \fBfifo\fP(7), \fBpath_resolution\fP(7), \fBsymlink\fP(7)
800 .SH この文書について
801 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
802 である。プロジェクトの説明とバグ報告に関する情報は
803 http://www.kernel.org/doc/man\-pages/ に書かれている。