[PATCH blktests v5 3/4] nvme/rc: Add helper to wait for nvme ctrl state

Daniel Wagner dwagner at suse.de
Wed Apr 5 08:46:29 PDT 2023


The function waits until it sees a given state (or timeouts).
Unfortunately, we can't use /sys/class/nvme/nvme%d because this symlink
will be removed if the controller is going through resetting state. Thus
use the real nvme%d directly.

Signed-off-by: Daniel Wagner <dwagner at suse.de>
---
 tests/nvme/rc | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/tests/nvme/rc b/tests/nvme/rc
index e720a35113aa..ae4a69fe8438 100644
--- a/tests/nvme/rc
+++ b/tests/nvme/rc
@@ -16,6 +16,7 @@ def_local_wwnn="0x10001100aa000002"
 def_local_wwpn="0x20001100aa000002"
 def_hostnqn="$(cat /etc/nvme/hostnqn 2> /dev/null)"
 def_hostid="$(cat /etc/nvme/hostid 2> /dev/null)"
+def_state_timeout=20
 nvme_trtype=${nvme_trtype:-"loop"}
 
 _nvme_requires() {
@@ -198,6 +199,28 @@ _nvme_fcloop_del_tport() {
 	echo "wwnn=${wwnn},wwpn=${wwpn}" > ${loopctl}/del_target_port 2> /dev/null
 }
 
+_nvmf_wait_for_state() {
+	local subsys_name="$1"
+	local state="$2"
+	local timeout="${3:-$def_state_timeout}"
+
+	local nvmedev=$(_find_nvme_dev "${subsys_name}")
+	local state_file="/sys/class/nvme-fabrics/ctl/${nvmedev}/state"
+
+	local start_time=$(date +%s)
+	local end_time
+
+	while ! grep -q "${state}" "${state_file}"; do
+		sleep 1
+		end_time=$(date +%s)
+                if (( end_time - start_time > timeout )); then
+                        echo "expected state \"${state}\" not " \
+			     "reached within ${timeout} seconds"
+                        break
+                fi
+	done
+}
+
 _cleanup_fcloop() {
 	local local_wwnn="${1:-$def_local_wwnn}"
 	local local_wwpn="${2:-$def_local_wwpn}"
-- 
2.40.0




More information about the Linux-nvme mailing list