Ext4文件系統日誌模式——journal、ordered、writeback

1、關於文件系統日誌

其功能主要是爲了提高文件系統的一致性和性能,

所謂一致性,就是通過文件系統記錄用戶寫操作,當系統出現崩潰或者掉電等異常情況時,系統重啓時能通過重放文件系統日誌來保證文件系統中元數據和文件數據一致。

至於性能,可以通過將文件系統日誌放到快速設備中存放,比如ssd,這樣寫操作提交到文件系統日誌後就可以返回,提高寫速度。而且,日誌系統也可以作爲寫操作的緩衝區,將寫操作合併達到優化效果。

2、三種日誌模式

我們先看下內核文檔裏對這三種日誌模式的說明,

There are 3 different data modes:

  • journal mode
    data=journal mode provides full data and metadata journaling. All new data is written to the journal first, and then to its final location. In the event of a crash, the journal can be replayed, bringing both data and metadata into a consistent state. This mode is the slowest except when data needs to be read from and written to disk at the same time where it outperforms all others modes. Enabling this mode will disable delayed allocation and O_DIRECT support.

  • ordered mode
    In data=ordered mode, ext4 only officially journals metadata, but it logically groups metadata information related to data changes with the data blocks into a single unit called a transaction. When it’s time to write the new metadata out to disk, the associated data blocks are written first. In general, this mode performs slightly slower than writeback but significantly faster than journal mode.

  • writeback mode
    In data=writeback mode, ext4 does not journal data at all. This mode provides a similar level of journaling as that of XFS, JFS, and ReiserFS in its default mode - metadata journaling. A crash+recovery can cause incorrect data to appear in files which were written shortly before the crash. This mode will typically provide the best ext4 performance.

  • journal

data=journal模式可靠性最高,提供了完全的數據塊和元數據塊的日誌,所有的數據都會被先寫入到日誌裏,然後再寫入磁盤上。在文件系統崩潰的時候,可以通過日誌重放,把數據和元數據恢復到一致性的狀態。但同時,journal模式性能是三種模式中最差的,因爲所有的數據都需要日誌來記錄。並且該模式不支持delayed allocation(延遲分配)以及O_DIRECT(直接IO)。

  • ordered(*)

data=ordered模式是ext4文件系統默認日誌模式,在該模式下,文件系統只提供元數據的日誌,但它邏輯上將與數據更改相關的元數據信息與數據塊分組到一個稱爲事務的單元中。當需要把元數據寫入到磁盤上的時候,與元數據關聯的數據塊會首先寫入。也就是數據先落盤,再將元數據的日誌刷到磁盤。 在機器crash時,未完成的寫操作對應的元數據仍然保存在文件系統日誌中,因此文件系統可以通過回滾日誌將未完成的寫操作清除。所以,在ordered模式下,crash可能會導致在crash時操作的文件損壞,但對於文件系統本身以及其他未操作的文件,是可以確保安全的。一般情況下,ordered模式的性能會略遜色於writeback但是比journal模式要快的多。

  • writeback

在data=writeback模式下,當元數據提交到日誌後,data可以直接被提交到磁盤。即會做元數據日誌,數據不做日誌,並且不保證數據比元數據先落盤。metadata journal是串行操作,因此採用writeback模式就不會出現由於其他進程寫journal,阻塞另一個進程的情況,因此IOPS也能得到提升。writeback是ext4提供的性能最好的模式。不過,儘管writeback模式也能保證文件系統自身的安全性,但是在系統crash時文件數據也更容易丟失和損壞。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章