1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
4 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
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.
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
47 .\"WORD: disk ¥Ç¥£¥¹¥¯
48 .\"WORD: synchronize Ʊ´ü¤µ¤»¤ë
50 .TH FSYNC 2 2008-11-07 "Linux" "Linux Programmer's Manual"
53 .\"O fsync, fdatasync \- synchronize a file's in-core state with storage device
54 fsync \- ¥á¥â¥ê¾å¤Ë¤¢¤ë¥Õ¥¡¥¤¥ë¤ÎÆâÍƤò¥¹¥È¥ì¡¼¥¸¥Ç¥Ð¥¤¥¹¾å¤Î¤â¤Î¤ÈƱ´ü¤µ¤»¤ë
57 .B #include <unistd.h>
59 .BI "int fsync(int " fd );
61 .BI "int fdatasync(int " fd );
64 .\"O Feature Test Macro Requirements for glibc (see
65 .\"O .BR feature_test_macros (7)):
66 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
67 .RB ( feature_test_macros (7)
72 _BSD_SOURCE || _XOPEN_SOURCE
74 .\"O || /* since glibc 2.8: */ _POSIX_C_SOURCE\ >=\ 200112L
75 || /* glibc 2.8 °Ê¹ß¤Ç¤Ï: */ _POSIX_C_SOURCE\ >=\ 200112L
76 .\"O .\" _POSIX_C_SOURCE\ >=\ 200112L only since glibc 2.8
77 .\" _POSIX_C_SOURCE\ >=\ 200112L ¤Ï glibc 2.8 °Ê¹ß¤Î¤ß
80 _POSIX_C_SOURCE\ >=\ 199309L || _XOPEN_SOURCE\ >=\ 500
84 .\"O transfers ("flushes") all modified in-core data of
85 .\"O (i.e., modified buffer cache pages for) the
86 .\"O file referred to by the file descriptor
88 .\"O to the disk device (or other permanent storage device)
89 .\"O where that file resides.
93 ¤Ç»²¾È¤µ¤ì¤ë¥Õ¥¡¥¤¥ë¤ÎÆâÉô¤Ç»ý¤Ã¤Æ¤¤¤ë¥Ç¡¼¥¿
94 (¤Ä¤Þ¤ê¥Ð¥Ã¥Õ¥¡¥¥ã¥Ã¥·¥å¥Ú¡¼¥¸) ¤Î¤¦¤Á½¤Àµ¤µ¤ì¤¿¥Ç¡¼¥¿¤ò¡¢
95 ¤½¤Î¥Õ¥¡¥¤¥ë¤¬Â°¤¹¤ë¥Ç¥£¥¹¥¯¥Ç¥Ð¥¤¥¹ (¤Þ¤¿¤Ï¤½¤Î¾¤Î±Ê³¥¹¥È¥ì¡¼¥¸¥Ç¥Ð¥¤¥¹)
96 ¤ËžÁ÷ (¡Ö¥Õ¥é¥Ã¥·¥å¡×) ¤¹¤ë¡£
97 .\"O The call blocks until the device reports that the transfer has completed.
98 .\"O It also flushes metadata information associated with the file (see
100 ¤³¤Î¸Æ¤Ó½Ð¤·¤ÏžÁ÷¤¬½ª¤ï¤Ã¤¿¤È¥Ç¥Ð¥¤¥¹¤¬Êó¹ð¤¹¤ë¤Þ¤Ç¥Ö¥í¥Ã¥¯¤¹¤ë¡£
101 ¤Þ¤¿¥Õ¥¡¥¤¥ë¤Ë·ë¤Ó¤Ä¤¤¤¿¥á¥¿¥Ç¡¼¥¿¾ðÊó
103 »²¾È) ¤â¥Õ¥é¥Ã¥·¥å¤¹¤ë¡£
107 .\"O does not necessarily ensure
108 .\"O that the entry in the directory containing the file has also reached disk.
109 .\"O For that an explicit
111 .\"O on a file descriptor for the directory is also needed.
113 ¤Î¸Æ¤Ó½Ð¤·¤Ï¡¢¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤·¤Æ¤¤¤ë¥Ç¥£¥ì¥¯¥È¥ê¤Î¥¨¥ó¥È¥ê¤¬¥Ç¥£¥¹¥¯¤Ø
114 ½ñ¤¹þ¤Þ¤ì¤¿¤³¤È¤òÊݾڤ¹¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤¡£
115 Êݾڤ¹¤ë¤¿¤á¤Ë¤ÏÌÀ¼¨Åª¤Ë¤½¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Õ¥¡¥¤¥ëµ½Ò»Ò¤ËÂФ·¤Æ¤â
119 .\"O .BR fdatasync ()
122 .\"O but does not flush modified metadata unless that metadata
123 .\"O is needed in order to allow a subsequent data retrieval to be
124 .\"O correctly handled.
128 ¤ÈƱÍͤǤ¢¤ë¤¬¡¢¥á¥¿¥Ç¡¼¥¿¤Î°·¤¤¤¬°Û¤Ê¤ë¡£
130 ¤Ï¡¢¤½¤ì°Ê¹ß¤Î¥Ç¡¼¥¿Æɤ߹þ¤ß¤òÀµ¤·¤¯°·¤¦¤¿¤á¤Ë¤½¤Î¥á¥¿¥Ç¡¼¥¿¤¬É¬ÍפË
131 ¤Ê¤é¤Ê¤¤¸Â¤ê¡¢Êѹ¹¤µ¤ì¤¿¥á¥¿¥Ç¡¼¥¿¤ò¥Õ¥é¥Ã¥·¥å¤·¤Ê¤¤¡£
132 .\"O For example, changes to
136 .\"O (respectively, time of last access and
137 .\"O time of last modification; see
139 .\"O do not require flushing because they are not necessary for
140 .\"O a subsequent data read to be handled correctly.
143 (¤½¤ì¤¾¤ìºÇ½ª¥¢¥¯¥»¥¹»þ¹ï¡¢ºÇ½ª½¤Àµ»þ¹ï;
145 »²¾È) ¤ÎÊѹ¹¤Ï¥Õ¥é¥Ã¥·¥å¤òɬÍפȤ·¤Ê¤¤¡£
146 ¤Ê¤¼¤Ê¤é¤³¤ì¤é¤Ï¤½¤ì°Ê¹ß¤Î¥Ç¡¼¥¿Æɤ߹þ¤ß¤òÀµ¤·¤¯°·¤¦¤¿¤á¤Ë
147 ɬÍפǤϤʤ¤¤«¤é¤Ç¤¢¤ë¡£
148 .\"O On the other hand, a change to the file size
151 .\"O .BR ftruncate (2)),
152 .\"O would require a metadata flush.
157 ¤ÎÊѹ¹¤Ï¥á¥¿¥Ç¡¼¥¿¤Î¥Õ¥é¥Ã¥·¥å¤¬É¬ÍפǤ¢¤ë¡£
159 .\"O .BR fdatasync ()
160 .\"O is to reduce disk activity for applications that do not
161 .\"O require all metadata to be synchronized with the disk.
163 ¤ÎÁÀ¤¤¤Ï¡¢Á´¤Æ¤Î¥á¥¿¥Ç¡¼¥¿¤ò¥Ç¥£¥¹¥¯¤ÈƱ´ü¤¹¤ëɬÍפΤʤ¤
164 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ËÂФ·¤Æ¡¢¥Ç¥£¥¹¥¯¥¢¥¯¥»¥¹¤ò¸º¤é¤¹¤³¤È¤Ç¤¢¤ë¡£
165 .\"O .SH "RETURN VALUE"
167 .\"O On success, these system calls return zero.
168 .\"O On error, \-1 is returned, and
170 .\"O is set appropriately.
171 À®¸ù¤·¤¿¾ì¹ç¡¢¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¥¼¥í¤òÊÖ¤¹¡£
172 ¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤¬ÊÖ¤µ¤ì¡¢
180 .\"O is not a valid file descriptor open for writing.
182 ¤¬½ñ¤¹þ¤ß¤Î¤¿¤á¤Ë¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ëµ½Ò»Ò¤Ç¤Ê¤¤¡£
185 .\"O An error occurred during synchronization.
186 Ʊ´üÁàºî¤Î´Ö¤Ë¥¨¥é¡¼¤¬È¯À¸¤·¤¿¡£
188 .BR EROFS ", " EINVAL
190 .\"O is bound to a special file which does not support synchronization.
192 ¤¬Æ±´üÁàºî¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤Ê¤¤Æüì¤Ê¥Õ¥¡¥¤¥ë¤ò»²¾È¤·¤Æ¤¤¤ë¡£
193 .\"O .SH "CONFORMING TO"
195 4.3BSD, POSIX.1-2001.
196 .\"O .SH AVAILABILITY
198 .\"O On POSIX systems on which
199 .\"O .BR fdatasync ()
201 .\"O .B _POSIX_SYNCHRONIZED_IO
204 .\"O to a value greater than 0.
206 .\"O .BR sysconf (3).)
208 ¤¬ÍøÍѲÄǽ¤Ê POSIX ¥·¥¹¥Æ¥à¤Ç¤Ï¡¢
209 .B _POSIX_SYNCHRONIZED_IO
212 ¤Ç 0 ¤è¤êÂ礤ÊÃͤËÄêµÁ¤µ¤ì¤ë
215 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
216 .\" -1: unavailable, 0: ask using sysconf().
217 .\" glibc defines them to 1.
220 .\"O Applications that access databases or log files often write a tiny
221 .\"O data fragment (e.g., one line in a log file) and then call
223 .\"O immediately in order to ensure that the written data is physically
224 .\"O stored on the harddisk.
227 .\"O will always initiate two write operations: one for the newly written
228 .\"O data and another one in order to update the modification time stored
230 .\"O If the modification time is not a part of the transaction
232 .\"O .BR fdatasync ()
233 .\"O can be used to avoid unnecessary inode disk write operations.
234 ¥Ç¡¼¥¿¥Ù¡¼¥¹¤ä¥í¥°¥Õ¥¡¥¤¥ë¤Ë¥¢¥¯¥»¥¹¤¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢
235 Èó¾ï¤Ë¾®¤µ¤Ê¥Ç¡¼¥¿¤ÎÃÇÊҤνñ¤¹þ¤ß¤ò¹Ô¤¤¡¢¤½¤Î¸å¤¹¤°¤Ë
237 ¤ò¸Æ¤Ó½Ð¤·¤Æ¡¢½ñ¤¹þ¤ó¤À¥Ç¡¼¥¿¤¬¥Ï¡¼¥É¥Ç¥£¥¹¥¯¤ËʪÍýŪ¤Ë
238 ³Î¼Â¤Ë³ÊǼ¤µ¤ì¤ë¤è¤¦¤Ë¤¹¤ë¤³¤È¤¬Â¿¤¤¡£
241 ¤Ï¾ï¤Ë 2¤Ä¤Î½ñ¤¹þ¤ßÁàºî¤ò³«»Ï¤¹¤ë¡£
242 °ì¤Ä¤Ï¿·¤·¤¯½ñ¤¹þ¤Þ¤ì¤¿¥Ç¡¼¥¿¤ËÂФ¹¤ë¤â¤Î¤Ç¤¢¤ê¡¢
243 ¤â¤¦°ì¤Ä¤Ï inode ¤Ë³ÊǼ¤µ¤ì¤ë½¤Àµ»þ¹ï (modification time) ¤ò
244 ¹¹¿·¤¹¤ë¤¿¤á¤Î¤â¤Î¤Ç¤¢¤ë¡£
245 ½¤Àµ»þ¹ï¤Î¹¹¿·¤¬ÉÔ²Äʬ¤ÊÁàºî (¥È¥é¥ó¥¶¥¯¥·¥ç¥ó) ¤Î°ìÉô¤Ç¤Ï¤Ê¤¤¾ì¹ç¡¢
247 ¤ò»È¤¦¤³¤È¤Ç¡¢ÉÔɬÍ×¤Ê inode ¤Î¥Ç¥£¥¹¥¯½ñ¤¹þ¤ßÁàºî¤òÈò¤±¤ë¤³¤È¤¬¤Ç¤¤ë¡£
249 .\"O If the underlying hard disk has write caching enabled, then
250 .\"O the data may not really be on permanent storage when
253 .\"O .BR fdatasync ()
256 .\"O .\" .BR hdparm (8)
257 .\"O .\" for how to disable that cache for IDE disks.
258 ¥Ç¡¼¥¿¤¬ÇÛÃÖ¤µ¤ì¤Æ¤¤¤ë¥Ï¡¼¥É¥Ç¥£¥¹¥¯¤Î½ñ¤¹þ¤ß¥¥ã¥Ã¥·¥å¤¬Í¸ú¤Ë
263 ¤«¤éÌá¤Ã¤Æ¤¤¿¤È¤·¤Æ¤â¡¢¤½¤Î¥Ç¡¼¥¿¤Ï¼ÂºÝ¤Ë¤Ï±Ê³Ū¤Êµ²±ÇÞÂΤË
264 ³ÊǼ¤µ¤ì¤Æ¤¤¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£
265 .\" IDE ¥Ç¥£¥¹¥¯¤Î¥¥ã¥Ã¥·¥å¤ò̵¸ú¤Ë¤¹¤ëÊýË¡¤Ë¤Ä¤¤¤Æ¤Ï
269 .\"O When an ext2 file system is mounted with the
271 .\"O option, directory entries are also implicitly synced by
273 ext2 ¥Õ¥¡¥¤¥ë¡¦¥·¥¹¥Æ¥à¤¬
275 ¥ª¥×¥·¥ç¥ó¤Ä¤¤Ç¥Þ¥¦¥ó¥È¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
277 ¤Ç¥Ç¥£¥ì¥¯¥È¥ê¡¦¥¨¥ó¥È¥ê¤â°ÅÌۤΤ¦¤Á¤ËƱ´ü¤¹¤ë¡£
279 .\"O On kernels before 2.4,
281 .\"O on big files can be inefficient.
282 .\"O An alternative might be to use the
286 2.4 ¤è¤êÁ°¤Î¥«¡¼¥Í¥ë¤Ç¤ÏµðÂç¤Ê¥Õ¥¡¥¤¥ë¤Ë
288 ¤ò»ÈÍѤ¹¤ë¤³¤È¤Ï¸úΨ¤¬°¤¤¾ì¹ç¤¬¤¢¤ë¡£
293 ¥Õ¥é¥°¤ò»ÈÍѤ¹¤ë¤Î¤¬Îɤ¤¤«¤â¤·¤ì¤Ê¤¤¡£
295 .\"O In Linux 2.2 and earlier,
296 .\"O .BR fdatasync ()
297 .\"O is equivalent to
299 .\"O and so has no performance advantage.
304 ¤ÈÅù²Á¤Ç¤¢¤ê¡¢ÀǽÌ̤ǤΥá¥ê¥Ã¥È¤Ï¤Ê¤¤¡£
310 .BR sync_file_range (2),