Skip to content

Commit 3d8b5a2

Browse files
Kanchan Joshiaxboe
authored andcommitted
block: add support to pass user meta buffer
If an iocb contains metadata, extract that and prepare the bip. Based on flags specified by the user, set corresponding guard/app/ref tags to be checked in bip. Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Anuj Gupta <[email protected]> Signed-off-by: Kanchan Joshi <[email protected]> Reviewed-by: Keith Busch <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 1862350 commit 3d8b5a2

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

block/bio-integrity.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,55 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
364364
return ret;
365365
}
366366

367+
static void bio_uio_meta_to_bip(struct bio *bio, struct uio_meta *meta)
368+
{
369+
struct bio_integrity_payload *bip = bio_integrity(bio);
370+
371+
if (meta->flags & IO_INTEGRITY_CHK_GUARD)
372+
bip->bip_flags |= BIP_CHECK_GUARD;
373+
if (meta->flags & IO_INTEGRITY_CHK_APPTAG)
374+
bip->bip_flags |= BIP_CHECK_APPTAG;
375+
if (meta->flags & IO_INTEGRITY_CHK_REFTAG)
376+
bip->bip_flags |= BIP_CHECK_REFTAG;
377+
378+
bip->app_tag = meta->app_tag;
379+
}
380+
381+
int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
382+
{
383+
struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
384+
unsigned int integrity_bytes;
385+
int ret;
386+
struct iov_iter it;
387+
388+
if (!bi)
389+
return -EINVAL;
390+
/*
391+
* original meta iterator can be bigger.
392+
* process integrity info corresponding to current data buffer only.
393+
*/
394+
it = meta->iter;
395+
integrity_bytes = bio_integrity_bytes(bi, bio_sectors(bio));
396+
if (it.count < integrity_bytes)
397+
return -EINVAL;
398+
399+
/* should fit into two bytes */
400+
BUILD_BUG_ON(IO_INTEGRITY_VALID_FLAGS >= (1 << 16));
401+
402+
if (meta->flags && (meta->flags & ~IO_INTEGRITY_VALID_FLAGS))
403+
return -EINVAL;
404+
405+
it.count = integrity_bytes;
406+
ret = bio_integrity_map_user(bio, &it);
407+
if (!ret) {
408+
bio_uio_meta_to_bip(bio, meta);
409+
bip_set_seed(bio_integrity(bio), meta->seed);
410+
iov_iter_advance(&meta->iter, integrity_bytes);
411+
meta->seed += bio_integrity_intervals(bi, bio_sectors(bio));
412+
}
413+
return ret;
414+
}
415+
367416
/**
368417
* bio_integrity_prep - Prepare bio for integrity I/O
369418
* @bio: bio to prepare
@@ -564,6 +613,7 @@ int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
564613
bip->bip_vec = bip_src->bip_vec;
565614
bip->bip_iter = bip_src->bip_iter;
566615
bip->bip_flags = bip_src->bip_flags & BIP_CLONE_FLAGS;
616+
bip->app_tag = bip_src->app_tag;
567617

568618
return 0;
569619
}

block/fops.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
5454
struct bio bio;
5555
ssize_t ret;
5656

57+
WARN_ON_ONCE(iocb->ki_flags & IOCB_HAS_METADATA);
5758
if (nr_pages <= DIO_INLINE_BIO_VECS)
5859
vecs = inline_vecs;
5960
else {
@@ -124,12 +125,16 @@ static void blkdev_bio_end_io(struct bio *bio)
124125
{
125126
struct blkdev_dio *dio = bio->bi_private;
126127
bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
128+
bool is_sync = dio->flags & DIO_IS_SYNC;
127129

128130
if (bio->bi_status && !dio->bio.bi_status)
129131
dio->bio.bi_status = bio->bi_status;
130132

133+
if (!is_sync && (dio->iocb->ki_flags & IOCB_HAS_METADATA))
134+
bio_integrity_unmap_user(bio);
135+
131136
if (atomic_dec_and_test(&dio->ref)) {
132-
if (!(dio->flags & DIO_IS_SYNC)) {
137+
if (!is_sync) {
133138
struct kiocb *iocb = dio->iocb;
134139
ssize_t ret;
135140

@@ -221,14 +226,16 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
221226
* a retry of this from blocking context.
222227
*/
223228
if (unlikely(iov_iter_count(iter))) {
224-
bio_release_pages(bio, false);
225-
bio_clear_flag(bio, BIO_REFFED);
226-
bio_put(bio);
227-
blk_finish_plug(&plug);
228-
return -EAGAIN;
229+
ret = -EAGAIN;
230+
goto fail;
229231
}
230232
bio->bi_opf |= REQ_NOWAIT;
231233
}
234+
if (!is_sync && (iocb->ki_flags & IOCB_HAS_METADATA)) {
235+
ret = bio_integrity_map_iter(bio, iocb->private);
236+
if (unlikely(ret))
237+
goto fail;
238+
}
232239

233240
if (is_read) {
234241
if (dio->flags & DIO_SHOULD_DIRTY)
@@ -269,6 +276,12 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
269276

270277
bio_put(&dio->bio);
271278
return ret;
279+
fail:
280+
bio_release_pages(bio, false);
281+
bio_clear_flag(bio, BIO_REFFED);
282+
bio_put(bio);
283+
blk_finish_plug(&plug);
284+
return ret;
272285
}
273286

274287
static void blkdev_bio_end_io_async(struct bio *bio)
@@ -286,6 +299,9 @@ static void blkdev_bio_end_io_async(struct bio *bio)
286299
ret = blk_status_to_errno(bio->bi_status);
287300
}
288301

302+
if (iocb->ki_flags & IOCB_HAS_METADATA)
303+
bio_integrity_unmap_user(bio);
304+
289305
iocb->ki_complete(iocb, ret);
290306

291307
if (dio->flags & DIO_SHOULD_DIRTY) {
@@ -330,10 +346,8 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
330346
bio_iov_bvec_set(bio, iter);
331347
} else {
332348
ret = bio_iov_iter_get_pages(bio, iter);
333-
if (unlikely(ret)) {
334-
bio_put(bio);
335-
return ret;
336-
}
349+
if (unlikely(ret))
350+
goto out_bio_put;
337351
}
338352
dio->size = bio->bi_iter.bi_size;
339353

@@ -346,6 +360,13 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
346360
task_io_account_write(bio->bi_iter.bi_size);
347361
}
348362

363+
if (iocb->ki_flags & IOCB_HAS_METADATA) {
364+
ret = bio_integrity_map_iter(bio, iocb->private);
365+
WRITE_ONCE(iocb->private, NULL);
366+
if (unlikely(ret))
367+
goto out_bio_put;
368+
}
369+
349370
if (iocb->ki_flags & IOCB_ATOMIC)
350371
bio->bi_opf |= REQ_ATOMIC;
351372

@@ -360,6 +381,10 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
360381
submit_bio(bio);
361382
}
362383
return -EIOCBQUEUED;
384+
385+
out_bio_put:
386+
bio_put(bio);
387+
return ret;
363388
}
364389

365390
static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)

include/linux/bio-integrity.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct bio_integrity_payload {
2323
unsigned short bip_vcnt; /* # of integrity bio_vecs */
2424
unsigned short bip_max_vcnt; /* integrity bio_vec slots */
2525
unsigned short bip_flags; /* control flags */
26+
u16 app_tag; /* application tag value */
2627

2728
struct bvec_iter bio_iter; /* for rewinding parent bio */
2829

@@ -78,6 +79,7 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, gfp_t gfp,
7879
int bio_integrity_add_page(struct bio *bio, struct page *page, unsigned int len,
7980
unsigned int offset);
8081
int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter);
82+
int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta);
8183
void bio_integrity_unmap_user(struct bio *bio);
8284
bool bio_integrity_prep(struct bio *bio);
8385
void bio_integrity_advance(struct bio *bio, unsigned int bytes_done);
@@ -108,6 +110,11 @@ static int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
108110
return -EINVAL;
109111
}
110112

113+
static inline int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
114+
{
115+
return -EINVAL;
116+
}
117+
111118
static inline void bio_integrity_unmap_user(struct bio *bio)
112119
{
113120
}

0 commit comments

Comments
 (0)