From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=42660 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OmxGh-0008Sq-1Y for qemu-devel@nongnu.org; Sat, 21 Aug 2010 19:14:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OmxGf-0005CI-T8 for qemu-devel@nongnu.org; Sat, 21 Aug 2010 19:14:30 -0400 Received: from smtp126.sbc.mail.sp1.yahoo.com ([69.147.65.185]:39149) by eggs.gnu.org with smtp (Exim 4.69) (envelope-from ) id 1OmxGf-0005CC-IF for qemu-devel@nongnu.org; Sat, 21 Aug 2010 19:14:29 -0400 From: "Nicholas A. Bellinger" In-Reply-To: <1282431675-16021-1-git-send-email-nab@linux-iscsi.org> References: <1282431675-16021-1-git-send-email-nab@linux-iscsi.org> Content-Type: text/plain Date: Sat, 21 Aug 2010 16:10:46 -0700 Message-Id: <1282432246.30453.519.camel@haakon2.linux-iscsi.org> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] Re: [PATCH] block: Make BSG detection more sane in hdev_open() List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: linux-iscsi-target-dev@googlegroups.com Cc: Kevin Wolf , kvm-devel , qemu-devel , Mark Harvey , FUJITA Tomonori , Gerd Hoffmann , Christoph Hellwig , Hannes Reinecke On Sat, 2010-08-21 at 16:01 -0700, Nicholas A. Bellinger wrote: > From: Nicholas Bellinger > > 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 > --- > 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; Ugh, one quick followup on this to address missing free(buf) calls from the usage of buf = strdup(filename). http://git.kernel.org/?p=virt/kvm/nab/qemu-kvm.git;a=commitdiff;h=9cc774b1766c07065dee4637febe9f7d9237ff7e Thanks, --nab > + /* > + * 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 >