162306a36Sopenharmony_ci================ 262306a36Sopenharmony_ciRAID 4/5/6 cache 362306a36Sopenharmony_ci================ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciRaid 4/5/6 could include an extra disk for data cache besides normal RAID 662306a36Sopenharmony_cidisks. The role of RAID disks isn't changed with the cache disk. The cache disk 762306a36Sopenharmony_cicaches data to the RAID disks. The cache can be in write-through (supported 862306a36Sopenharmony_cisince 4.4) or write-back mode (supported since 4.10). mdadm (supported since 962306a36Sopenharmony_ci3.4) has a new option '--write-journal' to create array with cache. Please 1062306a36Sopenharmony_cirefer to mdadm manual for details. By default (RAID array starts), the cache is 1162306a36Sopenharmony_ciin write-through mode. A user can switch it to write-back mode by:: 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci echo "write-back" > /sys/block/md0/md/journal_mode 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciAnd switch it back to write-through mode by:: 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci echo "write-through" > /sys/block/md0/md/journal_mode 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciIn both modes, all writes to the array will hit cache disk first. This means 2062306a36Sopenharmony_cithe cache disk must be fast and sustainable. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciwrite-through mode 2362306a36Sopenharmony_ci================== 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciThis mode mainly fixes the 'write hole' issue. For RAID 4/5/6 array, an unclean 2662306a36Sopenharmony_cishutdown can cause data in some stripes to not be in consistent state, eg, data 2762306a36Sopenharmony_ciand parity don't match. The reason is that a stripe write involves several RAID 2862306a36Sopenharmony_cidisks and it's possible the writes don't hit all RAID disks yet before the 2962306a36Sopenharmony_ciunclean shutdown. We call an array degraded if it has inconsistent data. MD 3062306a36Sopenharmony_citries to resync the array to bring it back to normal state. But before the 3162306a36Sopenharmony_ciresync completes, any system crash will expose the chance of real data 3262306a36Sopenharmony_cicorruption in the RAID array. This problem is called 'write hole'. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciThe write-through cache will cache all data on cache disk first. After the data 3562306a36Sopenharmony_ciis safe on the cache disk, the data will be flushed onto RAID disks. The 3662306a36Sopenharmony_citwo-step write will guarantee MD can recover correct data after unclean 3762306a36Sopenharmony_cishutdown even the array is degraded. Thus the cache can close the 'write hole'. 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ciIn write-through mode, MD reports IO completion to upper layer (usually 4062306a36Sopenharmony_cifilesystems) after the data is safe on RAID disks, so cache disk failure 4162306a36Sopenharmony_cidoesn't cause data loss. Of course cache disk failure means the array is 4262306a36Sopenharmony_ciexposed to 'write hole' again. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciIn write-through mode, the cache disk isn't required to be big. Several 4562306a36Sopenharmony_cihundreds megabytes are enough. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciwrite-back mode 4862306a36Sopenharmony_ci=============== 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ciwrite-back mode fixes the 'write hole' issue too, since all write data is 5162306a36Sopenharmony_cicached on cache disk. But the main goal of 'write-back' cache is to speed up 5262306a36Sopenharmony_ciwrite. If a write crosses all RAID disks of a stripe, we call it full-stripe 5362306a36Sopenharmony_ciwrite. For non-full-stripe writes, MD must read old data before the new parity 5462306a36Sopenharmony_cican be calculated. These synchronous reads hurt write throughput. Some writes 5562306a36Sopenharmony_ciwhich are sequential but not dispatched in the same time will suffer from this 5662306a36Sopenharmony_cioverhead too. Write-back cache will aggregate the data and flush the data to 5762306a36Sopenharmony_ciRAID disks only after the data becomes a full stripe write. This will 5862306a36Sopenharmony_cicompletely avoid the overhead, so it's very helpful for some workloads. A 5962306a36Sopenharmony_citypical workload which does sequential write followed by fsync is an example. 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciIn write-back mode, MD reports IO completion to upper layer (usually 6262306a36Sopenharmony_cifilesystems) right after the data hits cache disk. The data is flushed to raid 6362306a36Sopenharmony_cidisks later after specific conditions met. So cache disk failure will cause 6462306a36Sopenharmony_cidata loss. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciIn write-back mode, MD also caches data in memory. The memory cache includes 6762306a36Sopenharmony_cithe same data stored on cache disk, so a power loss doesn't cause data loss. 6862306a36Sopenharmony_ciThe memory cache size has performance impact for the array. It's recommended 6962306a36Sopenharmony_cithe size is big. A user can configure the size by:: 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci echo "2048" > /sys/block/md0/md/stripe_cache_size 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciToo small cache disk will make the write aggregation less efficient in this 7462306a36Sopenharmony_cimode depending on the workloads. It's recommended to use a cache disk with at 7562306a36Sopenharmony_cileast several gigabytes size in write-back mode. 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ciThe implementation 7862306a36Sopenharmony_ci================== 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ciThe write-through and write-back cache use the same disk format. The cache disk 8162306a36Sopenharmony_ciis organized as a simple write log. The log consists of 'meta data' and 'data' 8262306a36Sopenharmony_cipairs. The meta data describes the data. It also includes checksum and sequence 8362306a36Sopenharmony_ciID for recovery identification. Data can be IO data and parity data. Data is 8462306a36Sopenharmony_cichecksummed too. The checksum is stored in the meta data ahead of the data. The 8562306a36Sopenharmony_cichecksum is an optimization because MD can write meta and data freely without 8662306a36Sopenharmony_ciworry about the order. MD superblock has a field pointed to the valid meta data 8762306a36Sopenharmony_ciof log head. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ciThe log implementation is pretty straightforward. The difficult part is the 9062306a36Sopenharmony_ciorder in which MD writes data to cache disk and RAID disks. Specifically, in 9162306a36Sopenharmony_ciwrite-through mode, MD calculates parity for IO data, writes both IO data and 9262306a36Sopenharmony_ciparity to the log, writes the data and parity to RAID disks after the data and 9362306a36Sopenharmony_ciparity is settled down in log and finally the IO is finished. Read just reads 9462306a36Sopenharmony_cifrom raid disks as usual. 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciIn write-back mode, MD writes IO data to the log and reports IO completion. The 9762306a36Sopenharmony_cidata is also fully cached in memory at that time, which means read must query 9862306a36Sopenharmony_cimemory cache. If some conditions are met, MD will flush the data to RAID disks. 9962306a36Sopenharmony_ciMD will calculate parity for the data and write parity into the log. After this 10062306a36Sopenharmony_ciis finished, MD will write both data and parity into RAID disks, then MD can 10162306a36Sopenharmony_cirelease the memory cache. The flush conditions could be stripe becomes a full 10262306a36Sopenharmony_cistripe write, free cache disk space is low or free in-kernel memory cache space 10362306a36Sopenharmony_ciis low. 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciAfter an unclean shutdown, MD does recovery. MD reads all meta data and data 10662306a36Sopenharmony_cifrom the log. The sequence ID and checksum will help us detect corrupted meta 10762306a36Sopenharmony_cidata and data. If MD finds a stripe with data and valid parities (1 parity for 10862306a36Sopenharmony_ciraid4/5 and 2 for raid6), MD will write the data and parities to RAID disks. If 10962306a36Sopenharmony_ciparities are incompleted, they are discarded. If part of data is corrupted, 11062306a36Sopenharmony_cithey are discarded too. MD then loads valid data and writes them to RAID disks 11162306a36Sopenharmony_ciin normal way. 112