From b7a8bc02e3530adc92f5287285330d926f999b5a Mon Sep 17 00:00:00 2001
From: Liu Xiaodong <xiaodong.liu@intel.com>
Date: Mon, 1 Feb 2021 08:19:14 -0500
Subject: [PATCH 11/15] nbd: set io timeout

After setting io timeout, host can avoid nbd io
stuck or kernel hang occasionally caused by nbd
stop or underlying bdev removal.

Signed-off-by: Liu Xiaodong <xiaodong.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/6191 (master)

(cherry picked from commit b7c33b5e610fd8894a0d4a67894f71c750f357d1)
Change-Id: I4ba2a0af7ff7bed369cdaf86121f082136dc1a0b
Signed-off-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/6394
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: <dongx.yi@intel.com>
---
 lib/nbd/nbd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lib/nbd/nbd.c b/lib/nbd/nbd.c
index 2da9066..3a9299d 100644
--- a/lib/nbd/nbd.c
+++ b/lib/nbd/nbd.c
@@ -50,6 +50,7 @@
 #define GET_IO_LOOP_COUNT		16
 #define NBD_BUSY_WAITING_MS		1000
 #define NBD_BUSY_POLLING_INTERVAL_US	20000
+#define NBD_IO_TIMEOUT_S		60
 
 enum nbd_io_state_t {
 	/* Receiving or ready to receive nbd request header */
@@ -922,6 +923,17 @@ nbd_start_complete(struct spdk_nbd_start_ctx *ctx)
 		goto err;
 	}
 
+#ifdef NBD_SET_TIMEOUT
+	rc = ioctl(ctx->nbd->dev_fd, NBD_SET_TIMEOUT, NBD_IO_TIMEOUT_S);
+	if (rc == -1) {
+		SPDK_ERRLOG("ioctl(NBD_SET_TIMEOUT) failed: %s\n", spdk_strerror(errno));
+		rc = -errno;
+		goto err;
+	}
+#else
+	SPDK_NOTICELOG("ioctl(NBD_SET_TIMEOUT) is not supported.\n");
+#endif
+
 #ifdef NBD_FLAG_SEND_TRIM
 	rc = ioctl(ctx->nbd->dev_fd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM);
 	if (rc == -1) {
-- 
1.8.3.1

