Chapter 17: Data Synchronization
The implementation offsyncandfdatasyncdiffers only at a single point (to be more accurate, in a single
character):
fs/sync.c
asmlinkage long sys_fsync(unsigned int fd)
{
return __do_fsync(fd, 0);
}
asmlinkage long sys_fdatasync(unsigned int fd)
{
return __do_fsync(fd, 1);
}
The code flow diagram for the common function__do_fsyncis shown in Figure 17-12.
filemap_fdatawait
Get file descriptor
__do_fsync
do_fsync
filemap_fdatawrite
file->f_op->fsync
Figure 17-12: Code flow diagram for
__do_sync.
Synchronization of a single file is relatively straightforward.fgetis used to find the appropriatefile
instance by reference to the file descriptor, and then work is delegated to three functions:
- filemap_fdatawrite(viaadetourover__filemap_fdatawriteand_filemap
fdatawrite_range) first generates awriteback_controlinstance whosenr_to_write
value (maximum number of pages to be flushed) is set to double the number of pages of the
mapping to ensure that all pages are written back. Afterward, the familiardo_writepages
method invokes the low-level write routines of the filesystem in which the file is located.
- The filesystem-dependentfsyncfunction found using thefile_operationsstructure of the
file is then invoked to write back the cached file data. This is wherefsyncandfdatasync
differ —fsynchas a parameter to specify whether metadata are also to be flushed as well as
the regular caches. The parameter is set to 0 forfsyncand to 1 forfdatasync.
- Synchronization is then concluded by invokingfilemap_fdatawaitto wait for the end of
the write operation initiated infilemap_fdatawrite. This ensures that the asynchronous
write operations appear as synchronous to the user application because the system call does
not return control to userspace until writeback of the desired data has been completed in the
view of both the block layer and the filesystem layer.