qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/3] dmg updates
@ 2010-05-07 14:55 Christoph Hellwig
  2010-05-07 14:55 ` [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-07 14:55 UTC (permalink / raw)
  To: qemu-devel

This patchset fixes a bug found through code inspection in dmg,
and converts it to the qemu block driver API.  Note that I have no
way to actually generate dmg images, so it's entirely tested.

And compared to the dmg2img tool it seems we're missing support for
a lot of features in the dmg format, so I'm not even sure images
generated on recent MacOS version will work at all.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks
  2010-05-07 14:55 [Qemu-devel] [PATCH 0/3] dmg updates Christoph Hellwig
@ 2010-05-07 14:55 ` Christoph Hellwig
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 3/3] dmg: use qemu block API Christoph Hellwig
  2 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-07 14:55 UTC (permalink / raw)
  To: qemu-devel

When dmg_read_chunk encounters an uncompressed chunk it currently
calls read without any previous adjustment of the file postion.

This seems very wrong, and the "reference" implementation in
dmg2img does a search to the same offset as done in the various
compression cases, so do the same here.

Signed-off-by: Christoph Hellwig <hch@lst.de>

Index: qemu-kevin/block/dmg.c
===================================================================
--- qemu-kevin.orig/block/dmg.c	2010-05-03 13:15:34.112253995 +0200
+++ qemu-kevin/block/dmg.c	2010-05-03 13:17:40.696287171 +0200
@@ -239,7 +239,8 @@ static inline int dmg_read_chunk(BDRVDMG
 		return -1;
 	    break; }
 	case 1: /* copy */
-	    ret = read(s->fd, s->uncompressed_chunk, s->lengths[chunk]);
+	    ret = pread(s->fd, s->uncompressed_chunk, s->lengths[chunk],
+                        s->offsets[chunk]);
 	    if (ret != s->lengths[chunk])
 		return -1;
 	    break;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] [PATCH 2/3] dmg: use pread
  2010-05-07 14:55 [Qemu-devel] [PATCH 0/3] dmg updates Christoph Hellwig
  2010-05-07 14:55 ` [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks Christoph Hellwig
@ 2010-05-07 14:56 ` Christoph Hellwig
  2010-05-10 10:07   ` Kevin Wolf
  2010-05-12 14:31   ` [Qemu-devel] [PATCH 2/3 v2] " Christoph Hellwig
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 3/3] dmg: use qemu block API Christoph Hellwig
  2 siblings, 2 replies; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-07 14:56 UTC (permalink / raw)
  To: qemu-devel

Use pread instead of lseek + read in preparation of using the qemu
block API.  Note that dmg actually uses the implicit file offset
a lot in dmg_open, and we had to replace it with an offset variable.

Signed-off-by: Christoph Hellwig <hch@lst.de>

Index: qemu-kevin/block/dmg.c
===================================================================
--- qemu-kevin.orig/block/dmg.c	2010-05-03 13:17:40.696287171 +0200
+++ qemu-kevin/block/dmg.c	2010-05-03 13:29:39.560024006 +0200
@@ -58,18 +58,18 @@ static int dmg_probe(const uint8_t *buf,
     return 0;
 }
 
-static off_t read_off(int fd)
+static off_t read_off(int fd, int64_t offset)
 {
 	uint64_t buffer;
-	if(read(fd,&buffer,8)<8)
+	if (pread(fd, &buffer, 8, offset) < 8)
 		return 0;
 	return be64_to_cpu(buffer);
 }
 
-static off_t read_uint32(int fd)
+static off_t read_uint32(int fd, int64_t offset)
 {
 	uint32_t buffer;
-	if(read(fd,&buffer,4)<4)
+	if (pread(fd, &buffer, 4, offset) < 4)
 		return 0;
 	return be32_to_cpu(buffer);
 }
@@ -80,6 +80,7 @@ static int dmg_open(BlockDriverState *bs
     off_t info_begin,info_end,last_in_offset,last_out_offset;
     uint32_t count;
     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
+    int64_t offset;
 
     s->fd = open(filename, O_RDONLY | O_BINARY);
     if (s->fd < 0)
@@ -89,38 +90,45 @@ static int dmg_open(BlockDriverState *bs
     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
 
     /* read offset of info blocks */
-    if(lseek(s->fd,-0x1d8,SEEK_END)<0) {
+    offset = lseek(s->fd, -0x1d8, SEEK_END);
+    if (offset < 0) {
         goto fail;
     }
 
-    info_begin=read_off(s->fd);
-    if(info_begin==0)
-	goto fail;
-    if(lseek(s->fd,info_begin,SEEK_SET)<0)
-	goto fail;
-    if(read_uint32(s->fd)!=0x100)
-	goto fail;
-    if((count = read_uint32(s->fd))==0)
-	goto fail;
-    info_end = info_begin+count;
-    if(lseek(s->fd,0xf8,SEEK_CUR)<0)
+    info_begin = read_off(s->fd, offset);
+    if (info_begin == 0) {
 	goto fail;
+    }
+
+    if (read_uint32(s->fd, info_begin) != 0x100) {
+        goto fail;
+    }
+
+    count = read_uint32(s->fd, info_begin + 4);
+    if (count == 0) {
+        goto fail;
+    }
+    info_end = info_begin + count;
+
+    offset = info_begin + 0xfc;
 
     /* read offsets */
     last_in_offset = last_out_offset = 0;
-    while(lseek(s->fd,0,SEEK_CUR)<info_end) {
+    while (offset < info_end) {
         uint32_t type;
 
-	count = read_uint32(s->fd);
+	count = read_uint32(s->fd, offset);
 	if(count==0)
 	    goto fail;
-	type = read_uint32(s->fd);
-	if(type!=0x6d697368 || count<244)
-	    lseek(s->fd,count-4,SEEK_CUR);
-	else {
+        offset += 4;
+
+	type = read_uint32(s->fd, offset);
+	if (type == 0x6d697368 && count >= 244) {
 	    int new_size, chunk_count;
-	    if(lseek(s->fd,200,SEEK_CUR)<0)
-	        goto fail;
+
+            offset += 4;
+            offset += 200;
+
 	    chunk_count = (count-204)/40;
 	    new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
 	    s->types = qemu_realloc(s->types, new_size/2);
@@ -130,7 +138,8 @@ static int dmg_open(BlockDriverState *bs
 	    s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
 
 	    for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
-		s->types[i] = read_uint32(s->fd);
+		s->types[i] = read_uint32(s->fd, offset);
+		offset += 4;
 		if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
 		    if(s->types[i]==0xffffffff) {
 			last_in_offset = s->offsets[i-1]+s->lengths[i-1];
@@ -138,15 +147,24 @@ static int dmg_open(BlockDriverState *bs
 		    }
 		    chunk_count--;
 		    i--;
-		    if(lseek(s->fd,36,SEEK_CUR)<0)
-			goto fail;
+		    offset += 36;
 		    continue;
 		}
-		read_uint32(s->fd);
-		s->sectors[i] = last_out_offset+read_off(s->fd);
-		s->sectorcounts[i] = read_off(s->fd);
-		s->offsets[i] = last_in_offset+read_off(s->fd);
-		s->lengths[i] = read_off(s->fd);
+		read_uint32(s->fd, offset);
+		offset += 4;
+
+		s->sectors[i] = last_out_offset+read_off(s->fd, offset);
+		offset += 8;
+
+		s->sectorcounts[i] = read_off(s->fd, offset);
+		offset += 8;
+
+		s->offsets[i] = last_in_offset+read_off(s->fd, offset);
+		offset += 8;
+
+		s->lengths[i] = read_off(s->fd, offset);
+		offset += 8;
+
 		if(s->lengths[i]>max_compressed_size)
 		    max_compressed_size = s->lengths[i];
 		if(s->sectorcounts[i]>max_sectors_per_chunk)
@@ -210,15 +228,12 @@ static inline int dmg_read_chunk(BDRVDMG
 	case 0x80000005: { /* zlib compressed */
 	    int i;
 
-	    ret = lseek(s->fd, s->offsets[chunk], SEEK_SET);
-	    if(ret<0)
-		return -1;
-
 	    /* we need to buffer, because only the chunk as whole can be
 	     * inflated. */
 	    i=0;
 	    do {
-		ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
+		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
+                            s->offsets[chunk]);
 		if(ret<0 && errno==EINTR)
 		    ret=0;
 		i+=ret;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] [PATCH 3/3] dmg: use qemu block API
  2010-05-07 14:55 [Qemu-devel] [PATCH 0/3] dmg updates Christoph Hellwig
  2010-05-07 14:55 ` [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks Christoph Hellwig
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
@ 2010-05-07 14:56 ` Christoph Hellwig
  2010-05-12 14:31   ` [Qemu-devel] [PATCH 3/3 v2] " Christoph Hellwig
  2 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-07 14:56 UTC (permalink / raw)
  To: qemu-devel

Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.

Dmg actually does an lseek to a negative offset in the open routine,
which we replace with offset arithmetics after doing a bdrv_getlength.

Signed-off-by: Christoph Hellwig <hch@lst.de>


Index: qemu-kevin/block/dmg.c
===================================================================
--- qemu-kevin.orig/block/dmg.c	2010-05-07 16:44:10.487253603 +0200
+++ qemu-kevin/block/dmg.c	2010-05-07 16:44:10.829253954 +0200
@@ -28,8 +28,6 @@
 #include <zlib.h>
 
 typedef struct BDRVDMGState {
-    int fd;
-
     /* each chunk contains a certain number of sectors,
      * offsets[i] is the offset in the .dmg file,
      * lengths[i] is the length of the compressed chunk,
@@ -58,23 +56,23 @@ static int dmg_probe(const uint8_t *buf,
     return 0;
 }
 
-static off_t read_off(int fd, int64_t offset)
+static off_t read_off(BlockDriverState *bs, int64_t offset)
 {
 	uint64_t buffer;
-	if (pread(fd, &buffer, 8, offset) < 8)
+	if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
 		return 0;
 	return be64_to_cpu(buffer);
 }
 
-static off_t read_uint32(int fd, int64_t offset)
+static off_t read_uint32(BlockDriverState *bs, int64_t offset)
 {
 	uint32_t buffer;
-	if (pread(fd, &buffer, 4, offset) < 4)
+	if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
 		return 0;
 	return be32_to_cpu(buffer);
 }
 
-static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
+static int dmg_open(BlockDriverState *bs, int flags)
 {
     BDRVDMGState *s = bs->opaque;
     off_t info_begin,info_end,last_in_offset,last_out_offset;
@@ -82,29 +80,27 @@ static int dmg_open(BlockDriverState *bs
     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
     int64_t offset;
 
-    s->fd = open(filename, O_RDONLY | O_BINARY);
-    if (s->fd < 0)
-        return -errno;
     bs->read_only = 1;
     s->n_chunks = 0;
     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
 
     /* read offset of info blocks */
-    offset = lseek(s->fd, -0x1d8, SEEK_END);
+    offset = bdrv_getlength(bs->file);
     if (offset < 0) {
         goto fail;
     }
+    offset -= 0x1d8;
 
-    info_begin = read_off(s->fd, offset);
+    info_begin = read_off(bs, offset);
     if (info_begin == 0) {
 	goto fail;
     }
 
-    if (read_uint32(s->fd, info_begin) != 0x100) {
+    if (read_uint32(bs, info_begin) != 0x100) {
         goto fail;
     }
 
-    count = read_uint32(s->fd, info_begin + 4);
+    count = read_uint32(bs, info_begin + 4);
     if (count == 0) {
         goto fail;
     }
@@ -117,12 +113,12 @@ static int dmg_open(BlockDriverState *bs
     while (offset < info_end) {
         uint32_t type;
 
-	count = read_uint32(s->fd, offset);
+	count = read_uint32(bs, offset);
 	if(count==0)
 	    goto fail;
         offset += 4;
 
-	type = read_uint32(s->fd, offset);
+	type = read_uint32(bs, offset);
 	if (type == 0x6d697368 && count >= 244) {
 	    int new_size, chunk_count;
 
@@ -138,7 +134,7 @@ static int dmg_open(BlockDriverState *bs
 	    s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
 
 	    for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
-		s->types[i] = read_uint32(s->fd, offset);
+		s->types[i] = read_uint32(bs, offset);
 		offset += 4;
 		if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
 		    if(s->types[i]==0xffffffff) {
@@ -150,19 +146,19 @@ static int dmg_open(BlockDriverState *bs
 		    offset += 36;
 		    continue;
 		}
-		read_uint32(s->fd, offset);
+		read_uint32(bs, offset);
 		offset += 4;
 
-		s->sectors[i] = last_out_offset+read_off(s->fd, offset);
+		s->sectors[i] = last_out_offset+read_off(bs, offset);
 		offset += 8;
 
-		s->sectorcounts[i] = read_off(s->fd, offset);
+		s->sectorcounts[i] = read_off(bs, offset);
 		offset += 8;
 
-		s->offsets[i] = last_in_offset+read_off(s->fd, offset);
+		s->offsets[i] = last_in_offset+read_off(bs, offset);
 		offset += 8;
 
-		s->lengths[i] = read_off(s->fd, offset);
+		s->lengths[i] = read_off(bs, offset);
 		offset += 8;
 
 		if(s->lengths[i]>max_compressed_size)
@@ -184,7 +180,6 @@ static int dmg_open(BlockDriverState *bs
 
     return 0;
 fail:
-    close(s->fd);
     return -1;
 }
 
@@ -214,8 +209,10 @@ static inline uint32_t search_chunk(BDRV
     return s->n_chunks; /* error */
 }
 
-static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
+static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
 {
+    BDRVDMGState *s = bs->opaque;
+
     if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
 	int ret;
 	uint32_t chunk = search_chunk(s,sector_num);
@@ -232,8 +229,8 @@ static inline int dmg_read_chunk(BDRVDMG
 	     * inflated. */
 	    i=0;
 	    do {
-		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
-                            s->offsets[chunk]);
+		ret = bdrv_pread(bs->file, s->offsets[chunk],
+                                 s->compressed_chunk+i, s->lengths[chunk]-i);
 		if(ret<0 && errno==EINTR)
 		    ret=0;
 		i+=ret;
@@ -254,8 +251,8 @@ static inline int dmg_read_chunk(BDRVDMG
 		return -1;
 	    break; }
 	case 1: /* copy */
-	    ret = pread(s->fd, s->uncompressed_chunk, s->lengths[chunk],
-                        s->offsets[chunk]);
+	    ret = bdrv_pread(bs->file, s->offsets[chunk],
+                             s->uncompressed_chunk, s->lengths[chunk]);
 	    if (ret != s->lengths[chunk])
 		return -1;
 	    break;
@@ -276,7 +273,7 @@ static int dmg_read(BlockDriverState *bs
 
     for(i=0;i<nb_sectors;i++) {
 	uint32_t sector_offset_in_chunk;
-	if(dmg_read_chunk(s, sector_num+i) != 0)
+	if(dmg_read_chunk(bs, sector_num+i) != 0)
 	    return -1;
 	sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
 	memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
@@ -287,7 +284,6 @@ static int dmg_read(BlockDriverState *bs
 static void dmg_close(BlockDriverState *bs)
 {
     BDRVDMGState *s = bs->opaque;
-    close(s->fd);
     if(s->n_chunks>0) {
 	free(s->types);
 	free(s->offsets);
@@ -304,7 +300,7 @@ static BlockDriver bdrv_dmg = {
     .format_name	= "dmg",
     .instance_size	= sizeof(BDRVDMGState),
     .bdrv_probe		= dmg_probe,
-    .bdrv_file_open	= dmg_open,
+    .bdrv_open		= dmg_open,
     .bdrv_read		= dmg_read,
     .bdrv_close		= dmg_close,
 };

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH 2/3] dmg: use pread
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
@ 2010-05-10 10:07   ` Kevin Wolf
  2010-05-10 20:20     ` Christoph Hellwig
  2010-05-12 14:31   ` [Qemu-devel] [PATCH 2/3 v2] " Christoph Hellwig
  1 sibling, 1 reply; 10+ messages in thread
From: Kevin Wolf @ 2010-05-10 10:07 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: qemu-devel

Am 07.05.2010 16:56, schrieb Christoph Hellwig:
> Use pread instead of lseek + read in preparation of using the qemu
> block API.  Note that dmg actually uses the implicit file offset
> a lot in dmg_open, and we had to replace it with an offset variable.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> 
> Index: qemu-kevin/block/dmg.c
> ===================================================================
> --- qemu-kevin.orig/block/dmg.c	2010-05-03 13:17:40.696287171 +0200
> +++ qemu-kevin/block/dmg.c	2010-05-03 13:29:39.560024006 +0200
> @@ -58,18 +58,18 @@ static int dmg_probe(const uint8_t *buf,
>      return 0;
>  }
>  
> -static off_t read_off(int fd)
> +static off_t read_off(int fd, int64_t offset)
>  {
>  	uint64_t buffer;
> -	if(read(fd,&buffer,8)<8)
> +	if (pread(fd, &buffer, 8, offset) < 8)
>  		return 0;
>  	return be64_to_cpu(buffer);
>  }
>  
> -static off_t read_uint32(int fd)
> +static off_t read_uint32(int fd, int64_t offset)
>  {
>  	uint32_t buffer;
> -	if(read(fd,&buffer,4)<4)
> +	if (pread(fd, &buffer, 4, offset) < 4)
>  		return 0;
>  	return be32_to_cpu(buffer);
>  }
> @@ -80,6 +80,7 @@ static int dmg_open(BlockDriverState *bs
>      off_t info_begin,info_end,last_in_offset,last_out_offset;
>      uint32_t count;
>      uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
> +    int64_t offset;
>  
>      s->fd = open(filename, O_RDONLY | O_BINARY);
>      if (s->fd < 0)
> @@ -89,38 +90,45 @@ static int dmg_open(BlockDriverState *bs
>      s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
>  
>      /* read offset of info blocks */
> -    if(lseek(s->fd,-0x1d8,SEEK_END)<0) {
> +    offset = lseek(s->fd, -0x1d8, SEEK_END);
> +    if (offset < 0) {
>          goto fail;
>      }
>  
> -    info_begin=read_off(s->fd);
> -    if(info_begin==0)
> -	goto fail;
> -    if(lseek(s->fd,info_begin,SEEK_SET)<0)
> -	goto fail;

We seek to info_begin.

> -    if(read_uint32(s->fd)!=0x100)
> -	goto fail;

Now we are at info_begin + 4

> -    if((count = read_uint32(s->fd))==0)
> -	goto fail;

info_begin + 8

> -    info_end = info_begin+count;
> -    if(lseek(s->fd,0xf8,SEEK_CUR)<0)

info_begin + 0x100

> +    info_begin = read_off(s->fd, offset);
> +    if (info_begin == 0) {
>  	goto fail;
> +    }
> +
> +    if (read_uint32(s->fd, info_begin) != 0x100) {
> +        goto fail;
> +    }
> +
> +    count = read_uint32(s->fd, info_begin + 4);
> +    if (count == 0) {
> +        goto fail;
> +    }
> +    info_end = info_begin + count;
> +
> +    offset = info_begin + 0xfc;

So, wrong offset here?

>  
>      /* read offsets */
>      last_in_offset = last_out_offset = 0;
> -    while(lseek(s->fd,0,SEEK_CUR)<info_end) {
> +    while (offset < info_end) {
>          uint32_t type;
>  
> -	count = read_uint32(s->fd);
> +	count = read_uint32(s->fd, offset);
>  	if(count==0)
>  	    goto fail;
> -	type = read_uint32(s->fd);
> -	if(type!=0x6d697368 || count<244)
> -	    lseek(s->fd,count-4,SEEK_CUR);
> -	else {
> +        offset += 4;
> +
> +	type = read_uint32(s->fd, offset);
> +	if (type == 0x6d697368 && count >= 244) {
>  	    int new_size, chunk_count;
> -	    if(lseek(s->fd,200,SEEK_CUR)<0)
> -	        goto fail;
> +
> +            offset += 4;

Isn't this needed in the else case, too?

> +            offset += 200;
> +
>  	    chunk_count = (count-204)/40;
>  	    new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
>  	    s->types = qemu_realloc(s->types, new_size/2);
> @@ -130,7 +138,8 @@ static int dmg_open(BlockDriverState *bs
>  	    s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
>  
>  	    for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
> -		s->types[i] = read_uint32(s->fd);
> +		s->types[i] = read_uint32(s->fd, offset);
> +		offset += 4;
>  		if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
>  		    if(s->types[i]==0xffffffff) {
>  			last_in_offset = s->offsets[i-1]+s->lengths[i-1];
> @@ -138,15 +147,24 @@ static int dmg_open(BlockDriverState *bs
>  		    }
>  		    chunk_count--;
>  		    i--;
> -		    if(lseek(s->fd,36,SEEK_CUR)<0)
> -			goto fail;
> +		    offset += 36;
>  		    continue;
>  		}
> -		read_uint32(s->fd);
> -		s->sectors[i] = last_out_offset+read_off(s->fd);
> -		s->sectorcounts[i] = read_off(s->fd);
> -		s->offsets[i] = last_in_offset+read_off(s->fd);
> -		s->lengths[i] = read_off(s->fd);
> +		read_uint32(s->fd, offset);

This read is useless. offset += 4 alone should be enough.

> +		offset += 4;
> +
> +		s->sectors[i] = last_out_offset+read_off(s->fd, offset);
> +		offset += 8;
> +
> +		s->sectorcounts[i] = read_off(s->fd, offset);
> +		offset += 8;
> +
> +		s->offsets[i] = last_in_offset+read_off(s->fd, offset);
> +		offset += 8;
> +
> +		s->lengths[i] = read_off(s->fd, offset);
> +		offset += 8;
> +
>  		if(s->lengths[i]>max_compressed_size)
>  		    max_compressed_size = s->lengths[i];
>  		if(s->sectorcounts[i]>max_sectors_per_chunk)
> @@ -210,15 +228,12 @@ static inline int dmg_read_chunk(BDRVDMG
>  	case 0x80000005: { /* zlib compressed */
>  	    int i;
>  
> -	    ret = lseek(s->fd, s->offsets[chunk], SEEK_SET);
> -	    if(ret<0)
> -		return -1;
> -
>  	    /* we need to buffer, because only the chunk as whole can be
>  	     * inflated. */
>  	    i=0;
>  	    do {
> -		ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
> +		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
> +                            s->offsets[chunk]);

This is in a loop, whereas the lseek was outside the loop. From the
second iteration on you'll repeat the first read instead of advancing.

Kevin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH 2/3] dmg: use pread
  2010-05-10 10:07   ` Kevin Wolf
@ 2010-05-10 20:20     ` Christoph Hellwig
  2010-05-11  8:00       ` Kevin Wolf
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-10 20:20 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: qemu-devel

On Mon, May 10, 2010 at 12:07:40PM +0200, Kevin Wolf wrote:
> >  
> > -    info_begin=read_off(s->fd);
> > -    if(info_begin==0)
> > -	goto fail;
> > -    if(lseek(s->fd,info_begin,SEEK_SET)<0)
> > -	goto fail;
> 
> We seek to info_begin.
> 
> > -    if(read_uint32(s->fd)!=0x100)
> > -	goto fail;
> 
> Now we are at info_begin + 4
> 
> > -    if((count = read_uint32(s->fd))==0)
> > -	goto fail;
> 
> info_begin + 8
> 
> > -    info_end = info_begin+count;
> > -    if(lseek(s->fd,0xf8,SEEK_CUR)<0)
> 
> info_begin + 0x100
> 
> > +    info_begin = read_off(s->fd, offset);
> > +    if (info_begin == 0) {
> >  	goto fail;
> > +    }
> > +
> > +    if (read_uint32(s->fd, info_begin) != 0x100) {
> > +        goto fail;
> > +    }
> > +
> > +    count = read_uint32(s->fd, info_begin + 4);
> > +    if (count == 0) {
> > +        goto fail;
> > +    }
> > +    info_end = info_begin + count;
> > +
> > +    offset = info_begin + 0xfc;
> 
> So, wrong offset here?

Yeah, should be 0x100.  That's what you get for quickly doing hex
calculation in your head.

> > +	if (type == 0x6d697368 && count >= 244) {
> >  	    int new_size, chunk_count;
> > -	    if(lseek(s->fd,200,SEEK_CUR)<0)
> > -	        goto fail;
> > +
> > +            offset += 4;
> 
> Isn't this needed in the else case, too?

I don't think so.  For that case we previously did a

	lseek(s->fd,count-4,SEEK_CUR)

to undo the 4 byte advance done by the read.

> > -		s->sectors[i] = last_out_offset+read_off(s->fd);
> > -		s->sectorcounts[i] = read_off(s->fd);
> > -		s->offsets[i] = last_in_offset+read_off(s->fd);
> > -		s->lengths[i] = read_off(s->fd);
> > +		read_uint32(s->fd, offset);
> 
> This read is useless. offset += 4 alone should be enough.

Thanks, fixed.

> >  	    /* we need to buffer, because only the chunk as whole can be
> >  	     * inflated. */
> >  	    i=0;
> >  	    do {
> > -		ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
> > +		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
> > +                            s->offsets[chunk]);
> 
> This is in a loop, whereas the lseek was outside the loop. From the
> second iteration on you'll repeat the first read instead of advancing.

You're right.  The EINTR check confused me an I took this for just
retrying reads on EINTR.  Now this code i quite nasty for error returns
except EINTR because we'll subtract one from the i loop iteration,
yikes.  I'll just reuse the i variable to keep the same kind of bug
for both sides of the equation.

God, do I hate this code..

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH 2/3] dmg: use pread
  2010-05-10 20:20     ` Christoph Hellwig
@ 2010-05-11  8:00       ` Kevin Wolf
  0 siblings, 0 replies; 10+ messages in thread
From: Kevin Wolf @ 2010-05-11  8:00 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: qemu-devel

Am 10.05.2010 22:20, schrieb Christoph Hellwig:
> On Mon, May 10, 2010 at 12:07:40PM +0200, Kevin Wolf wrote:
>>> +	if (type == 0x6d697368 && count >= 244) {
>>>  	    int new_size, chunk_count;
>>> -	    if(lseek(s->fd,200,SEEK_CUR)<0)
>>> -	        goto fail;
>>> +
>>> +            offset += 4;
>>
>> Isn't this needed in the else case, too?
> 
> I don't think so.  For that case we previously did a
> 
> 	lseek(s->fd,count-4,SEEK_CUR)
> 
> to undo the 4 byte advance done by the read.

You're right. Somehow I completely missed the original then branch and
that you changed the condition.

Kevin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] [PATCH 2/3 v2] dmg: use pread
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
  2010-05-10 10:07   ` Kevin Wolf
@ 2010-05-12 14:31   ` Christoph Hellwig
  1 sibling, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-12 14:31 UTC (permalink / raw)
  To: qemu-devel

Use pread instead of lseek + read in preparation of using the qemu
block API.  Note that dmg actually uses the implicit file offset
a lot in dmg_open, and we had to replace it with an offset variable.

Signed-off-by: Christoph Hellwig <hch@lst.de>

Index: qemu-kevin/block/dmg.c
===================================================================
--- qemu-kevin.orig/block/dmg.c	2010-05-12 16:19:59.586012160 +0200
+++ qemu-kevin/block/dmg.c	2010-05-12 16:23:07.510005522 +0200
@@ -58,18 +58,18 @@ static int dmg_probe(const uint8_t *buf,
     return 0;
 }
 
-static off_t read_off(int fd)
+static off_t read_off(int fd, int64_t offset)
 {
 	uint64_t buffer;
-	if(read(fd,&buffer,8)<8)
+	if (pread(fd, &buffer, 8, offset) < 8)
 		return 0;
 	return be64_to_cpu(buffer);
 }
 
-static off_t read_uint32(int fd)
+static off_t read_uint32(int fd, int64_t offset)
 {
 	uint32_t buffer;
-	if(read(fd,&buffer,4)<4)
+	if (pread(fd, &buffer, 4, offset) < 4)
 		return 0;
 	return be32_to_cpu(buffer);
 }
@@ -80,6 +80,7 @@ static int dmg_open(BlockDriverState *bs
     off_t info_begin,info_end,last_in_offset,last_out_offset;
     uint32_t count;
     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
+    int64_t offset;
 
     s->fd = open(filename, O_RDONLY | O_BINARY);
     if (s->fd < 0)
@@ -89,38 +90,45 @@ static int dmg_open(BlockDriverState *bs
     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
 
     /* read offset of info blocks */
-    if(lseek(s->fd,-0x1d8,SEEK_END)<0) {
+    offset = lseek(s->fd, -0x1d8, SEEK_END);
+    if (offset < 0) {
         goto fail;
     }
 
-    info_begin=read_off(s->fd);
-    if(info_begin==0)
-	goto fail;
-    if(lseek(s->fd,info_begin,SEEK_SET)<0)
-	goto fail;
-    if(read_uint32(s->fd)!=0x100)
-	goto fail;
-    if((count = read_uint32(s->fd))==0)
-	goto fail;
-    info_end = info_begin+count;
-    if(lseek(s->fd,0xf8,SEEK_CUR)<0)
+    info_begin = read_off(s->fd, offset);
+    if (info_begin == 0) {
 	goto fail;
+    }
+
+    if (read_uint32(s->fd, info_begin) != 0x100) {
+        goto fail;
+    }
+
+    count = read_uint32(s->fd, info_begin + 4);
+    if (count == 0) {
+        goto fail;
+    }
+    info_end = info_begin + count;
+
+    offset = info_begin + 0x100;
 
     /* read offsets */
     last_in_offset = last_out_offset = 0;
-    while(lseek(s->fd,0,SEEK_CUR)<info_end) {
+    while (offset < info_end) {
         uint32_t type;
 
-	count = read_uint32(s->fd);
+	count = read_uint32(s->fd, offset);
 	if(count==0)
 	    goto fail;
-	type = read_uint32(s->fd);
-	if(type!=0x6d697368 || count<244)
-	    lseek(s->fd,count-4,SEEK_CUR);
-	else {
+        offset += 4;
+
+	type = read_uint32(s->fd, offset);
+	if (type == 0x6d697368 && count >= 244) {
 	    int new_size, chunk_count;
-	    if(lseek(s->fd,200,SEEK_CUR)<0)
-	        goto fail;
+
+            offset += 4;
+            offset += 200;
+
 	    chunk_count = (count-204)/40;
 	    new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
 	    s->types = qemu_realloc(s->types, new_size/2);
@@ -130,7 +138,8 @@ static int dmg_open(BlockDriverState *bs
 	    s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
 
 	    for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
-		s->types[i] = read_uint32(s->fd);
+		s->types[i] = read_uint32(s->fd, offset);
+		offset += 4;
 		if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
 		    if(s->types[i]==0xffffffff) {
 			last_in_offset = s->offsets[i-1]+s->lengths[i-1];
@@ -138,15 +147,23 @@ static int dmg_open(BlockDriverState *bs
 		    }
 		    chunk_count--;
 		    i--;
-		    if(lseek(s->fd,36,SEEK_CUR)<0)
-			goto fail;
+		    offset += 36;
 		    continue;
 		}
-		read_uint32(s->fd);
-		s->sectors[i] = last_out_offset+read_off(s->fd);
-		s->sectorcounts[i] = read_off(s->fd);
-		s->offsets[i] = last_in_offset+read_off(s->fd);
-		s->lengths[i] = read_off(s->fd);
+		offset += 4;
+
+		s->sectors[i] = last_out_offset+read_off(s->fd, offset);
+		offset += 8;
+
+		s->sectorcounts[i] = read_off(s->fd, offset);
+		offset += 8;
+
+		s->offsets[i] = last_in_offset+read_off(s->fd, offset);
+		offset += 8;
+
+		s->lengths[i] = read_off(s->fd, offset);
+		offset += 8;
+
 		if(s->lengths[i]>max_compressed_size)
 		    max_compressed_size = s->lengths[i];
 		if(s->sectorcounts[i]>max_sectors_per_chunk)
@@ -210,15 +227,12 @@ static inline int dmg_read_chunk(BDRVDMG
 	case 0x80000005: { /* zlib compressed */
 	    int i;
 
-	    ret = lseek(s->fd, s->offsets[chunk], SEEK_SET);
-	    if(ret<0)
-		return -1;
-
 	    /* we need to buffer, because only the chunk as whole can be
 	     * inflated. */
 	    i=0;
 	    do {
-		ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
+		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
+                            s->offsets[chunk] + i);
 		if(ret<0 && errno==EINTR)
 		    ret=0;
 		i+=ret;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] [PATCH 3/3 v2] dmg: use qemu block API
  2010-05-07 14:56 ` [Qemu-devel] [PATCH 3/3] dmg: use qemu block API Christoph Hellwig
@ 2010-05-12 14:31   ` Christoph Hellwig
  2010-05-12 15:51     ` Kevin Wolf
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2010-05-12 14:31 UTC (permalink / raw)
  To: qemu-devel

Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.

Dmg actually does an lseek to a negative offset in the open routine,
which we replace with offset arithmetics after doing a bdrv_getlength.

Signed-off-by: Christoph Hellwig <hch@lst.de>


Index: qemu-kevin/block/dmg.c
===================================================================
--- qemu-kevin.orig/block/dmg.c	2010-05-12 16:23:07.510005522 +0200
+++ qemu-kevin/block/dmg.c	2010-05-12 16:28:08.370253535 +0200
@@ -28,8 +28,6 @@
 #include <zlib.h>
 
 typedef struct BDRVDMGState {
-    int fd;
-
     /* each chunk contains a certain number of sectors,
      * offsets[i] is the offset in the .dmg file,
      * lengths[i] is the length of the compressed chunk,
@@ -58,23 +56,23 @@ static int dmg_probe(const uint8_t *buf,
     return 0;
 }
 
-static off_t read_off(int fd, int64_t offset)
+static off_t read_off(BlockDriverState *bs, int64_t offset)
 {
 	uint64_t buffer;
-	if (pread(fd, &buffer, 8, offset) < 8)
+	if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
 		return 0;
 	return be64_to_cpu(buffer);
 }
 
-static off_t read_uint32(int fd, int64_t offset)
+static off_t read_uint32(BlockDriverState *bs, int64_t offset)
 {
 	uint32_t buffer;
-	if (pread(fd, &buffer, 4, offset) < 4)
+	if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
 		return 0;
 	return be32_to_cpu(buffer);
 }
 
-static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
+static int dmg_open(BlockDriverState *bs, int flags)
 {
     BDRVDMGState *s = bs->opaque;
     off_t info_begin,info_end,last_in_offset,last_out_offset;
@@ -82,29 +80,27 @@ static int dmg_open(BlockDriverState *bs
     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
     int64_t offset;
 
-    s->fd = open(filename, O_RDONLY | O_BINARY);
-    if (s->fd < 0)
-        return -errno;
     bs->read_only = 1;
     s->n_chunks = 0;
     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
 
     /* read offset of info blocks */
-    offset = lseek(s->fd, -0x1d8, SEEK_END);
+    offset = bdrv_getlength(bs->file);
     if (offset < 0) {
         goto fail;
     }
+    offset -= 0x1d8;
 
-    info_begin = read_off(s->fd, offset);
+    info_begin = read_off(bs, offset);
     if (info_begin == 0) {
 	goto fail;
     }
 
-    if (read_uint32(s->fd, info_begin) != 0x100) {
+    if (read_uint32(bs, info_begin) != 0x100) {
         goto fail;
     }
 
-    count = read_uint32(s->fd, info_begin + 4);
+    count = read_uint32(bs, info_begin + 4);
     if (count == 0) {
         goto fail;
     }
@@ -117,12 +113,12 @@ static int dmg_open(BlockDriverState *bs
     while (offset < info_end) {
         uint32_t type;
 
-	count = read_uint32(s->fd, offset);
+	count = read_uint32(bs, offset);
 	if(count==0)
 	    goto fail;
         offset += 4;
 
-	type = read_uint32(s->fd, offset);
+	type = read_uint32(bs, offset);
 	if (type == 0x6d697368 && count >= 244) {
 	    int new_size, chunk_count;
 
@@ -138,7 +134,7 @@ static int dmg_open(BlockDriverState *bs
 	    s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
 
 	    for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
-		s->types[i] = read_uint32(s->fd, offset);
+		s->types[i] = read_uint32(bs, offset);
 		offset += 4;
 		if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
 		    if(s->types[i]==0xffffffff) {
@@ -152,16 +148,16 @@ static int dmg_open(BlockDriverState *bs
 		}
 		offset += 4;
 
-		s->sectors[i] = last_out_offset+read_off(s->fd, offset);
+		s->sectors[i] = last_out_offset+read_off(bs, offset);
 		offset += 8;
 
-		s->sectorcounts[i] = read_off(s->fd, offset);
+		s->sectorcounts[i] = read_off(bs, offset);
 		offset += 8;
 
-		s->offsets[i] = last_in_offset+read_off(s->fd, offset);
+		s->offsets[i] = last_in_offset+read_off(bs, offset);
 		offset += 8;
 
-		s->lengths[i] = read_off(s->fd, offset);
+		s->lengths[i] = read_off(bs, offset);
 		offset += 8;
 
 		if(s->lengths[i]>max_compressed_size)
@@ -183,7 +179,6 @@ static int dmg_open(BlockDriverState *bs
 
     return 0;
 fail:
-    close(s->fd);
     return -1;
 }
 
@@ -213,8 +208,10 @@ static inline uint32_t search_chunk(BDRV
     return s->n_chunks; /* error */
 }
 
-static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
+static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
 {
+    BDRVDMGState *s = bs->opaque;
+
     if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
 	int ret;
 	uint32_t chunk = search_chunk(s,sector_num);
@@ -231,8 +228,8 @@ static inline int dmg_read_chunk(BDRVDMG
 	     * inflated. */
 	    i=0;
 	    do {
-		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
-                            s->offsets[chunk] + i);
+                ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
+                                 s->compressed_chunk+i, s->lengths[chunk]-i);
 		if(ret<0 && errno==EINTR)
 		    ret=0;
 		i+=ret;
@@ -253,8 +250,8 @@ static inline int dmg_read_chunk(BDRVDMG
 		return -1;
 	    break; }
 	case 1: /* copy */
-	    ret = pread(s->fd, s->uncompressed_chunk, s->lengths[chunk],
-                        s->offsets[chunk]);
+	    ret = bdrv_pread(bs->file, s->offsets[chunk],
+                             s->uncompressed_chunk, s->lengths[chunk]);
 	    if (ret != s->lengths[chunk])
 		return -1;
 	    break;
@@ -275,7 +272,7 @@ static int dmg_read(BlockDriverState *bs
 
     for(i=0;i<nb_sectors;i++) {
 	uint32_t sector_offset_in_chunk;
-	if(dmg_read_chunk(s, sector_num+i) != 0)
+	if(dmg_read_chunk(bs, sector_num+i) != 0)
 	    return -1;
 	sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
 	memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
@@ -286,7 +283,6 @@ static int dmg_read(BlockDriverState *bs
 static void dmg_close(BlockDriverState *bs)
 {
     BDRVDMGState *s = bs->opaque;
-    close(s->fd);
     if(s->n_chunks>0) {
 	free(s->types);
 	free(s->offsets);
@@ -303,7 +299,7 @@ static BlockDriver bdrv_dmg = {
     .format_name	= "dmg",
     .instance_size	= sizeof(BDRVDMGState),
     .bdrv_probe		= dmg_probe,
-    .bdrv_file_open	= dmg_open,
+    .bdrv_open		= dmg_open,
     .bdrv_read		= dmg_read,
     .bdrv_close		= dmg_close,
 };

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH 3/3 v2] dmg: use qemu block API
  2010-05-12 14:31   ` [Qemu-devel] [PATCH 3/3 v2] " Christoph Hellwig
@ 2010-05-12 15:51     ` Kevin Wolf
  0 siblings, 0 replies; 10+ messages in thread
From: Kevin Wolf @ 2010-05-12 15:51 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: qemu-devel

Am 12.05.2010 16:31, schrieb Christoph Hellwig:
> Use bdrv_pwrite to access the backing device instead of pread, and
> convert the driver to implementing the bdrv_open method which gives
> it an already opened BlockDriverState for the underlying device.
> 
> Dmg actually does an lseek to a negative offset in the open routine,
> which we replace with offset arithmetics after doing a bdrv_getlength.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Thanks, applied all to the block branch.

Kevin

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-05-12 15:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-07 14:55 [Qemu-devel] [PATCH 0/3] dmg updates Christoph Hellwig
2010-05-07 14:55 ` [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks Christoph Hellwig
2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
2010-05-10 10:07   ` Kevin Wolf
2010-05-10 20:20     ` Christoph Hellwig
2010-05-11  8:00       ` Kevin Wolf
2010-05-12 14:31   ` [Qemu-devel] [PATCH 2/3 v2] " Christoph Hellwig
2010-05-07 14:56 ` [Qemu-devel] [PATCH 3/3] dmg: use qemu block API Christoph Hellwig
2010-05-12 14:31   ` [Qemu-devel] [PATCH 3/3 v2] " Christoph Hellwig
2010-05-12 15:51     ` Kevin Wolf

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).