Skip to content

Commit bbab30b

Browse files
hreineckedwsuse
authored andcommitted
Use argument structure for nvme_format_nvm() and nvme_ns_mgmt()
Use an argument structure instead of passing all arguments one by one. This allows for a future expansion of the argument list without having to change the library ABI. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 869fad3 commit bbab30b

3 files changed

Lines changed: 153 additions & 78 deletions

File tree

src/nvme/ioctl.c

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,87 +1147,63 @@ int nvme_get_features_iocs_profile(int fd, enum nvme_get_features_sel sel,
11471147
return __nvme_get_features(fd, NVME_FEAT_FID_IOCS_PROFILE, sel, result);
11481148
}
11491149

1150-
int nvme_format_nvm(int fd, __u32 nsid, __u8 lbaf,
1151-
enum nvme_cmd_format_mset mset, enum nvme_cmd_format_pi pi,
1152-
enum nvme_cmd_format_pil pil, enum nvme_cmd_format_ses ses,
1153-
__u32 timeout, __u32 *result)
1150+
int nvme_format_nvm(struct nvme_format_nvm_args *args)
11541151
{
1155-
__u32 cdw10 = NVME_SET(lbaf, FORMAT_CDW10_LBAF) |
1156-
NVME_SET(mset, FORMAT_CDW10_MSET) |
1157-
NVME_SET(pi, FORMAT_CDW10_PI) |
1158-
NVME_SET(pil, FORMAT_CDW10_PIL) |
1159-
NVME_SET(ses, FORMAT_CDW10_SES);
1152+
__u32 cdw10 = NVME_SET(args->lbaf, FORMAT_CDW10_LBAF) |
1153+
NVME_SET(args->mset, FORMAT_CDW10_MSET) |
1154+
NVME_SET(args->pi, FORMAT_CDW10_PI) |
1155+
NVME_SET(args->pil, FORMAT_CDW10_PIL) |
1156+
NVME_SET(args->ses, FORMAT_CDW10_SES);
11601157

11611158
struct nvme_passthru_cmd cmd = {
11621159
.opcode = nvme_admin_format_nvm,
1163-
.nsid = nsid,
1160+
.nsid = args->nsid,
11641161
.cdw10 = cdw10,
1165-
.timeout_ms = timeout,
1162+
.timeout_ms = args->timeout,
11661163
};
11671164

1168-
return nvme_submit_admin_passthru(fd, &cmd, result);
1165+
if (args->args_size < sizeof(*args))
1166+
return -EINVAL;
1167+
return nvme_submit_admin_passthru(args->fd, &cmd, args->result);
11691168
}
11701169

1171-
int nvme_ns_mgmt(int fd, __u32 nsid, enum nvme_ns_mgmt_sel sel,
1172-
struct nvme_id_ns *ns, __u32 *result, __u32 timeout, __u8 csi)
1170+
int nvme_ns_mgmt(struct nvme_ns_mgmt_args *args)
11731171
{
1174-
__u32 cdw10 = NVME_SET(sel, NAMESPACE_MGMT_CDW10_SEL);
1175-
__u32 cdw11 = NVME_SET(csi, NAMESPACE_MGMT_CDW11_CSI);
1176-
__u32 data_len = ns ? sizeof(*ns) : 0;
1172+
__u32 cdw10 = NVME_SET(args->sel, NAMESPACE_MGMT_CDW10_SEL);
1173+
__u32 cdw11 = NVME_SET(args->csi, NAMESPACE_MGMT_CDW11_CSI);
1174+
__u32 data_len = args->ns ? sizeof(*args->ns) : 0;
11771175

11781176
struct nvme_passthru_cmd cmd = {
1179-
.nsid = nsid,
1177+
.nsid = args->nsid,
11801178
.opcode = nvme_admin_ns_mgmt,
11811179
.cdw10 = cdw10,
11821180
.cdw11 = cdw11,
1183-
.timeout_ms = timeout,
1181+
.timeout_ms = args->timeout,
11841182
.data_len = data_len,
1185-
.addr = (__u64)(uintptr_t)ns,
1183+
.addr = (__u64)(uintptr_t)args->ns,
11861184
};
11871185

1188-
return nvme_submit_admin_passthru(fd, &cmd, result);
1189-
}
1190-
1191-
int nvme_ns_mgmt_create(int fd, struct nvme_id_ns *ns, __u32 *nsid,
1192-
__u32 timeout, __u8 csi)
1193-
{
1194-
return nvme_ns_mgmt(fd, NVME_NSID_NONE, NVME_NS_MGMT_SEL_CREATE, ns,
1195-
nsid, timeout, csi);
1196-
}
1197-
1198-
int nvme_ns_mgmt_delete(int fd, __u32 nsid)
1199-
{
1200-
return nvme_ns_mgmt(fd, nsid, NVME_NS_MGMT_SEL_DELETE, NULL, NULL, 0,
1201-
0);
1186+
if (args->args_size < sizeof(*args))
1187+
return -EINVAL;
1188+
return nvme_submit_admin_passthru(args->fd, &cmd, args->result);
12021189
}
12031190

1204-
int nvme_ns_attach(int fd, __u32 nsid, enum nvme_ns_attach_sel sel,
1205-
struct nvme_ctrl_list *ctrlist, __u32 timeout)
1191+
int nvme_ns_attach(struct nvme_ns_attach_args *args)
12061192
{
1207-
__u32 cdw10 = NVME_SET(sel, NAMESPACE_ATTACH_CDW10_SEL);
1193+
__u32 cdw10 = NVME_SET(args->sel, NAMESPACE_ATTACH_CDW10_SEL);
12081194

12091195
struct nvme_passthru_cmd cmd = {
12101196
.opcode = nvme_admin_ns_attach,
1211-
.nsid = nsid,
1197+
.nsid = args->nsid,
12121198
.cdw10 = cdw10,
1213-
.data_len = sizeof(*ctrlist),
1214-
.addr = (__u64)(uintptr_t)ctrlist,
1215-
.timeout_ms = timeout,
1199+
.data_len = sizeof(*args->ctrlist),
1200+
.addr = (__u64)(uintptr_t)args->ctrlist,
1201+
.timeout_ms = args->timeout,
12161202
};
12171203

1218-
return nvme_submit_admin_passthru(fd, &cmd, NULL);
1219-
}
1220-
1221-
int nvme_ns_attach_ctrls(int fd, __u32 nsid, struct nvme_ctrl_list *ctrlist)
1222-
{
1223-
return nvme_ns_attach(fd, nsid, NVME_NS_ATTACH_SEL_CTRL_ATTACH,
1224-
ctrlist, NVME_DEFAULT_IOCTL_TIMEOUT);
1225-
}
1226-
1227-
int nvme_ns_detach_ctrls(int fd, __u32 nsid, struct nvme_ctrl_list *ctrlist)
1228-
{
1229-
return nvme_ns_attach(fd, nsid, NVME_NS_ATTACH_SEL_CTRL_DEATTACH,
1230-
ctrlist, NVME_DEFAULT_IOCTL_TIMEOUT);
1204+
if (args->args_size < sizeof(*args))
1205+
return -EINVAL;
1206+
return nvme_submit_admin_passthru(args->fd, &cmd, NULL);
12311207
}
12321208

12331209
int nvme_fw_download(int fd, __u32 offset, __u32 data_len, void *data,

src/nvme/ioctl.h

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,7 +2666,7 @@ int nvme_get_features_iocs_profile(int fd, enum nvme_get_features_sel sel,
26662666
__u32 *result);
26672667

26682668
/**
2669-
* nvme_format_nvm() - Format nvme namespace(s)
2669+
* nvme_format_nvm_args - Arguments for the Format Nvme Namespace command
26702670
* @fd: File descriptor of nvme device
26712671
* @nsid: Namespace ID to format
26722672
* @lbaf: Logical block address format
@@ -2677,6 +2677,23 @@ int nvme_get_features_iocs_profile(int fd, enum nvme_get_features_sel sel,
26772677
* @timeout: Set to override default timeout to this value in milliseconds;
26782678
* useful for long running formats. 0 will use system default.
26792679
* @result: The command completion result from CQE dword0
2680+
*/
2681+
struct nvme_format_nvm_args {
2682+
int args_size;
2683+
int fd;
2684+
__u32 nsid;
2685+
__u8 lbaf;
2686+
enum nvme_cmd_format_mset mset;
2687+
enum nvme_cmd_format_pi pi;
2688+
enum nvme_cmd_format_pil pil;
2689+
enum nvme_cmd_format_ses ses;
2690+
__u32 timeout;
2691+
__u32 *result;
2692+
};
2693+
2694+
/**
2695+
* nvme_format_nvm() - Format nvme namespace(s)
2696+
* @args: &struct nvme_format_nvme_args argument structure
26802697
*
26812698
* The Format NVM command low level formats the NVM media. This command is used
26822699
* by the host to change the LBA data size and/or metadata size. A low level
@@ -2686,25 +2703,34 @@ int nvme_get_features_iocs_profile(int fd, enum nvme_get_features_sel sel,
26862703
* Return: The nvme command status if a response was received (see
26872704
* &enum nvme_status_field) or -1 with errno set otherwise.
26882705
*/
2689-
int nvme_format_nvm(int fd, __u32 nsid, __u8 lbaf,
2690-
enum nvme_cmd_format_mset mset,
2691-
enum nvme_cmd_format_pi pi,
2692-
enum nvme_cmd_format_pil pil,
2693-
enum nvme_cmd_format_ses ses,
2694-
__u32 timeout, __u32 *result);
2706+
int nvme_format_nvm(struct nvme_format_nvm_args *args);
26952707

26962708
/**
2697-
* nvme_ns_mgmt() -
2709+
* nvme_ns_mgmt_args - Arguments for NVMe Namespace Management command
26982710
* @fd: File descriptor of nvme device
26992711
* @nsid: Namespace identifier
2700-
* @sel:
2712+
* @sel: Type of management operation to perform
27012713
* @ns: Namespace identication descriptors
27022714
* @result: NVMe command result
27032715
* @timeout: Timeout in ms
27042716
* @csi: Command Set Identifier
27052717
*/
2706-
int nvme_ns_mgmt(int fd, __u32 nsid, enum nvme_ns_mgmt_sel sel,
2707-
struct nvme_id_ns *ns, __u32 *result, __u32 timeout, __u8 csi);
2718+
struct nvme_ns_mgmt_args {
2719+
int args_size;
2720+
int fd;
2721+
__u32 nsid;
2722+
enum nvme_ns_mgmt_sel sel;
2723+
struct nvme_id_ns *ns;
2724+
__u32 *result;
2725+
__u32 timeout;
2726+
__u8 csi;
2727+
};
2728+
2729+
/**
2730+
* nvme_ns_mgmt() -
2731+
* @args: &struct nvme_ns_mgmt_args Argument structure
2732+
*/
2733+
int nvme_ns_mgmt(struct nvme_ns_mgmt_args *args);
27082734

27092735
/**
27102736
* nvme_ns_mgmt_create() -
@@ -2722,8 +2748,22 @@ int nvme_ns_mgmt(int fd, __u32 nsid, enum nvme_ns_mgmt_sel sel,
27222748
* Return: The nvme command status if a response was received (see
27232749
* &enum nvme_status_field) or -1 with errno set otherwise.
27242750
*/
2725-
int nvme_ns_mgmt_create(int fd, struct nvme_id_ns *ns, __u32 *nsid,
2726-
__u32 timeout, __u8 csi);
2751+
static inline int nvme_ns_mgmt_create(int fd, struct nvme_id_ns *ns,
2752+
__u32 *nsid, __u32 timeout, __u8 csi)
2753+
{
2754+
struct nvme_ns_mgmt_args args = {
2755+
.args_size = sizeof(args),
2756+
.fd = fd,
2757+
.nsid = NVME_NSID_NONE,
2758+
.sel = NVME_NS_MGMT_SEL_CREATE,
2759+
.ns = ns,
2760+
.result = nsid,
2761+
.timeout = timeout,
2762+
.csi = csi,
2763+
};
2764+
2765+
return nvme_ns_mgmt(&args);
2766+
}
27272767

27282768
/**
27292769
* nvme_ns_mgmt_delete() -
@@ -2737,34 +2777,86 @@ int nvme_ns_mgmt_create(int fd, struct nvme_id_ns *ns, __u32 *nsid,
27372777
* Return: The nvme command status if a response was received (see
27382778
* &enum nvme_status_field) or -1 with errno set otherwise.
27392779
*/
2740-
int nvme_ns_mgmt_delete(int fd, __u32 nsid);
2780+
static inline int nvme_ns_mgmt_delete(int fd, __u32 nsid)
2781+
{
2782+
struct nvme_ns_mgmt_args args = {
2783+
.args_size = sizeof(args),
2784+
.fd = fd,
2785+
.nsid = nsid,
2786+
.sel = NVME_NS_MGMT_SEL_DELETE,
2787+
.ns = NULL,
2788+
.result = NULL,
2789+
.timeout = 0,
2790+
.csi = 0,
2791+
};
2792+
2793+
return nvme_ns_mgmt(&args);
2794+
}
27412795

27422796
/**
2743-
* nvme_ns_attach() - Attach or detach namespace to controller(s)
2797+
* nvme_ns_attach_args - Arguments for Nvme Namespace Management command
27442798
* @fd: File descriptor of nvme device
27452799
* @nsid: Namespace ID to execute attach selection
27462800
* @sel: Attachment selection, see &enum nvme_ns_attach_sel
27472801
* @ctrlist: Controller list to modify attachment state of nsid
27482802
* @timeout: Timeout in ms
27492803
*/
2750-
int nvme_ns_attach(int fd, __u32 nsid, enum nvme_ns_attach_sel sel,
2751-
struct nvme_ctrl_list *ctrlist, __u32 timeout);
2804+
struct nvme_ns_attach_args {
2805+
int args_size;
2806+
int fd;
2807+
__u32 nsid;
2808+
enum nvme_ns_attach_sel sel;
2809+
struct nvme_ctrl_list *ctrlist;
2810+
__u32 timeout;
2811+
};
2812+
2813+
/**
2814+
* nvme_ns_attach_args - Attach or detach namespace to controller(s)
2815+
* @args: &struct nvme_ns_attach_args Argument structure
2816+
*/
2817+
int nvme_ns_attach(struct nvme_ns_attach_args *args);
27522818

27532819
/**
27542820
* nvme_ns_attach_ctrls() -
27552821
* @fd: File descriptor of nvme device
27562822
* @nsid: Namespace ID to attach
27572823
* @ctrlist: Controller list to modify attachment state of nsid
27582824
*/
2759-
int nvme_ns_attach_ctrls(int fd, __u32 nsid, struct nvme_ctrl_list *ctrlist);
2825+
static inline int nvme_ns_attach_ctrls(int fd, __u32 nsid,
2826+
struct nvme_ctrl_list *ctrlist)
2827+
{
2828+
struct nvme_ns_attach_args args = {
2829+
.args_size = sizeof(args),
2830+
.fd = fd,
2831+
.nsid = nsid,
2832+
.sel = NVME_NS_ATTACH_SEL_CTRL_ATTACH,
2833+
.ctrlist = ctrlist,
2834+
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
2835+
};
2836+
2837+
return nvme_ns_attach(&args);
2838+
}
27602839

27612840
/**
27622841
* nvme_ns_detach_ctrls() -
27632842
* @fd: File descriptor of nvme device
27642843
* @nsid: Namespace ID to detach
27652844
* @ctrlist: Controller list to modify attachment state of nsid
27662845
*/
2767-
int nvme_ns_detach_ctrls(int fd, __u32 nsid, struct nvme_ctrl_list *ctrlist);
2846+
static inline int nvme_ns_detach_ctrls(int fd, __u32 nsid,
2847+
struct nvme_ctrl_list *ctrlist)
2848+
{
2849+
struct nvme_ns_attach_args args = {
2850+
.args_size = sizeof(args),
2851+
.fd = fd,
2852+
.nsid = nsid,
2853+
.sel = NVME_NS_ATTACH_SEL_CTRL_DEATTACH,
2854+
.ctrlist = ctrlist,
2855+
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
2856+
};
2857+
2858+
return nvme_ns_attach(&args);
2859+
}
27682860

27692861
/**
27702862
* nvme_fw_download() - Download part or all of a firmware image to the
@@ -3016,7 +3108,7 @@ int nvme_directive_recv_stream_allocate(int fd, __u32 nsid, __u16 nsr,
30163108
__u32 *result);
30173109

30183110
/**
3019-
* nvme_capacity_mgmt() -
3111+
* nvme_capacity_mgmt_args - Arguments for the NVMe Capacity Management command
30203112
* @fd: File descriptor of nvme device
30213113
* @op: Operation to be performed by the controller
30223114
* @element_id: Value specific to the value of the Operation field

src/nvme/linux.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,21 @@ int nvme_get_lba_status_log(int fd, bool rae, struct nvme_lba_status_log **log)
280280
static int nvme_ns_attachment(int fd, __u32 nsid, __u16 num_ctrls,
281281
__u16 *ctrlist, bool attach, __u32 timeout)
282282
{
283-
enum nvme_ns_attach_sel sel = NVME_NS_ATTACH_SEL_CTRL_DEATTACH;
284283
struct nvme_ctrl_list cntlist = { 0 };
284+
struct nvme_ns_attach_args args = {
285+
.args_size = sizeof(args),
286+
.fd = fd,
287+
.nsid = nsid,
288+
.sel = NVME_NS_ATTACH_SEL_CTRL_DEATTACH,
289+
.ctrlist = &cntlist,
290+
.timeout = timeout,
291+
};
285292

286293
if (attach)
287-
sel = NVME_NS_ATTACH_SEL_CTRL_ATTACH;
294+
args.sel = NVME_NS_ATTACH_SEL_CTRL_ATTACH;
288295

289-
nvme_init_ctrl_list(&cntlist, num_ctrls, ctrlist);
290-
return nvme_ns_attach(fd, nsid, sel, &cntlist, timeout);
296+
nvme_init_ctrl_list(args.ctrlist, num_ctrls, ctrlist);
297+
return nvme_ns_attach(&args);
291298
}
292299

293300
int nvme_namespace_attach_ctrls(int fd, __u32 nsid, __u16 num_ctrls,

0 commit comments

Comments
 (0)