Skip to contentSkip to conversation

Remove bdrv_read() and bdrv_write()

PATCH Applied Alberto Garcia 4 participants 16 messages May 1, 2019 – May 8, 2019 Block layer

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/5 qcow2: Replace bdrv_write() with bdrv_pwrite()

In reply to Alberto Garcia

There's only one bdrv_write() call left in the qcow2 code, and it can be trivially replaced with the byte-based bdrv_pwrite().

---
 block/qcow2-refcount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-refcount.c +2 -2
index e0fe322500..83f66eed7a 100644
@@ -2409,8 +2409,8 @@ write_refblocks:
         on_disk_refblock = (void *)((char *) *refcount_table +
                                     refblock_index * s->cluster_size);
 
-        ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE,
-                         on_disk_refblock, s->cluster_sectors);
+        ret = bdrv_pwrite(bs->file, refblock_offset, on_disk_refblock,
+                          s->cluster_size);
         if (ret < 0) {
             fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
             goto fail;
Signature · 1 line
2.11.0
#11 lore raw

PATCH v2 4/5 block: Remove bdrv_read() and bdrv_write()

In reply to Alberto Garcia

No one is using these functions anymore, all callers have switched to the byte-based bdrv_pread() and bdrv_pwrite()

---
 block/io.c            | 43 +++++++------------------------------------
 include/block/block.h |  4 ----
 2 files changed, 7 insertions(+), 40 deletions(-)
block/io.c +7 -36
index dfc153b8d8..396d5364ba 100644
@@ -837,42 +837,6 @@ static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
     return rwco.ret;
 }
 
-/*
- * Process a synchronous request using coroutines
- */
-static int bdrv_rw_co(BdrvChild *child, int64_t sector_num, uint8_t *buf,
-                      int nb_sectors, bool is_write, BdrvRequestFlags flags)
-{
-    QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf,
-                                            nb_sectors * BDRV_SECTOR_SIZE);
-
-    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
-        return -EINVAL;
-    }
-
-    return bdrv_prwv_co(child, sector_num << BDRV_SECTOR_BITS,
-                        &qiov, is_write, flags);
-}
-
-/* return < 0 if error. See bdrv_write() for the return codes */
-int bdrv_read(BdrvChild *child, int64_t sector_num,
-              uint8_t *buf, int nb_sectors)
-{
-    return bdrv_rw_co(child, sector_num, buf, nb_sectors, false, 0);
-}
-
-/* Return < 0 if error. Important errors are:
-  -EIO         generic I/O error (may happen for all errors)
-  -ENOMEDIUM   No media inserted.
-  -EINVAL      Invalid sector number or nb_sectors
-  -EACCES      Trying to write a read-only device
-*/
-int bdrv_write(BdrvChild *child, int64_t sector_num,
-               const uint8_t *buf, int nb_sectors)
-{
-    return bdrv_rw_co(child, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
-}
-
 int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
                        int bytes, BdrvRequestFlags flags)
 {
@@ -935,6 +899,7 @@ int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
     return qiov->size;
 }
 
+/* See bdrv_pwrite() for the return codes */
 int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
 {
     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
@@ -958,6 +923,12 @@ int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
     return qiov->size;
 }
 
+/* Return no. of bytes on success or < 0 on error. Important errors are:
+  -EIO         generic I/O error (may happen for all errors)
+  -ENOMEDIUM   No media inserted.
+  -EINVAL      Invalid offset or number of bytes
+  -EACCES      Trying to write a read-only device
+*/
 int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
 {
     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
include/block/block.h +0 -4
index c7a26199aa..5e2b98b0ee 100644
@@ -316,10 +316,6 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state,
                         BlockReopenQueue *queue, Error **errp);
 void bdrv_reopen_commit(BDRVReopenState *reopen_state);
 void bdrv_reopen_abort(BDRVReopenState *reopen_state);
-int bdrv_read(BdrvChild *child, int64_t sector_num,
-              uint8_t *buf, int nb_sectors);
-int bdrv_write(BdrvChild *child, int64_t sector_num,
-               const uint8_t *buf, int nb_sectors);
 int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
                        int bytes, BdrvRequestFlags flags);
 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags);
Signature · 1 line
2.11.0
#0 lore raw

v2 cover Remove bdrv_read() and bdrv_write()

Hi,

this API only had a few users left so it can be easily removed.

Regards,

Berto

v2:
- Patch 2: Add QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512) [Kevin] Correct size parameter in bdrv_pwrite() call [Kevin] Return 0 on success, never a positive number [Kevin]
- Patch 3: Check that the return values of bdrv_pread()/bdrv_pwrite()

           are strictly < 0 for errors, not simply != 0 [Kevin]

- Patch 4: Reuse the documenation of bdrv_write() for bdrv_pwrite()

v1: https://lists.gnu.org/archive/html/qemu-block/2019-04/msg00832.html

Output of git backport-diff against v1:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/5:[----] [--] 'qcow2: Replace bdrv_write() with bdrv_pwrite()' 002/5:[0006] [FC] 'vdi: Replace bdrv_{read,write}() with bdrv_{pread,pwrite}()' 003/5:[0004] [FC] 'vvfat: Replace bdrv_{read,write}() with bdrv_{pread,pwrite}()' 004/5:[0007] [FC] 'block: Remove bdrv_read() and bdrv_write()' 005/5:[----] [--] 'qcow2: Remove BDRVQcow2State.cluster_sectors'

Alberto Garcia (5):
qcow2: Replace bdrv_write() with bdrv_pwrite()
vdi: Replace bdrv_{read,write}() with bdrv_{pread,pwrite}()
vvfat: Replace bdrv_{read,write}() with bdrv_{pread,pwrite}()
block: Remove bdrv_read() and bdrv_write()
qcow2: Remove BDRVQcow2State.cluster_sectors

 block/io.c             | 43 +++++++------------------------------------
 block/qcow2-refcount.c |  4 ++--
 block/qcow2.c          |  1 -
 block/qcow2.h          |  1 -
 block/vdi.c            | 15 +++++++++------
 block/vvfat.c          | 12 +++++++-----
 include/block/block.h  |  4 ----
 7 files changed, 25 insertions(+), 55 deletions(-)
Signature · 1 line
2.11.0
#3 lore raw

PATCH v2 2/5 vdi: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

There's only a couple of bdrv_read() and bdrv_write() calls left in the vdi code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
 block/vdi.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
block/vdi.c +9 -6
index e1c42ad732..9caeb50dd1 100644
@@ -171,6 +171,8 @@ typedef struct {
     uint64_t unused2[7];
 } QEMU_PACKED VdiHeader;
 
+QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
+
 typedef struct {
     /* The block map entries are little endian (even in memory). */
     uint32_t *bmap;
@@ -384,7 +386,7 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
 
     logout("\n");
 
-    ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1);
+    ret = bdrv_pread(bs->file, 0, (uint8_t *)&header, sizeof(header));
     if (ret < 0) {
         goto fail;
     }
@@ -484,8 +486,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
-    ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap,
-                    bmap_size);
+    ret = bdrv_pread(bs->file, header.offset_bmap, (uint8_t *)s->bmap,
+                     bmap_size * SECTOR_SIZE);
     if (ret < 0) {
         goto fail_free_bmap;
     }
@@ -704,7 +706,7 @@ nonallocating_write:
         assert(VDI_IS_ALLOCATED(bmap_first));
         *header = s->header;
         vdi_header_to_le(header);
-        ret = bdrv_write(bs->file, 0, block, 1);
+        ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));
         g_free(block);
         block = NULL;
 
@@ -722,10 +724,11 @@ nonallocating_write:
         base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
         logout("will write %u block map sectors starting from entry %u\n",
                n_sectors, bmap_first);
-        ret = bdrv_write(bs->file, offset, base, n_sectors);
+        ret = bdrv_pwrite(bs->file, offset * SECTOR_SIZE, base,
+                          n_sectors * SECTOR_SIZE);
     }
 
-    return ret;
+    return ret < 0 ? ret : 0;
 }
 
 static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options,
Signature · 1 line
2.11.0
#6 lore raw

PATCH v2 3/5 vvfat: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

In reply to Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vvfat code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
 block/vvfat.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
block/vvfat.c +7 -5
index 5f66787890..253cc716dd 100644
@@ -1494,8 +1494,8 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
                 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                              " allocated\n", sector_num,
                              n >> BDRV_SECTOR_BITS));
-                if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
-                              n >> BDRV_SECTOR_BITS)) {
+                if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
+                               buf + i * 0x200, n) < 0) {
                     return -1;
                 }
                 i += (n >> BDRV_SECTOR_BITS) - 1;
@@ -1983,8 +1983,9 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
                         if (res) {
                             return -1;
                         }
-                        res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
-                        if (res) {
+                        res = bdrv_pwrite(s->qcow, offset * BDRV_SECTOR_SIZE,
+                                          s->cluster_buffer, BDRV_SECTOR_SIZE);
+                        if (res < 0) {
                             return -2;
                         }
                     }
@@ -3050,7 +3051,8 @@ DLOG(checkpoint());
      * Use qcow backend. Commit later.
      */
 DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
-    ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
+    ret = bdrv_pwrite(s->qcow, sector_num * BDRV_SECTOR_SIZE, buf,
+                      nb_sectors * BDRV_SECTOR_SIZE);
     if (ret < 0) {
         fprintf(stderr, "Error writing to qcow backend\n");
         return ret;
Signature · 1 line
2.11.0
#13 lore raw

PATCH v2 5/5 qcow2: Remove BDRVQcow2State.cluster_sectors

In reply to Alberto Garcia

The last user of this field disappeared when we replace the sector-based bdrv_write() with the byte-based bdrv_pwrite().

---
 block/qcow2.c | 1 -
 block/qcow2.h | 1 -
 2 files changed, 2 deletions(-)
block/qcow2.c +0 -1
index a520d116ef..8e024007db 100644
@@ -1259,7 +1259,6 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
 
     s->cluster_bits = header.cluster_bits;
     s->cluster_size = 1 << s->cluster_bits;
-    s->cluster_sectors = 1 << (s->cluster_bits - BDRV_SECTOR_BITS);
 
     /* Initialise version 3 header fields */
     if (header.version == 2) {
block/qcow2.h +0 -1
index fdee297f33..e62508d1ce 100644
@@ -266,7 +266,6 @@ typedef struct Qcow2BitmapHeaderExt {
 typedef struct BDRVQcow2State {
     int cluster_bits;
     int cluster_size;
-    int cluster_sectors;
     int l2_slice_size;
     int l2_bits;
     int l2_size;
Signature · 1 line
2.11.0
Reviewed-by on v2 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] qcow2: Replace bdrv_write() with bdrv_pwrite()

In reply to Alberto Garcia
Quoted 4 lines · Alberto Garcia

There's only one bdrv_write() call left in the qcow2 code, and it can be trivially replaced with the byte-based bdrv_pwrite().

Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v2 2/5
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] vdi: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

In reply to Alberto Garcia
Quoted 23 lines · Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vdi code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
  block/vdi.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
block/vdi.c +2 -0
index e1c42ad732..9caeb50dd1 100644
@@ -171,6 +171,8 @@ typedef struct {
      uint64_t unused2[7];
  } QEMU_PACKED VdiHeader;
 
+QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
+
  typedef struct {
      /* The block map entries are little endian (even in memory). */
      uint32_t *bmap;

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 389

Quoted 5 lines
block/vdi.c +1 -1
@@ -384,7 +386,7 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
 
      logout("\n");
 
-    ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1);
+    ret = bdrv_pread(bs->file, 0, (uint8_t *)&header, sizeof(header));

bdrv_pread parameter buf parameter is void, so (uint8_t *) conversion is not needed (and even confusing)

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 490

Quoted 11 lines
block/vdi.c +2 -2
      if (ret < 0) {
          goto fail;
      }
@@ -484,8 +486,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
-    ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap,
-                    bmap_size);
+    ret = bdrv_pread(bs->file, header.offset_bmap, (uint8_t *)s->bmap,
+                     bmap_size * SECTOR_SIZE);

and here

block/vdi.c nonallocating_write: · line 709

Quoted 9 lines
block/vdi.c +1 -1
      if (ret < 0) {
          goto fail_free_bmap;
      }
@@ -704,7 +706,7 @@ nonallocating_write:
          assert(VDI_IS_ALLOCATED(bmap_first));
          *header = s->header;
          vdi_header_to_le(header);
-        ret = bdrv_write(bs->file, 0, block, 1);
+        ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));

maybe, more self-descriptive:
ret = bdrv_pwrite(bs->file, 0, header, sizeof(*header));

with at least extra conversion dropped:

Quoted 18 lines
block/vdi.c +3 -2
          g_free(block);
          block = NULL;
 
@@ -722,10 +724,11 @@ nonallocating_write:
          base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
          logout("will write %u block map sectors starting from entry %u\n",
                 n_sectors, bmap_first);
-        ret = bdrv_write(bs->file, offset, base, n_sectors);
+        ret = bdrv_pwrite(bs->file, offset * SECTOR_SIZE, base,
+                          n_sectors * SECTOR_SIZE);
      }
 
-    return ret;
+    return ret < 0 ? ret : 0;
  }
 
  static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options,
Signature · 2 lines
Best regards,
Vladimir
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] vvfat: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

In reply to Alberto Garcia
Quoted 14 lines · Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vvfat code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
  block/vvfat.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
block/vvfat.c +0 -0
index 5f66787890..253cc716dd 100644

block/vvfat.c static int vvfat_read(BlockDriverState *bs, int64_t sector_num, · line 1498

Quoted 7 lines
block/vvfat.c +2 -2
@@ -1494,8 +1494,8 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
                  DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                               " allocated\n", sector_num,
                               n >> BDRV_SECTOR_BITS));
-                if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
-                              n >> BDRV_SECTOR_BITS)) {
+                if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
+                               buf + i * 0x200, n) < 0) {

Shouldn't we use QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE) ? Could bdrv_is_allocated give unaligned n?

Quoted 26 lines
block/vvfat.c +5 -3
                      return -1;
                  }
                  i += (n >> BDRV_SECTOR_BITS) - 1;
@@ -1983,8 +1983,9 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
                          if (res) {
                              return -1;
                          }
-                        res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
-                        if (res) {
+                        res = bdrv_pwrite(s->qcow, offset * BDRV_SECTOR_SIZE,
+                                          s->cluster_buffer, BDRV_SECTOR_SIZE);
+                        if (res < 0) {
                              return -2;
                          }
                      }
@@ -3050,7 +3051,8 @@ DLOG(checkpoint());
       * Use qcow backend. Commit later.
       */
  DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
-    ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
+    ret = bdrv_pwrite(s->qcow, sector_num * BDRV_SECTOR_SIZE, buf,
+                      nb_sectors * BDRV_SECTOR_SIZE);
      if (ret < 0) {
          fprintf(stderr, "Error writing to qcow backend\n");
          return ret;
Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v2 4/5
#12 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] block: Remove bdrv_read() and bdrv_write()

In reply to Alberto Garcia
Quoted 4 lines · Alberto Garcia

No one is using these functions anymore, all callers have switched to the byte-based bdrv_pread() and bdrv_pwrite()

However, I doubt that we want to save error codes descriptions. On the other hand, "Return no. of bytes on success or < 0 on error." is good addition, but it becomes unrelated to this patch if drop error codes description.

Quoted 89 lines
---
  block/io.c            | 43 +++++++------------------------------------
  include/block/block.h |  4 ----
  2 files changed, 7 insertions(+), 40 deletions(-)
block/io.c +7 -36
index dfc153b8d8..396d5364ba 100644
@@ -837,42 +837,6 @@ static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
      return rwco.ret;
  }
 
-/*
- * Process a synchronous request using coroutines
- */
-static int bdrv_rw_co(BdrvChild *child, int64_t sector_num, uint8_t *buf,
-                      int nb_sectors, bool is_write, BdrvRequestFlags flags)
-{
-    QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf,
-                                            nb_sectors * BDRV_SECTOR_SIZE);
-
-    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
-        return -EINVAL;
-    }
-
-    return bdrv_prwv_co(child, sector_num << BDRV_SECTOR_BITS,
-                        &qiov, is_write, flags);
-}
-
-/* return < 0 if error. See bdrv_write() for the return codes */
-int bdrv_read(BdrvChild *child, int64_t sector_num,
-              uint8_t *buf, int nb_sectors)
-{
-    return bdrv_rw_co(child, sector_num, buf, nb_sectors, false, 0);
-}
-
-/* Return < 0 if error. Important errors are:
-  -EIO         generic I/O error (may happen for all errors)
-  -ENOMEDIUM   No media inserted.
-  -EINVAL      Invalid sector number or nb_sectors
-  -EACCES      Trying to write a read-only device
-*/
-int bdrv_write(BdrvChild *child, int64_t sector_num,
-               const uint8_t *buf, int nb_sectors)
-{
-    return bdrv_rw_co(child, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
-}
-
  int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
                         int bytes, BdrvRequestFlags flags)
  {
@@ -935,6 +899,7 @@ int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
      return qiov->size;
  }
 
+/* See bdrv_pwrite() for the return codes */
  int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
  {
      QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
@@ -958,6 +923,12 @@ int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
      return qiov->size;
  }
 
+/* Return no. of bytes on success or < 0 on error. Important errors are:
+  -EIO         generic I/O error (may happen for all errors)
+  -ENOMEDIUM   No media inserted.
+  -EINVAL      Invalid offset or number of bytes
+  -EACCES      Trying to write a read-only device
+*/
  int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
  {
      QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
include/block/block.h +0 -4
index c7a26199aa..5e2b98b0ee 100644
@@ -316,10 +316,6 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state,
                          BlockReopenQueue *queue, Error **errp);
  void bdrv_reopen_commit(BDRVReopenState *reopen_state);
  void bdrv_reopen_abort(BDRVReopenState *reopen_state);
-int bdrv_read(BdrvChild *child, int64_t sector_num,
-              uint8_t *buf, int nb_sectors);
-int bdrv_write(BdrvChild *child, int64_t sector_num,
-               const uint8_t *buf, int nb_sectors);
  int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
                         int bytes, BdrvRequestFlags flags);
  int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags);
Signature · 2 lines
Best regards,
Vladimir
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] vvfat: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

In reply to Vladimir Sementsov-Ogievskiy
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 9 lines · Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vvfat code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
  block/vvfat.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
Quoted 14 lines

block/vvfat.c static int vvfat_read(BlockDriverState *bs, int64_t sector_num, · line 1498

Quoted 12 lines
block/vvfat.c +2 -2
index 5f66787890..253cc716dd 100644
@@ -1494,8 +1494,8 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
                  DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                               " allocated\n", sector_num,
                               n >> BDRV_SECTOR_BITS));
-                if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
-                              n >> BDRV_SECTOR_BITS)) {
+                if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
+                               buf + i * 0x200, n) < 0) {

Shouldn't we use QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE) ?

No, n should already be aligned, which makes align_down a no-op.

Quoted 2 lines

Could bdrv_is_allocated give unaligned n?

Yes, bdrv_is_allocated can return unaligned n in some situations; I had a patch that didn't make 4.0 that would add bdrv_block_status_aligned for cases where we need to guarantee that different alignment of a backing chain doesn't bleed through to the specified alignment of the current layer. But those situations are rare, and I need to revisit those and send a v2; so I don't see a problem with this one going in during the meantime as-is.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] vvfat: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

Quoted 31 lines · Eric Blake
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 9 lines · Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vvfat code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
   block/vvfat.c | 12 +++++++-----
   1 file changed, 7 insertions(+), 5 deletions(-)
Quoted 14 lines

block/vvfat.c static int vvfat_read(BlockDriverState *bs, int64_t sector_num, · line 1498

Quoted 12 lines
block/vvfat.c +2 -2
index 5f66787890..253cc716dd 100644
@@ -1494,8 +1494,8 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
                   DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                                " allocated\n", sector_num,
                                n >> BDRV_SECTOR_BITS));
-                if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
-                              n >> BDRV_SECTOR_BITS)) {
+                if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
+                               buf + i * 0x200, n) < 0) {

Shouldn't we use QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE) ?

No, n should already be aligned, which makes align_down a no-op.

Quoted 2 lines

Could bdrv_is_allocated give unaligned n?

Quoted 8 lines

Yes, bdrv_is_allocated can return unaligned n in some situations; I had a patch that didn't make 4.0 that would add bdrv_block_status_aligned for cases where we need to guarantee that different alignment of a backing chain doesn't bleed through to the specified alignment of the current layer. But those situations are rare, and I need to revisit those and send a v2; so I don't see a problem with this one going in during the meantime as-is.

Than, n is not already aligned, as it comes from bdrv_is_allocated.

Signature · 2 lines
Best regards,
Vladimir
#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] vvfat: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

Quoted 26 lines · Vladimir Sementsov-Ogievskiy
Quoted 17 lines
Quoted 11 lines

block/vvfat.c static int vvfat_read(BlockDriverState *bs, int64_t sector_num, · line 1498

Quoted 9 lines

+++ b/block/vvfat.c

block/vvfat.c +2 -2
@@ -1494,8 +1494,8 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
                   DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                                " allocated\n", sector_num,
                                n >> BDRV_SECTOR_BITS));
-                if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
-                              n >> BDRV_SECTOR_BITS)) {
+                if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
+                               buf + i * 0x200, n) < 0) {

Shouldn't we use QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE) ?

No, n should already be aligned, which makes align_down a no-op.

Quoted 2 lines

Could bdrv_is_allocated give unaligned n?

Quoted 8 lines

Yes, bdrv_is_allocated can return unaligned n in some situations; I had a patch that didn't make 4.0 that would add bdrv_block_status_aligned for cases where we need to guarantee that different alignment of a backing chain doesn't bleed through to the specified alignment of the current layer. But those situations are rare, and I need to revisit those and send a v2; so I don't see a problem with this one going in during the meantime as-is.

Quoted 1 line

Than, n is not already aligned, as it comes from bdrv_is_allocated.

Note that whether bdrv_is_allocated can return data not aligned to 512 depends on the driver. It is possible when querying file-posix.c, but only for a POSIX file that encounters EOF mid-sector. However, it is not possible for the qcow2 driver. The patches I need to rework are worried more about cases where a block device with request_alignment of 4k can still see 512-alignment leak through from a backing file. But since vvfat is grabbing alignment from a qcow2 image, and not a raw POSIX file, we should never see sub-sector alignment.

So my answers above were terse but correct: bdrv_is_allocated can return unaligned data in some cases, but vvfat should not be one of those cases. If you'd like to add an assert instead of a QEMU_ALIGN_DOWN, that should be reasonable.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] vdi: Replace bdrv_{read, write}() with bdrv_{pread, pwrite}()

In reply to Vladimir Sementsov-Ogievskiy

Am 06.05.2019 um 18:38 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 61 lines
Quoted 23 lines · Alberto Garcia

There's only a couple of bdrv_read() and bdrv_write() calls left in the vdi code, and they can be trivially replaced with the byte-based bdrv_pread() and bdrv_pwrite().

---
  block/vdi.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
block/vdi.c +2 -0
index e1c42ad732..9caeb50dd1 100644
@@ -171,6 +171,8 @@ typedef struct {
      uint64_t unused2[7];
  } QEMU_PACKED VdiHeader;
 
+QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
+
  typedef struct {
      /* The block map entries are little endian (even in memory). */
      uint32_t *bmap;

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 389

Quoted 5 lines
block/vdi.c +1 -1
@@ -384,7 +386,7 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
 
      logout("\n");
 
-    ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1);
+    ret = bdrv_pread(bs->file, 0, (uint8_t *)&header, sizeof(header));

bdrv_pread parameter buf parameter is void, so (uint8_t *) conversion is not needed (and even confusing)

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 490

Quoted 11 lines
block/vdi.c +2 -2
      if (ret < 0) {
          goto fail;
      }
@@ -484,8 +486,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
-    ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap,
-                    bmap_size);
+    ret = bdrv_pread(bs->file, header.offset_bmap, (uint8_t *)s->bmap,
+                     bmap_size * SECTOR_SIZE);

and here

block/vdi.c nonallocating_write: · line 709

Quoted 9 lines
block/vdi.c +1 -1
      if (ret < 0) {
          goto fail_free_bmap;
      }
@@ -704,7 +706,7 @@ nonallocating_write:
          assert(VDI_IS_ALLOCATED(bmap_first));
          *header = s->header;
          vdi_header_to_le(header);
-        ret = bdrv_write(bs->file, 0, block, 1);
+        ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));

maybe, more self-descriptive:
ret = bdrv_pwrite(bs->file, 0, header, sizeof(*header));

Quoted 2 lines

with at least extra conversion dropped:

I'll drop the casts while applying.

Kevin

Applied
#15 lore raw
In reply to Alberto Garcia

Am 01.05.2019 um 20:13 hat Alberto Garcia geschrieben:

Quoted 3 lines

Hi,

this API only had a few users left so it can be easily removed.

Thanks, applied to the block branch.

Kevin