qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Nicholas A. Bellinger" <nab@linux-iscsi.org>
To: Christoph Hellwig <hch@lst.de>,
	FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>,
	Hannes Reinecke <hare@suse.de>
Cc: Kevin Wolf <kwolf@redhat.com>, kvm-devel <kvm@vger.kernel.org>,
	qemu-devel <qemu-devel@nongnu.org>,
	Nicholas Bellinger <nab@linux-iscsi.org>,
	Gerd Hoffmann <kraxel@redhat.com>,
	Mark Harvey <markh794@gmail.com>
Subject: [Qemu-devel] [PATCH] block: Make BSG detection more sane in hdev_open()
Date: Sat, 21 Aug 2010 16:01:15 -0700	[thread overview]
Message-ID: <1282431675-16021-1-git-send-email-nab@linux-iscsi.org> (raw)

From: Nicholas Bellinger <nab@linux-iscsi.org>

Greetings hch, tomo and Co,

This patch changes the Linux BSG backstore detect logic in hdev_open()
in order to determine when to actually set 'bs->sg = BDS_BSG;' by obtaining
the BSG major from a SysFS attribute in /sys/class/bsg/$H:C:T:L/dev,
instead of the original hardcoded 254 major check.

This patch has been tested with Linux KVM guest v2.6.26 using the megasas
HBA emulation SGL passthrough from a TCM_Loop IBLOCK backstore running on
v2.6.35 x86_64.

Thanks to Mark Harvey for initially contributing code for doing this with
tgt.git/usr/bs_sg.c!

Thanks!

Signed-off-by: Nicholas A. Bellinger <nab@linux-iscsi.org>
---
 block/raw-posix.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index e7afc4a..487e7f0 100644
+++ b/block/raw-posix.c
@@ -842,7 +842,10 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
 {
     BDRVRawState *s = bs->opaque;
 #if defined(__linux__)
-    struct stat st;
+    struct stat st, st2;
+    FILE *file;
+    char major[8], dev[64], path[128], *p, *buf;
+    int ch, i;
 #endif
 
 #ifdef CONFIG_COCOA
@@ -881,11 +884,51 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
     if (S_ISCHR(st.st_mode)) {
         if (major(st.st_rdev) == SCSI_GENERIC_MAJOR) {
             bs->sg = BDS_SCSI_GENERIC;
-        } else if (major(st.st_rdev) == 254) {
-            /* This is not yet defined in include/linux/major.h.. */
-            bs->sg = BDS_BSG;
+        } else { /* Extract major for BSG backstore usage */
+            memset(major, 0, 8);
+            memset(dev, 0, 64);
+            memset(path, 0, 128);
+
+            buf = strdup(filename);
+            if (!(buf))
+                goto out;
+            /*
+             * Locate the device name from the path, we are interested
+             * in the last strsep() token..
+             */
+            while ((p = strsep(&buf, "/")))
+                snprintf(dev, 64, "%s", p);
+            /*
+             * Check to sure the sysfs entry exists before calling open
+             */
+            snprintf(path, 128, "/sys/class/bsg/%s/dev", dev);
+            if (stat(path, &st2) < 0)
+                goto out;
+
+            file = fopen(path, "r");
+            if (!(file)) {
+                printf("fopen() failed for BSG sysfs path: %s\n", path);
+                goto out;
+            }
+            ch = fgetc(file);
+            for (i = 0; i < 7; i++) {
+                if (ch == ':') {
+                    major[i] = '\0';
+                    break;
+                }
+                major[i] = ch;
+                ch = fgetc(file);
+            }
+            fclose(file);
+            /*
+             * If the major returned by /sys/class/bsg/$H:C:T:L/dev matches
+             * stat(), then we signal BDS_BSG usage.
+             */ 
+            if (major(st.st_rdev) == atoi(major))
+                bs->sg = BDS_BSG;
         }
     }
+out:
 #endif
 
     return raw_open_common(bs, filename, flags, 0);
-- 
1.5.6.5

             reply	other threads:[~2010-08-21 23:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-21 23:01 Nicholas A. Bellinger [this message]
2010-08-21 23:10 ` [Qemu-devel] Re: [PATCH] block: Make BSG detection more sane in hdev_open() Nicholas A. Bellinger
2010-08-22  8:31 ` Christoph Hellwig
2010-08-23  0:27   ` Nicholas A. Bellinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1282431675-16021-1-git-send-email-nab@linux-iscsi.org \
    --to=nab@linux-iscsi.org \
    --cc=fujita.tomonori@lab.ntt.co.jp \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kraxel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwolf@redhat.com \
    --cc=markh794@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).