OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / man2 / fsync.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
4 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
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 .\"
26 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
27 .\"   Removed note about old libc (pre-4.5.26) translating to 'sync'.
28 .\" Modified 15 Apr 1995 by Michael Chastain <mec@shell.portal.com>:
29 .\"   Added `see also' section.
30 .\" Modified 13 Apr 1996 by Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
31 .\"   Added remarks about fdatasync.
32 .\" Modified Fri Jan 31 16:27:42 1997 by Eric S. Raymond <esr@thyrsus.com>
33 .\" Modified 31 Jan 1997 by Eric S. Raymond <esr@thyrsus.com>
34 .\" Modified 18 Apr 2001 by Andi Kleen
35 .\"   Fix description to describe what it really does; add a few caveats.
36 .\" 2006-04-28, mtk, substantial rewrite of various parts.
37 .\"
38 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
39 .\"         all rights reserved.
40 .\" Translated 1997-02-22, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
41 .\" Modified 2001-05-19, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
42 .\" Updated 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
43 .\" Updated 2006-07-30, Kentaro Shirakata <argrath@ub32.org>
44 .\" Updated 2007-10-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.65
45 .\"
46 .\"WORD:        core                    メモリ
47 .\"WORD:        disk                    ディスク
48 .\"WORD:        synchronize             同期させる
49 ..\"
50 .TH FSYNC 2 2008-11-07 "Linux" "Linux Programmer's Manual"
51 .SH 名前
52 fsync \- メモリ上にあるファイルの内容をストレージデバイス上のものと同期させる
53 .SH 書式
54 .B #include <unistd.h>
55 .sp
56 .BI "int fsync(int " fd );
57 .sp
58 .BI "int fdatasync(int " fd );
59 .sp
60 .in -4n
61 glibc 向けの機能検査マクロの要件
62 .RB ( feature_test_macros (7)
63 参照):
64 .in
65 .sp
66 .BR fsync ():
67 _BSD_SOURCE || _XOPEN_SOURCE
68 .br
69          || /* glibc 2.8 以降では: */ _POSIX_C_SOURCE\ >=\ 200112L
70 .\" _POSIX_C_SOURCE\ >=\ 200112L は glibc 2.8 以降のみ
71 .br
72 .BR fdatasync ():
73 _POSIX_C_SOURCE\ >=\ 199309L || _XOPEN_SOURCE\ >=\ 500
74 .SH 説明
75 .BR fsync ()
76 は、ファイル記述子
77 .I fd
78 で参照されるファイルの内部で持っているデータ
79 (つまりバッファキャッシュページ) のうち修正されたデータを、
80 そのファイルが属するディスクデバイス (またはその他の永続ストレージデバイス)
81 に転送 (「フラッシュ」) する。
82 この呼び出しは転送が終わったとデバイスが報告するまでブロックする。
83 またファイルに結びついたメタデータ情報
84 .RB ( stat (2)
85 参照) もフラッシュする。
86
87 .BR fsync ()
88 の呼び出しは、ファイルが存在しているディレクトリのエントリがディスクへ
89 書き込まれたことを保証するわけではない。
90 保証するためには明示的にそのディレクトリのファイル記述子に対しても
91 .BR fsync ()
92 する必要がある。
93
94 .BR fdatasync ()
95
96 .BR fsync ()
97 と同様であるが、メタデータの扱いが異なる。
98 .BR fdatasync ()
99 は、それ以降のデータ読み込みを正しく扱うためにそのメタデータが必要に
100 ならない限り、変更されたメタデータをフラッシュしない。
101 例えば、
102 st_atime や st_mtime
103 (それぞれ最終アクセス時刻、最終修正時刻;
104 .BR stat (2)
105 参照) の変更はフラッシュを必要としない。
106 なぜならこれらはそれ以降のデータ読み込みを正しく扱うために
107 必要ではないからである。
108 一方、ファイルサイズ
109 .RB ( ftruncate (2)
110 では
111 .IR st_size )
112 の変更はメタデータのフラッシュが必要である。
113 .BR fdatasync ()
114 の狙いは、全てのメタデータをディスクと同期する必要のない
115 アプリケーションに対して、ディスクアクセスを減らすことである。
116 .SH 返り値
117 成功した場合、これらのシステムコールはゼロを返す。
118 エラーの場合、\-1 が返され、
119 .I errno
120 が適切に設定される。
121 .SH エラー
122 .TP
123 .B EBADF
124 .I fd
125 が書き込みのためにオープンされたファイル記述子でない。
126 .TP
127 .B EIO
128 同期操作の間にエラーが発生した。
129 .TP
130 .BR EROFS ", " EINVAL
131 .I fd
132 が同期操作をサポートしてない特殊なファイルを参照している。
133 .SH 準拠
134 4.3BSD, POSIX.1-2001.
135 .SH 可用性
136 .BR fdatasync ()
137 が利用可能な POSIX システムでは、
138 .B _POSIX_SYNCHRONIZED_IO
139
140 .I <unistd.h>
141 で 0 より大きな値に定義される
142 .RB ( sysconf (3)
143 参照)。
144 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
145 .\" -1: unavailable, 0: ask using sysconf().
146 .\" glibc defines them to 1.
147 .SH 注意
148 データベースやログファイルにアクセスするアプリケーションは、
149 非常に小さなデータの断片の書き込みを行い、その後すぐに
150 .BR fsync ()
151 を呼び出して、書き込んだデータがハードディスクに物理的に
152 確実に格納されるようにすることが多い。
153 残念ながら、
154 .BR fsync ()
155 は常に 2つの書き込み操作を開始する。
156 一つは新しく書き込まれたデータに対するものであり、
157 もう一つは inode に格納される修正時刻 (modification time) を
158 更新するためのものである。
159 修正時刻の更新が不可分な操作 (トランザクション) の一部ではない場合、
160 .BR fdatasync ()
161 を使うことで、不必要な inode のディスク書き込み操作を避けることができる。
162
163 データが配置されているハードディスクの書き込みキャッシュが有効に
164 なっている場合、
165 .BR fsync ()
166 /
167 .BR fdatasync ()
168 から戻ってきたとしても、そのデータは実際には永続的な記憶媒体に
169 格納されていないかもしれない。
170 .\" IDE ディスクのキャッシュを無効にする方法については
171 .\" .BR hdparm (8)
172 .\" を参照のこと。
173 .LP
174 ext2 ファイル・システムが
175 .I sync
176 オプションつきでマウントされている場合、
177 .BR fsync ()
178 でディレクトリ・エントリも暗黙のうちに同期する。
179 .LP
180 2.4 より前のカーネルでは巨大なファイルに
181 .BR fsync ()
182 を使用することは効率が悪い場合がある。
183 別の方法として
184 .BR open (2)
185 の際に
186 .B O_SYNC
187 フラグを使用するのが良いかもしれない。
188
189 Linux 2.2 以前では、
190 .BR fdatasync ()
191
192 .BR fsync ()
193 と等価であり、性能面でのメリットはない。
194 .SH 関連項目
195 .BR bdflush (2),
196 .BR open (2),
197 .BR sync (2),
198 .BR sync_file_range (2),
199 .BR hdparm (8),
200 .BR mount (8),
201 .BR sync (8),
202 .BR update (8)