From 56aac946d7888eb9696610ab4708f99cb76d0af0 Mon Sep 17 00:00:00 2001
From: Joursoir <chat@joursoir.net>
Date: Thu, 12 Jan 2023 18:13:00 +0400
Subject: add driver parameter for target disk

Task: 1
---
 sbdd.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

(limited to 'sbdd.c')

diff --git a/sbdd.c b/sbdd.c
index 8d8474f..fb62971 100644
--- a/sbdd.c
+++ b/sbdd.c
@@ -22,7 +22,6 @@
 
 #define SBDD_NAME              "sbdd"
 #define SBDD_BDEV_MODE         (FMODE_READ | FMODE_WRITE)
-#define TARGET_DEV             "/dev/disk/by-id/ata-QEMU_HARDDISK_QM00001"
 
 struct sbdd {
 	wait_queue_head_t       exitwait;
@@ -37,6 +36,7 @@ struct sbdd {
 
 static struct sbdd      __sbdd;
 static int              __sbdd_major = 0;
+static char             *__sbdd_disk = "/dev/disk/by-id/ata-QEMU_HARDDISK_QM00001";
 
 /*static sector_t sbdd_xfer(struct bio_vec* bvec, sector_t pos, int dir)
 {
@@ -132,11 +132,11 @@ static int sbdd_create(void)
 	__sbdd.gd = alloc_disk(1);
 
 	/* Get a handle on the device */
-	pr_info("opening %s\n", TARGET_DEV);
-	__sbdd.bdev = blkdev_get_by_path(TARGET_DEV, SBDD_BDEV_MODE, THIS_MODULE);
+	pr_info("opening %s\n", __sbdd_disk);
+	__sbdd.bdev = blkdev_get_by_path(__sbdd_disk, SBDD_BDEV_MODE, THIS_MODULE);
 	if (!__sbdd.bdev || IS_ERR(__sbdd.bdev)) {
 		pr_err("blkdev_get_by_path(\"%s\") failed with %ld\n",
-				TARGET_DEV, PTR_ERR(__sbdd.bdev));
+				__sbdd_disk, PTR_ERR(__sbdd.bdev));
 		return -ENOENT;
 	}
 
@@ -184,7 +184,7 @@ static void sbdd_delete(void)
 	}
 
 	if (__sbdd.bdev) {
-		pr_info("release a handle on the %s\n", TARGET_DEV);
+		pr_info("release a handle on the %s\n", __sbdd_disk);
 		blkdev_put(__sbdd.bdev, SBDD_BDEV_MODE);
 	}
 
@@ -245,6 +245,9 @@ module_init(sbdd_init);
 /* Called on module unloading. Unloading module is not allowed without it. */
 module_exit(sbdd_exit);
 
+/* Set desired target disk with insmod */
+module_param_named(disk, __sbdd_disk, charp, S_IRUGO);
+
 /* Note for the kernel: a free license module. A warning will be outputted without it. */
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Simple Block Device Driver");
-- 
cgit v1.2.3-18-g5258