[stgt] RFC [patch] Search /sys/class/bsg/<ctl>/dev for bsg major number.

Mark Harvey markh794 at gmail.com
Sat Aug 21 08:07:37 CEST 2010


Apologies for including this patch as an attachment..
I haven't figured out how to successfully do it from the office.

Included inline for easier reading / review.

On Fri, Aug 20, 2010 at 7:29 PM, Nicholas A. Bellinger
<nab at linux-iscsi.org> wrote:
> On Fri, 2010-08-20 at 11:22 +0900, FUJITA Tomonori wrote:
>> On Thu, 19 Aug 2010 12:51:54 -0700
>> "Nicholas A. Bellinger" <nab at linux-iscsi.org> wrote:
>> > So it looks like the failure is attributed to my hardcoding of major 254
>> > in the original STGT/BSG patch (note the comment above the conditional
>> > check).  Here is a quick fix:
>> >
>> > diff --git a/usr/bs_sg.c b/usr/bs_sg.c
>> > index cda5cd2..a369570 100644
>> > --- a/usr/bs_sg.c
>> > +++ b/usr/bs_sg.c
>> > @@ -289,7 +289,7 @@ static int chk_sg_device(char *path)
>> >                 return 0;
>> >
>> >         /* This is not yet defined in include/linux/major.h.. */
>> > -       if (major(st.st_rdev) == 254)
>> > +       if ((major(st.st_rdev) == 254) || (major(st.st_rdev) == 253))
>> >                 return 1;
>> >
>> >         return -1;
>>
>> Well, seems that nobody uses the bsg support except for you. :)
>>
>> The above patch is still wrong.
>>
>> bsg major number can be anything. So you need to get it from
>> /proc/devices.
>>
>> Or you can also get it from /sys/class/bsg/, which is created by
>> kernel so available on any distributions.
>>
>> You can steal the bsg device handling code from sg3-utils. Maybe
>> someone could create something like libbsg.so
>
> Hmmm indeed, thanks for point this one out.   I will have look at
> adressing for this the BSG efforts with STGT and QEMU-KVM using the
> recommended major:minor output from /sys/class/bsg/$HCTL/dev.
>
> Best,
>
> --nab
>
>

>From a90611630879205ef497821552474e894d463c94 Mon Sep 17 00:00:00 2001
From: Mark Harvey <markh794 at gmail.com>
Date: Sat, 21 Aug 2010 15:22:48 +1000
Subject: Check bsg major number from /sys/class/bsg/<c:t:l>/dev

Signed-off-by: Mark Harvey <markh794 at gmail.com>
---
 usr/bs_sg.c |   35 ++++++++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/usr/bs_sg.c b/usr/bs_sg.c
index dfd49c3..7fc9787 100644
--- a/usr/bs_sg.c
+++ b/usr/bs_sg.c
@@ -269,6 +269,34 @@ static void bs_sg_cmd_complete(int fd, int
events, void *data)
 	cmd->scsi_cmd_done(cmd, io_hdr.status);
 }

+static int get_bsg_major(char *path)
+{
+	FILE *devfd;
+	char majorno[8];
+	char dev[128];
+	int ch;
+	int i;
+
+	dev[0] = '\0';
+	strncat(dev, "/sys/class/bsg/", 16);
+	strncat(dev, &path[9], 64);
+	strncat(dev, "/dev", 5);
+	devfd = fopen(dev, "r");
+	if (!devfd)
+		return -1;
+	ch = fgetc(devfd);
+	for (i = 0; i < 7; i++) {
+		if (ch == ':') {
+			majorno[i] = '\0';
+			break;
+		}
+		majorno[i] = ch;
+		ch = fgetc(devfd);
+	}
+	fclose(devfd);
+	return atoi(majorno);
+}
+
 static int chk_sg_device(char *path)
 {
 	struct stat st;
@@ -287,9 +315,10 @@ static int chk_sg_device(char *path)
 	if (major(st.st_rdev) == SCSI_GENERIC_MAJOR)
 		return 0;

-	/* This is not yet defined in include/linux/major.h.. */
-	if (major(st.st_rdev) == 254)
-		return 1;
+	if (!strncmp("/dev/bsg", path, 8)) {
+		if (major(st.st_rdev) == get_bsg_major(path))
+			return 0;
+	}

 	return -1;
 }
-- 
1.6.3.3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Check-bsg-major-number-from-sys-class-bsg-c-t-l-dev.patch
Type: application/octet-stream
Size: 1510 bytes
Desc: not available
URL: <http://lists.wpkg.org/pipermail/stgt/attachments/20100821/3bb19289/attachment-0001.obj>


More information about the stgt mailing list