| 1 |
.\" Copyright 2006, Michael Kerrisk <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH sync_file_range 2 2026-02-08 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
sync_file_range \- sync a file segment with disk |
| 9 |
.SH LIBRARY |
| 10 |
Standard C library |
| 11 |
.RI ( libc ,\~ \-lc ) |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
| 15 |
.B #define _FILE_OFFSET_BITS 64 |
| 16 |
.B #include <fcntl.h> |
| 17 |
.P |
| 18 |
.BI "int sync_file_range(int " fd ", off_t " offset ", off_t " nbytes , |
| 19 |
.BI " unsigned int " flags ); |
| 20 |
.fi |
| 21 |
.SH DESCRIPTION |
| 22 |
.BR sync_file_range () |
| 23 |
permits fine control when synchronizing the open file referred to by the |
| 24 |
file descriptor |
| 25 |
.I fd |
| 26 |
with disk. |
| 27 |
.P |
| 28 |
.I offset |
| 29 |
is the starting byte of the file range to be synchronized. |
| 30 |
.I nbytes |
| 31 |
specifies the length of the range to be synchronized, in bytes; |
| 32 |
if |
| 33 |
.I nbytes |
| 34 |
is zero, then all bytes from |
| 35 |
.I offset |
| 36 |
through to the end of file are synchronized. |
| 37 |
Synchronization is in units of the system page size: |
| 38 |
.I offset |
| 39 |
is rounded down to a page boundary; |
| 40 |
.I (offset+nbytes\-1) |
| 41 |
is rounded up to a page boundary. |
| 42 |
.P |
| 43 |
The |
| 44 |
.I flags |
| 45 |
bit-mask argument can include any of the following values: |
| 46 |
.TP |
| 47 |
.B SYNC_FILE_RANGE_WAIT_BEFORE |
| 48 |
Wait upon write-out of all pages in the specified range |
| 49 |
that have already been submitted to the device driver for write-out |
| 50 |
before performing any write. |
| 51 |
.TP |
| 52 |
.B SYNC_FILE_RANGE_WRITE |
| 53 |
Initiate write-out of all dirty pages in the specified |
| 54 |
range which are not presently submitted write-out. |
| 55 |
Note that even this may block if you attempt to |
| 56 |
write more than request queue size. |
| 57 |
.TP |
| 58 |
.B SYNC_FILE_RANGE_WAIT_AFTER |
| 59 |
Wait upon write-out of all pages in the range |
| 60 |
after performing any write. |
| 61 |
.P |
| 62 |
Specifying |
| 63 |
.I flags |
| 64 |
as 0 is permitted, as a no-op. |
| 65 |
.SS Warning |
| 66 |
This system call is extremely dangerous and should not be used in portable |
| 67 |
programs. |
| 68 |
None of these operations writes out the file's metadata. |
| 69 |
Therefore, unless the application is strictly performing overwrites of |
| 70 |
already-instantiated disk blocks, there are no guarantees that the data will |
| 71 |
be available after a crash. |
| 72 |
There is no user interface to know if a write is purely an overwrite. |
| 73 |
On filesystems using copy-on-write semantics (e.g., |
| 74 |
.IR btrfs ) |
| 75 |
an overwrite of existing allocated blocks is impossible. |
| 76 |
When writing into preallocated space, |
| 77 |
many filesystems also require calls into the block |
| 78 |
allocator, which this system call does not sync out to disk. |
| 79 |
This system call does not flush disk write caches and thus does not provide |
| 80 |
any data integrity on systems with volatile disk write caches. |
| 81 |
.SS Some details |
| 82 |
.B SYNC_FILE_RANGE_WAIT_BEFORE |
| 83 |
and |
| 84 |
.B SYNC_FILE_RANGE_WAIT_AFTER |
| 85 |
will detect any |
| 86 |
I/O errors or |
| 87 |
.B ENOSPC |
| 88 |
conditions and will return these to the caller. |
| 89 |
.P |
| 90 |
Useful combinations of the |
| 91 |
.I flags |
| 92 |
bits are: |
| 93 |
.TP |
| 94 |
.B SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
| 95 |
Ensures that all pages |
| 96 |
in the specified range which were dirty when |
| 97 |
.BR sync_file_range () |
| 98 |
was called are placed |
| 99 |
under write-out. |
| 100 |
This is a start-write-for-data-integrity operation. |
| 101 |
.TP |
| 102 |
.B SYNC_FILE_RANGE_WRITE |
| 103 |
Start write-out of all dirty pages in the specified range which |
| 104 |
are not presently under write-out. |
| 105 |
This is an asynchronous flush-to-disk |
| 106 |
operation. |
| 107 |
This is not suitable for data integrity operations. |
| 108 |
.TP |
| 109 |
.BR SYNC_FILE_RANGE_WAIT_BEFORE " (or " SYNC_FILE_RANGE_WAIT_AFTER ) |
| 110 |
Wait for |
| 111 |
completion of write-out of all pages in the specified range. |
| 112 |
This can be used after an earlier |
| 113 |
.B SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
| 114 |
operation to wait for completion of that operation, and obtain its result. |
| 115 |
.TP |
| 116 |
.B SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | \ |
| 117 |
SYNC_FILE_RANGE_WAIT_AFTER |
| 118 |
This is a write-for-data-integrity operation |
| 119 |
that will ensure that all pages in the specified range which were dirty when |
| 120 |
.BR sync_file_range () |
| 121 |
was called are committed to disk. |
| 122 |
.SH RETURN VALUE |
| 123 |
On success, |
| 124 |
.BR sync_file_range () |
| 125 |
returns 0; |
| 126 |
on failure \-1 is returned and |
| 127 |
.I errno |
| 128 |
is set to indicate the error. |
| 129 |
.SH ERRORS |
| 130 |
.TP |
| 131 |
.B EBADF |
| 132 |
.I fd |
| 133 |
is not a valid file descriptor. |
| 134 |
.TP |
| 135 |
.B EINVAL |
| 136 |
.I flags |
| 137 |
specifies an invalid bit; |
| 138 |
or |
| 139 |
.I offset |
| 140 |
or |
| 141 |
.I nbytes |
| 142 |
is invalid. |
| 143 |
.TP |
| 144 |
.B EIO |
| 145 |
I/O error. |
| 146 |
.TP |
| 147 |
.B ENOMEM |
| 148 |
Out of memory. |
| 149 |
.TP |
| 150 |
.B ENOSPC |
| 151 |
Out of disk space. |
| 152 |
.TP |
| 153 |
.B ESPIPE |
| 154 |
.I fd |
| 155 |
refers to something other than a regular file, a block device, or |
| 156 |
a directory. |
| 157 |
.SH VERSIONS |
| 158 |
.SS sync_file_range2() |
| 159 |
Some architectures (e.g., PowerPC, ARM) |
| 160 |
need 64-bit arguments to be aligned in a suitable pair of registers. |
| 161 |
.\" See kernel commit edd5cd4a9424f22b0fa08bef5e299d41befd5622 |
| 162 |
On such architectures, the call signature of |
| 163 |
.BR sync_file_range () |
| 164 |
shown in the SYNOPSIS would force |
| 165 |
a register to be wasted as padding between the |
| 166 |
.I fd |
| 167 |
and |
| 168 |
.I offset |
| 169 |
arguments. |
| 170 |
(See |
| 171 |
.BR syscall (2) |
| 172 |
for details.) |
| 173 |
Therefore, these architectures define a different |
| 174 |
system call that orders the arguments suitably: |
| 175 |
.P |
| 176 |
.in +4n |
| 177 |
.EX |
| 178 |
.BI "int sync_file_range2(int " fd ", unsigned int " flags , |
| 179 |
.BI " off_t " offset ", off_t " nbytes ); |
| 180 |
.EE |
| 181 |
.in |
| 182 |
.P |
| 183 |
The behavior of this system call is otherwise exactly the same as |
| 184 |
.BR sync_file_range (). |
| 185 |
.SH STANDARDS |
| 186 |
Linux. |
| 187 |
.SH HISTORY |
| 188 |
Linux 2.6.17. |
| 189 |
.SS sync_file_range2() |
| 190 |
A system call with this signature first appeared on the ARM architecture |
| 191 |
in Linux 2.6.20, with the name |
| 192 |
.BR arm_sync_file_range (). |
| 193 |
It was renamed in Linux 2.6.22, |
| 194 |
when the analogous system call was added for PowerPC. |
| 195 |
On architectures where glibc support is provided, |
| 196 |
glibc transparently wraps |
| 197 |
.BR sync_file_range2 () |
| 198 |
under the name |
| 199 |
.BR sync_file_range (). |
| 200 |
.SH NOTES |
| 201 |
.B _FILE_OFFSET_BITS |
| 202 |
should be defined to be 64 in code that takes the address of |
| 203 |
.BR sync_file_range , |
| 204 |
if the code is intended to be portable |
| 205 |
to traditional 32-bit x86 and ARM platforms where |
| 206 |
.BR off_t 's |
| 207 |
width defaults to 32 bits. |
| 208 |
.SH SEE ALSO |
| 209 |
.BR fdatasync (2), |
| 210 |
.BR fsync (2), |
| 211 |
.BR msync (2), |
| 212 |
.BR sync (2) |