Skip to content

Commit e2291cf

Browse files
ikegami-tigaw
authored andcommitted
ioctl: refactor nvme_get_log_partial uring
Refactore so we maintain nvme_get_log_partial easier. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent b24a6c4 commit e2291cf

1 file changed

Lines changed: 41 additions & 26 deletions

File tree

libnvme/src/nvme/ioctl.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static int nvme_uring_cmd_admin_passthru_async(struct nvme_transport_handle *hdl
304304

305305
memcpy(&sqe->cmd, cmd, sizeof(*cmd));
306306

307-
sqe->fd = l->fd;
307+
sqe->fd = hdl->fd;
308308
sqe->opcode = IORING_OP_URING_CMD;
309309
sqe->cmd_op = NVME_URING_CMD_ADMIN;
310310
sqe->user_data = (__u64)(uintptr_t)result;
@@ -340,7 +340,19 @@ static int nvme_uring_cmd_wait_complete(struct io_uring *ring, int n)
340340

341341
return ret;
342342
}
343-
#endif
343+
344+
static bool nvme_uring_is_usable(struct nvme_transport_handle *hdl)
345+
{
346+
struct stat st;
347+
348+
if (io_uring_kernel_support != IO_URING_AVAILABLE ||
349+
hdl->type != NVME_TRANSPORT_HANDLE_TYPE_DIRECT ||
350+
fstat(hdl->fd, &st) || !S_ISCHR(st.st_mode))
351+
return false;
352+
353+
return true;
354+
}
355+
#endif /* CONFIG_LIBURING */
344356

345357
int nvme_get_log(struct nvme_transport_handle *hdl,
346358
struct nvme_passthru_cmd *cmd, bool rae,
@@ -357,26 +369,21 @@ int nvme_get_log(struct nvme_transport_handle *hdl,
357369
__u32 cdw10 = cmd->cdw10 & (NVME_VAL(LOG_CDW10_LID) |
358370
NVME_VAL(LOG_CDW10_LSP));
359371
__u32 cdw11 = cmd->cdw11 & NVME_VAL(LOG_CDW11_LSI);
360-
361-
if (force_4k)
362-
xfer_len = NVME_LOG_PAGE_PDU_SIZE;
363-
364372
#ifdef CONFIG_LIBURING
365-
int n = 0;
373+
bool use_uring = nvme_uring_is_usable(hdl);
366374
struct io_uring ring;
367-
struct stat st;
368-
bool use_uring = false;
369-
370-
if (io_uring_kernel_support == IO_URING_AVAILABLE && l->type == NVME_LINK_TYPE_DIRECT) {
371-
if (fstat(l->fd, &st) == 0 && S_ISCHR(st.st_mode)) {
372-
use_uring = true;
375+
int n = 0;
373376

374-
ret = nvme_uring_cmd_setup(&ring);
375-
if (ret)
376-
return ret;
377-
}
377+
if (use_uring) {
378+
ret = nvme_uring_cmd_setup(&ring);
379+
if (ret)
380+
return ret;
378381
}
379-
#endif
382+
#endif /* CONFIG_LIBURING */
383+
384+
if (force_4k)
385+
xfer_len = NVME_LOG_PAGE_PDU_SIZE;
386+
380387
/*
381388
* 4k is the smallest possible transfer unit, so restricting to 4k
382389
* avoids having to check the MDTS value of the controller.
@@ -410,20 +417,27 @@ int nvme_get_log(struct nvme_transport_handle *hdl,
410417
cmd->cdw13 = lpo >> 32;
411418
cmd->data_len = xfer;
412419
cmd->addr = (__u64)(uintptr_t)ptr;
420+
413421
#ifdef CONFIG_LIBURING
414-
if (io_uring_kernel_support == IO_URING_AVAILABLE && use_uring) {
422+
if (use_uring) {
415423
if (n >= NVME_URING_ENTRIES) {
416-
ret = nvme_uring_cmd_wait_complete(&ring, n);
424+
nvme_uring_cmd_wait_complete(&ring, n);
417425
n = 0;
418426
}
419427
n += 1;
420-
ret = nvme_uring_cmd_admin_passthru_async(hdl, &ring, cmd, result);
428+
ret = nvme_uring_cmd_admin_passthru_async(hdl, &ring,
429+
cmd, result);
421430

422431
if (ret)
423432
nvme_uring_cmd_exit(&ring);
424-
} else
425-
#endif
433+
} else {
434+
ret = nvme_submit_admin_passthru(hdl, cmd, result);
435+
if (ret)
436+
return ret;
437+
}
438+
#else /* CONFIG_LIBURING */
426439
ret = nvme_submit_admin_passthru(hdl, cmd, result);
440+
#endif /* CONFIG_LIBURING */
427441
if (ret)
428442
return ret;
429443

@@ -432,13 +446,14 @@ int nvme_get_log(struct nvme_transport_handle *hdl,
432446
} while (offset < data_len);
433447

434448
#ifdef CONFIG_LIBURING
435-
if (io_uring_kernel_support == IO_URING_AVAILABLE && use_uring) {
436-
ret = nvme_uring_cmd_wait_complete(&ring, n);
449+
if (use_uring) {
450+
nvme_uring_cmd_wait_complete(&ring, n);
437451
nvme_uring_cmd_exit(&ring);
438452
if (ret)
439453
return ret;
440454
}
441-
#endif
455+
#endif /* CONFIG_LIBURING */
456+
442457
return 0;
443458
}
444459

0 commit comments

Comments
 (0)