From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:33805) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLWnb-00005M-Nq for qemu-devel@nongnu.org; Tue, 09 Oct 2012 06:12:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TLWnW-0005y6-Ay for qemu-devel@nongnu.org; Tue, 09 Oct 2012 06:12:27 -0400 Received: from [222.73.24.84] (port=62763 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLWnW-0005vu-1J for qemu-devel@nongnu.org; Tue, 09 Oct 2012 06:12:22 -0400 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id q999g6aT026901 for ; Tue, 9 Oct 2012 17:42:06 +0800 From: "Chen HanXiao" Date: Tue, 9 Oct 2012 17:42:01 +0800 Message-ID: <00c501cda602$583de850$08b9b8f0$@cn.fujitsu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii" Content-language: zh-cn Subject: [Qemu-devel] [PATCH] Use SCSI command to get size of SG device List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org When we use SCSI generic device as disk image, function lseek could not get the size of this kind of device. So try to use SCSI command Read Capacity(10) when lseek failed to get the size of SCSI generic device. Signed-off-by: Chen Hanxiao --- block/raw-posix.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 44 insertions(+), 2 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index 28d439f..0be7db8 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -53,6 +53,7 @@ #include #include #include +#include #endif #ifdef CONFIG_FIEMAP #include @@ -147,6 +148,7 @@ typedef struct BDRVRawReopenState { } BDRVRawReopenState; static int fd_open(BlockDriverState *bs); +static int64_t raw_getlength_ioctl(int fd); static int64_t raw_getlength(BlockDriverState *bs); #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) @@ -653,13 +655,53 @@ static int64_t raw_getlength(BlockDriverState *bs) { BDRVRawState *s = bs->opaque; int ret; - + int64_t len; + ret = fd_open(bs); if (ret < 0) { return ret; } - return lseek(s->fd, 0, SEEK_END); + len = lseek(s->fd, 0, SEEK_END); + if ( len >= 0) { + return len; + } else { + len = raw_getlength_ioctl(s->fd); + return len; + } +} + +/* Use SCSI Read Capacity(10) Command to get length */ +static int64_t raw_getlength_ioctl(int fd) +{ + unsigned char CDB[10] = + {0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char sense_buffer[32]; + unsigned char resp_buffer[32]; + uint32_t block_size; + uint64_t last_blk_addr; + struct sg_io_hdr io_hdr; + int64_t ret; + + memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); + memset(sense_buffer, 0, sizeof(sense_buffer)); + memset(sense_buffer, 0, sizeof(resp_buffer)); + io_hdr.interface_id = 'S'; + io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; + io_hdr.cmd_len = sizeof(CDB); + io_hdr.cmdp = CDB; + io_hdr.sbp = sense_buffer; + io_hdr.dxferp = resp_buffer; + io_hdr.dxfer_len = sizeof(resp_buffer); + if((ret = ioctl(fd, SG_IO, &io_hdr)) < 0) + return ret; + + last_blk_addr = ((resp_buffer[0] << 24) | (resp_buffer[1] << 16) | + (resp_buffer[2] << 8) | resp_buffer[3]); + block_size = ((resp_buffer[4] << 24) | (resp_buffer[5] << 16) | + (resp_buffer[6] << 8) | resp_buffer[7]); + ret = (int64_t)((last_blk_addr + 1) * block_size); + return ret; } #endif -- 1.7.1