From dcd3d6cf9bfab6b664aee341779465977762a2d3 Mon Sep 17 00:00:00 2001
Date: Tue, 7 Nov 2023 11:40:52 +0900
Subject: [PATCH] add max_connections
---
drivers/block/nbd.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 800f131222fc..2cd41db20e31 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -162,6 +162,7 @@ static struct dentry *nbd_dbg_dir;
static unsigned int nbds_max = 16;
static int max_part = 16;
static int part_shift;
+static unsigned long max_connections;
static int nbd_dev_dbg_init(struct nbd_device *nbd);
static void nbd_dev_dbg_close(struct nbd_device *nbd);
@@ -1117,6 +1118,14 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
/* Arg will be cast to int, check it to avoid overflow */
if (arg > INT_MAX)
return -EINVAL;
+
+ if(config->num_connections >= max_connections)
+ {
+ dev_err(disk_to_dev(nbd->disk),
+ "Number of socket connections exceeded limit.\n");
+ return -ENOMEM;
+ }
+
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
@@ -2482,6 +2491,11 @@ static void nbd_dead_link_work(struct work_struct *work)
kfree(args);
}
+static void set_max_connections(void)
+{
+ max_connections = PAGE_SIZE / sizeof(struct nbd_sock *);
+}
+
static int __init nbd_init(void)
{
int i;
@@ -2529,6 +2543,7 @@ static int __init nbd_init(void)
return -EINVAL;
}
nbd_dbg_init();
+ set_max_connections();
for (i = 0; i < nbds_max; i++)
nbd_dev_add(i, 1);
--
2.34.1