fsync

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

fsync - メモリー上にあるファイルの内容をストレージデバイス上のものと同期させる  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>

int fsync(int fd);

int fdatasync(int fd);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

fsync():
    glibc 2.16 and later:
        No feature test macros need be defined
    glibc up to and including 2.15:
        _BSD_SOURCE || _XOPEN_SOURCE
            || /* Since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L

fdatasync():

    _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
 

説明

fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) so that all changed information can be retrieved even if the system crashes or is rebooted. This includes writing through or flushing a disk cache if present. The call blocks until the device reports that the transfer has completed.

As well as flushing the file data, fsync() also flushes the metadata information associated with the file (see inode(7)).

fsync() の呼び出しは、ファイルが存在しているディレクトリのエントリーがディスクへ 書き込まれたことを保証するわけではない。 保証するためには明示的にそのディレクトリのファイルディスクリプターに対しても fsync() する必要がある。

fdatasync() は fsync() と同様であるが、メタデータの扱いが異なる。 fdatasync() は、それ以降のデータ読み込みを正しく扱うためにそのメタデータが必要に ならない限り、変更されたメタデータをフラッシュしない。 例えば、 st_atime や st_mtime (それぞれ最終アクセス時刻、最終修正時刻; inode(7) 参照) の変更はフラッシュを必要としない。 なぜならこれらはそれ以降のデータ読み込みを正しく扱うために 必要ではないからである。 一方、ファイルサイズ (ftruncate(2) では st_size) の変更はメタデータのフラッシュが必要である。

fdatasync() の狙いは、全てのメタデータをディスクと同期する必要のない アプリケーションに対して、ディスクアクセスを減らすことである。  

返り値

On success, these system calls return zero. On error, -1 is returned, and errno is set to indicate the error.  

エラー

EBADF
fd が有効なオープンされたディスクリプターでない。
EINTR
The function was interrupted by a signal; see signal(7).
EIO
An error occurred during synchronization. This error may relate to data written to some other file descriptor on the same file. Since Linux 4.13, errors from write-back will be reported to all file descriptors that might have written the data which triggered the error. Some filesystems (e.g., NFS) keep close track of which data came through which file descriptor, and give more precise reporting. Other filesystems (e.g., most local filesystems) will report errors to all file descriptors that were open on the file when the error was recorded.
ENOSPC
Disk space was exhausted while synchronizing.
EROFS
EINVAL
fd が同期操作をサポートしてない特殊なファイル (パイプ、FIFO、ソケットなど) を参照している。
ENOSPC
EDQUOT
fd is bound to a file on NFS or another filesystem which does not allocate space at the time of a write(2) system call, and some previous write failed due to insufficient storage space.
 

バージョン

fdatasync() が利用可能な POSIX システムでは、 _POSIX_SYNCHRONIZED_IO が <unistd.h> で 0 より大きな値に定義される (sysconf(3) 参照)。  

標準規格

POSIX.1-2024.  

歴史

fsync()
4.2BSD, SUSv1, POSIX.1-1996.
fdatasync()
POSIX.1-1996.

Linux 2.2 以前では、 fdatasync() は fsync() と等価であり、性能面でのメリットはない。

古いカーネルやあまり使われていないファイルシステムの fsync() の実装では、ディスクキャッシュをフラッシュする方法が分からない場合がある。そのような場合には、安全に操作が行われることを保証するため、hdparm(8) や sdparm(8) を使ってディスクキャッシュを無効にする必要がある。

Under AT&T UNIX System V Release 4 fd needs to be opened for writing. This is by itself incompatible with the original BSD interface and forbidden by POSIX, but nevertheless survives in HP-UX and AIX.  

関連項目


 sync(1), bdflush(2), open(2), posix_fadvise(2), pwritev(2), sync(2), sync_file_range(2), fflush(3), fileno(3), hdparm(8), mount(8)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
バージョン
標準規格
歴史
関連項目

This document was created by man2html, using the manual pages.