Skip to content

Commit 9eb22f7

Browse files
Anuj Guptabrauner
authored andcommitted
fs: add ioctl to query metadata and protection info capabilities
Add a new ioctl, FS_IOC_GETLBMD_CAP, to query metadata and protection info (PI) capabilities. This ioctl returns information about the files integrity profile. This is useful for userspace applications to understand a files end-to-end data protection support and configure the I/O accordingly. For now this interface is only supported by block devices. However the design and placement of this ioctl in generic FS ioctl space allows us to extend it to work over files as well. This maybe useful when filesystems start supporting PI-aware layouts. A new structure struct logical_block_metadata_cap is introduced, which contains the following fields: 1. lbmd_flags: bitmask of logical block metadata capability flags 2. lbmd_interval: the amount of data described by each unit of logical block metadata 3. lbmd_size: size in bytes of the logical block metadata associated with each interval 4. lbmd_opaque_size: size in bytes of the opaque block tag associated with each interval 5. lbmd_opaque_offset: offset in bytes of the opaque block tag within the logical block metadata 6. lbmd_pi_size: size in bytes of the T10 PI tuple associated with each interval 7. lbmd_pi_offset: offset in bytes of T10 PI tuple within the logical block metadata 8. lbmd_pi_guard_tag_type: T10 PI guard tag type 9. lbmd_pi_app_tag_size: size in bytes of the T10 PI application tag 10. lbmd_pi_ref_tag_size: size in bytes of the T10 PI reference tag 11. lbmd_pi_storage_tag_size: size in bytes of the T10 PI storage tag The internal logic to fetch the capability is encapsulated in a helper function blk_get_meta_cap(), which uses the blk_integrity profile associated with the device. The ioctl returns -EOPNOTSUPP, if CONFIG_BLK_DEV_INTEGRITY is not enabled. Suggested-by: Martin K. Petersen <[email protected]> Signed-off-by: Anuj Gupta <[email protected]> Signed-off-by: Kanchan Joshi <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Martin K. Petersen <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent f3ee506 commit 9eb22f7

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

block/blk-integrity.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/scatterlist.h>
1414
#include <linux/export.h>
1515
#include <linux/slab.h>
16+
#include <linux/t10-pi.h>
1617

1718
#include "blk.h"
1819

@@ -54,6 +55,57 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
5455
return segments;
5556
}
5657

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 (!argp)
66+
return -EINVAL;
67+
if (usize < LBMD_SIZE_VER0)
68+
return -EINVAL;
69+
if (!bi)
70+
goto out;
71+
72+
if (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE)
73+
meta_cap.lbmd_flags |= LBMD_PI_CAP_INTEGRITY;
74+
if (bi->flags & BLK_INTEGRITY_REF_TAG)
75+
meta_cap.lbmd_flags |= LBMD_PI_CAP_REFTAG;
76+
meta_cap.lbmd_interval = 1 << bi->interval_exp;
77+
meta_cap.lbmd_size = bi->metadata_size;
78+
meta_cap.lbmd_pi_size = bi->pi_tuple_size;
79+
meta_cap.lbmd_pi_offset = bi->pi_offset;
80+
meta_cap.lbmd_opaque_size = bi->metadata_size - bi->pi_tuple_size;
81+
if (meta_cap.lbmd_opaque_size && !bi->pi_offset)
82+
meta_cap.lbmd_opaque_offset = bi->pi_tuple_size;
83+
84+
meta_cap.lbmd_guard_tag_type = bi->csum_type;
85+
if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE)
86+
meta_cap.lbmd_app_tag_size = 2;
87+
88+
if (bi->flags & BLK_INTEGRITY_REF_TAG) {
89+
switch (bi->csum_type) {
90+
case BLK_INTEGRITY_CSUM_CRC64:
91+
meta_cap.lbmd_ref_tag_size =
92+
sizeof_field(struct crc64_pi_tuple, ref_tag);
93+
break;
94+
case BLK_INTEGRITY_CSUM_CRC:
95+
case BLK_INTEGRITY_CSUM_IP:
96+
meta_cap.lbmd_ref_tag_size =
97+
sizeof_field(struct t10_pi_tuple, ref_tag);
98+
break;
99+
default:
100+
break;
101+
}
102+
}
103+
104+
out:
105+
return copy_struct_to_user(argp, usize, &meta_cap, sizeof(meta_cap),
106+
NULL);
107+
}
108+
57109
/**
58110
* blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
59111
* @rq: request to map

block/ioctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/uaccess.h>
1414
#include <linux/pagemap.h>
1515
#include <linux/io_uring/cmd.h>
16+
#include <linux/blk-integrity.h>
1617
#include <uapi/linux/blkdev.h>
1718
#include "blk.h"
1819
#include "blk-crypto-internal.h"
@@ -566,6 +567,9 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
566567
{
567568
unsigned int max_sectors;
568569

570+
if (_IOC_NR(cmd) == _IOC_NR(FS_IOC_GETLBMD_CAP))
571+
return blk_get_meta_cap(bdev, cmd, argp);
572+
569573
switch (cmd) {
570574
case BLKFLSBUF:
571575
return blkdev_flushbuf(bdev, cmd, arg);

include/linux/blk-integrity.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int blk_rq_map_integrity_sg(struct request *, struct scatterlist *);
2929
int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
3030
int blk_rq_integrity_map_user(struct request *rq, void __user *ubuf,
3131
ssize_t bytes);
32+
int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
33+
struct logical_block_metadata_cap __user *argp);
3234

3335
static inline bool
3436
blk_integrity_queue_supports_integrity(struct request_queue *q)
@@ -92,6 +94,11 @@ static inline struct bio_vec rq_integrity_vec(struct request *rq)
9294
rq->bio->bi_integrity->bip_iter);
9395
}
9496
#else /* CONFIG_BLK_DEV_INTEGRITY */
97+
static inline int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
98+
struct logical_block_metadata_cap __user *argp)
99+
{
100+
return -EOPNOTSUPP;
101+
}
95102
static inline int blk_rq_count_integrity_sg(struct request_queue *q,
96103
struct bio *b)
97104
{

include/uapi/linux/fs.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,63 @@ struct fs_sysfs_path {
9191
__u8 name[128];
9292
};
9393

94+
/* Protection info capability flags */
95+
#define LBMD_PI_CAP_INTEGRITY (1 << 0)
96+
#define LBMD_PI_CAP_REFTAG (1 << 1)
97+
98+
/* Checksum types for Protection Information */
99+
#define LBMD_PI_CSUM_NONE 0
100+
#define LBMD_PI_CSUM_IP 1
101+
#define LBMD_PI_CSUM_CRC16_T10DIF 2
102+
#define LBMD_PI_CSUM_CRC64_NVME 4
103+
104+
/* sizeof first published struct */
105+
#define LBMD_SIZE_VER0 16
106+
107+
/*
108+
* Logical block metadata capability descriptor
109+
* If the device does not support metadata, all the fields will be zero.
110+
* Applications must check lbmd_flags to determine whether metadata is
111+
* supported or not.
112+
*/
113+
struct logical_block_metadata_cap {
114+
/* Bitmask of logical block metadata capability flags */
115+
__u32 lbmd_flags;
116+
/*
117+
* The amount of data described by each unit of logical block
118+
* metadata
119+
*/
120+
__u16 lbmd_interval;
121+
/*
122+
* Size in bytes of the logical block metadata associated with each
123+
* interval
124+
*/
125+
__u8 lbmd_size;
126+
/*
127+
* Size in bytes of the opaque block tag associated with each
128+
* interval
129+
*/
130+
__u8 lbmd_opaque_size;
131+
/*
132+
* Offset in bytes of the opaque block tag within the logical block
133+
* metadata
134+
*/
135+
__u8 lbmd_opaque_offset;
136+
/* Size in bytes of the T10 PI tuple associated with each interval */
137+
__u8 lbmd_pi_size;
138+
/* Offset in bytes of T10 PI tuple within the logical block metadata */
139+
__u8 lbmd_pi_offset;
140+
/* T10 PI guard tag type */
141+
__u8 lbmd_guard_tag_type;
142+
/* Size in bytes of the T10 PI application tag */
143+
__u8 lbmd_app_tag_size;
144+
/* Size in bytes of the T10 PI reference tag */
145+
__u8 lbmd_ref_tag_size;
146+
/* Size in bytes of the T10 PI storage tag */
147+
__u8 lbmd_storage_tag_size;
148+
__u8 pad;
149+
};
150+
94151
/* extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions */
95152
#define FILE_DEDUPE_RANGE_SAME 0
96153
#define FILE_DEDUPE_RANGE_DIFFERS 1
@@ -247,6 +304,8 @@ struct fsxattr {
247304
* also /sys/kernel/debug/ for filesystems with debugfs exports
248305
*/
249306
#define FS_IOC_GETFSSYSFSPATH _IOR(0x15, 1, struct fs_sysfs_path)
307+
/* Get logical block metadata capability details */
308+
#define FS_IOC_GETLBMD_CAP _IOWR(0x15, 2, struct logical_block_metadata_cap)
250309

251310
/*
252311
* Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)

0 commit comments

Comments
 (0)