2 * blk-integrity.c - Block layer data integrity extensions
4 * Copyright (C) 2007, 2008 Oracle Corporation
5 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
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.
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.
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,
23 #include <linux/blkdev.h>
24 #include <linux/mempool.h>
25 #include <linux/bio.h>
26 #include <linux/scatterlist.h>
27 #include <linux/export.h>
28 #include <linux/slab.h>
32 static struct kmem_cache
*integrity_cachep
;
34 static const char *bi_unsupported_name
= "unsupported";
37 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
39 * @bio: bio with integrity metadata attached
41 * Description: Returns the number of elements required in a
42 * scatterlist corresponding to the integrity metadata in a bio.
44 int blk_rq_count_integrity_sg(struct request_queue
*q
, struct bio
*bio
)
46 struct bio_vec iv
, ivprv
= { NULL
};
47 unsigned int segments
= 0;
48 unsigned int seg_size
= 0;
49 struct bvec_iter iter
;
52 bio_for_each_integrity_vec(iv
, bio
, iter
) {
55 if (!BIOVEC_PHYS_MERGEABLE(&ivprv
, &iv
))
58 if (!BIOVEC_SEG_BOUNDARY(q
, &ivprv
, &iv
))
61 if (seg_size
+ iv
.bv_len
> queue_max_segment_size(q
))
64 seg_size
+= iv
.bv_len
;
77 EXPORT_SYMBOL(blk_rq_count_integrity_sg
);
80 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
82 * @bio: bio with integrity metadata attached
83 * @sglist: target scatterlist
85 * Description: Map the integrity vectors in request into a
86 * scatterlist. The scatterlist must be big enough to hold all
87 * elements. I.e. sized using blk_rq_count_integrity_sg().
89 int blk_rq_map_integrity_sg(struct request_queue
*q
, struct bio
*bio
,
90 struct scatterlist
*sglist
)
92 struct bio_vec iv
, ivprv
= { NULL
};
93 struct scatterlist
*sg
= NULL
;
94 unsigned int segments
= 0;
95 struct bvec_iter iter
;
98 bio_for_each_integrity_vec(iv
, bio
, iter
) {
101 if (!BIOVEC_PHYS_MERGEABLE(&ivprv
, &iv
))
104 if (!BIOVEC_SEG_BOUNDARY(q
, &ivprv
, &iv
))
107 if (sg
->length
+ iv
.bv_len
> queue_max_segment_size(q
))
110 sg
->length
+= iv
.bv_len
;
120 sg_set_page(sg
, iv
.bv_page
, iv
.bv_len
, iv
.bv_offset
);
133 EXPORT_SYMBOL(blk_rq_map_integrity_sg
);
136 * blk_integrity_compare - Compare integrity profile of two disks
137 * @gd1: Disk to compare
138 * @gd2: Disk to compare
140 * Description: Meta-devices like DM and MD need to verify that all
141 * sub-devices use the same integrity format before advertising to
142 * upper layers that they can send/receive integrity metadata. This
143 * function can be used to check whether two gendisk devices have
144 * compatible integrity formats.
146 int blk_integrity_compare(struct gendisk
*gd1
, struct gendisk
*gd2
)
148 struct blk_integrity
*b1
= gd1
->integrity
;
149 struct blk_integrity
*b2
= gd2
->integrity
;
157 if (b1
->sector_size
!= b2
->sector_size
) {
158 printk(KERN_ERR
"%s: %s/%s sector sz %u != %u\n", __func__
,
159 gd1
->disk_name
, gd2
->disk_name
,
160 b1
->sector_size
, b2
->sector_size
);
164 if (b1
->tuple_size
!= b2
->tuple_size
) {
165 printk(KERN_ERR
"%s: %s/%s tuple sz %u != %u\n", __func__
,
166 gd1
->disk_name
, gd2
->disk_name
,
167 b1
->tuple_size
, b2
->tuple_size
);
171 if (b1
->tag_size
&& b2
->tag_size
&& (b1
->tag_size
!= b2
->tag_size
)) {
172 printk(KERN_ERR
"%s: %s/%s tag sz %u != %u\n", __func__
,
173 gd1
->disk_name
, gd2
->disk_name
,
174 b1
->tag_size
, b2
->tag_size
);
178 if (strcmp(b1
->name
, b2
->name
)) {
179 printk(KERN_ERR
"%s: %s/%s type %s != %s\n", __func__
,
180 gd1
->disk_name
, gd2
->disk_name
,
187 EXPORT_SYMBOL(blk_integrity_compare
);
189 int blk_integrity_merge_rq(struct request_queue
*q
, struct request
*req
,
190 struct request
*next
)
192 if (blk_integrity_rq(req
) != blk_integrity_rq(next
))
195 if (req
->nr_integrity_segments
+ next
->nr_integrity_segments
>
196 q
->limits
.max_integrity_segments
)
201 EXPORT_SYMBOL(blk_integrity_merge_rq
);
203 int blk_integrity_merge_bio(struct request_queue
*q
, struct request
*req
,
206 int nr_integrity_segs
;
207 struct bio
*next
= bio
->bi_next
;
210 nr_integrity_segs
= blk_rq_count_integrity_sg(q
, bio
);
213 if (req
->nr_integrity_segments
+ nr_integrity_segs
>
214 q
->limits
.max_integrity_segments
)
217 req
->nr_integrity_segments
+= nr_integrity_segs
;
221 EXPORT_SYMBOL(blk_integrity_merge_bio
);
223 struct integrity_sysfs_entry
{
224 struct attribute attr
;
225 ssize_t (*show
)(struct blk_integrity
*, char *);
226 ssize_t (*store
)(struct blk_integrity
*, const char *, size_t);
229 static ssize_t
integrity_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
232 struct blk_integrity
*bi
=
233 container_of(kobj
, struct blk_integrity
, kobj
);
234 struct integrity_sysfs_entry
*entry
=
235 container_of(attr
, struct integrity_sysfs_entry
, attr
);
237 return entry
->show(bi
, page
);
240 static ssize_t
integrity_attr_store(struct kobject
*kobj
,
241 struct attribute
*attr
, const char *page
,
244 struct blk_integrity
*bi
=
245 container_of(kobj
, struct blk_integrity
, kobj
);
246 struct integrity_sysfs_entry
*entry
=
247 container_of(attr
, struct integrity_sysfs_entry
, attr
);
251 ret
= entry
->store(bi
, page
, count
);
256 static ssize_t
integrity_format_show(struct blk_integrity
*bi
, char *page
)
258 if (bi
!= NULL
&& bi
->name
!= NULL
)
259 return sprintf(page
, "%s\n", bi
->name
);
261 return sprintf(page
, "none\n");
264 static ssize_t
integrity_tag_size_show(struct blk_integrity
*bi
, char *page
)
267 return sprintf(page
, "%u\n", bi
->tag_size
);
269 return sprintf(page
, "0\n");
272 static ssize_t
integrity_read_store(struct blk_integrity
*bi
,
273 const char *page
, size_t count
)
275 char *p
= (char *) page
;
276 unsigned long val
= simple_strtoul(p
, &p
, 10);
279 bi
->flags
|= INTEGRITY_FLAG_READ
;
281 bi
->flags
&= ~INTEGRITY_FLAG_READ
;
286 static ssize_t
integrity_read_show(struct blk_integrity
*bi
, char *page
)
288 return sprintf(page
, "%d\n", (bi
->flags
& INTEGRITY_FLAG_READ
) != 0);
291 static ssize_t
integrity_write_store(struct blk_integrity
*bi
,
292 const char *page
, size_t count
)
294 char *p
= (char *) page
;
295 unsigned long val
= simple_strtoul(p
, &p
, 10);
298 bi
->flags
|= INTEGRITY_FLAG_WRITE
;
300 bi
->flags
&= ~INTEGRITY_FLAG_WRITE
;
305 static ssize_t
integrity_write_show(struct blk_integrity
*bi
, char *page
)
307 return sprintf(page
, "%d\n", (bi
->flags
& INTEGRITY_FLAG_WRITE
) != 0);
310 static struct integrity_sysfs_entry integrity_format_entry
= {
311 .attr
= { .name
= "format", .mode
= S_IRUGO
},
312 .show
= integrity_format_show
,
315 static struct integrity_sysfs_entry integrity_tag_size_entry
= {
316 .attr
= { .name
= "tag_size", .mode
= S_IRUGO
},
317 .show
= integrity_tag_size_show
,
320 static struct integrity_sysfs_entry integrity_read_entry
= {
321 .attr
= { .name
= "read_verify", .mode
= S_IRUGO
| S_IWUSR
},
322 .show
= integrity_read_show
,
323 .store
= integrity_read_store
,
326 static struct integrity_sysfs_entry integrity_write_entry
= {
327 .attr
= { .name
= "write_generate", .mode
= S_IRUGO
| S_IWUSR
},
328 .show
= integrity_write_show
,
329 .store
= integrity_write_store
,
332 static struct attribute
*integrity_attrs
[] = {
333 &integrity_format_entry
.attr
,
334 &integrity_tag_size_entry
.attr
,
335 &integrity_read_entry
.attr
,
336 &integrity_write_entry
.attr
,
340 static const struct sysfs_ops integrity_ops
= {
341 .show
= &integrity_attr_show
,
342 .store
= &integrity_attr_store
,
345 static int __init
blk_dev_integrity_init(void)
347 integrity_cachep
= kmem_cache_create("blkdev_integrity",
348 sizeof(struct blk_integrity
),
349 0, SLAB_PANIC
, NULL
);
352 subsys_initcall(blk_dev_integrity_init
);
354 static void blk_integrity_release(struct kobject
*kobj
)
356 struct blk_integrity
*bi
=
357 container_of(kobj
, struct blk_integrity
, kobj
);
359 kmem_cache_free(integrity_cachep
, bi
);
362 static struct kobj_type integrity_ktype
= {
363 .default_attrs
= integrity_attrs
,
364 .sysfs_ops
= &integrity_ops
,
365 .release
= blk_integrity_release
,
368 bool blk_integrity_is_initialized(struct gendisk
*disk
)
370 struct blk_integrity
*bi
= blk_get_integrity(disk
);
372 return (bi
&& bi
->name
&& strcmp(bi
->name
, bi_unsupported_name
) != 0);
374 EXPORT_SYMBOL(blk_integrity_is_initialized
);
377 * blk_integrity_register - Register a gendisk as being integrity-capable
378 * @disk: struct gendisk pointer to make integrity-aware
379 * @template: optional integrity profile to register
381 * Description: When a device needs to advertise itself as being able
382 * to send/receive integrity metadata it must use this function to
383 * register the capability with the block layer. The template is a
384 * blk_integrity struct with values appropriate for the underlying
385 * hardware. If template is NULL the new profile is allocated but
386 * not filled out. See Documentation/block/data-integrity.txt.
388 int blk_integrity_register(struct gendisk
*disk
, struct blk_integrity
*template)
390 struct blk_integrity
*bi
;
392 BUG_ON(disk
== NULL
);
394 if (disk
->integrity
== NULL
) {
395 bi
= kmem_cache_alloc(integrity_cachep
,
396 GFP_KERNEL
| __GFP_ZERO
);
400 if (kobject_init_and_add(&bi
->kobj
, &integrity_ktype
,
401 &disk_to_dev(disk
)->kobj
,
402 "%s", "integrity")) {
403 kmem_cache_free(integrity_cachep
, bi
);
407 kobject_uevent(&bi
->kobj
, KOBJ_ADD
);
409 bi
->flags
|= INTEGRITY_FLAG_READ
| INTEGRITY_FLAG_WRITE
;
410 bi
->sector_size
= queue_logical_block_size(disk
->queue
);
411 disk
->integrity
= bi
;
413 bi
= disk
->integrity
;
415 /* Use the provided profile as template */
416 if (template != NULL
) {
417 bi
->name
= template->name
;
418 bi
->generate_fn
= template->generate_fn
;
419 bi
->verify_fn
= template->verify_fn
;
420 bi
->tuple_size
= template->tuple_size
;
421 bi
->set_tag_fn
= template->set_tag_fn
;
422 bi
->get_tag_fn
= template->get_tag_fn
;
423 bi
->tag_size
= template->tag_size
;
425 bi
->name
= bi_unsupported_name
;
427 disk
->queue
->backing_dev_info
.capabilities
|= BDI_CAP_STABLE_WRITES
;
431 EXPORT_SYMBOL(blk_integrity_register
);
434 * blk_integrity_unregister - Remove block integrity profile
435 * @disk: disk whose integrity profile to deallocate
437 * Description: This function frees all memory used by the block
438 * integrity profile. To be called at device teardown.
440 void blk_integrity_unregister(struct gendisk
*disk
)
442 struct blk_integrity
*bi
;
444 if (!disk
|| !disk
->integrity
)
447 disk
->queue
->backing_dev_info
.capabilities
&= ~BDI_CAP_STABLE_WRITES
;
449 bi
= disk
->integrity
;
451 kobject_uevent(&bi
->kobj
, KOBJ_REMOVE
);
452 kobject_del(&bi
->kobj
);
453 kobject_put(&bi
->kobj
);
454 disk
->integrity
= NULL
;
456 EXPORT_SYMBOL(blk_integrity_unregister
);