// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2007, 2008, 2009 Oracle Corporation * Written by: Martin K. Petersen <martin.petersen@oracle.com> * * Automatically generate and verify integrity data on PI capable devices if the * bio submitter didn't provide PI itself. This ensures that kernel verifies * data integrity even if the file system (or other user of the block device) is * not aware of PI.
*/ #include <linux/blk-integrity.h> #include <linux/t10-pi.h> #include <linux/workqueue.h> #include"blk.h"
staticbool bi_offload_capable(struct blk_integrity *bi)
{ switch (bi->csum_type) { case BLK_INTEGRITY_CSUM_CRC64: return bi->metadata_size == sizeof(struct crc64_pi_tuple); case BLK_INTEGRITY_CSUM_CRC: case BLK_INTEGRITY_CSUM_IP: return bi->metadata_size == sizeof(struct t10_pi_tuple); default:
pr_warn_once("%s: unknown integrity checksum type:%d\n",
__func__, bi->csum_type);
fallthrough; case BLK_INTEGRITY_CSUM_NONE: returnfalse;
}
}
/** * __bio_integrity_endio - Integrity I/O completion function * @bio: Protected bio * * Normally I/O completion is done in interrupt context. However, verifying I/O * integrity is a time-consuming task which must be run in process context. * * This function postpones completion accordingly.
*/ bool __bio_integrity_endio(struct bio *bio)
{ struct bio_integrity_payload *bip = bio_integrity(bio); struct bio_integrity_data *bid =
container_of(bip, struct bio_integrity_data, bip);
/** * bio_integrity_prep - Prepare bio for integrity I/O * @bio: bio to prepare * * Checks if the bio already has an integrity payload attached. If it does, the * payload has been generated by another kernel subsystem, and we just pass it * through. * Otherwise allocates integrity payload and for writes the integrity metadata * will be generated. For reads, the completion handler will verify the * metadata.
*/ bool bio_integrity_prep(struct bio *bio)
{ struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk); struct bio_integrity_data *bid; bool set_flags = true;
gfp_t gfp = GFP_NOIO; unsignedint len; void *buf;
if (!bi) returntrue;
if (!bio_sectors(bio)) returntrue;
/* Already protected? */ if (bio_integrity(bio)) returntrue;
switch (bio_op(bio)) { case REQ_OP_READ: if (bi->flags & BLK_INTEGRITY_NOVERIFY) { if (bi_offload_capable(bi)) returntrue;
set_flags = false;
} break; case REQ_OP_WRITE: /* * Zero the memory allocated to not leak uninitialized kernel * memory to disk for non-integrity metadata where nothing else * initializes the memory.
*/ if (bi->flags & BLK_INTEGRITY_NOGENERATE) { if (bi_offload_capable(bi)) returntrue;
set_flags = false;
gfp |= __GFP_ZERO;
} elseif (bi->csum_type == BLK_INTEGRITY_CSUM_NONE)
gfp |= __GFP_ZERO; break; default: returntrue;
}
if (WARN_ON_ONCE(bio_has_crypt_ctx(bio))) returntrue;
/* Allocate kernel buffer for protection data */
len = bio_integrity_bytes(bi, bio_sectors(bio));
buf = kmalloc(len, gfp); if (!buf) goto err_end_io;
bid = mempool_alloc(&bid_pool, GFP_NOIO); if (!bid) goto err_free_buf;
bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
if (set_flags) { if (bi->csum_type == BLK_INTEGRITY_CSUM_IP)
bid->bip.bip_flags |= BIP_IP_CHECKSUM; if (bi->csum_type)
bid->bip.bip_flags |= BIP_CHECK_GUARD; if (bi->flags & BLK_INTEGRITY_REF_TAG)
bid->bip.bip_flags |= BIP_CHECK_REFTAG;
}
if (bio_integrity_add_page(bio, virt_to_page(buf), len,
offset_in_page(buf)) < len) goto err_end_io;
/* Auto-generate integrity metadata if this is a write */ if (bio_data_dir(bio) == WRITE && bip_should_check(&bid->bip))
blk_integrity_generate(bio); else
bid->saved_bio_iter = bio->bi_iter; returntrue;
if (mempool_init_slab_pool(&bid_pool, BIO_POOL_SIZE, bid_slab))
panic("bio: can't create integrity pool\n");
/* * kintegrityd won't block much but may burn a lot of CPU cycles. * Make it highpri CPU intensive wq with max concurrency of 1.
*/
kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1); if (!kintegrityd_wq)
panic("Failed to create kintegrityd\n"); return 0;
}
subsys_initcall(blk_integrity_auto_init);
Messung V0.5
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.