FSYNC

Section: Linux Programmer's Manual (2)
Updated: 2020-06-09
Index JM Home Page
 

名前

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

書式

#include <unistd.h>

int fsync(int fd);

int fdatasync(int fd);

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

fsync():
    2.16 以降:
        No feature test macros need be defined
    glibc 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() の狙いは、全てのメタデータをディスクと同期する必要のない アプリケーションに対して、ディスクアクセスを減らすことである。  

返り値

成功した場合、これらのシステムコールはゼロを返す。 エラーの場合、-1 が返され、 errno が適切に設定される。  

エラー

EBADF
fd が有効なオープンされたディスクリプターでない。
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.
 

準拠


 POSIX.1-2001, POSIX.1-2008, 4.3BSD.

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

注意

(Linux はそうではないが) いくつかの UNIX システムでは fd書き込み可能なファイルディスクリプターでなければならない。

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

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

関連項目


 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)  

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

関連キーワード

file, fsync, fdatasync, FSYNC, data, ディスク, SOURCE, メタデータ, 参照, int

Linux マニュアル 一覧

[man1] [man2] [man3] [man4] [man5] [man6] [man7] [man8]
[a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y] [z]

 

Index

名前
書式
説明
返り値
エラー
準拠
注意
関連項目
この文書について

This document was created by man2html, using the manual pages.
Time: 12:08:48 GMT, June 11, 2022