static void integrity_metadata()

in dm-integrity.c [1700:1852]


static void integrity_metadata(struct work_struct *w)
{
	struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
	struct dm_integrity_c *ic = dio->ic;

	int r;

	if (ic->internal_hash) {
		struct bvec_iter iter;
		struct bio_vec bv;
		unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
		struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
		char *checksums;
		unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
		char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
		sector_t sector;
		unsigned sectors_to_process;

		if (unlikely(ic->mode == 'R'))
			goto skip_io;

		if (likely(dio->op != REQ_OP_DISCARD))
			checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
					    GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
		else
			checksums = kmalloc(PAGE_SIZE, GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
		if (!checksums) {
			checksums = checksums_onstack;
			if (WARN_ON(extra_space &&
				    digest_size > sizeof(checksums_onstack))) {
				r = -EINVAL;
				goto error;
			}
		}

		if (unlikely(dio->op == REQ_OP_DISCARD)) {
			sector_t bi_sector = dio->bio_details.bi_iter.bi_sector;
			unsigned bi_size = dio->bio_details.bi_iter.bi_size;
			unsigned max_size = likely(checksums != checksums_onstack) ? PAGE_SIZE : HASH_MAX_DIGESTSIZE;
			unsigned max_blocks = max_size / ic->tag_size;
			memset(checksums, DISCARD_FILLER, max_size);

			while (bi_size) {
				unsigned this_step_blocks = bi_size >> (SECTOR_SHIFT + ic->sb->log2_sectors_per_block);
				this_step_blocks = min(this_step_blocks, max_blocks);
				r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
							this_step_blocks * ic->tag_size, TAG_WRITE);
				if (unlikely(r)) {
					if (likely(checksums != checksums_onstack))
						kfree(checksums);
					goto error;
				}

				/*if (bi_size < this_step_blocks << (SECTOR_SHIFT + ic->sb->log2_sectors_per_block)) {
					printk("BUGG: bi_sector: %llx, bi_size: %u\n", bi_sector, bi_size);
					printk("BUGG: this_step_blocks: %u\n", this_step_blocks);
					BUG();
				}*/
				bi_size -= this_step_blocks << (SECTOR_SHIFT + ic->sb->log2_sectors_per_block);
				bi_sector += this_step_blocks << ic->sb->log2_sectors_per_block;
			}

			if (likely(checksums != checksums_onstack))
				kfree(checksums);
			goto skip_io;
		}

		sector = dio->range.logical_sector;
		sectors_to_process = dio->range.n_sectors;

		__bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
			unsigned pos;
			char *mem, *checksums_ptr;

again:
			mem = bvec_kmap_local(&bv);
			pos = 0;
			checksums_ptr = checksums;
			do {
				integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
				checksums_ptr += ic->tag_size;
				sectors_to_process -= ic->sectors_per_block;
				pos += ic->sectors_per_block << SECTOR_SHIFT;
				sector += ic->sectors_per_block;
			} while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
			kunmap_local(mem);

			r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
						checksums_ptr - checksums, dio->op == REQ_OP_READ ? TAG_CMP : TAG_WRITE);
			if (unlikely(r)) {
				if (r > 0) {
					char b[BDEVNAME_SIZE];
					sector_t s;

					s = sector - ((r + ic->tag_size - 1) / ic->tag_size);
					DMERR_LIMIT("%s: Checksum failed at sector 0x%llx",
						    bio_devname(bio, b), s);
					r = -EILSEQ;
					atomic64_inc(&ic->number_of_mismatches);
					dm_audit_log_bio(DM_MSG_PREFIX, "integrity-checksum",
							 bio, s, 0);
				}
				if (likely(checksums != checksums_onstack))
					kfree(checksums);
				goto error;
			}

			if (!sectors_to_process)
				break;

			if (unlikely(pos < bv.bv_len)) {
				bv.bv_offset += pos;
				bv.bv_len -= pos;
				goto again;
			}
		}

		if (likely(checksums != checksums_onstack))
			kfree(checksums);
	} else {
		struct bio_integrity_payload *bip = dio->bio_details.bi_integrity;

		if (bip) {
			struct bio_vec biv;
			struct bvec_iter iter;
			unsigned data_to_process = dio->range.n_sectors;
			sector_to_block(ic, data_to_process);
			data_to_process *= ic->tag_size;

			bip_for_each_vec(biv, bip, iter) {
				unsigned char *tag;
				unsigned this_len;

				BUG_ON(PageHighMem(biv.bv_page));
				tag = bvec_virt(&biv);
				this_len = min(biv.bv_len, data_to_process);
				r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
							this_len, dio->op == REQ_OP_READ ? TAG_READ : TAG_WRITE);
				if (unlikely(r))
					goto error;
				data_to_process -= this_len;
				if (!data_to_process)
					break;
			}
		}
	}
skip_io:
	dec_in_flight(dio);
	return;
error:
	dio->bi_status = errno_to_blk_status(r);
	dec_in_flight(dio);
}