The sync( ), fsync( ), and fdatasync( ) System Calls

In this section, we examine briefly the three system calls available to user applications to flush dirty buffers to disk:

sync( )

Allows a process to flush all dirty buffers to disk

fsync( )

Allows a process to flush all blocks that belong to a specific open file to disk

fdatasync( )

Very similar to fsync( ), but doesn’t flush the inode block of the file

The sync ( ) System Call

The service routine sys_sync( ) of the sync( ) system call invokes a series of auxiliary functions:

    wakeup_bdflush(0);
    sync_inodes(0);
    sync_supers( );
    sync_filesystems(0);
    sync_filesystems(1);
    sync_inodes(1);

As described in the previous section, wakeup_bdflush( ) starts a pdflush kernel thread, which flushes to disk all dirty pages contained in the page cache.

The sync_inodes( ) function scans the list of superblocks looking for dirty inodes to be flushed; it acts on a wait parameter that specifies whether it must wait until flushing has been performed or not. The function scans the superblocks of all currently mounted filesystems; for each superblock containing dirty inodes, sync_inodes( ) first invokes sync_sb_inodes( ) to flush the corresponding dirty pages (we described this function earlier in the section "Looking for Dirty Pages To Be Flushed“), then invokes sync_blockdev( ) to explicitly flush the dirty buffer pages owned by the block device that includes the superblock. This is done because the write_inode superblock method of many disk-based filesystems ...

Get Understanding the Linux Kernel, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.