On Wed, Feb 26, 2020 at 04:37:17PM +0800, Bob Liu wrote: > Introduce two funcs handle protect information passthrough from > user space. > > iter_slice_protect_info() will slice the last segment as protect > information. > > bio_integrity_prep_from_iovec() attach the protect information to > a bio. > > Signed-off-by: Bob Liu <bob.liu@xxxxxxxxxx> > --- > block/bio-integrity.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ > include/linux/bio.h | 14 ++++++++++ > 2 files changed, 91 insertions(+) > > diff --git a/block/bio-integrity.c b/block/bio-integrity.c > index 575df98..0b22c5d 100644 > --- a/block/bio-integrity.c > +++ b/block/bio-integrity.c > @@ -12,6 +12,7 @@ > #include <linux/bio.h> > #include <linux/workqueue.h> > #include <linux/slab.h> > +#include <linux/uio.h> > #include "blk.h" > > #define BIP_INLINE_VECS 4 > @@ -305,6 +306,53 @@ bool bio_integrity_prep(struct bio *bio) > } > EXPORT_SYMBOL(bio_integrity_prep); > > +int bio_integrity_prep_from_iovec(struct bio *bio, struct iovec *pi_iov) > +{ > + struct blk_integrity *bi = blk_get_integrity(bio->bi_disk); > + struct bio_integrity_payload *bip; > + struct page *user_pi_page; > + int nr_vec_page = 0; > + int ret = 0, interval = 0; > + > + if (!pi_iov || !pi_iov->iov_base) > + return 1; > + > + nr_vec_page = (pi_iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; > + if (nr_vec_page > 1) { > + printk("Now only support 1 page containing integrity " > + "metadata, while requires %d pages.\n", nr_vec_page); > + return 1; I would've thought this would be -EINVAL or something given the -ENOMEM below...? > + } > + > + interval = bio_integrity_intervals(bi, bio_sectors(bio)); > + if ((interval * bi->tuple_size) != pi_iov->iov_len) > + return 1; > + > + bip = bio_integrity_alloc(bio, GFP_NOIO, nr_vec_page); > + if (IS_ERR(bip)) > + return PTR_ERR(bip); > + > + bip->bip_iter.bi_size = pi_iov->iov_len; > + bip->bio_iter = bio->bi_iter; > + bip_set_seed(bip, bio->bi_iter.bi_sector); > + > + if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM) > + bip->bip_flags |= BIP_IP_CHECKSUM; > + > + ret = get_user_pages_fast((unsigned long)(pi_iov->iov_base), nr_vec_page, > + op_is_write(bio_op(bio)) ? FOLL_WRITE : 0, > + &user_pi_page); > + if (unlikely(ret < 0)) > + return 1; > + > + ret = bio_integrity_add_page(bio, user_pi_page, pi_iov->iov_len, 0); > + if (unlikely(ret != pi_iov->iov_len)) > + return -ENOMEM; > + > + return 0; > +} > +EXPORT_SYMBOL(bio_integrity_prep_from_iovec); > + > /** > * bio_integrity_verify_fn - Integrity I/O completion worker > * @work: Work struct stored in bio to be verified > @@ -378,6 +426,35 @@ void bio_integrity_advance(struct bio *bio, unsigned int bytes_done) > } > > /** > + * iter_slice_protect_info > + * > + * Description: slice protection information from iter. > + * The last iovec contains protection information pass from user space. What do the return values here mean? Also kinda wondering about the slice & dice of the iovec here, but <shrug> I guess this is RFC. :) --D > + */ > +int iter_slice_protect_info(struct iov_iter *iter, int nr_pages, > + struct iovec **pi_iov) > +{ > + size_t len = 0; > + > + /* TBD: now only support one bio. */ > + if (!iter_is_iovec(iter) || nr_pages >= BIO_MAX_PAGES - 1) > + return 1; > + > + /* Last iovec contains protection information. */ > + iter->nr_segs--; > + *pi_iov = (struct iovec *)(iter->iov + iter->nr_segs); > + > + len = (*pi_iov)->iov_len; > + if (len > 0 && len < iter->count) { > + iter->count -= len; > + return 0; > + } > + > + return 1; > +} > +EXPORT_SYMBOL(iter_slice_protect_info); > + > +/** > * bio_integrity_trim - Trim integrity vector > * @bio: bio whose integrity vector to update > * > diff --git a/include/linux/bio.h b/include/linux/bio.h > index 3cdb84c..6172b13 100644 > --- a/include/linux/bio.h > +++ b/include/linux/bio.h > @@ -749,6 +749,8 @@ static inline bool bioset_initialized(struct bio_set *bs) > extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); > extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); > extern bool bio_integrity_prep(struct bio *); > +extern int bio_integrity_prep_from_iovec(struct bio *bio, struct iovec *pi_iov); > +extern int iter_slice_protect_info(struct iov_iter *iter, int nr_pages, struct iovec **pi_iov); > extern void bio_integrity_advance(struct bio *, unsigned int); > extern void bio_integrity_trim(struct bio *); > extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t); > @@ -778,6 +780,18 @@ static inline bool bio_integrity_prep(struct bio *bio) > return true; > } > > +static inline int bio_integrity_prep_from_iovec(struct bio *bio, > + struct iovec *pi_iov) > +{ > + return 0; > +} > + > +static inline int iter_slice_protect_info(struct iov_iter *iter, int nr_pages, > + struct iovec **pi_iov) > +{ > + return 0; > +} > + > static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src, > gfp_t gfp_mask) > { > -- > 2.9.5 >