1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef USER_BLK_DRV_CMD_INC_H
3 #define USER_BLK_DRV_CMD_INC_H
5 #include <linux/types.h>
7 /* ublk server command definition */
10 * Admin commands, issued by ublk server, and handled by ublk driver.
12 * Legacy command definition, don't use in new application, and don't
13 * add new such definition any more
15 #define UBLK_CMD_GET_QUEUE_AFFINITY 0x01
16 #define UBLK_CMD_GET_DEV_INFO 0x02
17 #define UBLK_CMD_ADD_DEV 0x04
18 #define UBLK_CMD_DEL_DEV 0x05
19 #define UBLK_CMD_START_DEV 0x06
20 #define UBLK_CMD_STOP_DEV 0x07
21 #define UBLK_CMD_SET_PARAMS 0x08
22 #define UBLK_CMD_GET_PARAMS 0x09
23 #define UBLK_CMD_START_USER_RECOVERY 0x10
24 #define UBLK_CMD_END_USER_RECOVERY 0x11
25 #define UBLK_CMD_GET_DEV_INFO2 0x12
27 /* Any new ctrl command should encode by __IO*() */
28 #define UBLK_U_CMD_GET_QUEUE_AFFINITY \
29 _IOR('u', UBLK_CMD_GET_QUEUE_AFFINITY, struct ublksrv_ctrl_cmd)
30 #define UBLK_U_CMD_GET_DEV_INFO \
31 _IOR('u', UBLK_CMD_GET_DEV_INFO, struct ublksrv_ctrl_cmd)
32 #define UBLK_U_CMD_ADD_DEV \
33 _IOWR('u', UBLK_CMD_ADD_DEV, struct ublksrv_ctrl_cmd)
34 #define UBLK_U_CMD_DEL_DEV \
35 _IOWR('u', UBLK_CMD_DEL_DEV, struct ublksrv_ctrl_cmd)
36 #define UBLK_U_CMD_START_DEV \
37 _IOWR('u', UBLK_CMD_START_DEV, struct ublksrv_ctrl_cmd)
38 #define UBLK_U_CMD_STOP_DEV \
39 _IOWR('u', UBLK_CMD_STOP_DEV, struct ublksrv_ctrl_cmd)
40 #define UBLK_U_CMD_SET_PARAMS \
41 _IOWR('u', UBLK_CMD_SET_PARAMS, struct ublksrv_ctrl_cmd)
42 #define UBLK_U_CMD_GET_PARAMS \
43 _IOR('u', UBLK_CMD_GET_PARAMS, struct ublksrv_ctrl_cmd)
44 #define UBLK_U_CMD_START_USER_RECOVERY \
45 _IOWR('u', UBLK_CMD_START_USER_RECOVERY, struct ublksrv_ctrl_cmd)
46 #define UBLK_U_CMD_END_USER_RECOVERY \
47 _IOWR('u', UBLK_CMD_END_USER_RECOVERY, struct ublksrv_ctrl_cmd)
48 #define UBLK_U_CMD_GET_DEV_INFO2 \
49 _IOR('u', UBLK_CMD_GET_DEV_INFO2, struct ublksrv_ctrl_cmd)
50 #define UBLK_U_CMD_GET_FEATURES \
51 _IOR('u', 0x13, struct ublksrv_ctrl_cmd)
52 #define UBLK_U_CMD_DEL_DEV_ASYNC \
53 _IOR('u', 0x14, struct ublksrv_ctrl_cmd)
56 * 64bits are enough now, and it should be easy to extend in case of
57 * running out of feature flags
59 #define UBLK_FEATURES_LEN 8
62 * IO commands, issued by ublk server, and handled by ublk driver.
64 * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request
65 * from ublk driver, should be issued only when starting device. After
66 * the associated cqe is returned, request's tag can be retrieved via
69 * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled
70 * this IO request, request's handling result is committed to ublk
71 * driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be
72 * handled before completing io request.
74 * NEED_GET_DATA: only used for write requests to set io addr and copy data
75 * When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA
76 * command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA.
78 * It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag
79 * while starting a ublk device.
83 * Legacy IO command definition, don't use in new application, and don't
84 * add new such definition any more
86 #define UBLK_IO_FETCH_REQ 0x20
87 #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21
88 #define UBLK_IO_NEED_GET_DATA 0x22
90 /* Any new IO command should encode by __IOWR() */
91 #define UBLK_U_IO_FETCH_REQ \
92 _IOWR('u', UBLK_IO_FETCH_REQ, struct ublksrv_io_cmd)
93 #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \
94 _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd)
95 #define UBLK_U_IO_NEED_GET_DATA \
96 _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd)
98 /* only ABORT means that no re-fetch */
99 #define UBLK_IO_RES_OK 0
100 #define UBLK_IO_RES_NEED_GET_DATA 1
101 #define UBLK_IO_RES_ABORT (-ENODEV)
103 #define UBLKSRV_CMD_BUF_OFFSET 0
104 #define UBLKSRV_IO_BUF_OFFSET 0x80000000
106 /* tag bit is 16bit, so far limit at most 4096 IOs for each queue */
107 #define UBLK_MAX_QUEUE_DEPTH 4096
109 /* single IO buffer max size is 32MB */
110 #define UBLK_IO_BUF_OFF 0
111 #define UBLK_IO_BUF_BITS 25
112 #define UBLK_IO_BUF_BITS_MASK ((1ULL << UBLK_IO_BUF_BITS) - 1)
114 /* so at most 64K IOs for each queue */
115 #define UBLK_TAG_OFF UBLK_IO_BUF_BITS
116 #define UBLK_TAG_BITS 16
117 #define UBLK_TAG_BITS_MASK ((1ULL << UBLK_TAG_BITS) - 1)
119 /* max 4096 queues */
120 #define UBLK_QID_OFF (UBLK_TAG_OFF + UBLK_TAG_BITS)
121 #define UBLK_QID_BITS 12
122 #define UBLK_QID_BITS_MASK ((1ULL << UBLK_QID_BITS) - 1)
124 #define UBLK_MAX_NR_QUEUES (1U << UBLK_QID_BITS)
126 #define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS)
127 #define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS)
130 * zero copy requires 4k block size, and can remap ublk driver's io
131 * request into ublksrv's vm space
133 #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0)
136 * Force to complete io cmd via io_uring_cmd_complete_in_task so that
137 * performance comparison is done easily with using task_work_add
139 #define UBLK_F_URING_CMD_COMP_IN_TASK (1ULL << 1)
142 * User should issue io cmd again for write requests to
143 * set io buffer address and copy data from bio vectors
144 * to the userspace io buffer.
146 * In this mode, task_work is not used.
148 #define UBLK_F_NEED_GET_DATA (1UL << 2)
150 #define UBLK_F_USER_RECOVERY (1UL << 3)
152 #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4)
155 * Unprivileged user can create /dev/ublkcN and /dev/ublkbN.
157 * /dev/ublk-control needs to be available for unprivileged user, and it
158 * can be done via udev rule to make all control commands available to
159 * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all
160 * other commands are only allowed for the owner of the specified device.
162 * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and
163 * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only
164 * the current user's uid/gid is stored, that said owner of the created
165 * device is always the current user.
167 * We still need udev rule to apply OWNER/GROUP with the stored owner_uid
170 * Then ublk server can be run as unprivileged user, and /dev/ublkbN can
171 * be accessed and managed by its owner represented by owner_uid/owner_gid.
173 #define UBLK_F_UNPRIVILEGED_DEV (1UL << 5)
175 /* use ioctl encoding for uring command */
176 #define UBLK_F_CMD_IOCTL_ENCODE (1UL << 6)
178 /* Copy between request and user buffer by pread()/pwrite() */
179 #define UBLK_F_USER_COPY (1UL << 7)
182 * User space sets this flag when setting up the device to request zoned storage support. Kernel may
183 * deny the request by returning an error.
185 #define UBLK_F_ZONED (1ULL << 8)
188 #define UBLK_S_DEV_DEAD 0
189 #define UBLK_S_DEV_LIVE 1
190 #define UBLK_S_DEV_QUIESCED 2
192 /* shipped via sqe->cmd of io_uring command */
193 struct ublksrv_ctrl_cmd {
194 /* sent to which device, must be valid */
197 /* sent to which queue, must be -1 if the cmd isn't for queue */
200 * cmd specific buffer, can be IN or OUT.
209 * Used for UBLK_F_UNPRIVILEGED_DEV and UBLK_CMD_GET_DEV_INFO2
210 * only, include null char
217 struct ublksrv_ctrl_dev_info {
223 __u32 max_io_buf_bytes;
231 /* For ublksrv internal use, invisible to ublk driver */
234 __u32 owner_uid; /* store by kernel */
235 __u32 owner_gid; /* store by kernel */
240 #define UBLK_IO_OP_READ 0
241 #define UBLK_IO_OP_WRITE 1
242 #define UBLK_IO_OP_FLUSH 2
243 #define UBLK_IO_OP_DISCARD 3
244 #define UBLK_IO_OP_WRITE_SAME 4
245 #define UBLK_IO_OP_WRITE_ZEROES 5
246 #define UBLK_IO_OP_ZONE_OPEN 10
247 #define UBLK_IO_OP_ZONE_CLOSE 11
248 #define UBLK_IO_OP_ZONE_FINISH 12
249 #define UBLK_IO_OP_ZONE_APPEND 13
250 #define UBLK_IO_OP_ZONE_RESET_ALL 14
251 #define UBLK_IO_OP_ZONE_RESET 15
253 * Construct a zone report. The report request is carried in `struct
254 * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone
255 * and shall indicate the first zone of the report. The `nr_zones` shall
256 * indicate how many zones should be reported at most. The report shall be
257 * delivered as a `struct blk_zone` array. To report fewer zones than requested,
258 * zero the last entry of the returned array.
260 * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in
261 * include/uapi/linux/blkzoned.h are part of ublk UAPI.
263 #define UBLK_IO_OP_REPORT_ZONES 18
265 #define UBLK_IO_F_FAILFAST_DEV (1U << 8)
266 #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9)
267 #define UBLK_IO_F_FAILFAST_DRIVER (1U << 10)
268 #define UBLK_IO_F_META (1U << 11)
269 #define UBLK_IO_F_FUA (1U << 13)
270 #define UBLK_IO_F_NOUNMAP (1U << 15)
271 #define UBLK_IO_F_SWAP (1U << 16)
274 * io cmd is described by this structure, and stored in share memory, indexed
277 * The data is stored by ublk driver, and read by ublksrv after one fetch command
280 struct ublksrv_io_desc {
281 /* op: bit 0-7, flags: bit 8-31 */
286 __u32 nr_zones; /* for UBLK_IO_OP_REPORT_ZONES */
289 /* start sector for this io */
292 /* buffer address in ublksrv daemon vm space, from ublk driver */
296 static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
298 return iod->op_flags & 0xff;
301 static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
303 return iod->op_flags >> 8;
306 /* issued to ublk driver via /dev/ublkcN */
307 struct ublksrv_io_cmd {
310 /* for fetch/commit which result */
313 /* io result, it is valid for COMMIT* command only */
318 * userspace buffer address in ublksrv daemon process, valid for
319 * FETCH* command only
321 * `addr` should not be used when UBLK_F_USER_COPY is enabled,
322 * because userspace handles data copy by pread()/pwrite() over
323 * /dev/ublkcN. But in case of UBLK_F_ZONED, this union is
324 * re-used to pass back the allocated LBA for
325 * UBLK_IO_OP_ZONE_APPEND which actually depends on
329 __u64 zone_append_lba;
333 struct ublk_param_basic {
334 #define UBLK_ATTR_READ_ONLY (1 << 0)
335 #define UBLK_ATTR_ROTATIONAL (1 << 1)
336 #define UBLK_ATTR_VOLATILE_CACHE (1 << 2)
337 #define UBLK_ATTR_FUA (1 << 3)
339 __u8 logical_bs_shift;
340 __u8 physical_bs_shift;
348 __u64 virt_boundary_mask;
351 struct ublk_param_discard {
352 __u32 discard_alignment;
354 __u32 discard_granularity;
355 __u32 max_discard_sectors;
357 __u32 max_write_zeroes_sectors;
358 __u16 max_discard_segments;
363 * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available
364 * after device is started
366 struct ublk_param_devt {
373 struct ublk_param_zoned {
374 __u32 max_open_zones;
375 __u32 max_active_zones;
376 __u32 max_zone_append_sectors;
382 * Total length of parameters, userspace has to set 'len' for both
383 * SET_PARAMS and GET_PARAMS command, and driver may update len
384 * if two sides use different version of 'ublk_params', same with
388 #define UBLK_PARAM_TYPE_BASIC (1 << 0)
389 #define UBLK_PARAM_TYPE_DISCARD (1 << 1)
390 #define UBLK_PARAM_TYPE_DEVT (1 << 2)
391 #define UBLK_PARAM_TYPE_ZONED (1 << 3)
392 __u32 types; /* types of parameter included */
394 struct ublk_param_basic basic;
395 struct ublk_param_discard discard;
396 struct ublk_param_devt devt;
397 struct ublk_param_zoned zoned;