OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / fsync.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
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 .\" 2012-02-27 Various changes by Christoph Hellwig <hch@lst.de>
37 .\"
38 .TH FSYNC 2 2014-08-19 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 fsync, fdatasync \- synchronize a file's in-core state with storage device
41 .SH SYNOPSIS
42 .B #include <unistd.h>
43 .sp
44 .BI "int fsync(int " fd );
45 .sp
46 .BI "int fdatasync(int " fd );
47 .sp
48 .in -4n
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .in
52 .sp
53 .BR fsync ():
54 _BSD_SOURCE || _XOPEN_SOURCE
55 .br
56          || /* since glibc 2.8: */ _POSIX_C_SOURCE\ >=\ 200112L
57 .\" _POSIX_C_SOURCE\ >=\ 200112L only since glibc 2.8
58 .br
59 .BR fdatasync ():
60 _POSIX_C_SOURCE\ >=\ 199309L || _XOPEN_SOURCE\ >=\ 500
61 .SH DESCRIPTION
62 .BR fsync ()
63 transfers ("flushes") all modified in-core data of
64 (i.e., modified buffer cache pages for) the
65 file referred to by the file descriptor
66 .I fd
67 to the disk device (or other permanent storage device) so that all
68 changed information can be retrieved even after the system crashed or
69 was rebooted.
70 This includes writing through or flushing a disk cache if present.
71 The call blocks until the device reports that the transfer has completed.
72 It also flushes metadata information associated with the file (see
73 .BR stat (2)).
74
75 Calling
76 .BR fsync ()
77 does not necessarily ensure
78 that the entry in the directory containing the file has also reached disk.
79 For that an explicit
80 .BR fsync ()
81 on a file descriptor for the directory is also needed.
82
83 .BR fdatasync ()
84 is similar to
85 .BR fsync (),
86 but does not flush modified metadata unless that metadata
87 is needed in order to allow a subsequent data retrieval to be
88 correctly handled.
89 For example, changes to
90 .I st_atime
91 or
92 .I st_mtime
93 (respectively, time of last access and
94 time of last modification; see
95 .BR stat (2))
96 do not require flushing because they are not necessary for
97 a subsequent data read to be handled correctly.
98 On the other hand, a change to the file size
99 .RI ( st_size ,
100 as made by say
101 .BR ftruncate (2)),
102 would require a metadata flush.
103
104 The aim of
105 .BR fdatasync ()
106 is to reduce disk activity for applications that do not
107 require all metadata to be synchronized with the disk.
108 .SH RETURN VALUE
109 On success, these system calls return zero.
110 On error, \-1 is returned, and
111 .I errno
112 is set appropriately.
113 .SH ERRORS
114 .TP
115 .B EBADF
116 .I fd
117 is not a valid open file descriptor.
118 .TP
119 .B EIO
120 An error occurred during synchronization.
121 .TP
122 .BR EROFS ", " EINVAL
123 .I fd
124 is bound to a special file which does not support synchronization.
125 .SH CONFORMING TO
126 4.3BSD, POSIX.1-2001.
127 .SH AVAILABILITY
128 On POSIX systems on which
129 .BR fdatasync ()
130 is available,
131 .B _POSIX_SYNCHRONIZED_IO
132 is defined in
133 .I <unistd.h>
134 to a value greater than 0.
135 (See also
136 .BR sysconf (3).)
137 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
138 .\" -1: unavailable, 0: ask using sysconf().
139 .\" glibc defines them to 1.
140 .SH NOTES
141 On some UNIX systems (but not Linux),
142 .I fd
143 must be a
144 .I writable
145 file descriptor.
146
147 In Linux 2.2 and earlier,
148 .BR fdatasync ()
149 is equivalent to
150 .BR fsync (),
151 and so has no performance advantage.
152
153 The
154 .BR fsync ()
155 implementations in older kernels and lesser used filesystems
156 does not know how to flush disk caches.
157 In these cases disk caches need to be disabled using
158 .BR hdparm (8)
159 or
160 .BR sdparm (8)
161 to guarantee safe operation.
162 .SH SEE ALSO
163 .BR bdflush (2),
164 .BR open (2),
165 .BR sync (2),
166 .BR sync_file_range (2),
167 .BR hdparm (8),
168 .BR mount (8),
169 .BR sync (1)
170 .SH COLOPHON
171 This page is part of release 3.79 of the Linux
172 .I man-pages
173 project.
174 A description of the project,
175 information about reporting bugs,
176 and the latest version of this page,
177 can be found at
178 \%http://www.kernel.org/doc/man\-pages/.