summaryrefslogtreecommitdiffstats
path: root/block/bio-integrity.c
diff options
context:
space:
mode:
authorDmitry Monakhov <dmonakhov@openvz.org>2017-06-29 14:31:11 -0400
committerJens Axboe <axboe@kernel.dk>2017-07-03 18:56:24 -0400
commite23947bd76f00701f9407af23e671f4da96f5f25 (patch)
tree94f24da2825f34c411a31f4aa54564c2294aef1d /block/bio-integrity.c
parentfbd08e7673f950854679e5d79a30bb25e77a9d08 (diff)
bio-integrity: fold bio_integrity_enabled to bio_integrity_prep
Currently all integrity prep hooks are open-coded, and if prepare fails we ignore it's code and fail bio with EIO. Let's return real error to upper layer, so later caller may react accordingly. In fact no one want to use bio_integrity_prep() w/o bio_integrity_enabled, so it is reasonable to fold it in to one function. Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> [hch: merged with the latest block tree, return bool from bio_integrity_prep] Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block/bio-integrity.c')
-rw-r--r--block/bio-integrity.c88
1 files changed, 39 insertions, 49 deletions
diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index 3a0d71199fb0..44c4c52681c2 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -160,44 +160,6 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
160EXPORT_SYMBOL(bio_integrity_add_page); 160EXPORT_SYMBOL(bio_integrity_add_page);
161 161
162/** 162/**
163 * bio_integrity_enabled - Check whether integrity can be passed
164 * @bio: bio to check
165 *
166 * Description: Determines whether bio_integrity_prep() can be called
167 * on this bio or not. bio data direction and target device must be
168 * set prior to calling. The functions honors the write_generate and
169 * read_verify flags in sysfs.
170 */
171bool bio_integrity_enabled(struct bio *bio)
172{
173 struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
174
175 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
176 return false;
177
178 if (!bio_sectors(bio))
179 return false;
180
181 /* Already protected? */
182 if (bio_integrity(bio))
183 return false;
184
185 if (bi == NULL)
186 return false;
187
188 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
189 (bi->flags & BLK_INTEGRITY_VERIFY))
190 return true;
191
192 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
193 (bi->flags & BLK_INTEGRITY_GENERATE))
194 return true;
195
196 return false;
197}
198EXPORT_SYMBOL(bio_integrity_enabled);
199
200/**
201 * bio_integrity_intervals - Return number of integrity intervals for a bio 163 * bio_integrity_intervals - Return number of integrity intervals for a bio
202 * @bi: blk_integrity profile for device 164 * @bi: blk_integrity profile for device
203 * @sectors: Size of the bio in 512-byte sectors 165 * @sectors: Size of the bio in 512-byte sectors
@@ -262,14 +224,15 @@ static blk_status_t bio_integrity_process(struct bio *bio,
262 * bio_integrity_prep - Prepare bio for integrity I/O 224 * bio_integrity_prep - Prepare bio for integrity I/O
263 * @bio: bio to prepare 225 * @bio: bio to prepare
264 * 226 *
265 * Description: Allocates a buffer for integrity metadata, maps the 227 * Description: Checks if the bio already has an integrity payload attached.
266 * pages and attaches them to a bio. The bio must have data 228 * If it does, the payload has been generated by another kernel subsystem,
267 * direction, target device and start sector set priot to calling. In 229 * and we just pass it through. Otherwise allocates integrity payload.
268 * the WRITE case, integrity metadata will be generated using the 230 * The bio must have data direction, target device and start sector set priot
269 * block device's integrity function. In the READ case, the buffer 231 * to calling. In the WRITE case, integrity metadata will be generated using
232 * the block device's integrity function. In the READ case, the buffer
270 * will be prepared for DMA and a suitable end_io handler set up. 233 * will be prepared for DMA and a suitable end_io handler set up.
271 */ 234 */
272int bio_integrity_prep(struct bio *bio) 235bool bio_integrity_prep(struct bio *bio)
273{ 236{
274 struct bio_integrity_payload *bip; 237 struct bio_integrity_payload *bip;
275 struct blk_integrity *bi; 238 struct blk_integrity *bi;
@@ -279,20 +242,41 @@ int bio_integrity_prep(struct bio *bio)
279 unsigned int len, nr_pages; 242 unsigned int len, nr_pages;
280 unsigned int bytes, offset, i; 243 unsigned int bytes, offset, i;
281 unsigned int intervals; 244 unsigned int intervals;
245 blk_status_t status;
282 246
283 bi = bdev_get_integrity(bio->bi_bdev); 247 bi = bdev_get_integrity(bio->bi_bdev);
284 q = bdev_get_queue(bio->bi_bdev); 248 q = bdev_get_queue(bio->bi_bdev);
285 BUG_ON(bi == NULL); 249 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
286 BUG_ON(bio_integrity(bio)); 250 return true;
251
252 if (!bio_sectors(bio))
253 return true;
287 254
255 /* Already protected? */
256 if (bio_integrity(bio))
257 return true;
258
259 if (bi == NULL)
260 return true;
261
262 if (bio_data_dir(bio) == READ) {
263 if (!bi->profile->verify_fn ||
264 !(bi->flags & BLK_INTEGRITY_VERIFY))
265 return true;
266 } else {
267 if (!bi->profile->generate_fn ||
268 !(bi->flags & BLK_INTEGRITY_GENERATE))
269 return true;
270 }
288 intervals = bio_integrity_intervals(bi, bio_sectors(bio)); 271 intervals = bio_integrity_intervals(bi, bio_sectors(bio));
289 272
290 /* Allocate kernel buffer for protection data */ 273 /* Allocate kernel buffer for protection data */
291 len = intervals * bi->tuple_size; 274 len = intervals * bi->tuple_size;
292 buf = kmalloc(len, GFP_NOIO | q->bounce_gfp); 275 buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
276 status = BLK_STS_RESOURCE;
293 if (unlikely(buf == NULL)) { 277 if (unlikely(buf == NULL)) {
294 printk(KERN_ERR "could not allocate integrity buffer\n"); 278 printk(KERN_ERR "could not allocate integrity buffer\n");
295 return -ENOMEM; 279 goto err_end_io;
296 } 280 }
297 281
298 end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 282 end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
@@ -304,7 +288,8 @@ int bio_integrity_prep(struct bio *bio)
304 if (IS_ERR(bip)) { 288 if (IS_ERR(bip)) {
305 printk(KERN_ERR "could not allocate data integrity bioset\n"); 289 printk(KERN_ERR "could not allocate data integrity bioset\n");
306 kfree(buf); 290 kfree(buf);
307 return PTR_ERR(bip); 291 status = BLK_STS_RESOURCE;
292 goto err_end_io;
308 } 293 }
309 294
310 bip->bip_flags |= BIP_BLOCK_INTEGRITY; 295 bip->bip_flags |= BIP_BLOCK_INTEGRITY;
@@ -349,8 +334,13 @@ int bio_integrity_prep(struct bio *bio)
349 /* Auto-generate integrity metadata if this is a write */ 334 /* Auto-generate integrity metadata if this is a write */
350 if (bio_data_dir(bio) == WRITE) 335 if (bio_data_dir(bio) == WRITE)
351 bio_integrity_process(bio, bi->profile->generate_fn); 336 bio_integrity_process(bio, bi->profile->generate_fn);
337 return true;
338
339err_end_io:
340 bio->bi_status = status;
341 bio_endio(bio);
342 return false;
352 343
353 return 0;
354} 344}
355EXPORT_SYMBOL(bio_integrity_prep); 345EXPORT_SYMBOL(bio_integrity_prep);
356 346