]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fsync.2
Updated CONFORMING TO
[thirdparty/man-pages.git] / 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@gmx.net>
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 2006-04-28 "Linux 1.3.85" "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 .SH DESCRIPTION
47 .BR fsync ()
48 transfers ("flushes") all modified in-core data of
49 (i.e., modified buffer cache pages for) the
50 file referred to by the file descriptor
51 .I fd
52 to the disk device (or other permanent storage device)
53 where that file resides.
54 The call blocks until the device reports that the transfer has completed.
55 It also flushes metadata information associated with the file (see
56 .BR stat (2)).
57
58 Calling
59 .BR fsync ()
60 does not necessarily ensure
61 that the entry in the directory containing the file has also reached disk.
62 For that an explicit
63 .BR fsync ()
64 on a file descriptor for the directory is also needed.
65
66 .BR fdatasync ()
67 is similar to
68 .BR fsync (),
69 but does not flush modified metadata unless that metadata
70 is needed in order to allow a subsequent data retrieval to be
71 correctly handled.
72 For example, changes to
73 .I st_atime
74 or
75 .I st_mtime
76 (respectively, time of last access and
77 time of last modification; see
78 .BR stat (2))
79 do not not require flushing because they are not necessary for
80 a subsequent data read to be handled correctly.
81 On the other hand, a change to the file size
82 .RI ( st_size ,
83 as made by say
84 .BR ftruncate (2)),
85 would require a metadata flush.
86
87 The aim of
88 .BR fdatasync (2)
89 is to reduce disk activity for applications that do not
90 require all metadata to be synchronised with the disk.
91 .SH "RETURN VALUE"
92 On success, zero is returned. On error, \-1 is returned, and
93 .I errno
94 is set appropriately.
95 .SH ERRORS
96 .TP
97 .B EBADF
98 .I fd
99 is not a valid file descriptor open for writing.
100 .TP
101 .B EIO
102 An error occurred during synchronization.
103 .TP
104 .BR EROFS ", " EINVAL
105 .I fd
106 is bound to a special file which does not support synchronization.
107 .SH NOTES
108 If the underlying hard disk has write caching enabled, then
109 the data may not really be on permanent storage when
110 .BR fsync ()
111 /
112 .BR fdatasync ()
113 return.
114 .\" See
115 .\" .BR hdparm (8)
116 .\" for how to disable that cache for IDE disks.
117 .LP
118 When an ext2 file system is mounted with the
119 .I sync
120 option, directory entries are also implicitly synced by
121 .BR fsync ().
122 .LP
123 On kernels before 2.4,
124 .BR fsync ()
125 on big files can be inefficient.
126 An alternative might be to use the
127 .I O_SYNC
128 flag to
129 .BR open (2).
130 .SH "CONFORMING TO"
131 4.3BSD, POSIX.1-2001
132 .SH "SEE ALSO"
133 .BR bdflush (2),
134 .BR open (2),
135 .BR sync (2),
136 .BR sync_file_range (2),
137 .BR hdparm (8),
138 .BR mount (8),
139 .BR sync (8),
140 .BR update (8)