Original link:
Http://www.cnblogs.com/promise6522/archive/2012/05/27/2520028.html
The writing is very good and very detailed. Summary:
Fsync need sync file modification and metadata; Fdatasync only sync metadata when necessary to save a file sync operation.
Berkeley DB ensures that Fdatasync does not go to sync metadata by pre-setting the log file size.
Here is a simple list of the corresponding BDB code:
OS/OS_FSYNC.C:
__os_fsync ()if(Db_global (j_fsync)! =NULL) ret= Db_global (J_fsync) (fhp->FD); User-defined sync functionElse {#ifDefined (F_fullfsync)//MAC OS XRetry_chk ((Fcntl (FHP->FD, F_fullfsync,0) ), ret); if(ret = =enotsup) Retry_chk ((Fsync (FHP-FD)), ret);#elifDefined (HAVE_QNX)//QNX real-time OSret=__qnx_fsync (FHP);#elifDefined (Have_fdatasync)//priority use FdatasyncRetry_chk ((Fdatasync (FHP-FD)), ret);#elseRetry_chk ((Fsync (FHP-FD)), ret);#endif }
LOG/LOG_PUT.C:
__log_write () #ifdef Have_filesystem_notzeroif(Lp->w_off = =0&&!__os_fs_notzero ()) {#else if(Lp->w_off = =0) {//Lp->w_off write offset for the current log file#endif//Lp->w_off is 0, marking the first time this log file is written (void) __db_file_extend (env, DBLP->LFHP, lp->log_size); if( f_isset (DBLP, Dblog_zero)) (void) __db_zero_extend (env, dblp->LFHP,0, Lp->log_size/lp->buffer_size, lp->buffer_size); }
ENV/ENV_FILE.C:
__db_file_extend ()//size is a log file, the last byte of the write file is 0 ' / ' ; sizeof (BUF)) / megabyte); sizeof (BUF)) % megabyte); if 0 sizeof (buf), &NW);
[Turn] discussion on synchronous Io:fsync and Fdatasync