|
13 | 13 | #include <linux/scatterlist.h> |
14 | 14 | #include <linux/export.h> |
15 | 15 | #include <linux/slab.h> |
| 16 | +#include <linux/t10-pi.h> |
16 | 17 |
|
17 | 18 | #include "blk.h" |
18 | 19 |
|
@@ -54,6 +55,73 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio) |
54 | 55 | return segments; |
55 | 56 | } |
56 | 57 |
|
| 58 | +int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd, |
| 59 | + struct logical_block_metadata_cap __user *argp) |
| 60 | +{ |
| 61 | + struct blk_integrity *bi = blk_get_integrity(bdev->bd_disk); |
| 62 | + struct logical_block_metadata_cap meta_cap = {}; |
| 63 | + size_t usize = _IOC_SIZE(cmd); |
| 64 | + |
| 65 | + if (_IOC_DIR(cmd) != _IOC_DIR(FS_IOC_GETLBMD_CAP) || |
| 66 | + _IOC_TYPE(cmd) != _IOC_TYPE(FS_IOC_GETLBMD_CAP) || |
| 67 | + _IOC_NR(cmd) != _IOC_NR(FS_IOC_GETLBMD_CAP) || |
| 68 | + _IOC_SIZE(cmd) < LBMD_SIZE_VER0) |
| 69 | + return -ENOIOCTLCMD; |
| 70 | + |
| 71 | + if (!bi) |
| 72 | + goto out; |
| 73 | + |
| 74 | + if (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) |
| 75 | + meta_cap.lbmd_flags |= LBMD_PI_CAP_INTEGRITY; |
| 76 | + if (bi->flags & BLK_INTEGRITY_REF_TAG) |
| 77 | + meta_cap.lbmd_flags |= LBMD_PI_CAP_REFTAG; |
| 78 | + meta_cap.lbmd_interval = 1 << bi->interval_exp; |
| 79 | + meta_cap.lbmd_size = bi->metadata_size; |
| 80 | + meta_cap.lbmd_pi_size = bi->pi_tuple_size; |
| 81 | + meta_cap.lbmd_pi_offset = bi->pi_offset; |
| 82 | + meta_cap.lbmd_opaque_size = bi->metadata_size - bi->pi_tuple_size; |
| 83 | + if (meta_cap.lbmd_opaque_size && !bi->pi_offset) |
| 84 | + meta_cap.lbmd_opaque_offset = bi->pi_tuple_size; |
| 85 | + |
| 86 | + switch (bi->csum_type) { |
| 87 | + case BLK_INTEGRITY_CSUM_NONE: |
| 88 | + meta_cap.lbmd_guard_tag_type = LBMD_PI_CSUM_NONE; |
| 89 | + break; |
| 90 | + case BLK_INTEGRITY_CSUM_IP: |
| 91 | + meta_cap.lbmd_guard_tag_type = LBMD_PI_CSUM_IP; |
| 92 | + break; |
| 93 | + case BLK_INTEGRITY_CSUM_CRC: |
| 94 | + meta_cap.lbmd_guard_tag_type = LBMD_PI_CSUM_CRC16_T10DIF; |
| 95 | + break; |
| 96 | + case BLK_INTEGRITY_CSUM_CRC64: |
| 97 | + meta_cap.lbmd_guard_tag_type = LBMD_PI_CSUM_CRC64_NVME; |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE) |
| 102 | + meta_cap.lbmd_app_tag_size = 2; |
| 103 | + |
| 104 | + if (bi->flags & BLK_INTEGRITY_REF_TAG) { |
| 105 | + switch (bi->csum_type) { |
| 106 | + case BLK_INTEGRITY_CSUM_CRC64: |
| 107 | + meta_cap.lbmd_ref_tag_size = |
| 108 | + sizeof_field(struct crc64_pi_tuple, ref_tag); |
| 109 | + break; |
| 110 | + case BLK_INTEGRITY_CSUM_CRC: |
| 111 | + case BLK_INTEGRITY_CSUM_IP: |
| 112 | + meta_cap.lbmd_ref_tag_size = |
| 113 | + sizeof_field(struct t10_pi_tuple, ref_tag); |
| 114 | + break; |
| 115 | + default: |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +out: |
| 121 | + return copy_struct_to_user(argp, usize, &meta_cap, sizeof(meta_cap), |
| 122 | + NULL); |
| 123 | +} |
| 124 | + |
57 | 125 | /** |
58 | 126 | * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist |
59 | 127 | * @rq: request to map |
@@ -239,7 +307,7 @@ static ssize_t format_show(struct device *dev, struct device_attribute *attr, |
239 | 307 | { |
240 | 308 | struct blk_integrity *bi = dev_to_bi(dev); |
241 | 309 |
|
242 | | - if (!bi->tuple_size) |
| 310 | + if (!bi->metadata_size) |
243 | 311 | return sysfs_emit(page, "none\n"); |
244 | 312 | return sysfs_emit(page, "%s\n", blk_integrity_profile_name(bi)); |
245 | 313 | } |
|
0 commit comments