linux/include/uapi/linux/ublk_cmd.h

/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef USER_BLK_DRV_CMD_INC_H
#define USER_BLK_DRV_CMD_INC_H

#include <linux/types.h>

/* ublk server command definition */

/*
 * Admin commands, issued by ublk server, and handled by ublk driver.
 *
 * Legacy command definition, don't use in new application, and don't
 * add new such definition any more
 */
#define UBLK_CMD_GET_QUEUE_AFFINITY
#define UBLK_CMD_GET_DEV_INFO
#define UBLK_CMD_ADD_DEV
#define UBLK_CMD_DEL_DEV
#define UBLK_CMD_START_DEV
#define UBLK_CMD_STOP_DEV
#define UBLK_CMD_SET_PARAMS
#define UBLK_CMD_GET_PARAMS
#define UBLK_CMD_START_USER_RECOVERY
#define UBLK_CMD_END_USER_RECOVERY
#define UBLK_CMD_GET_DEV_INFO2

/* Any new ctrl command should encode by __IO*() */
#define UBLK_U_CMD_GET_QUEUE_AFFINITY
#define UBLK_U_CMD_GET_DEV_INFO
#define UBLK_U_CMD_ADD_DEV
#define UBLK_U_CMD_DEL_DEV
#define UBLK_U_CMD_START_DEV
#define UBLK_U_CMD_STOP_DEV
#define UBLK_U_CMD_SET_PARAMS
#define UBLK_U_CMD_GET_PARAMS
#define UBLK_U_CMD_START_USER_RECOVERY
#define UBLK_U_CMD_END_USER_RECOVERY
#define UBLK_U_CMD_GET_DEV_INFO2
#define UBLK_U_CMD_GET_FEATURES
#define UBLK_U_CMD_DEL_DEV_ASYNC

/*
 * 64bits are enough now, and it should be easy to extend in case of
 * running out of feature flags
 */
#define UBLK_FEATURES_LEN

/*
 * IO commands, issued by ublk server, and handled by ublk driver.
 *
 * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request
 *      from ublk driver, should be issued only when starting device. After
 *      the associated cqe is returned, request's tag can be retrieved via
 *      cqe->userdata.
 *
 * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled
 *      this IO request, request's handling result is committed to ublk
 *      driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be
 *      handled before completing io request.
 *
 * NEED_GET_DATA: only used for write requests to set io addr and copy data
 *      When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA
 *      command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA.
 *
 *      It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag
 *      while starting a ublk device.
 */

/*
 * Legacy IO command definition, don't use in new application, and don't
 * add new such definition any more
 */
#define UBLK_IO_FETCH_REQ
#define UBLK_IO_COMMIT_AND_FETCH_REQ
#define UBLK_IO_NEED_GET_DATA

/* Any new IO command should encode by __IOWR() */
#define UBLK_U_IO_FETCH_REQ
#define UBLK_U_IO_COMMIT_AND_FETCH_REQ
#define UBLK_U_IO_NEED_GET_DATA

/* only ABORT means that no re-fetch */
#define UBLK_IO_RES_OK
#define UBLK_IO_RES_NEED_GET_DATA
#define UBLK_IO_RES_ABORT

#define UBLKSRV_CMD_BUF_OFFSET
#define UBLKSRV_IO_BUF_OFFSET

/* tag bit is 16bit, so far limit at most 4096 IOs for each queue */
#define UBLK_MAX_QUEUE_DEPTH

/* single IO buffer max size is 32MB */
#define UBLK_IO_BUF_OFF
#define UBLK_IO_BUF_BITS
#define UBLK_IO_BUF_BITS_MASK

/* so at most 64K IOs for each queue */
#define UBLK_TAG_OFF
#define UBLK_TAG_BITS
#define UBLK_TAG_BITS_MASK

/* max 4096 queues */
#define UBLK_QID_OFF
#define UBLK_QID_BITS
#define UBLK_QID_BITS_MASK

#define UBLK_MAX_NR_QUEUES

#define UBLKSRV_IO_BUF_TOTAL_BITS
#define UBLKSRV_IO_BUF_TOTAL_SIZE

/*
 * zero copy requires 4k block size, and can remap ublk driver's io
 * request into ublksrv's vm space
 */
#define UBLK_F_SUPPORT_ZERO_COPY

/*
 * Force to complete io cmd via io_uring_cmd_complete_in_task so that
 * performance comparison is done easily with using task_work_add
 */
#define UBLK_F_URING_CMD_COMP_IN_TASK

/*
 * User should issue io cmd again for write requests to
 * set io buffer address and copy data from bio vectors
 * to the userspace io buffer.
 *
 * In this mode, task_work is not used.
 */
#define UBLK_F_NEED_GET_DATA

#define UBLK_F_USER_RECOVERY

#define UBLK_F_USER_RECOVERY_REISSUE

/*
 * Unprivileged user can create /dev/ublkcN and /dev/ublkbN.
 *
 * /dev/ublk-control needs to be available for unprivileged user, and it
 * can be done via udev rule to make all control commands available to
 * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all
 * other commands are only allowed for the owner of the specified device.
 *
 * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and
 * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only
 * the current user's uid/gid is stored, that said owner of the created
 * device is always the current user.
 *
 * We still need udev rule to apply OWNER/GROUP with the stored owner_uid
 * and owner_gid.
 *
 * Then ublk server can be run as unprivileged user, and /dev/ublkbN can
 * be accessed and managed by its owner represented by owner_uid/owner_gid.
 */
#define UBLK_F_UNPRIVILEGED_DEV

/* use ioctl encoding for uring command */
#define UBLK_F_CMD_IOCTL_ENCODE

/*
 *  Copy between request and user buffer by pread()/pwrite()
 *
 *  Not available for UBLK_F_UNPRIVILEGED_DEV, otherwise userspace may
 *  deceive us by not filling request buffer, then kernel uninitialized
 *  data may be leaked.
 */
#define UBLK_F_USER_COPY

/*
 * User space sets this flag when setting up the device to request zoned storage support. Kernel may
 * deny the request by returning an error.
 */
#define UBLK_F_ZONED

/* device state */
#define UBLK_S_DEV_DEAD
#define UBLK_S_DEV_LIVE
#define UBLK_S_DEV_QUIESCED

/* shipped via sqe->cmd of io_uring command */
struct ublksrv_ctrl_cmd {};

struct ublksrv_ctrl_dev_info {};

#define UBLK_IO_OP_READ
#define UBLK_IO_OP_WRITE
#define UBLK_IO_OP_FLUSH
#define UBLK_IO_OP_DISCARD
#define UBLK_IO_OP_WRITE_SAME
#define UBLK_IO_OP_WRITE_ZEROES
#define UBLK_IO_OP_ZONE_OPEN
#define UBLK_IO_OP_ZONE_CLOSE
#define UBLK_IO_OP_ZONE_FINISH
#define UBLK_IO_OP_ZONE_APPEND
#define UBLK_IO_OP_ZONE_RESET_ALL
#define UBLK_IO_OP_ZONE_RESET
/*
 * Construct a zone report. The report request is carried in `struct
 * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone
 * and shall indicate the first zone of the report. The `nr_zones` shall
 * indicate how many zones should be reported at most. The report shall be
 * delivered as a `struct blk_zone` array. To report fewer zones than requested,
 * zero the last entry of the returned array.
 *
 * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in
 * include/uapi/linux/blkzoned.h are part of ublk UAPI.
 */
#define UBLK_IO_OP_REPORT_ZONES

#define UBLK_IO_F_FAILFAST_DEV
#define UBLK_IO_F_FAILFAST_TRANSPORT
#define UBLK_IO_F_FAILFAST_DRIVER
#define UBLK_IO_F_META
#define UBLK_IO_F_FUA
#define UBLK_IO_F_NOUNMAP
#define UBLK_IO_F_SWAP

/*
 * io cmd is described by this structure, and stored in share memory, indexed
 * by request tag.
 *
 * The data is stored by ublk driver, and read by ublksrv after one fetch command
 * returns.
 */
struct ublksrv_io_desc {};

static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
{}

static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
{}

/* issued to ublk driver via /dev/ublkcN */
struct ublksrv_io_cmd {};

struct ublk_param_basic {};

struct ublk_param_discard {};

/*
 * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available
 * after device is started
 */
struct ublk_param_devt {};

struct ublk_param_zoned {};

struct ublk_params {};

#endif