Commit | Line | Data |
---|---|---|
7ba1ba12 MP |
1 | /* |
2 | * bio-integrity.c - bio data integrity extensions | |
3 | * | |
7878cba9 | 4 | * Copyright (C) 2007, 2008, 2009 Oracle Corporation |
7ba1ba12 MP |
5 | * Written by: Martin K. Petersen <martin.petersen@oracle.com> |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; see the file COPYING. If not, write to | |
18 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, | |
19 | * USA. | |
20 | * | |
21 | */ | |
22 | ||
23 | #include <linux/blkdev.h> | |
24 | #include <linux/mempool.h> | |
afeacc8c | 25 | #include <linux/export.h> |
7ba1ba12 MP |
26 | #include <linux/bio.h> |
27 | #include <linux/workqueue.h> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
7ba1ba12 | 29 | |
7878cba9 MP |
30 | struct integrity_slab { |
31 | struct kmem_cache *slab; | |
32 | unsigned short nr_vecs; | |
33 | char name[8]; | |
34 | }; | |
35 | ||
36 | #define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) } | |
37 | struct integrity_slab bip_slab[BIOVEC_NR_POOLS] __read_mostly = { | |
38 | IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES), | |
39 | }; | |
40 | #undef IS | |
41 | ||
7ba1ba12 MP |
42 | static struct workqueue_struct *kintegrityd_wq; |
43 | ||
7878cba9 MP |
44 | static inline unsigned int vecs_to_idx(unsigned int nr) |
45 | { | |
46 | switch (nr) { | |
47 | case 1: | |
48 | return 0; | |
49 | case 2 ... 4: | |
50 | return 1; | |
51 | case 5 ... 16: | |
52 | return 2; | |
53 | case 17 ... 64: | |
54 | return 3; | |
55 | case 65 ... 128: | |
56 | return 4; | |
57 | case 129 ... BIO_MAX_PAGES: | |
58 | return 5; | |
59 | default: | |
60 | BUG(); | |
61 | } | |
62 | } | |
63 | ||
64 | static inline int use_bip_pool(unsigned int idx) | |
65 | { | |
9e9432c2 | 66 | if (idx == BIOVEC_MAX_IDX) |
7878cba9 MP |
67 | return 1; |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
7ba1ba12 | 72 | /** |
1e2a410f | 73 | * bio_integrity_alloc - Allocate integrity payload and attach it to bio |
7ba1ba12 MP |
74 | * @bio: bio to attach integrity metadata to |
75 | * @gfp_mask: Memory allocation mask | |
76 | * @nr_vecs: Number of integrity metadata scatter-gather elements | |
7ba1ba12 MP |
77 | * |
78 | * Description: This function prepares a bio for attaching integrity | |
79 | * metadata. nr_vecs specifies the maximum number of pages containing | |
80 | * integrity metadata that can be attached. | |
81 | */ | |
1e2a410f KO |
82 | struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, |
83 | gfp_t gfp_mask, | |
84 | unsigned int nr_vecs) | |
7ba1ba12 MP |
85 | { |
86 | struct bio_integrity_payload *bip; | |
7878cba9 | 87 | unsigned int idx = vecs_to_idx(nr_vecs); |
1e2a410f KO |
88 | struct bio_set *bs = bio->bi_pool; |
89 | ||
90 | if (!bs) | |
91 | bs = fs_bio_set; | |
7ba1ba12 MP |
92 | |
93 | BUG_ON(bio == NULL); | |
7878cba9 | 94 | bip = NULL; |
7ba1ba12 | 95 | |
7878cba9 MP |
96 | /* Lower order allocations come straight from slab */ |
97 | if (!use_bip_pool(idx)) | |
98 | bip = kmem_cache_alloc(bip_slab[idx].slab, gfp_mask); | |
7ba1ba12 | 99 | |
7878cba9 MP |
100 | /* Use mempool if lower order alloc failed or max vecs were requested */ |
101 | if (bip == NULL) { | |
9e9432c2 | 102 | idx = BIOVEC_MAX_IDX; /* so we free the payload properly later */ |
7878cba9 | 103 | bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask); |
7ba1ba12 | 104 | |
7878cba9 MP |
105 | if (unlikely(bip == NULL)) { |
106 | printk(KERN_ERR "%s: could not alloc bip\n", __func__); | |
107 | return NULL; | |
108 | } | |
7ba1ba12 MP |
109 | } |
110 | ||
7878cba9 MP |
111 | memset(bip, 0, sizeof(*bip)); |
112 | ||
113 | bip->bip_slab = idx; | |
7ba1ba12 | 114 | bip->bip_bio = bio; |
6fda981c | 115 | bip->bip_vec = bip->bip_inline_vecs; |
7ba1ba12 MP |
116 | bio->bi_integrity = bip; |
117 | ||
118 | return bip; | |
119 | } | |
7ba1ba12 MP |
120 | EXPORT_SYMBOL(bio_integrity_alloc); |
121 | ||
122 | /** | |
123 | * bio_integrity_free - Free bio integrity payload | |
124 | * @bio: bio containing bip to be freed | |
7ba1ba12 MP |
125 | * |
126 | * Description: Used to free the integrity portion of a bio. Usually | |
127 | * called from bio_free(). | |
128 | */ | |
1e2a410f | 129 | void bio_integrity_free(struct bio *bio) |
7ba1ba12 MP |
130 | { |
131 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
1e2a410f KO |
132 | struct bio_set *bs = bio->bi_pool; |
133 | ||
134 | if (!bs) | |
135 | bs = fs_bio_set; | |
7ba1ba12 MP |
136 | |
137 | BUG_ON(bip == NULL); | |
138 | ||
139 | /* A cloned bio doesn't own the integrity metadata */ | |
74aa8c2c MP |
140 | if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY) |
141 | && bip->bip_buf != NULL) | |
7ba1ba12 MP |
142 | kfree(bip->bip_buf); |
143 | ||
7878cba9 MP |
144 | if (use_bip_pool(bip->bip_slab)) |
145 | mempool_free(bip, bs->bio_integrity_pool); | |
146 | else | |
147 | kmem_cache_free(bip_slab[bip->bip_slab].slab, bip); | |
7ba1ba12 MP |
148 | |
149 | bio->bi_integrity = NULL; | |
150 | } | |
151 | EXPORT_SYMBOL(bio_integrity_free); | |
152 | ||
153 | /** | |
154 | * bio_integrity_add_page - Attach integrity metadata | |
155 | * @bio: bio to update | |
156 | * @page: page containing integrity metadata | |
157 | * @len: number of bytes of integrity metadata in page | |
158 | * @offset: start offset within page | |
159 | * | |
160 | * Description: Attach a page containing integrity metadata to bio. | |
161 | */ | |
162 | int bio_integrity_add_page(struct bio *bio, struct page *page, | |
163 | unsigned int len, unsigned int offset) | |
164 | { | |
165 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
166 | struct bio_vec *iv; | |
167 | ||
7878cba9 | 168 | if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_slab)) { |
7ba1ba12 MP |
169 | printk(KERN_ERR "%s: bip_vec full\n", __func__); |
170 | return 0; | |
171 | } | |
172 | ||
173 | iv = bip_vec_idx(bip, bip->bip_vcnt); | |
174 | BUG_ON(iv == NULL); | |
7ba1ba12 MP |
175 | |
176 | iv->bv_page = page; | |
177 | iv->bv_len = len; | |
178 | iv->bv_offset = offset; | |
179 | bip->bip_vcnt++; | |
180 | ||
181 | return len; | |
182 | } | |
183 | EXPORT_SYMBOL(bio_integrity_add_page); | |
184 | ||
9c02f2b0 JA |
185 | static int bdev_integrity_enabled(struct block_device *bdev, int rw) |
186 | { | |
187 | struct blk_integrity *bi = bdev_get_integrity(bdev); | |
188 | ||
189 | if (bi == NULL) | |
190 | return 0; | |
191 | ||
192 | if (rw == READ && bi->verify_fn != NULL && | |
193 | (bi->flags & INTEGRITY_FLAG_READ)) | |
194 | return 1; | |
195 | ||
196 | if (rw == WRITE && bi->generate_fn != NULL && | |
197 | (bi->flags & INTEGRITY_FLAG_WRITE)) | |
198 | return 1; | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
7ba1ba12 MP |
203 | /** |
204 | * bio_integrity_enabled - Check whether integrity can be passed | |
205 | * @bio: bio to check | |
206 | * | |
207 | * Description: Determines whether bio_integrity_prep() can be called | |
208 | * on this bio or not. bio data direction and target device must be | |
209 | * set prior to calling. The functions honors the write_generate and | |
210 | * read_verify flags in sysfs. | |
211 | */ | |
212 | int bio_integrity_enabled(struct bio *bio) | |
213 | { | |
214 | /* Already protected? */ | |
215 | if (bio_integrity(bio)) | |
216 | return 0; | |
217 | ||
218 | return bdev_integrity_enabled(bio->bi_bdev, bio_data_dir(bio)); | |
219 | } | |
220 | EXPORT_SYMBOL(bio_integrity_enabled); | |
221 | ||
222 | /** | |
223 | * bio_integrity_hw_sectors - Convert 512b sectors to hardware ditto | |
224 | * @bi: blk_integrity profile for device | |
225 | * @sectors: Number of 512 sectors to convert | |
226 | * | |
227 | * Description: The block layer calculates everything in 512 byte | |
228 | * sectors but integrity metadata is done in terms of the hardware | |
229 | * sector size of the storage device. Convert the block layer sectors | |
230 | * to physical sectors. | |
231 | */ | |
b984679e JA |
232 | static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi, |
233 | unsigned int sectors) | |
7ba1ba12 MP |
234 | { |
235 | /* At this point there are only 512b or 4096b DIF/EPP devices */ | |
236 | if (bi->sector_size == 4096) | |
237 | return sectors >>= 3; | |
238 | ||
239 | return sectors; | |
240 | } | |
241 | ||
242 | /** | |
243 | * bio_integrity_tag_size - Retrieve integrity tag space | |
244 | * @bio: bio to inspect | |
245 | * | |
246 | * Description: Returns the maximum number of tag bytes that can be | |
247 | * attached to this bio. Filesystems can use this to determine how | |
248 | * much metadata to attach to an I/O. | |
249 | */ | |
250 | unsigned int bio_integrity_tag_size(struct bio *bio) | |
251 | { | |
252 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
253 | ||
254 | BUG_ON(bio->bi_size == 0); | |
255 | ||
256 | return bi->tag_size * (bio->bi_size / bi->sector_size); | |
257 | } | |
258 | EXPORT_SYMBOL(bio_integrity_tag_size); | |
259 | ||
260 | int bio_integrity_tag(struct bio *bio, void *tag_buf, unsigned int len, int set) | |
261 | { | |
262 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
263 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
264 | unsigned int nr_sectors; | |
265 | ||
266 | BUG_ON(bip->bip_buf == NULL); | |
267 | ||
268 | if (bi->tag_size == 0) | |
269 | return -1; | |
270 | ||
b984679e JA |
271 | nr_sectors = bio_integrity_hw_sectors(bi, |
272 | DIV_ROUND_UP(len, bi->tag_size)); | |
7ba1ba12 MP |
273 | |
274 | if (nr_sectors * bi->tuple_size > bip->bip_size) { | |
275 | printk(KERN_ERR "%s: tag too big for bio: %u > %u\n", | |
276 | __func__, nr_sectors * bi->tuple_size, bip->bip_size); | |
277 | return -1; | |
278 | } | |
279 | ||
280 | if (set) | |
281 | bi->set_tag_fn(bip->bip_buf, tag_buf, nr_sectors); | |
282 | else | |
283 | bi->get_tag_fn(bip->bip_buf, tag_buf, nr_sectors); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | /** | |
289 | * bio_integrity_set_tag - Attach a tag buffer to a bio | |
290 | * @bio: bio to attach buffer to | |
291 | * @tag_buf: Pointer to a buffer containing tag data | |
292 | * @len: Length of the included buffer | |
293 | * | |
294 | * Description: Use this function to tag a bio by leveraging the extra | |
295 | * space provided by devices formatted with integrity protection. The | |
296 | * size of the integrity buffer must be <= to the size reported by | |
297 | * bio_integrity_tag_size(). | |
298 | */ | |
299 | int bio_integrity_set_tag(struct bio *bio, void *tag_buf, unsigned int len) | |
300 | { | |
301 | BUG_ON(bio_data_dir(bio) != WRITE); | |
302 | ||
303 | return bio_integrity_tag(bio, tag_buf, len, 1); | |
304 | } | |
305 | EXPORT_SYMBOL(bio_integrity_set_tag); | |
306 | ||
307 | /** | |
308 | * bio_integrity_get_tag - Retrieve a tag buffer from a bio | |
309 | * @bio: bio to retrieve buffer from | |
310 | * @tag_buf: Pointer to a buffer for the tag data | |
311 | * @len: Length of the target buffer | |
312 | * | |
313 | * Description: Use this function to retrieve the tag buffer from a | |
314 | * completed I/O. The size of the integrity buffer must be <= to the | |
315 | * size reported by bio_integrity_tag_size(). | |
316 | */ | |
317 | int bio_integrity_get_tag(struct bio *bio, void *tag_buf, unsigned int len) | |
318 | { | |
319 | BUG_ON(bio_data_dir(bio) != READ); | |
320 | ||
321 | return bio_integrity_tag(bio, tag_buf, len, 0); | |
322 | } | |
323 | EXPORT_SYMBOL(bio_integrity_get_tag); | |
324 | ||
325 | /** | |
326 | * bio_integrity_generate - Generate integrity metadata for a bio | |
327 | * @bio: bio to generate integrity metadata for | |
328 | * | |
329 | * Description: Generates integrity metadata for a bio by calling the | |
330 | * block device's generation callback function. The bio must have a | |
331 | * bip attached with enough room to accommodate the generated | |
332 | * integrity metadata. | |
333 | */ | |
334 | static void bio_integrity_generate(struct bio *bio) | |
335 | { | |
336 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
337 | struct blk_integrity_exchg bix; | |
338 | struct bio_vec *bv; | |
339 | sector_t sector = bio->bi_sector; | |
340 | unsigned int i, sectors, total; | |
341 | void *prot_buf = bio->bi_integrity->bip_buf; | |
342 | ||
343 | total = 0; | |
344 | bix.disk_name = bio->bi_bdev->bd_disk->disk_name; | |
345 | bix.sector_size = bi->sector_size; | |
346 | ||
347 | bio_for_each_segment(bv, bio, i) { | |
e8e3c3d6 | 348 | void *kaddr = kmap_atomic(bv->bv_page); |
7ba1ba12 MP |
349 | bix.data_buf = kaddr + bv->bv_offset; |
350 | bix.data_size = bv->bv_len; | |
351 | bix.prot_buf = prot_buf; | |
352 | bix.sector = sector; | |
353 | ||
354 | bi->generate_fn(&bix); | |
355 | ||
356 | sectors = bv->bv_len / bi->sector_size; | |
357 | sector += sectors; | |
358 | prot_buf += sectors * bi->tuple_size; | |
359 | total += sectors * bi->tuple_size; | |
360 | BUG_ON(total > bio->bi_integrity->bip_size); | |
361 | ||
e8e3c3d6 | 362 | kunmap_atomic(kaddr); |
7ba1ba12 MP |
363 | } |
364 | } | |
365 | ||
9c02f2b0 JA |
366 | static inline unsigned short blk_integrity_tuple_size(struct blk_integrity *bi) |
367 | { | |
368 | if (bi) | |
369 | return bi->tuple_size; | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
7ba1ba12 MP |
374 | /** |
375 | * bio_integrity_prep - Prepare bio for integrity I/O | |
376 | * @bio: bio to prepare | |
377 | * | |
378 | * Description: Allocates a buffer for integrity metadata, maps the | |
379 | * pages and attaches them to a bio. The bio must have data | |
380 | * direction, target device and start sector set priot to calling. In | |
381 | * the WRITE case, integrity metadata will be generated using the | |
382 | * block device's integrity function. In the READ case, the buffer | |
383 | * will be prepared for DMA and a suitable end_io handler set up. | |
384 | */ | |
385 | int bio_integrity_prep(struct bio *bio) | |
386 | { | |
387 | struct bio_integrity_payload *bip; | |
388 | struct blk_integrity *bi; | |
389 | struct request_queue *q; | |
390 | void *buf; | |
391 | unsigned long start, end; | |
392 | unsigned int len, nr_pages; | |
393 | unsigned int bytes, offset, i; | |
394 | unsigned int sectors; | |
395 | ||
396 | bi = bdev_get_integrity(bio->bi_bdev); | |
397 | q = bdev_get_queue(bio->bi_bdev); | |
398 | BUG_ON(bi == NULL); | |
399 | BUG_ON(bio_integrity(bio)); | |
400 | ||
401 | sectors = bio_integrity_hw_sectors(bi, bio_sectors(bio)); | |
402 | ||
403 | /* Allocate kernel buffer for protection data */ | |
404 | len = sectors * blk_integrity_tuple_size(bi); | |
72f46503 | 405 | buf = kmalloc(len, GFP_NOIO | q->bounce_gfp); |
7ba1ba12 MP |
406 | if (unlikely(buf == NULL)) { |
407 | printk(KERN_ERR "could not allocate integrity buffer\n"); | |
220eb7fd | 408 | return -ENOMEM; |
7ba1ba12 MP |
409 | } |
410 | ||
411 | end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
412 | start = ((unsigned long) buf) >> PAGE_SHIFT; | |
413 | nr_pages = end - start; | |
414 | ||
415 | /* Allocate bio integrity payload and integrity vectors */ | |
416 | bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages); | |
417 | if (unlikely(bip == NULL)) { | |
418 | printk(KERN_ERR "could not allocate data integrity bioset\n"); | |
419 | kfree(buf); | |
420 | return -EIO; | |
421 | } | |
422 | ||
423 | bip->bip_buf = buf; | |
424 | bip->bip_size = len; | |
425 | bip->bip_sector = bio->bi_sector; | |
426 | ||
427 | /* Map it */ | |
428 | offset = offset_in_page(buf); | |
429 | for (i = 0 ; i < nr_pages ; i++) { | |
430 | int ret; | |
431 | bytes = PAGE_SIZE - offset; | |
432 | ||
433 | if (len <= 0) | |
434 | break; | |
435 | ||
436 | if (bytes > len) | |
437 | bytes = len; | |
438 | ||
439 | ret = bio_integrity_add_page(bio, virt_to_page(buf), | |
440 | bytes, offset); | |
441 | ||
442 | if (ret == 0) | |
443 | return 0; | |
444 | ||
445 | if (ret < bytes) | |
446 | break; | |
447 | ||
448 | buf += bytes; | |
449 | len -= bytes; | |
450 | offset = 0; | |
451 | } | |
452 | ||
453 | /* Install custom I/O completion handler if read verify is enabled */ | |
454 | if (bio_data_dir(bio) == READ) { | |
455 | bip->bip_end_io = bio->bi_end_io; | |
456 | bio->bi_end_io = bio_integrity_endio; | |
457 | } | |
458 | ||
459 | /* Auto-generate integrity metadata if this is a write */ | |
460 | if (bio_data_dir(bio) == WRITE) | |
461 | bio_integrity_generate(bio); | |
462 | ||
463 | return 0; | |
464 | } | |
465 | EXPORT_SYMBOL(bio_integrity_prep); | |
466 | ||
467 | /** | |
468 | * bio_integrity_verify - Verify integrity metadata for a bio | |
469 | * @bio: bio to verify | |
470 | * | |
471 | * Description: This function is called to verify the integrity of a | |
472 | * bio. The data in the bio io_vec is compared to the integrity | |
473 | * metadata returned by the HBA. | |
474 | */ | |
475 | static int bio_integrity_verify(struct bio *bio) | |
476 | { | |
477 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
478 | struct blk_integrity_exchg bix; | |
479 | struct bio_vec *bv; | |
480 | sector_t sector = bio->bi_integrity->bip_sector; | |
481 | unsigned int i, sectors, total, ret; | |
482 | void *prot_buf = bio->bi_integrity->bip_buf; | |
483 | ||
484 | ret = total = 0; | |
485 | bix.disk_name = bio->bi_bdev->bd_disk->disk_name; | |
486 | bix.sector_size = bi->sector_size; | |
487 | ||
488 | bio_for_each_segment(bv, bio, i) { | |
e8e3c3d6 | 489 | void *kaddr = kmap_atomic(bv->bv_page); |
7ba1ba12 MP |
490 | bix.data_buf = kaddr + bv->bv_offset; |
491 | bix.data_size = bv->bv_len; | |
492 | bix.prot_buf = prot_buf; | |
493 | bix.sector = sector; | |
494 | ||
495 | ret = bi->verify_fn(&bix); | |
496 | ||
497 | if (ret) { | |
e8e3c3d6 | 498 | kunmap_atomic(kaddr); |
7b24fc4d | 499 | return ret; |
7ba1ba12 MP |
500 | } |
501 | ||
502 | sectors = bv->bv_len / bi->sector_size; | |
503 | sector += sectors; | |
504 | prot_buf += sectors * bi->tuple_size; | |
505 | total += sectors * bi->tuple_size; | |
506 | BUG_ON(total > bio->bi_integrity->bip_size); | |
507 | ||
e8e3c3d6 | 508 | kunmap_atomic(kaddr); |
7ba1ba12 MP |
509 | } |
510 | ||
511 | return ret; | |
512 | } | |
513 | ||
514 | /** | |
515 | * bio_integrity_verify_fn - Integrity I/O completion worker | |
516 | * @work: Work struct stored in bio to be verified | |
517 | * | |
518 | * Description: This workqueue function is called to complete a READ | |
519 | * request. The function verifies the transferred integrity metadata | |
520 | * and then calls the original bio end_io function. | |
521 | */ | |
522 | static void bio_integrity_verify_fn(struct work_struct *work) | |
523 | { | |
b984679e | 524 | struct bio_integrity_payload *bip = |
7ba1ba12 MP |
525 | container_of(work, struct bio_integrity_payload, bip_work); |
526 | struct bio *bio = bip->bip_bio; | |
7b24fc4d | 527 | int error; |
7ba1ba12 | 528 | |
7b24fc4d | 529 | error = bio_integrity_verify(bio); |
7ba1ba12 MP |
530 | |
531 | /* Restore original bio completion handler */ | |
532 | bio->bi_end_io = bip->bip_end_io; | |
7b24fc4d | 533 | bio_endio(bio, error); |
7ba1ba12 MP |
534 | } |
535 | ||
536 | /** | |
537 | * bio_integrity_endio - Integrity I/O completion function | |
538 | * @bio: Protected bio | |
539 | * @error: Pointer to errno | |
540 | * | |
541 | * Description: Completion for integrity I/O | |
542 | * | |
543 | * Normally I/O completion is done in interrupt context. However, | |
544 | * verifying I/O integrity is a time-consuming task which must be run | |
545 | * in process context. This function postpones completion | |
546 | * accordingly. | |
547 | */ | |
548 | void bio_integrity_endio(struct bio *bio, int error) | |
549 | { | |
550 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
551 | ||
552 | BUG_ON(bip->bip_bio != bio); | |
553 | ||
7b24fc4d MP |
554 | /* In case of an I/O error there is no point in verifying the |
555 | * integrity metadata. Restore original bio end_io handler | |
556 | * and run it. | |
557 | */ | |
558 | if (error) { | |
559 | bio->bi_end_io = bip->bip_end_io; | |
560 | bio_endio(bio, error); | |
561 | ||
562 | return; | |
563 | } | |
564 | ||
7ba1ba12 MP |
565 | INIT_WORK(&bip->bip_work, bio_integrity_verify_fn); |
566 | queue_work(kintegrityd_wq, &bip->bip_work); | |
567 | } | |
568 | EXPORT_SYMBOL(bio_integrity_endio); | |
569 | ||
570 | /** | |
571 | * bio_integrity_mark_head - Advance bip_vec skip bytes | |
572 | * @bip: Integrity vector to advance | |
573 | * @skip: Number of bytes to advance it | |
574 | */ | |
b984679e JA |
575 | void bio_integrity_mark_head(struct bio_integrity_payload *bip, |
576 | unsigned int skip) | |
7ba1ba12 MP |
577 | { |
578 | struct bio_vec *iv; | |
579 | unsigned int i; | |
580 | ||
581 | bip_for_each_vec(iv, bip, i) { | |
582 | if (skip == 0) { | |
583 | bip->bip_idx = i; | |
584 | return; | |
585 | } else if (skip >= iv->bv_len) { | |
586 | skip -= iv->bv_len; | |
587 | } else { /* skip < iv->bv_len) */ | |
588 | iv->bv_offset += skip; | |
589 | iv->bv_len -= skip; | |
590 | bip->bip_idx = i; | |
591 | return; | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | /** | |
597 | * bio_integrity_mark_tail - Truncate bip_vec to be len bytes long | |
598 | * @bip: Integrity vector to truncate | |
599 | * @len: New length of integrity vector | |
600 | */ | |
b984679e JA |
601 | void bio_integrity_mark_tail(struct bio_integrity_payload *bip, |
602 | unsigned int len) | |
7ba1ba12 MP |
603 | { |
604 | struct bio_vec *iv; | |
605 | unsigned int i; | |
606 | ||
607 | bip_for_each_vec(iv, bip, i) { | |
608 | if (len == 0) { | |
609 | bip->bip_vcnt = i; | |
610 | return; | |
611 | } else if (len >= iv->bv_len) { | |
612 | len -= iv->bv_len; | |
613 | } else { /* len < iv->bv_len) */ | |
614 | iv->bv_len = len; | |
615 | len = 0; | |
616 | } | |
617 | } | |
618 | } | |
619 | ||
620 | /** | |
621 | * bio_integrity_advance - Advance integrity vector | |
622 | * @bio: bio whose integrity vector to update | |
623 | * @bytes_done: number of data bytes that have been completed | |
624 | * | |
625 | * Description: This function calculates how many integrity bytes the | |
626 | * number of completed data bytes correspond to and advances the | |
627 | * integrity vector accordingly. | |
628 | */ | |
629 | void bio_integrity_advance(struct bio *bio, unsigned int bytes_done) | |
630 | { | |
631 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
632 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
633 | unsigned int nr_sectors; | |
634 | ||
635 | BUG_ON(bip == NULL); | |
636 | BUG_ON(bi == NULL); | |
637 | ||
638 | nr_sectors = bio_integrity_hw_sectors(bi, bytes_done >> 9); | |
639 | bio_integrity_mark_head(bip, nr_sectors * bi->tuple_size); | |
640 | } | |
641 | EXPORT_SYMBOL(bio_integrity_advance); | |
642 | ||
643 | /** | |
644 | * bio_integrity_trim - Trim integrity vector | |
645 | * @bio: bio whose integrity vector to update | |
646 | * @offset: offset to first data sector | |
647 | * @sectors: number of data sectors | |
648 | * | |
649 | * Description: Used to trim the integrity vector in a cloned bio. | |
650 | * The ivec will be advanced corresponding to 'offset' data sectors | |
651 | * and the length will be truncated corresponding to 'len' data | |
652 | * sectors. | |
653 | */ | |
b984679e JA |
654 | void bio_integrity_trim(struct bio *bio, unsigned int offset, |
655 | unsigned int sectors) | |
7ba1ba12 MP |
656 | { |
657 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
658 | struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); | |
659 | unsigned int nr_sectors; | |
660 | ||
661 | BUG_ON(bip == NULL); | |
662 | BUG_ON(bi == NULL); | |
663 | BUG_ON(!bio_flagged(bio, BIO_CLONED)); | |
664 | ||
665 | nr_sectors = bio_integrity_hw_sectors(bi, sectors); | |
666 | bip->bip_sector = bip->bip_sector + offset; | |
667 | bio_integrity_mark_head(bip, offset * bi->tuple_size); | |
668 | bio_integrity_mark_tail(bip, sectors * bi->tuple_size); | |
669 | } | |
670 | EXPORT_SYMBOL(bio_integrity_trim); | |
671 | ||
672 | /** | |
673 | * bio_integrity_split - Split integrity metadata | |
674 | * @bio: Protected bio | |
675 | * @bp: Resulting bio_pair | |
676 | * @sectors: Offset | |
677 | * | |
678 | * Description: Splits an integrity page into a bio_pair. | |
679 | */ | |
680 | void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors) | |
681 | { | |
682 | struct blk_integrity *bi; | |
683 | struct bio_integrity_payload *bip = bio->bi_integrity; | |
684 | unsigned int nr_sectors; | |
685 | ||
686 | if (bio_integrity(bio) == 0) | |
687 | return; | |
688 | ||
689 | bi = bdev_get_integrity(bio->bi_bdev); | |
690 | BUG_ON(bi == NULL); | |
691 | BUG_ON(bip->bip_vcnt != 1); | |
692 | ||
693 | nr_sectors = bio_integrity_hw_sectors(bi, sectors); | |
694 | ||
695 | bp->bio1.bi_integrity = &bp->bip1; | |
696 | bp->bio2.bi_integrity = &bp->bip2; | |
697 | ||
698 | bp->iv1 = bip->bip_vec[0]; | |
699 | bp->iv2 = bip->bip_vec[0]; | |
700 | ||
6fda981c KO |
701 | bp->bip1.bip_vec = &bp->iv1; |
702 | bp->bip2.bip_vec = &bp->iv2; | |
7ba1ba12 MP |
703 | |
704 | bp->iv1.bv_len = sectors * bi->tuple_size; | |
705 | bp->iv2.bv_offset += sectors * bi->tuple_size; | |
706 | bp->iv2.bv_len -= sectors * bi->tuple_size; | |
707 | ||
708 | bp->bip1.bip_sector = bio->bi_integrity->bip_sector; | |
709 | bp->bip2.bip_sector = bio->bi_integrity->bip_sector + nr_sectors; | |
710 | ||
711 | bp->bip1.bip_vcnt = bp->bip2.bip_vcnt = 1; | |
712 | bp->bip1.bip_idx = bp->bip2.bip_idx = 0; | |
713 | } | |
714 | EXPORT_SYMBOL(bio_integrity_split); | |
715 | ||
716 | /** | |
717 | * bio_integrity_clone - Callback for cloning bios with integrity metadata | |
718 | * @bio: New bio | |
719 | * @bio_src: Original bio | |
87092698 | 720 | * @gfp_mask: Memory allocation mask |
7ba1ba12 MP |
721 | * |
722 | * Description: Called to allocate a bip when cloning a bio | |
723 | */ | |
7878cba9 | 724 | int bio_integrity_clone(struct bio *bio, struct bio *bio_src, |
1e2a410f | 725 | gfp_t gfp_mask) |
7ba1ba12 MP |
726 | { |
727 | struct bio_integrity_payload *bip_src = bio_src->bi_integrity; | |
728 | struct bio_integrity_payload *bip; | |
729 | ||
730 | BUG_ON(bip_src == NULL); | |
731 | ||
1e2a410f | 732 | bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt); |
7ba1ba12 MP |
733 | |
734 | if (bip == NULL) | |
735 | return -EIO; | |
736 | ||
737 | memcpy(bip->bip_vec, bip_src->bip_vec, | |
738 | bip_src->bip_vcnt * sizeof(struct bio_vec)); | |
739 | ||
740 | bip->bip_sector = bip_src->bip_sector; | |
741 | bip->bip_vcnt = bip_src->bip_vcnt; | |
742 | bip->bip_idx = bip_src->bip_idx; | |
743 | ||
744 | return 0; | |
745 | } | |
746 | EXPORT_SYMBOL(bio_integrity_clone); | |
747 | ||
7878cba9 | 748 | int bioset_integrity_create(struct bio_set *bs, int pool_size) |
7ba1ba12 | 749 | { |
7878cba9 MP |
750 | unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES); |
751 | ||
a91a2785 MP |
752 | if (bs->bio_integrity_pool) |
753 | return 0; | |
754 | ||
7878cba9 MP |
755 | bs->bio_integrity_pool = |
756 | mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab); | |
7ba1ba12 | 757 | |
7878cba9 MP |
758 | if (!bs->bio_integrity_pool) |
759 | return -1; | |
760 | ||
761 | return 0; | |
762 | } | |
763 | EXPORT_SYMBOL(bioset_integrity_create); | |
764 | ||
765 | void bioset_integrity_free(struct bio_set *bs) | |
766 | { | |
767 | if (bs->bio_integrity_pool) | |
768 | mempool_destroy(bs->bio_integrity_pool); | |
769 | } | |
770 | EXPORT_SYMBOL(bioset_integrity_free); | |
771 | ||
772 | void __init bio_integrity_init(void) | |
773 | { | |
774 | unsigned int i; | |
775 | ||
a6e8dc46 TH |
776 | /* |
777 | * kintegrityd won't block much but may burn a lot of CPU cycles. | |
778 | * Make it highpri CPU intensive wq with max concurrency of 1. | |
779 | */ | |
780 | kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM | | |
781 | WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1); | |
6d2a78e7 MP |
782 | if (!kintegrityd_wq) |
783 | panic("Failed to create kintegrityd\n"); | |
7ba1ba12 | 784 | |
7878cba9 MP |
785 | for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) { |
786 | unsigned int size; | |
7ba1ba12 | 787 | |
7878cba9 MP |
788 | size = sizeof(struct bio_integrity_payload) |
789 | + bip_slab[i].nr_vecs * sizeof(struct bio_vec); | |
7ba1ba12 | 790 | |
7878cba9 MP |
791 | bip_slab[i].slab = |
792 | kmem_cache_create(bip_slab[i].name, size, 0, | |
793 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); | |
794 | } | |
7ba1ba12 | 795 | } |