在笔记本上用了2年的SSD,这几天突然想了个问题–挂载SSD是否该开启’nobarrier’标示?问了朋友和网友,众说纷纭,查找了一些资料,做了总结–It depends!
Depending什么?要搞清楚Depending什么,先要明白何谓barrier?何谓barrier
引用mount manual:

<code>barrier=0 / barrier=1
              This  disables  /  enables the use of write barriers in the jbd code.  barrier=0 disables, barrier=1 enables (default). This also requires an IO stack which can support barriers, and if jbd gets an error on a barrier write, it will disable barriers again with a warning.  Write  barriers  enforce  proper  on-disk ordering  of  journal  commits,  making volatile disk write caches safe to use, at some performance penalty.  If your disks are battery-backed in one way or another, disabling barriers may safely improve performance.
</code>

写的不清不楚.
其实简单说barrier是保证日志文件系统的WAL(write ahead logging)一种手段–现代日志文件系统如ext4有个journal区,类似数据库领域的redo log,用于意外崩溃后的快速恢复.数据写入磁盘时,理应先写入journal区,再写入数据在磁盘的实际对应位置;磁盘厂商为了加快磁盘写入速度,磁盘 都内置cache,数据一般都先写入磁盘的cache.
cache能加快写入速度,当然是极好的东西,但磁盘一般会对cache内缓存数据排序使之最优刷新到磁盘,这样就可能导致要刷新的实际数据和 journal顺序错乱;一旦系统崩溃,下次开机时磁盘要参考journal区来恢复,但此时journal记录顺序与数据实际刷新顺序不同就会导致数据 反而”恢复”到不一致了.而barrier如其名–’栅栏’,先加一个’栅栏’,保证journal总是先写入记录,然后对应数据才刷新到磁盘,这种方 式保证了系统崩溃后磁盘恢复的正确性,但对写入性能有蛮大影响.

何时开启SSD’nobarrier’标示
前面说道barrier不开启情况下journal记录会和数据刷新顺序不一致,但journal只有恢复时才会用,所以只要disk无需恢复就行:
1.BBWC (Battery-Backed Write Cache)
通常企业级服务器都有HBA,提供单独电池,这样即使系统崩溃,磁盘一直有电供着.
2.FBWC (Flash-Backed Write Cache)
类似BBW,但不用电池而是使用flash做存储,掉电时有一个大电容供电,足够将磁盘cache中的内容写入flash.
3.其他供电方式
UPS、笔记本电池等等
4.无cache或禁用cache
这样子写就是同步的了,自然barrier也就没什么意义;Percona的《Tuning For Speed: Percona Server and Fusion-io》上说利用ioMemory的无cache特性,自然无需write barrier.

除了以上4种情况,其他情况下SSD不要开启”nobarrier”标示

参考
Barriers and journaling filesystems
Barriers, Caches, Filesystems
Is disabling barriers for ext4 safe on a laptop with battery?