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 Ensur