1SYNC_FILE_RANGE(2)         Linux Programmer's Manual        SYNC_FILE_RANGE(2)
2
3
4

NAME

6       sync_file_range - sync a file segment with disk
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <fcntl.h>
11
12       int sync_file_range(int fd, off64_t offset, off64_t nbytes,
13                           unsigned int flags);
14

DESCRIPTION

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

RETURN VALUE

90       On  success, sync_file_range() returns 0; on failure -1 is returned and
91       errno is set to indicate the error.
92

ERRORS

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

VERSIONS

108       sync_file_range() appeared on Linux in kernel 2.6.17.
109

CONFORMING TO

111       This  system  call is Linux-specific, and should be avoided in portable
112       programs.
113

NOTES

115   sync_file_range2()
116       Some architectures (e.g., PowerPC, ARM) need  64-bit  arguments  to  be
117       aligned  in  a  suitable pair of registers.  On such architectures, the
118       call signature of sync_file_range() shown in the SYNOPSIS would force a
119       register  to  be wasted as padding between the fd and offset arguments.
120       (See syscall(2) for details.)  Therefore, these architectures define  a
121       different system call that orders the arguments suitably:
122
123           int sync_file_range2(int fd, unsigned int flags,
124                                off64_t offset, off64_t nbytes);
125
126       The  behavior  of  this  system  call  is otherwise exactly the same as
127       sync_file_range().
128
129       A system call with this signature first appeared on the  ARM  architec‐
130       ture  in  Linux  2.6.20,  with  the name arm_sync_file_range().  It was
131       renamed in Linux 2.6.22, when the analogous system call was  added  for
132       PowerPC.   On  architectures  where  glibc  support  is provided, glibc
133       transparently    wraps    sync_file_range2()     under     the     name
134       sync_file_range().
135

SEE ALSO

137       fdatasync(2), fsync(2), msync(2), sync(2)
138

COLOPHON

140       This  page  is  part of release 4.16 of the Linux man-pages project.  A
141       description of the project, information about reporting bugs,  and  the
142       latest     version     of     this    page,    can    be    found    at
143       https://www.kernel.org/doc/man-pages/.
144
145
146
147Linux                             2017-09-15                SYNC_FILE_RANGE(2)
Impressum