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时文件数据也更容易丢失和损坏。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值