All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, pbonzini@redhat.com, kwolf@redhat.com,
	stefanha@redhat.com, Max Reitz <mreitz@redhat.com>,
	Fam Zheng <famz@redhat.com>
Subject: [Qemu-devel] [PATCH v2 01/19] block: Convert bdrv_co_discard() to byte-based
Date: Fri, 15 Jul 2016 17:22:50 -0600	[thread overview]
Message-ID: <1468624988-423-2-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1468624988-423-1-git-send-email-eblake@redhat.com>

Another step towards byte-based interfaces everywhere.  Replace
the sector-based bdrv_co_discard() with a new byte-based
bdrv_co_pdiscard(), which silently ignores any unaligned head
or tail.  Driver callbacks will be converted in followup patches.

By calculating the alignment outside of the loop, and clamping
the max discard to an aligned value, we can simplify the actions
done within the loop.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---

Yes, this patch is yet one more place that will need to be fixed up
before 2.7, in order to support iscsi devices that advertise a 15M
opt_discard and max_discard.  I plan on submitting that as a followup
series (as a bug fix, that qualifies as post-hard-freeze; while this
series has been around since before soft freeze but probably doesn't
qualify for inclusion once hard freeze hits)

 include/block/block.h |  2 +-
 block/blkreplay.c     |  3 ++-
 block/block-backend.c |  3 ++-
 block/io.c            | 67 +++++++++++++++++++++++++++------------------------
 block/raw_bsd.c       |  3 ++-
 5 files changed, 42 insertions(+), 36 deletions(-)

diff --git a/include/block/block.h b/include/block/block.h
index 616d8b9..4f5cebf 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -342,7 +342,7 @@ void coroutine_fn bdrv_co_drain(BlockDriverState *bs);
 void bdrv_drain_all(void);

 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
-int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
+int bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, int count);
 int bdrv_has_zero_init_1(BlockDriverState *bs);
 int bdrv_has_zero_init(BlockDriverState *bs);
 bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs);
diff --git a/block/blkreplay.c b/block/blkreplay.c
index 3368c8c..c69e5a5 100755
--- a/block/blkreplay.c
+++ b/block/blkreplay.c
@@ -118,7 +118,8 @@ static int coroutine_fn blkreplay_co_discard(BlockDriverState *bs,
     int64_t sector_num, int nb_sectors)
 {
     uint64_t reqid = request_id++;
-    int ret = bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
+    int ret = bdrv_co_pdiscard(bs->file->bs, sector_num << BDRV_SECTOR_BITS,
+                               nb_sectors << BDRV_SECTOR_BITS);
     block_request_create(reqid, bs, qemu_coroutine_self());
     qemu_coroutine_yield();

diff --git a/block/block-backend.c b/block/block-backend.c
index f9cea1b..d982cf9 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -1113,7 +1113,8 @@ int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
         return ret;
     }

-    return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors);
+    return bdrv_co_pdiscard(blk_bs(blk), sector_num << BDRV_SECTOR_BITS,
+                            nb_sectors << BDRV_SECTOR_BITS);
 }

 int blk_co_flush(BlockBackend *blk)
diff --git a/block/io.c b/block/io.c
index 86db77e..4e04df2 100644
--- a/block/io.c
+++ b/block/io.c
@@ -2198,7 +2198,8 @@ static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
     BlockAIOCBCoroutine *acb = opaque;
     BlockDriverState *bs = acb->common.bs;

-    acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
+    acb->req.error = bdrv_co_pdiscard(bs, acb->req.sector << BDRV_SECTOR_BITS,
+                                      acb->req.nb_sectors << BDRV_SECTOR_BITS);
     bdrv_co_complete(acb);
 }

@@ -2378,20 +2379,22 @@ static void coroutine_fn bdrv_discard_co_entry(void *opaque)
 {
     DiscardCo *rwco = opaque;

-    rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
+    rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->sector_num << BDRV_SECTOR_BITS,
+                                 rwco->nb_sectors << BDRV_SECTOR_BITS);
 }

-int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
-                                 int nb_sectors)
+int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset,
+                                  int count)
 {
     BdrvTrackedRequest req;
-    int max_discard, ret;
+    int max_pdiscard, ret;
+    int head, align;

     if (!bs->drv) {
         return -ENOMEDIUM;
     }

-    ret = bdrv_check_request(bs, sector_num, nb_sectors);
+    ret = bdrv_check_byte_request(bs, offset, count);
     if (ret < 0) {
         return ret;
     } else if (bs->read_only) {
@@ -2408,45 +2411,45 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
         return 0;
     }

-    tracked_request_begin(&req, bs, sector_num << BDRV_SECTOR_BITS,
-                          nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
+    /* Discard is advisory, so ignore any unaligned head or tail */
+    align = MAX(BDRV_SECTOR_SIZE,
+                MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment));
+    assert(is_power_of_2(align));
+    head = MIN(count, -offset & (align - 1));
+    if (head) {
+        count -= head;
+        offset += head;
+    }
+    count = QEMU_ALIGN_DOWN(count, align);
+    if (!count) {
+        return 0;
+    }
+
+    tracked_request_begin(&req, bs, offset, count, BDRV_TRACKED_DISCARD);

     ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
     if (ret < 0) {
         goto out;
     }

-    max_discard = MIN_NON_ZERO(bs->bl.max_pdiscard >> BDRV_SECTOR_BITS,
-                               BDRV_REQUEST_MAX_SECTORS);
-    while (nb_sectors > 0) {
+    max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT_MAX),
+                                   align);
+
+    while (count > 0) {
         int ret;
-        int num = nb_sectors;
-        int discard_alignment = bs->bl.pdiscard_alignment >> BDRV_SECTOR_BITS;
-
-        /* align request */
-        if (discard_alignment &&
-            num >= discard_alignment &&
-            sector_num % discard_alignment) {
-            if (num > discard_alignment) {
-                num = discard_alignment;
-            }
-            num -= sector_num % discard_alignment;
-        }
-
-        /* limit request size */
-        if (num > max_discard) {
-            num = max_discard;
-        }
+        int num = MIN(count, max_pdiscard);

         if (bs->drv->bdrv_co_discard) {
-            ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
+            ret = bs->drv->bdrv_co_discard(bs, offset >> BDRV_SECTOR_BITS,
+                                           num >> BDRV_SECTOR_BITS);
         } else {
             BlockAIOCB *acb;
             CoroutineIOCompletion co = {
                 .coroutine = qemu_coroutine_self(),
             };

-            acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
+            acb = bs->drv->bdrv_aio_discard(bs, offset >> BDRV_SECTOR_BITS,
+                                            num >> BDRV_SECTOR_BITS,
                                             bdrv_co_io_em_complete, &co);
             if (acb == NULL) {
                 ret = -EIO;
@@ -2460,8 +2463,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
             goto out;
         }

-        sector_num += num;
-        nb_sectors -= num;
+        offset += num;
+        count -= num;
     }
     ret = 0;
 out:
diff --git a/block/raw_bsd.c b/block/raw_bsd.c
index d767413..68f0a91 100644
--- a/block/raw_bsd.c
+++ b/block/raw_bsd.c
@@ -137,7 +137,8 @@ static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs,
 static int coroutine_fn raw_co_discard(BlockDriverState *bs,
                                        int64_t sector_num, int nb_sectors)
 {
-    return bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
+    return bdrv_co_pdiscard(bs->file->bs, sector_num << BDRV_SECTOR_BITS,
+                            nb_sectors << BDRV_SECTOR_BITS);
 }

 static int64_t raw_getlength(BlockDriverState *bs)
-- 
2.5.5

  reply	other threads:[~2016-07-15 23:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-15 23:22 [Qemu-devel] [PATCH for-2.7 v2 00/19] byte-based block discard Eric Blake
2016-07-15 23:22 ` Eric Blake [this message]
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 02/19] block: Convert bdrv_discard() to byte-based Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 03/19] block: Switch BlockRequest " Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 04/19] block: Convert bdrv_aio_discard() " Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 05/19] block: Convert BB interface to byte-based discards Eric Blake
2016-07-15 23:22   ` Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 06/19] raw-posix: Switch paio_submit() to byte-based Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 07/19] rbd: Switch rbd_start_aio() " Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 08/19] block: Convert .bdrv_aio_discard() " Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 09/19] block: Add .bdrv_co_pdiscard() driver callback Eric Blake
2016-07-15 23:22 ` [Qemu-devel] [PATCH v2 10/19] blkreplay: Switch .bdrv_co_discard() to byte-based Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 11/19] gluster: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 12/19] iscsi: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 13/19] nbd: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 14/19] qcow2: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 15/19] raw_bsd: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 16/19] sheepdog: " Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 17/19] block: Kill .bdrv_co_discard() Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 18/19] nbd: Convert to byte-based interface Eric Blake
2016-07-15 23:23 ` [Qemu-devel] [PATCH v2 19/19] raw_bsd: " Eric Blake
2016-07-19 16:12 ` [Qemu-devel] [Qemu-block] [PATCH for-2.7 v2 00/19] byte-based block discard Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468624988-423-2-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.