From patchwork Mon Jun 14 09:44:16 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 105904 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o5E9iNmZ029234 for ; Mon, 14 Jun 2010 09:44:23 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755921Ab0FNJnx (ORCPT ); Mon, 14 Jun 2010 05:43:53 -0400 Received: from smtp128.sbc.mail.sp1.yahoo.com ([69.147.65.187]:32721 "HELO smtp128.sbc.mail.sp1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1755662Ab0FNJnx (ORCPT ); Mon, 14 Jun 2010 05:43:53 -0400 Received: (qmail 37951 invoked from network); 14 Jun 2010 09:43:51 -0000 Received: from adsl-70-231-239-201.dsl.snfc21.sbcglobal.net (nab@70.231.239.201 with login) by smtp128.sbc.mail.sp1.yahoo.com with SMTP; 14 Jun 2010 02:43:51 -0700 PDT X-Yahoo-SMTP: fzDSGlOswBCWnIOrNw7KwwK1j9PqyNbe5PtLKiS4dDU.UNl_t6bdEZu9tTLW X-YMail-OSG: 7eIfsaAVM1kt2HBsFrvY4fDgjkBFXQsTb4ZrPH5JXGBAVbSSSMTIPBs4pF4k.S2E78KvkNW1kAnlyXZF6CY0yu_z844jdsJ.BrIBGkPF6Fc_fPPG0ehNy_4lF8Mr2NOZjuF6Sgw9ZuAZdqS2JKC3af3aoClMJK458sQozI6K.gH5Xe8HvuWP9GWaYdk7NG1OPfXM4tm3DBtKHbK0Vz12HBT4d0XkmbxA.cYcfrVN5aTiN025BGLuNw5IDeNYPbckoWS45gML2o2jMlrCkkyCx0tcqT58KVAXu0tNV0oMAb.DP0GuiCpONh.QLTL4uJLLPGIZB5.lg2RoRQHxsE20ricLNvi2Ybtjbc6g6jEaomzeSA-- X-Yahoo-Newman-Property: ymail-3 From: "Nicholas A. Bellinger" To: Gerd Hoffmann , Kevin Wolf , FUJITA Tomonori Cc: Hannes Reinecke , Paul Brook , Christoph Hellwig , kvm-devel , qemu-devel , Nicholas Bellinger Subject: [PATCH 1/5] [block]: Add top level BSG support Date: Mon, 14 Jun 2010 02:44:16 -0700 Message-Id: <1276508656-3024-1-git-send-email-nab@linux-iscsi.org> X-Mailer: git-send-email 1.5.6.5 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 14 Jun 2010 09:44:23 +0000 (UTC) diff --git a/block.c b/block.c index 88dbc00..3cd18ec 100644 --- a/block.c +++ b/block.c @@ -285,8 +285,12 @@ static BlockDriver *find_protocol(const char *filename) { BlockDriver *drv1; char protocol[128]; - int len; + int len, bsg = 0; const char *p; +#if defined(__linux__) + struct stat st; +#endif + /* TODO Drivers without bdrv_file_open must be specified explicitly */ @@ -296,7 +300,15 @@ static BlockDriver *find_protocol(const char *filename) return bdrv_find_format("file"); #endif p = strchr(filename, ':'); - if (!p) { +#if defined(__linux__) + if (stat(filename, &st) < 0) + return NULL; + /* This is not yet defined in include/linux/major.h.. */ + if (S_ISCHR(st.st_mode) && major(st.st_rdev) == 254) + bsg = 1; +#endif + + if (!p || bsg) { drv1 = find_hdev_driver(filename); if (!drv1) { drv1 = bdrv_find_format("file"); @@ -1209,7 +1221,12 @@ int bdrv_is_read_only(BlockDriverState *bs) int bdrv_is_sg(BlockDriverState *bs) { - return bs->sg; + return bs->sg == BDS_SCSI_GENERIC; +} + +int bdrv_is_bsg(BlockDriverState *bs) +{ + return bs->sg == BDS_BSG; } int bdrv_enable_write_cache(BlockDriverState *bs) diff --git a/block.h b/block.h index f87d24e..4faeedc 100644 --- a/block.h +++ b/block.h @@ -146,6 +146,7 @@ int bdrv_get_translation_hint(BlockDriverState *bs); int bdrv_is_removable(BlockDriverState *bs); int bdrv_is_read_only(BlockDriverState *bs); int bdrv_is_sg(BlockDriverState *bs); +int bdrv_is_bsg(BlockDriverState *bs); int bdrv_enable_write_cache(BlockDriverState *bs); int bdrv_is_inserted(BlockDriverState *bs); int bdrv_media_changed(BlockDriverState *bs); diff --git a/block/raw-posix.c b/block/raw-posix.c index 1515ca9..d349109 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -52,6 +52,7 @@ #include #include #include +#include #endif #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) #include @@ -843,6 +844,9 @@ static int hdev_probe_device(const char *filename) static int hdev_open(BlockDriverState *bs, const char *filename, int flags) { BDRVRawState *s = bs->opaque; +#if defined(__linux__) + struct stat st; +#endif #ifdef CONFIG_COCOA if (strstart(filename, "/dev/cdrom", NULL)) { @@ -873,8 +877,17 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) s->type = FTYPE_FILE; #if defined(__linux__) - if (strstart(filename, "/dev/sg", NULL)) { - bs->sg = 1; + if (stat(filename, &st) < 0) { + printf("stat() failed errno: %d\n", errno); + return -1; + } + 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; + } } #endif diff --git a/block_int.h b/block_int.h index 1a7240c..74bcb1a 100644 --- a/block_int.h +++ b/block_int.h @@ -40,6 +40,10 @@ #define BLOCK_OPT_CLUSTER_SIZE "cluster_size" #define BLOCK_OPT_PREALLOC "preallocation" +#define BDS_NONE 0 +#define BDS_SCSI_GENERIC 1 +#define BDS_BSG 2 + typedef struct AIOPool { void (*cancel)(BlockDriverAIOCB *acb); int aiocb_size; @@ -141,6 +145,7 @@ struct BlockDriverState { int encrypted; /* if true, the media is encrypted */ int valid_key; /* if true, a valid encryption key has been set */ int sg; /* if true, the device is a /dev/sg* */ + int fd; /* Used for BSG file descriptor */ /* event callback when inserting/removing */ void (*change_cb)(void *opaque); void *change_opaque;