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