ia64/xen-unstable

changeset 11429:323a89a3c01c

[blktap] Fix block device getsize bug in qcow when backing_filename is a block device.
author jchesterfield@dhcp92.uk.xensource.com
date Tue Sep 05 15:35:42 2006 +0100 (2006-09-05)
parents 45746c770018
children 0419253c81de
files tools/blktap/drivers/block-qcow.c
line diff
     1.1 --- a/tools/blktap/drivers/block-qcow.c	Tue Sep 05 14:28:19 2006 +0100
     1.2 +++ b/tools/blktap/drivers/block-qcow.c	Tue Sep 05 15:35:42 2006 +0100
     1.3 @@ -237,6 +237,25 @@ static uint32_t gen_cksum(char *ptr, int
     1.4  	return ret;
     1.5  }
     1.6  
     1.7 +static int get_filesize(char *filename, uint64_t *size, struct stat *st)
     1.8 +{
     1.9 +	int blockfd;
    1.10 +
    1.11 +	/*Set to the backing file size*/
    1.12 +	if(S_ISBLK(st->st_mode)) {
    1.13 +		blockfd = open(filename, O_RDONLY);
    1.14 +		if (blockfd < 0)
    1.15 +			return -1;
    1.16 +		if (ioctl(blockfd,BLKGETSIZE,size)!=0) {
    1.17 +			printf("Unable to get Block device size\n");
    1.18 +			close(blockfd);
    1.19 +			return -1;
    1.20 +		}
    1.21 +		close(blockfd);
    1.22 +	} else *size = (st->st_size >> SECTOR_SHIFT);	
    1.23 +	return 0;
    1.24 +}
    1.25 +
    1.26  static int qcow_set_key(struct td_state *bs, const char *key)
    1.27  {
    1.28  	struct tdqcow_state *s = (struct tdqcow_state *)bs->private;
    1.29 @@ -1204,12 +1223,14 @@ int qcow_create(const char *filename, ui
    1.30  			header_size += backing_filename_len;
    1.31  			
    1.32  			/*Set to the backing file size*/
    1.33 -			size = (st.st_size >> SECTOR_SHIFT);
    1.34 +			if(get_filesize(backing_filename, &size, &st)) {
    1.35 +				return -1;
    1.36 +			}
    1.37  			DPRINTF("Backing file size detected: %lld sectors" 
    1.38  				"(total %lld [%lld MB])\n", 
    1.39 -				(long long)total_size, 
    1.40 -				(long long)(total_size << SECTOR_SHIFT), 
    1.41 -				(long long)(total_size >> 11));
    1.42 +				(long long)size, 
    1.43 +				(long long)(size << SECTOR_SHIFT), 
    1.44 +				(long long)(size >> 11));
    1.45  		} else {
    1.46  			backing_file = NULL;
    1.47  			DPRINTF("Setting file size: %lld (total %lld)\n",