162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This file is released under the GPL.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef DM_BIO_RECORD_H
962306a36Sopenharmony_ci#define DM_BIO_RECORD_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/bio.h>
1262306a36Sopenharmony_ci#include <linux/blk-integrity.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * There are lots of mutable fields in the bio struct that get
1662306a36Sopenharmony_ci * changed by the lower levels of the block layer.  Some targets,
1762306a36Sopenharmony_ci * such as multipath, may wish to resubmit a bio on error.  The
1862306a36Sopenharmony_ci * functions in this file help the target record and restore the
1962306a36Sopenharmony_ci * original bio state.
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct dm_bio_details {
2362306a36Sopenharmony_ci	struct block_device *bi_bdev;
2462306a36Sopenharmony_ci	int __bi_remaining;
2562306a36Sopenharmony_ci	unsigned long bi_flags;
2662306a36Sopenharmony_ci	struct bvec_iter bi_iter;
2762306a36Sopenharmony_ci	bio_end_io_t *bi_end_io;
2862306a36Sopenharmony_ci#if defined(CONFIG_BLK_DEV_INTEGRITY)
2962306a36Sopenharmony_ci	struct bio_integrity_payload *bi_integrity;
3062306a36Sopenharmony_ci#endif
3162306a36Sopenharmony_ci};
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic inline void dm_bio_record(struct dm_bio_details *bd, struct bio *bio)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	bd->bi_bdev = bio->bi_bdev;
3662306a36Sopenharmony_ci	bd->bi_flags = bio->bi_flags;
3762306a36Sopenharmony_ci	bd->bi_iter = bio->bi_iter;
3862306a36Sopenharmony_ci	bd->__bi_remaining = atomic_read(&bio->__bi_remaining);
3962306a36Sopenharmony_ci	bd->bi_end_io = bio->bi_end_io;
4062306a36Sopenharmony_ci#if defined(CONFIG_BLK_DEV_INTEGRITY)
4162306a36Sopenharmony_ci	bd->bi_integrity = bio_integrity(bio);
4262306a36Sopenharmony_ci#endif
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic inline void dm_bio_restore(struct dm_bio_details *bd, struct bio *bio)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	bio->bi_bdev = bd->bi_bdev;
4862306a36Sopenharmony_ci	bio->bi_flags = bd->bi_flags;
4962306a36Sopenharmony_ci	bio->bi_iter = bd->bi_iter;
5062306a36Sopenharmony_ci	atomic_set(&bio->__bi_remaining, bd->__bi_remaining);
5162306a36Sopenharmony_ci	bio->bi_end_io = bd->bi_end_io;
5262306a36Sopenharmony_ci#if defined(CONFIG_BLK_DEV_INTEGRITY)
5362306a36Sopenharmony_ci	bio->bi_integrity = bd->bi_integrity;
5462306a36Sopenharmony_ci#endif
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#endif
58