1sync_file_range(2)            System Calls Manual           sync_file_range(2)
2
3
4

NAME

6       sync_file_range - sync a file segment with disk
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #define _GNU_SOURCE         /* See feature_test_macros(7) */
13       #define _FILE_OFFSET_BITS 64
14       #include <fcntl.h>
15
16       int sync_file_range(int fd, off_t offset, off_t nbytes,
17                           unsigned int flags);
18

DESCRIPTION

20       sync_file_range() permits fine control when synchronizing the open file
21       referred to by the file descriptor fd with disk.
22
23       offset is the starting byte of  the  file  range  to  be  synchronized.
24       nbytes  specifies the length of the range to be synchronized, in bytes;
25       if nbytes is zero, then all bytes from offset through  to  the  end  of
26       file  are synchronized.  Synchronization is in units of the system page
27       size: offset is rounded down to a page boundary;  (offset+nbytes-1)  is
28       rounded up to a page boundary.
29
30       The flags bit-mask argument can include any of the following values:
31
32       SYNC_FILE_RANGE_WAIT_BEFORE
33              Wait  upon  write-out  of  all pages in the specified range that
34              have already been submitted to the device driver  for  write-out
35              before performing any write.
36
37       SYNC_FILE_RANGE_WRITE
38              Initiate  write-out  of  all  dirty pages in the specified range
39              which are not presently submitted  write-out.   Note  that  even
40              this  may  block if you attempt to write more than request queue
41              size.
42
43       SYNC_FILE_RANGE_WAIT_AFTER
44              Wait upon write-out of all pages in the range  after  performing
45              any write.
46
47       Specifying flags as 0 is permitted, as a no-op.
48
49   Warning
50       This  system call is extremely dangerous and should not be used in por‐
51       table programs.  None of these operations writes out the  file's  meta‐
52       data.   Therefore,  unless the application is strictly performing over‐
53       writes of already-instantiated disk blocks,  there  are  no  guarantees
54       that the data will be available after a crash.  There is no user inter‐
55       face to know if a write is purely an overwrite.  On  filesystems  using
56       copy-on-write  semantics  (e.g.,  btrfs) an overwrite of existing allo‐
57       cated blocks is impossible.  When writing into preallocated space, many
58       filesystems  also  require  calls  into the block allocator, which this
59       system call does not sync out to disk.  This system call does not flush
60       disk  write caches and thus does not provide any data integrity on sys‐
61       tems with volatile disk write caches.
62
63   Some details
64       SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will  detect
65       any  I/O  errors  or  ENOSPC  conditions  and  will return these to the
66       caller.
67
68       Useful combinations of the flags bits are:
69
70       SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE
71              Ensures that all pages in the specified range which  were  dirty
72              when  sync_file_range()  was  called are placed under write-out.
73              This is a start-write-for-data-integrity operation.
74
75       SYNC_FILE_RANGE_WRITE
76              Start write-out of all dirty pages in the specified range  which
77              are  not  presently  under  write-out.   This is an asynchronous
78              flush-to-disk operation.  This is not suitable for  data  integ‐
79              rity operations.
80
81       SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER)
82              Wait  for  completion of write-out of all pages in the specified
83              range.     This    can    be    used    after     an     earlier
84              SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE operation to
85              wait for completion of that operation, and obtain its result.
86
87       SYNC_FILE_RANGE_WAIT_BEFORE       |       SYNC_FILE_RANGE_WRITE       |
88       SYNC_FILE_RANGE_WAIT_AFTER
89              This  is  a  write-for-data-integrity operation that will ensure
90              that all pages in the specified  range  which  were  dirty  when
91              sync_file_range() was called are committed to disk.
92

RETURN VALUE

94       On  success, sync_file_range() returns 0; on failure -1 is returned and
95       errno is set to indicate the error.
96

ERRORS

98       EBADF  fd is not a valid file descriptor.
99
100       EINVAL flags specifies an invalid bit; or offset or nbytes is invalid.
101
102       EIO    I/O error.
103
104       ENOMEM Out of memory.
105
106       ENOSPC Out of disk space.
107
108       ESPIPE fd refers to something other than a regular file,  a  block  de‐
109              vice, or a directory.
110

VERSIONS

112   sync_file_range2()
113       Some  architectures  (e.g.,  PowerPC,  ARM) need 64-bit arguments to be
114       aligned in a suitable pair of registers.  On  such  architectures,  the
115       call signature of sync_file_range() shown in the SYNOPSIS would force a
116       register to be wasted as padding between the fd and  offset  arguments.
117       (See  syscall(2) for details.)  Therefore, these architectures define a
118       different system call that orders the arguments suitably:
119
120           int sync_file_range2(int fd, unsigned int flags,
121                                off_t offset, off_t nbytes);
122
123       The behavior of this system call  is  otherwise  exactly  the  same  as
124       sync_file_range().
125

STANDARDS

127       Linux.
128

HISTORY

130       Linux 2.6.17.
131
132   sync_file_range2()
133       A  system  call with this signature first appeared on the ARM architec‐
134       ture in Linux 2.6.20, with the name arm_sync_file_range().  It was  re‐
135       named  in  Linux  2.6.22,  when the analogous system call was added for
136       PowerPC.  On architectures  where  glibc  support  is  provided,  glibc
137       transparently     wraps     sync_file_range2()     under    the    name
138       sync_file_range().
139

NOTES

141       _FILE_OFFSET_BITS should be defined to be 64 in code that takes the ad‐
142       dress  of  sync_file_range,  if  the code is intended to be portable to
143       traditional 32-bit x86 and ARM platforms where off_t's  width  defaults
144       to 32 bits.
145

SEE ALSO

147       fdatasync(2), fsync(2), msync(2), sync(2)
148
149
150
151Linux man-pages 6.05              2023-07-15                sync_file_range(2)
Impressum