All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-cli: Wait for device file if not present after successful add_ctrl
@ 2018-04-27 17:29 James Smart
  2018-04-27 21:45 ` Keith Busch
  0 siblings, 1 reply; 2+ messages in thread
From: James Smart @ 2018-04-27 17:29 UTC (permalink / raw)


It's possible for the transport to return very quickly after add_ctrl
such that the cli may attempt to access the /dev/nvme? device file
before the udev event has propagated to user space to create the device
file. In these cases, the open fails with EAGAIN.

As the add_ctrl call was successful, thus there "should" be a device
file, if the open fails with EAGAIN delay 500ms and try again. Wait
for at most 2 seconds for the device file to come into existence.

Signed-off-by: James Smart <james.smart at broadcom.com>
---
 fabrics.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/fabrics.c b/fabrics.c
index 7e9d56c..408afb0 100644
--- a/fabrics.c
+++ b/fabrics.c
@@ -171,6 +171,32 @@ static const char *cms_str(__u8 cm)
 
 static int do_discover(char *argstr, bool connect);
 
+#define OPEN_RETRIES	4
+#define OPEN_DELAY	500000		/* us units, thus 1/2 second */
+
+static void
+wait_for_devname(int instance)
+{
+	char *dev_name;
+	int retry = 0, fd;
+
+	if (asprintf(&dev_name, "/dev/nvme%d", instance) < 0)
+		return;
+
+	/* takes a little time for udev to make the dev node */
+	for ( ; retry < OPEN_RETRIES; retry++) {
+		fd = open(dev_name, O_RDWR);
+		if (fd >= 0) {
+			close(fd);
+			break;
+		}
+		if (errno != EAGAIN)
+			break;
+		usleep(OPEN_DELAY);
+	}
+	free(dev_name);
+}
+
 static int add_ctrl(const char *argstr)
 {
 	substring_t args[MAX_OPT_ARGS];
@@ -225,6 +251,8 @@ out_fail:
 out_close:
 	close(fd);
 out:
+	if (ret >= 0)
+		wait_for_devname(ret);
 	return ret;
 }
 
-- 
2.13.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] nvme-cli: Wait for device file if not present after successful add_ctrl
  2018-04-27 17:29 [PATCH] nvme-cli: Wait for device file if not present after successful add_ctrl James Smart
@ 2018-04-27 21:45 ` Keith Busch
  0 siblings, 0 replies; 2+ messages in thread
From: Keith Busch @ 2018-04-27 21:45 UTC (permalink / raw)


On Fri, Apr 27, 2018@10:29:39AM -0700, James Smart wrote:
> It's possible for the transport to return very quickly after add_ctrl
> such that the cli may attempt to access the /dev/nvme? device file
> before the udev event has propagated to user space to create the device
> file. In these cases, the open fails with EAGAIN.
> 
> As the add_ctrl call was successful, thus there "should" be a device
> file, if the open fails with EAGAIN delay 500ms and try again. Wait
> for at most 2 seconds for the device file to come into existence.
> 
> Signed-off-by: James Smart <james.smart at broadcom.com>

Applied.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-04-27 21:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-27 17:29 [PATCH] nvme-cli: Wait for device file if not present after successful add_ctrl James Smart
2018-04-27 21:45 ` Keith Busch

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.