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
4
5 #include <linux/types.h>
6
7 /* ublk server command definition */
8
9 /*
10 * Admin commands, issued by ublk server, and handled by ublk driver.
11 *
12 * Legacy command definition, don't use in new application, and don't
13 * add new such definition any more
14 */
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
26
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
53 /*
54 * 64bits are enough now, and it should be easy to extend in case of
55 * running out of feature flags
56 */
57 #define UBLK_FEATURES_LEN 8
58
59 /*
60 * IO commands, issued by ublk server, and handled by ublk driver.
61 *
62 * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request
63 * from ublk driver, should be issued only when starting device. After
64 * the associated cqe is returned, request's tag can be retrieved via
65 * cqe->userdata.
66 *
67 * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled
68 * this IO request, request's handling result is committed to ublk
69 * driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be
70 * handled before completing io request.
71 *
72 * NEED_GET_DATA: only used for write requests to set io addr and copy data
73 * When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA
74 * command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA.
75 *
76 * It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag
77 * while starting a ublk device.
78 */
79
80 /*
81 * Legacy IO command definition, don't use in new application, and don't
82 * add new such definition any more
83 */
84 #define UBLK_IO_FETCH_REQ 0x20
85 #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21
86 #define UBLK_IO_NEED_GET_DATA 0x22
87
88 /* Any new IO command should encode by __IOWR() */
89 #define UBLK_U_IO_FETCH_REQ \
90 _IOWR('u', UBLK_IO_FETCH_REQ, struct ublksrv_io_cmd)
91 #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \
92 _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd)
93 #define UBLK_U_IO_NEED_GET_DATA \
94 _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd)
95
96 /* only ABORT means that no re-fetch */
97 #define UBLK_IO_RES_OK 0
98 #define UBLK_IO_RES_NEED_GET_DATA 1
99 #define UBLK_IO_RES_ABORT (-ENODEV)
100
101 #define UBLKSRV_CMD_BUF_OFFSET 0
102 #define UBLKSRV_IO_BUF_OFFSET 0x80000000
103
104 /* tag bit is 16bit, so far limit at most 4096 IOs for each queue */
105 #define UBLK_MAX_QUEUE_DEPTH 4096
106
107 /* single IO buffer max size is 32MB */
108 #define UBLK_IO_BUF_OFF 0
109 #define UBLK_IO_BUF_BITS 25
110 #define UBLK_IO_BUF_BITS_MASK ((1ULL << UBLK_IO_BUF_BITS) - 1)
111
112 /* so at most 64K IOs for each queue */
113 #define UBLK_TAG_OFF UBLK_IO_BUF_BITS
114 #define UBLK_TAG_BITS 16
115 #define UBLK_TAG_BITS_MASK ((1ULL << UBLK_TAG_BITS) - 1)
116
117 /* max 4096 queues */
118 #define UBLK_QID_OFF (UBLK_TAG_OFF + UBLK_TAG_BITS)
119 #define UBLK_QID_BITS 12
120 #define UBLK_QID_BITS_MASK ((1ULL << UBLK_QID_BITS) - 1)
121
122 #define UBLK_MAX_NR_QUEUES (1U << UBLK_QID_BITS)
123
124 #define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS)
125 #define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS)
126
127 /*
128 * zero copy requires 4k block size, and can remap ublk driver's io
129 * request into ublksrv's vm space
130 */
131 #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0)
132
133 /*
134 * Force to complete io cmd via io_uring_cmd_complete_in_task so that
135 * performance comparison is done easily with using task_work_add
136 */
137 #define UBLK_F_URING_CMD_COMP_IN_TASK (1ULL << 1)
138
139 /*
140 * User should issue io cmd again for write requests to
141 * set io buffer address and copy data from bio vectors
142 * to the userspace io buffer.
143 *
144 * In this mode, task_work is not used.
145 */
146 #define UBLK_F_NEED_GET_DATA (1UL << 2)
147
148 #define UBLK_F_USER_RECOVERY (1UL << 3)
149
150 #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4)
151
152 /*
153 * Unprivileged user can create /dev/ublkcN and /dev/ublkbN.
154 *
155 * /dev/ublk-control needs to be available for unprivileged user, and it
156 * can be done via udev rule to make all control commands available to
157 * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all
158 * other commands are only allowed for the owner of the specified device.
159 *
160 * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and
161 * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only
162 * the current user's uid/gid is stored, that said owner of the created
163 * device is always the current user.
164 *
165 * We still need udev rule to apply OWNER/GROUP with the stored owner_uid
166 * and owner_gid.
167 *
168 * Then ublk server can be run as unprivileged user, and /dev/ublkbN can
169 * be accessed and managed by its owner represented by owner_uid/owner_gid.
170 */
171 #define UBLK_F_UNPRIVILEGED_DEV (1UL << 5)
172
173 /* use ioctl encoding for uring command */
174 #define UBLK_F_CMD_IOCTL_ENCODE (1UL << 6)
175
176 /*
177 * Copy between request and user buffer by pread()/pwrite()
178 *
179 * Not available for UBLK_F_UNPRIVILEGED_DEV, otherwise userspace may
180 * deceive us by not filling request buffer, then kernel uninitialized
181 * data may be leaked.
182 */
183 #define UBLK_F_USER_COPY (1UL << 7)
184
185 /*
186 * User space sets this flag when setting up the device to request zoned storage support. Kernel may
187 * deny the request by returning an error.
188 */
189 #define UBLK_F_ZONED (1ULL << 8)
190
191 /* device state */
192 #define UBLK_S_DEV_DEAD 0
193 #define UBLK_S_DEV_LIVE 1
194 #define UBLK_S_DEV_QUIESCED 2
195
196 /* shipped via sqe->cmd of io_uring command */
197 struct ublksrv_ctrl_cmd {
198 /* sent to which device, must be valid */
199 __u32 dev_id;
200
201 /* sent to which queue, must be -1 if the cmd isn't for queue */
202 __u16 queue_id;
203 /*
204 * cmd specific buffer, can be IN or OUT.
205 */
206 __u16 len;
207 __u64 addr;
208
209 /* inline data */
210 __u64 data[1];
211
212 /*
213 * Used for UBLK_F_UNPRIVILEGED_DEV and UBLK_CMD_GET_DEV_INFO2
214 * only, include null char
215 */
216 __u16 dev_path_len;
217 __u16 pad;
218 __u32 reserved;
219 };
220
221 struct ublksrv_ctrl_dev_info {
222 __u16 nr_hw_queues;
223 __u16 queue_depth;
224 __u16 state;
225 __u16 pad0;
226
227 __u32 max_io_buf_bytes;
228 __u32 dev_id;
229
230 __s32 ublksrv_pid;
231 __u32 pad1;
232
233 __u64 flags;
234
235 /* For ublksrv internal use, invisible to ublk driver */
236 __u64 ublksrv_flags;
237
238 __u32 owner_uid; /* store by kernel */
239 __u32 owner_gid; /* store by kernel */
240 __u64 reserved1;
241 __u64 reserved2;
242 };
243
244 #define UBLK_IO_OP_READ 0
245 #define UBLK_IO_OP_WRITE 1
246 #define UBLK_IO_OP_FLUSH 2
247 #define UBLK_IO_OP_DISCARD 3
248 #define UBLK_IO_OP_WRITE_SAME 4
249 #define UBLK_IO_OP_WRITE_ZEROES 5
250 #define UBLK_IO_OP_ZONE_OPEN 10
251 #define UBLK_IO_OP_ZONE_CLOSE 11
252 #define UBLK_IO_OP_ZONE_FINISH 12
253 #define UBLK_IO_OP_ZONE_APPEND 13
254 #define UBLK_IO_OP_ZONE_RESET_ALL 14
255 #define UBLK_IO_OP_ZONE_RESET 15
256 /*
257 * Construct a zone report. The report request is carried in `struct
258 * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone
259 * and shall indicate the first zone of the report. The `nr_zones` shall
260 * indicate how many zones should be reported at most. The report shall be
261 * delivered as a `struct blk_zone` array. To report fewer zones than requested,
262 * zero the last entry of the returned array.
263 *
264 * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in
265 * include/uapi/linux/blkzoned.h are part of ublk UAPI.
266 */
267 #define UBLK_IO_OP_REPORT_ZONES 18
268
269 #define UBLK_IO_F_FAILFAST_DEV (1U << 8)
270 #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9)
271 #define UBLK_IO_F_FAILFAST_DRIVER (1U << 10)
272 #define UBLK_IO_F_META (1U << 11)
273 #define UBLK_IO_F_FUA (1U << 13)
274 #define UBLK_IO_F_NOUNMAP (1U << 15)
275 #define UBLK_IO_F_SWAP (1U << 16)
276
277 /*
278 * io cmd is described by this structure, and stored in share memory, indexed
279 * by request tag.
280 *
281 * The data is stored by ublk driver, and read by ublksrv after one fetch command
282 * returns.
283 */
284 struct ublksrv_io_desc {
285 /* op: bit 0-7, flags: bit 8-31 */
286 __u32 op_flags;
287
288 union {
289 __u32 nr_sectors;
290 __u32 nr_zones; /* for UBLK_IO_OP_REPORT_ZONES */
291 };
292
293 /* start sector for this io */
294 __u64 start_sector;
295
296 /* buffer address in ublksrv daemon vm space, from ublk driver */
297 __u64 addr;
298 };
299
ublksrv_get_op(const struct ublksrv_io_desc * iod)300 static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
301 {
302 return iod->op_flags & 0xff;
303 }
304
ublksrv_get_flags(const struct ublksrv_io_desc * iod)305 static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
306 {
307 return iod->op_flags >> 8;
308 }
309
310 /* issued to ublk driver via /dev/ublkcN */
311 struct ublksrv_io_cmd {
312 __u16 q_id;
313
314 /* for fetch/commit which result */
315 __u16 tag;
316
317 /* io result, it is valid for COMMIT* command only */
318 __s32 result;
319
320 union {
321 /*
322 * userspace buffer address in ublksrv daemon process, valid for
323 * FETCH* command only
324 *
325 * `addr` should not be used when UBLK_F_USER_COPY is enabled,
326 * because userspace handles data copy by pread()/pwrite() over
327 * /dev/ublkcN. But in case of UBLK_F_ZONED, this union is
328 * re-used to pass back the allocated LBA for
329 * UBLK_IO_OP_ZONE_APPEND which actually depends on
330 * UBLK_F_USER_COPY
331 */
332 __u64 addr;
333 __u64 zone_append_lba;
334 };
335 };
336
337 struct ublk_param_basic {
338 #define UBLK_ATTR_READ_ONLY (1 << 0)
339 #define UBLK_ATTR_ROTATIONAL (1 << 1)
340 #define UBLK_ATTR_VOLATILE_CACHE (1 << 2)
341 #define UBLK_ATTR_FUA (1 << 3)
342 __u32 attrs;
343 __u8 logical_bs_shift;
344 __u8 physical_bs_shift;
345 __u8 io_opt_shift;
346 __u8 io_min_shift;
347
348 __u32 max_sectors;
349 __u32 chunk_sectors;
350
351 __u64 dev_sectors;
352 __u64 virt_boundary_mask;
353 };
354
355 struct ublk_param_discard {
356 __u32 discard_alignment;
357
358 __u32 discard_granularity;
359 __u32 max_discard_sectors;
360
361 __u32 max_write_zeroes_sectors;
362 __u16 max_discard_segments;
363 __u16 reserved0;
364 };
365
366 /*
367 * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available
368 * after device is started
369 */
370 struct ublk_param_devt {
371 __u32 char_major;
372 __u32 char_minor;
373 __u32 disk_major;
374 __u32 disk_minor;
375 };
376
377 struct ublk_param_zoned {
378 __u32 max_open_zones;
379 __u32 max_active_zones;
380 __u32 max_zone_append_sectors;
381 __u8 reserved[20];
382 };
383
384 struct ublk_params {
385 /*
386 * Total length of parameters, userspace has to set 'len' for both
387 * SET_PARAMS and GET_PARAMS command, and driver may update len
388 * if two sides use different version of 'ublk_params', same with
389 * 'types' fields.
390 */
391 __u32 len;
392 #define UBLK_PARAM_TYPE_BASIC (1 << 0)
393 #define UBLK_PARAM_TYPE_DISCARD (1 << 1)
394 #define UBLK_PARAM_TYPE_DEVT (1 << 2)
395 #define UBLK_PARAM_TYPE_ZONED (1 << 3)
396 __u32 types; /* types of parameter included */
397
398 struct ublk_param_basic basic;
399 struct ublk_param_discard discard;
400 struct ublk_param_devt devt;
401 struct ublk_param_zoned zoned;
402 };
403
404 #endif
405