Path: blob/master/Documentation/block/data-integrity.txt
10821 views
----------------------------------------------------------------------11. INTRODUCTION23Modern filesystems feature checksumming of data and metadata to4protect against data corruption. However, the detection of the5corruption is done at read time which could potentially be months6after the data was written. At that point the original data that the7application tried to write is most likely lost.89The solution is to ensure that the disk is actually storing what the10application meant it to. Recent additions to both the SCSI family11protocols (SBC Data Integrity Field, SCC protection proposal) as well12as SATA/T13 (External Path Protection) try to remedy this by adding13support for appending integrity metadata to an I/O. The integrity14metadata (or protection information in SCSI terminology) includes a15checksum for each sector as well as an incrementing counter that16ensures the individual sectors are written in the right order. And17for some protection schemes also that the I/O is written to the right18place on disk.1920Current storage controllers and devices implement various protective21measures, for instance checksumming and scrubbing. But these22technologies are working in their own isolated domains or at best23between adjacent nodes in the I/O path. The interesting thing about24DIF and the other integrity extensions is that the protection format25is well defined and every node in the I/O path can verify the26integrity of the I/O and reject it if corruption is detected. This27allows not only corruption prevention but also isolation of the point28of failure.2930----------------------------------------------------------------------312. THE DATA INTEGRITY EXTENSIONS3233As written, the protocol extensions only protect the path between34controller and storage device. However, many controllers actually35allow the operating system to interact with the integrity metadata36(IMD). We have been working with several FC/SAS HBA vendors to enable37the protection information to be transferred to and from their38controllers.3940The SCSI Data Integrity Field works by appending 8 bytes of protection41information to each sector. The data + integrity metadata is stored42in 520 byte sectors on disk. Data + IMD are interleaved when43transferred between the controller and target. The T13 proposal is44similar.4546Because it is highly inconvenient for operating systems to deal with47520 (and 4104) byte sectors, we approached several HBA vendors and48encouraged them to allow separation of the data and integrity metadata49scatter-gather lists.5051The controller will interleave the buffers on write and split them on52read. This means that Linux can DMA the data buffers to and from53host memory without changes to the page cache.5455Also, the 16-bit CRC checksum mandated by both the SCSI and SATA specs56is somewhat heavy to compute in software. Benchmarks found that57calculating this checksum had a significant impact on system58performance for a number of workloads. Some controllers allow a59lighter-weight checksum to be used when interfacing with the operating60system. Emulex, for instance, supports the TCP/IP checksum instead.61The IP checksum received from the OS is converted to the 16-bit CRC62when writing and vice versa. This allows the integrity metadata to be63generated by Linux or the application at very low cost (comparable to64software RAID5).6566The IP checksum is weaker than the CRC in terms of detecting bit67errors. However, the strength is really in the separation of the data68buffers and the integrity metadata. These two distinct buffers must69match up for an I/O to complete.7071The separation of the data and integrity metadata buffers as well as72the choice in checksums is referred to as the Data Integrity73Extensions. As these extensions are outside the scope of the protocol74bodies (T10, T13), Oracle and its partners are trying to standardize75them within the Storage Networking Industry Association.7677----------------------------------------------------------------------783. KERNEL CHANGES7980The data integrity framework in Linux enables protection information81to be pinned to I/Os and sent to/received from controllers that82support it.8384The advantage to the integrity extensions in SCSI and SATA is that85they enable us to protect the entire path from application to storage86device. However, at the same time this is also the biggest87disadvantage. It means that the protection information must be in a88format that can be understood by the disk.8990Generally Linux/POSIX applications are agnostic to the intricacies of91the storage devices they are accessing. The virtual filesystem switch92and the block layer make things like hardware sector size and93transport protocols completely transparent to the application.9495However, this level of detail is required when preparing the96protection information to send to a disk. Consequently, the very97concept of an end-to-end protection scheme is a layering violation.98It is completely unreasonable for an application to be aware whether99it is accessing a SCSI or SATA disk.100101The data integrity support implemented in Linux attempts to hide this102from the application. As far as the application (and to some extent103the kernel) is concerned, the integrity metadata is opaque information104that's attached to the I/O.105106The current implementation allows the block layer to automatically107generate the protection information for any I/O. Eventually the108intent is to move the integrity metadata calculation to userspace for109user data. Metadata and other I/O that originates within the kernel110will still use the automatic generation interface.111112Some storage devices allow each hardware sector to be tagged with a11316-bit value. The owner of this tag space is the owner of the block114device. I.e. the filesystem in most cases. The filesystem can use115this extra space to tag sectors as they see fit. Because the tag116space is limited, the block interface allows tagging bigger chunks by117way of interleaving. This way, 8*16 bits of information can be118attached to a typical 4KB filesystem block.119120This also means that applications such as fsck and mkfs will need121access to manipulate the tags from user space. A passthrough122interface for this is being worked on.123124125----------------------------------------------------------------------1264. BLOCK LAYER IMPLEMENTATION DETAILS1271284.1 BIO129130The data integrity patches add a new field to struct bio when131CONFIG_BLK_DEV_INTEGRITY is enabled. bio->bi_integrity is a pointer132to a struct bip which contains the bio integrity payload. Essentially133a bip is a trimmed down struct bio which holds a bio_vec containing134the integrity metadata and the required housekeeping information (bvec135pool, vector count, etc.)136137A kernel subsystem can enable data integrity protection on a bio by138calling bio_integrity_alloc(bio). This will allocate and attach the139bip to the bio.140141Individual pages containing integrity metadata can subsequently be142attached using bio_integrity_add_page().143144bio_free() will automatically free the bip.1451461474.2 BLOCK DEVICE148149Because the format of the protection data is tied to the physical150disk, each block device has been extended with a block integrity151profile (struct blk_integrity). This optional profile is registered152with the block layer using blk_integrity_register().153154The profile contains callback functions for generating and verifying155the protection data, as well as getting and setting application tags.156The profile also contains a few constants to aid in completing,157merging and splitting the integrity metadata.158159Layered block devices will need to pick a profile that's appropriate160for all subdevices. blk_integrity_compare() can help with that. DM161and MD linear, RAID0 and RAID1 are currently supported. RAID4/5/6162will require extra work due to the application tag.163164165----------------------------------------------------------------------1665.0 BLOCK LAYER INTEGRITY API1671685.1 NORMAL FILESYSTEM169170The normal filesystem is unaware that the underlying block device171is capable of sending/receiving integrity metadata. The IMD will172be automatically generated by the block layer at submit_bio() time173in case of a WRITE. A READ request will cause the I/O integrity174to be verified upon completion.175176IMD generation and verification can be toggled using the177178/sys/block/<bdev>/integrity/write_generate179180and181182/sys/block/<bdev>/integrity/read_verify183184flags.1851861875.2 INTEGRITY-AWARE FILESYSTEM188189A filesystem that is integrity-aware can prepare I/Os with IMD190attached. It can also use the application tag space if this is191supported by the block device.192193194int bdev_integrity_enabled(block_device, int rw);195196bdev_integrity_enabled() will return 1 if the block device197supports integrity metadata transfer for the data direction198specified in 'rw'.199200bdev_integrity_enabled() honors the write_generate and201read_verify flags in sysfs and will respond accordingly.202203204int bio_integrity_prep(bio);205206To generate IMD for WRITE and to set up buffers for READ, the207filesystem must call bio_integrity_prep(bio).208209Prior to calling this function, the bio data direction and start210sector must be set, and the bio should have all data pages211added. It is up to the caller to ensure that the bio does not212change while I/O is in progress.213214bio_integrity_prep() should only be called if215bio_integrity_enabled() returned 1.216217218int bio_integrity_tag_size(bio);219220If the filesystem wants to use the application tag space it will221first have to find out how much storage space is available.222Because tag space is generally limited (usually 2 bytes per223sector regardless of sector size), the integrity framework224supports interleaving the information between the sectors in an225I/O.226227Filesystems can call bio_integrity_tag_size(bio) to find out how228many bytes of storage are available for that particular bio.229230Another option is bdev_get_tag_size(block_device) which will231return the number of available bytes per hardware sector.232233234int bio_integrity_set_tag(bio, void *tag_buf, len);235236After a successful return from bio_integrity_prep(),237bio_integrity_set_tag() can be used to attach an opaque tag238buffer to a bio. Obviously this only makes sense if the I/O is239a WRITE.240241242int bio_integrity_get_tag(bio, void *tag_buf, len);243244Similarly, at READ I/O completion time the filesystem can245retrieve the tag buffer using bio_integrity_get_tag().2462472485.3 PASSING EXISTING INTEGRITY METADATA249250Filesystems that either generate their own integrity metadata or251are capable of transferring IMD from user space can use the252following calls:253254255struct bip * bio_integrity_alloc(bio, gfp_mask, nr_pages);256257Allocates the bio integrity payload and hangs it off of the bio.258nr_pages indicate how many pages of protection data need to be259stored in the integrity bio_vec list (similar to bio_alloc()).260261The integrity payload will be freed at bio_free() time.262263264int bio_integrity_add_page(bio, page, len, offset);265266Attaches a page containing integrity metadata to an existing267bio. The bio must have an existing bip,268i.e. bio_integrity_alloc() must have been called. For a WRITE,269the integrity metadata in the pages must be in a format270understood by the target device with the notable exception that271the sector numbers will be remapped as the request traverses the272I/O stack. This implies that the pages added using this call273will be modified during I/O! The first reference tag in the274integrity metadata must have a value of bip->bip_sector.275276Pages can be added using bio_integrity_add_page() as long as277there is room in the bip bio_vec array (nr_pages).278279Upon completion of a READ operation, the attached pages will280contain the integrity metadata received from the storage device.281It is up to the receiver to process them and verify data282integrity upon completion.2832842855.4 REGISTERING A BLOCK DEVICE AS CAPABLE OF EXCHANGING INTEGRITY286METADATA287288To enable integrity exchange on a block device the gendisk must be289registered as capable:290291int blk_integrity_register(gendisk, blk_integrity);292293The blk_integrity struct is a template and should contain the294following:295296static struct blk_integrity my_profile = {297.name = "STANDARDSBODY-TYPE-VARIANT-CSUM",298.generate_fn = my_generate_fn,299.verify_fn = my_verify_fn,300.get_tag_fn = my_get_tag_fn,301.set_tag_fn = my_set_tag_fn,302.tuple_size = sizeof(struct my_tuple_size),303.tag_size = <tag bytes per hw sector>,304};305306'name' is a text string which will be visible in sysfs. This is307part of the userland API so chose it carefully and never change308it. The format is standards body-type-variant.309E.g. T10-DIF-TYPE1-IP or T13-EPP-0-CRC.310311'generate_fn' generates appropriate integrity metadata (for WRITE).312313'verify_fn' verifies that the data buffer matches the integrity314metadata.315316'tuple_size' must be set to match the size of the integrity317metadata per sector. I.e. 8 for DIF and EPP.318319'tag_size' must be set to identify how many bytes of tag space320are available per hardware sector. For DIF this is either 2 or3210 depending on the value of the Control Mode Page ATO bit.322323See 6.2 for a description of get_tag_fn and set_tag_fn.324325----------------------------------------------------------------------3262007-12-24 Martin K. Petersen <[email protected]>327328329