diff --git a/include/spdk/env.h b/include/spdk/env.h index 830825687..921e32329 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -345,6 +345,20 @@ struct spdk_pci_id spdk_pci_device_get_id(struct spdk_pci_device *dev); int spdk_pci_device_get_socket_id(struct spdk_pci_device *dev); int spdk_pci_device_get_serial_number(struct spdk_pci_device *dev, char *sn, size_t len); + +/** + * Claim a PCI device for exclusive SPDK userspace access. + * + * Uses F_SETLK on a shared memory file with the PCI address embedded in its name. + * As long as this file remains open with the lock acquired, other processes will + * not be able to successfully call this function on the same PCI device. + * + * \param pci_addr PCI address of the device to claim + * + * \return -1 if the device has already been claimed, an fd otherwise. This fd + * should be closed when the application no longer needs access to the + * PCI device (including when it is hot removed). + */ int spdk_pci_device_claim(const struct spdk_pci_addr *pci_addr); void spdk_pci_device_detach(struct spdk_pci_device *device); diff --git a/lib/bdev/nvme/bdev_nvme.c b/lib/bdev/nvme/bdev_nvme.c index 8d099ea5b..d4fa82a54 100644 --- a/lib/bdev/nvme/bdev_nvme.c +++ b/lib/bdev/nvme/bdev_nvme.c @@ -742,7 +742,7 @@ probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, return false; } - if (spdk_pci_device_claim(&pci_addr) != 0) { + if (spdk_pci_device_claim(&pci_addr) < 0) { return false; } } diff --git a/lib/copy/ioat/copy_engine_ioat.c b/lib/copy/ioat/copy_engine_ioat.c index e0d0e8aeb..0e85c9033 100644 --- a/lib/copy/ioat/copy_engine_ioat.c +++ b/lib/copy/ioat/copy_engine_ioat.c @@ -248,7 +248,7 @@ probe_cb(void *cb_ctx, struct spdk_pci_device *pci_dev) } /* Claim the device in case conflict with other process */ - if (spdk_pci_device_claim(&pci_addr) != 0) { + if (spdk_pci_device_claim(&pci_addr) < 0) { return false; } diff --git a/lib/env_dpdk/pci.c b/lib/env_dpdk/pci.c index df2f289f8..144bd7798 100644 --- a/lib/env_dpdk/pci.c +++ b/lib/env_dpdk/pci.c @@ -495,7 +495,7 @@ spdk_pci_device_claim(const struct spdk_pci_addr *pci_addr) *(int *)dev_map = (int)getpid(); munmap(dev_map, sizeof(int)); /* Keep dev_fd open to maintain the lock. */ - return 0; + return dev_fd; } #endif /* __linux__ */ diff --git a/test/lib/env/pci/pci_ut.c b/test/lib/env/pci/pci_ut.c index 48a98c462..873f67aed 100644 --- a/test/lib/env/pci/pci_ut.c +++ b/test/lib/env/pci/pci_ut.c @@ -51,7 +51,7 @@ pci_test(void) pci_addr.func = 1; rc = spdk_pci_device_claim(&pci_addr); - CU_ASSERT(rc == 0); + CU_ASSERT(rc >= 0); childPid = fork(); CU_ASSERT(childPid >= 0);