OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / 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 31 Jan 1997 by Eric S. Raymond <esr@thyrsus.com>
33 .\" Modified 18 Apr 2001 by Andi Kleen
34 .\"   Fix description to describe what it really does; add a few caveats.
35 .\" 2006-04-28, mtk, substantial rewrite of various parts.
36 .\"
37 .TH FSYNC 2 2008-11-07 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 fsync, fdatasync \- synchronize a file's in-core state with storage device
40 .SH SYNOPSIS
41 .B #include <unistd.h>
42 .sp
43 .BI "int fsync(int " fd );
44 .sp
45 .BI "int fdatasync(int " fd );
46 .sp
47 .in -4n
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .in
51 .sp
52 .BR fsync ():
53 _BSD_SOURCE || _XOPEN_SOURCE
54 .br
55          || /* since glibc 2.8: */ _POSIX_C_SOURCE\ >=\ 200112L
56 .\" _POSIX_C_SOURCE\ >=\ 200112L only since glibc 2.8
57 .br
58 .BR fdatasync ():
59 _POSIX_C_SOURCE\ >=\ 199309L || _XOPEN_SOURCE\ >=\ 500
60 .SH DESCRIPTION
61 .BR fsync ()
62 transfers ("flushes") all modified in-core data of
63 (i.e., modified buffer cache pages for) the
64 file referred to by the file descriptor
65 .I fd
66 to the disk device (or other permanent storage device)
67 where that file resides.
68 The call blocks until the device reports that the transfer has completed.
69 It also flushes metadata information associated with the file (see
70 .BR stat (2)).
71
72 Calling
73 .BR fsync ()
74 does not necessarily ensure
75 that the entry in the directory containing the file has also reached disk.
76 For that an explicit
77 .BR fsync ()
78 on a file descriptor for the directory is also needed.
79
80 .BR fdatasync ()
81 is similar to
82 .BR fsync (),
83 but does not flush modified metadata unless that metadata
84 is needed in order to allow a subsequent data retrieval to be
85 correctly handled.
86 For example, changes to
87 .I st_atime
88 or
89 .I st_mtime
90 (respectively, time of last access and
91 time of last modification; see
92 .BR stat (2))
93 do not require flushing because they are not necessary for
94 a subsequent data read to be handled correctly.
95 On the other hand, a change to the file size
96 .RI ( st_size ,
97 as made by say
98 .BR ftruncate (2)),
99 would require a metadata flush.
100
101 The aim of
102 .BR fdatasync ()
103 is to reduce disk activity for applications that do not
104 require all metadata to be synchronized with the disk.
105 .SH "RETURN VALUE"
106 On success, these system calls return zero.
107 On error, \-1 is returned, and
108 .I errno
109 is set appropriately.
110 .SH ERRORS
111 .TP
112 .B EBADF
113 .I fd
114 is not a valid file descriptor open for writing.
115 .TP
116 .B EIO
117 An error occurred during synchronization.
118 .TP
119 .BR EROFS ", " EINVAL
120 .I fd
121 is bound to a special file which does not support synchronization.
122 .SH "CONFORMING TO"
123 4.3BSD, POSIX.1-2001.
124 .SH AVAILABILITY
125 On POSIX systems on which
126 .BR fdatasync ()
127 is available,
128 .B _POSIX_SYNCHRONIZED_IO
129 is defined in
130 .I <unistd.h>
131 to a value greater than 0.
132 (See also
133 .BR sysconf (3).)
134 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
135 .\" -1: unavailable, 0: ask using sysconf().
136 .\" glibc defines them to 1.
137 .SH NOTES
138 Applications that access databases or log files often write a tiny
139 data fragment (e.g., one line in a log file) and then call
140 .BR fsync ()
141 immediately in order to ensure that the written data is physically
142 stored on the harddisk.
143 Unfortunately,
144 .BR fsync ()
145 will always initiate two write operations: one for the newly written
146 data and another one in order to update the modification time stored
147 in the inode.
148 If the modification time is not a part of the transaction
149 concept
150 .BR fdatasync ()
151 can be used to avoid unnecessary inode disk write operations.
152
153 If the underlying hard disk has write caching enabled, then
154 the data may not really be on permanent storage when
155 .BR fsync ()
156 /
157 .BR fdatasync ()
158 return.
159 .\" See
160 .\" .BR hdparm (8)
161 .\" for how to disable that cache for IDE disks.
162 .LP
163 When an ext2 file system is mounted with the
164 .I sync
165 option, directory entries are also implicitly synced by
166 .BR fsync ().
167 .LP
168 On kernels before 2.4,
169 .BR fsync ()
170 on big files can be inefficient.
171 An alternative might be to use the
172 .B O_SYNC
173 flag to
174 .BR open (2).
175
176 In Linux 2.2 and earlier,
177 .BR fdatasync ()
178 is equivalent to
179 .BR fsync (),
180 and so has no performance advantage.
181 .SH "SEE ALSO"
182 .BR bdflush (2),
183 .BR open (2),
184 .BR sync (2),
185 .BR sync_file_range (2),
186 .BR hdparm (8),
187 .BR mount (8),
188 .BR sync (8),
189 .BR update (8)