1sync_file_range(2) System Calls Manual sync_file_range(2)
2
3
4
6 sync_file_range - sync a file segment with disk
7
9 Standard C library (libc, -lc)
10
12 #define _GNU_SOURCE /* See feature_test_macros(7) */
13 #include <fcntl.h>
14
15 int sync_file_range(int fd, off64_t offset, off64_t nbytes,
16 unsigned int flags);
17
19 sync_file_range() permits fine control when synchronizing the open file
20 referred to by the file descriptor fd with disk.
21
22 offset is the starting byte of the file range to be synchronized.
23 nbytes specifies the length of the range to be synchronized, in bytes;
24 if nbytes is zero, then all bytes from offset through to the end of
25 file are synchronized. Synchronization is in units of the system page
26 size: offset is rounded down to a page boundary; (offset+nbytes-1) is
27 rounded up to a page boundary.
28
29 The flags bit-mask argument can include any of the following values:
30
31 SYNC_FILE_RANGE_WAIT_BEFORE
32 Wait upon write-out of all pages in the specified range that
33 have already been submitted to the device driver for write-out
34 before performing any write.
35
36 SYNC_FILE_RANGE_WRITE
37 Initiate write-out of all dirty pages in the specified range
38 which are not presently submitted write-out. Note that even
39 this may block if you attempt to write more than request queue
40 size.
41
42 SYNC_FILE_RANGE_WAIT_AFTER
43 Wait upon write-out of all pages in the range after performing
44 any write.
45
46 Specifying flags as 0 is permitted, as a no-op.
47
48 Warning
49 This system call is extremely dangerous and should not be used in por‐
50 table programs. None of these operations writes out the file's meta‐
51 data. Therefore, unless the application is strictly performing over‐
52 writes of already-instantiated disk blocks, there are no guarantees
53 that the data will be available after a crash. There is no user inter‐
54 face to know if a write is purely an overwrite. On filesystems using
55 copy-on-write semantics (e.g., btrfs) an overwrite of existing allo‐
56 cated blocks is impossible. When writing into preallocated space, many
57 filesystems also require calls into the block allocator, which this
58 system call does not sync out to disk. This system call does not flush
59 disk write caches and thus does not provide any data integrity on sys‐
60 tems with volatile disk write caches.
61
62 Some details
63 SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect
64 any I/O errors or ENOSPC conditions and will return these to the
65 caller.
66
67 Useful combinations of the flags bits are:
68
69 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE
70 Ensures that all pages in the specified range which were dirty
71 when sync_file_range() was called are placed under write-out.
72 This is a start-write-for-data-integrity operation.
73
74 SYNC_FILE_RANGE_WRITE
75 Start write-out of all dirty pages in the specified range which
76 are not presently under write-out. This is an asynchronous
77 flush-to-disk operation. This is not suitable for data integ‐
78 rity operations.
79
80 SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER)
81 Wait for completion of write-out of all pages in the specified
82 range. This can be used after an earlier
83 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE operation to
84 wait for completion of that operation, and obtain its result.
85
86 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
87 SYNC_FILE_RANGE_WAIT_AFTER
88 This is a write-for-data-integrity operation that will ensure
89 that all pages in the specified range which were dirty when
90 sync_file_range() was called are committed to disk.
91
93 On success, sync_file_range() returns 0; on failure -1 is returned and
94 errno is set to indicate the error.
95
97 EBADF fd is not a valid file descriptor.
98
99 EINVAL flags specifies an invalid bit; or offset or nbytes is invalid.
100
101 EIO I/O error.
102
103 ENOMEM Out of memory.
104
105 ENOSPC Out of disk space.
106
107 ESPIPE fd refers to something other than a regular file, a block de‐
108 vice, or a directory.
109
111 sync_file_range2()
112 Some architectures (e.g., PowerPC, ARM) need 64-bit arguments to be
113 aligned in a suitable pair of registers. On such architectures, the
114 call signature of sync_file_range() shown in the SYNOPSIS would force a
115 register to be wasted as padding between the fd and offset arguments.
116 (See syscall(2) for details.) Therefore, these architectures define a
117 different system call that orders the arguments suitably:
118
119 int sync_file_range2(int fd, unsigned int flags,
120 off64_t offset, off64_t nbytes);
121
122 The behavior of this system call is otherwise exactly the same as
123 sync_file_range().
124
126 Linux.
127
129 Linux 2.6.17.
130
131 sync_file_range2()
132 A system call with this signature first appeared on the ARM architec‐
133 ture in Linux 2.6.20, with the name arm_sync_file_range(). It was re‐
134 named in Linux 2.6.22, when the analogous system call was added for
135 PowerPC. On architectures where glibc support is provided, glibc
136 transparently wraps sync_file_range2() under the name
137 sync_file_range().
138
141 fdatasync(2), fsync(2), msync(2), sync(2)
142
143
144
145Linux man-pages 6.04 2023-03-30 sync_file_range(2)