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