libxnvme_nvm.h#

Functions#

xnvme_nvm_compare#

int xnvme_nvm_compare(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb, void *dbuf, void *mbuf)#

Submit a NVMe Compare command.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • slba – The LBA to start the compare at

  • nlb – Number of LBAs to be compared. NOTE: nlb is a zero-based value

  • dbuf – Pointer to buffer; Payload as indicated by ‘ctx->opts’

  • mbuf – Pointer to buffer; Payload as indicated by ‘ctx->opts’

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_nvm_dsm#

int xnvme_nvm_dsm(struct xnvme_cmd_ctx *ctx, uint32_t nsid, struct xnvme_spec_dsm_range *dsm_range, uint8_t nr, bool ad, bool idw, bool idr)#

Submit a NVMe Dataset Management command.

Deallocate or hint at read/write usage of the given ranges.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • dsm_range – A buffer of ranges, each with context attributes, slba and nlb

  • nr – Number of ranges in the given ranges-buffer, zero-based value

  • ad – The NVM subsystem may deallocate all provided ranges

  • idw – The dataset should be optimized for write access as an integral unit

  • idr – The dataset should be optimized for read access as an integral unit

Returns:

On success, 0 is returned. On error, -1 is returned.

xnvme_nvm_mgmt_recv#

int xnvme_nvm_mgmt_recv(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint8_t mo, uint16_t mos, void *dbuf, uint32_t dbuf_nbytes)#

Submit a NVMe I/O Management Receive command.

See also

xnvme_cmd_opts

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • mo – Management operation

  • mos – Management operation specific field

  • dbuf – pointer to data-payload

  • dbuf_nbytes – size of the data-payload in bytes

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_nvm_mgmt_send#

int xnvme_nvm_mgmt_send(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint8_t mo, uint16_t mos, void *dbuf, uint32_t dbuf_nbytes)#

Submit a NVMe I/O Management Send command.

See also

xnvme_cmd_opts

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • mo – Management operation

  • mos – Management operation specific field

  • dbuf – pointer to data-payload

  • dbuf_nbytes – size of the data-payload in bytes

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_nvm_read#

int xnvme_nvm_read(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb, void *dbuf, void *mbuf)#

Submit a NVMe Read command.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • slba – The LBA to start reading from

  • nlb – The number of LBAs to read. NOTE: nlb is a zero-based value

  • dbuf – Pointer to data-payload

  • mbuf – Pointer to meta-payload

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_nvm_scopy#

int xnvme_nvm_scopy(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t sdlba, struct xnvme_spec_nvm_scopy_fmt_zero *ranges, uint8_t nr, uint32_t copy_fmt)#

Submit a NVMe Simple-Copy command.

See also

xnvme_cmd_opts

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • sdlba – The Starting Destination LBA to start copying to

  • ranges – Pointer to ranges-buffer, see xnvme_spec_nvm_scopy_fmt_zero

  • nr – Number of ranges in the given ranges-buffer, zero-based value

  • copy_fmt – Format of the ranges, a value of xnvme_nvm_scopy_fmt

Returns:

On success, 0 is returned. On error, -1 is returned, errno set to indicate and ctx.cpl filled with lower-level status codes

xnvme_nvm_write#

int xnvme_nvm_write(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb, const void *dbuf, const void *mbuf)#

Submit a NVMe Write command.

NOTE: fields which are not arguments here, such as ‘ctx->cmd.nvm.fua’, are left untouched; set them on the context before calling this. Neither this function nor xnvme_queue_get_cmd_ctx clears them, so a context which is reused for another command carries them over. Reset it with xnvme_cmd_ctx_clear when that is not intended.

NOTE: whether ‘ctx->cmd.nvm.fua’ is honoured is backend-dependent, and it applies to NVM writes only. Backends passing the command through to an NVMe device (ioctl, io_uring_cmd, spdk, libvfn, upcie) carry the bit in the command itself. The Linux io_uring and libaio backends translate it to RWF_DSYNC, which on a raw block device makes the kernel issue the write with REQ_FUA. The remaining backends (psync, POSIX-aio, ramdisk, FreeBSD, Windows, macOS) ignore it.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • slba – The LBA to start the write at

  • nlb – Number of LBAs to be written. NOTE: nlb is a zero-based value

  • dbuf – Pointer to buffer; Payload as indicated by ‘ctx->opts’

  • mbuf – Pointer to buffer; Payload as indicated by ‘ctx->opts’

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_nvm_write_uncorrectable#

int xnvme_nvm_write_uncorrectable(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb)#

Submit a NVMe Write Uncorrectable command.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • slba – The Starting Destination LBA

  • nlb – Number Of Logical Blocks

xnvme_nvm_write_zeroes#

int xnvme_nvm_write_zeroes(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb)#

Submit a NVMe Write Zeroes command.

See also

xnvme_cmd_opts

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • nsid – Namespace Identifier

  • slba – The LBA to start the write at

  • nlb – Number Of Logical Blocks

Returns:

On success, 0 is returned. On error, -1 is returned, errno set to indicate and param ctx.cpl filled with lower-level status codes

xnvme_prep_nvm#

void xnvme_prep_nvm(struct xnvme_cmd_ctx *ctx, uint8_t opcode, uint32_t nsid, uint64_t slba, uint16_t nlb)#

Prepare a NVMe command context for an I/O command.

This populates the command fields in the given context, it does not submit the command. Submit it with e.g. xnvme_cmd_pass.

NOTE: only the fields given as arguments are populated; the remaining ones, such as ‘ctx->cmd.nvm.fua’, are left as they were. Reset a reused context with xnvme_cmd_ctx_clear when carrying them over is not intended.

Parameters:
  • ctx – Pointer to command context (xnvme_cmd_ctx)

  • opcode – Opcode for the NVMe command

  • nsid – Namespace Identifier

  • slba – The LBA to start the operation at

  • nlb – Number of LBAs. NOTE: nlb is a zero-based value

Header#

/**
 * SPDX-FileCopyrightText: Samsung Electronics Co., Ltd
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * @file libxnvme_nvm.h
 */

#ifndef __LIBXNVME_NVM_H
#define __LIBXNVME_NVM_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Submit a NVMe Read command
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param slba The LBA to start reading from
 * @param nlb The number of LBAs to read. NOTE: nlb is a zero-based value
 * @param dbuf Pointer to data-payload
 * @param mbuf Pointer to meta-payload
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_nvm_read(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb, void *dbuf,
	       void *mbuf);

/**
 * Submit a NVMe Write command
 *
 * NOTE: fields which are not arguments here, such as 'ctx->cmd.nvm.fua', are
 * left untouched; set them on the context before calling this. Neither this
 * function nor ::xnvme_queue_get_cmd_ctx clears them, so a context which is
 * reused for another command carries them over. Reset it with
 * ::xnvme_cmd_ctx_clear when that is not intended.
 *
 * NOTE: whether 'ctx->cmd.nvm.fua' is honoured is backend-dependent, and it
 * applies to NVM writes only. Backends passing the command through to an NVMe
 * device (ioctl, io_uring_cmd, spdk, libvfn, upcie) carry the bit in the
 * command itself. The Linux io_uring and libaio backends translate it to
 * RWF_DSYNC, which on a raw block device makes the kernel issue the write
 * with REQ_FUA. The remaining backends (psync, POSIX-aio, ramdisk, FreeBSD,
 * Windows, macOS) ignore it.
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param slba The LBA to start the write at
 * @param nlb Number of LBAs to be written. NOTE: nlb is a zero-based value
 * @param dbuf Pointer to buffer; Payload as indicated by 'ctx->opts'
 * @param mbuf Pointer to buffer; Payload as indicated by 'ctx->opts'
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_nvm_write(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb,
		const void *dbuf, const void *mbuf);

/**
 * Submit a NVMe Write Uncorrectable command
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param slba The Starting Destination LBA
 * @param nlb Number Of Logical Blocks
 */
int
xnvme_nvm_write_uncorrectable(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba,
			      uint16_t nlb);

/**
 * Submit a NVMe Write Zeroes command
 *
 * @see xnvme_cmd_opts
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param slba The LBA to start the write at
 * @param nlb Number Of Logical Blocks
 *
 * @return On success, 0 is returned. On error, -1 is returned, `errno` set to
 * indicate and param ctx.cpl filled with lower-level status codes
 */
int
xnvme_nvm_write_zeroes(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb);

/**
 * Prepare a NVMe command context for an I/O command
 *
 * This populates the command fields in the given context, it does not submit
 * the command. Submit it with e.g. ::xnvme_cmd_pass.
 *
 * NOTE: only the fields given as arguments are populated; the remaining ones,
 * such as 'ctx->cmd.nvm.fua', are left as they were. Reset a reused context
 * with ::xnvme_cmd_ctx_clear when carrying them over is not intended.
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param opcode Opcode for the NVMe command
 * @param nsid Namespace Identifier
 * @param slba The LBA to start the operation at
 * @param nlb Number of LBAs. NOTE: nlb is a zero-based value
 */
void
xnvme_prep_nvm(struct xnvme_cmd_ctx *ctx, uint8_t opcode, uint32_t nsid, uint64_t slba,
	       uint16_t nlb);

/**
 * Submit a NVMe Simple-Copy command
 *
 * @see xnvme_cmd_opts
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param sdlba The Starting Destination LBA to start copying to
 * @param ranges Pointer to ranges-buffer, see ::xnvme_spec_nvm_scopy_fmt_zero
 * @param nr Number of ranges in the given ranges-buffer, zero-based value
 * @param copy_fmt Format of the ranges, a value of ::xnvme_nvm_scopy_fmt
 *
 * @return On success, 0 is returned. On error, -1 is returned, `errno` set to
 * indicate and `ctx.cpl` filled with lower-level status codes
 */
int
xnvme_nvm_scopy(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t sdlba,
		struct xnvme_spec_nvm_scopy_fmt_zero *ranges, uint8_t nr, uint32_t copy_fmt);

/**
 * Submit a NVMe Dataset Management command
 *
 * Deallocate or hint at read/write usage of the given ranges.
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param dsm_range A buffer of ranges, each with context attributes, slba and nlb
 * @param nr Number of ranges in the given ranges-buffer, zero-based value
 * @param ad The NVM subsystem may deallocate all provided ranges
 * @param idw The dataset should be optimized for write access as an integral unit
 * @param idr The dataset should be optimized for read access as an integral unit
 *
 * @return On success, 0 is returned. On error, -1 is returned.
 */
int
xnvme_nvm_dsm(struct xnvme_cmd_ctx *ctx, uint32_t nsid, struct xnvme_spec_dsm_range *dsm_range,
	      uint8_t nr, bool ad, bool idw, bool idr);

/**
 * Submit a NVMe I/O Management Receive command
 *
 * @see xnvme_cmd_opts
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param mo Management operation
 * @param mos Management operation specific field
 * @param dbuf pointer to data-payload
 * @param dbuf_nbytes size of the data-payload in bytes
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_nvm_mgmt_recv(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint8_t mo, uint16_t mos, void *dbuf,
		    uint32_t dbuf_nbytes);

/**
 * Submit a NVMe I/O Management Send command
 *
 * @see xnvme_cmd_opts
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param mo Management operation
 * @param mos Management operation specific field
 * @param dbuf pointer to data-payload
 * @param dbuf_nbytes size of the data-payload in bytes
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_nvm_mgmt_send(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint8_t mo, uint16_t mos, void *dbuf,
		    uint32_t dbuf_nbytes);

/**
 * Submit a NVMe Compare command
 *
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 * @param nsid Namespace Identifier
 * @param slba The LBA to start the compare at
 * @param nlb Number of LBAs to be compared. NOTE: nlb is a zero-based value
 * @param dbuf Pointer to buffer; Payload as indicated by 'ctx->opts'
 * @param mbuf Pointer to buffer; Payload as indicated by 'ctx->opts'
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_nvm_compare(struct xnvme_cmd_ctx *ctx, uint32_t nsid, uint64_t slba, uint16_t nlb,
		  void *dbuf, void *mbuf);

#ifdef __cplusplus
}
#endif

#endif /* __LIBXNVME_NVM */