zbd: add zbd_zone_remainder() helper function
authorShin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Mon, 14 Nov 2022 02:12:58 +0000 (11:12 +0900)
committerVincent Fu <vincent.fu@samsung.com>
Fri, 18 Nov 2022 14:55:16 +0000 (09:55 -0500)
Add the helper function zbd_zone_remainder(), which returns the number
of bytes that are still available for writing before the zone gets full.
Use this function to improve readability. It will also be used in the
following patch.

Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Tested-by: Dmitry Fomichev <dmitry.fomichev@wdc.com>
Reviewed-by: Dmitry Fomichev <dmitry.fomichev@wdc.com>
Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
zbd.c

diff --git a/zbd.c b/zbd.c
index 627fb968ecf78d3fa5e48480c95a2199ae5187c2..26a6404def08f4788b7a62f7c0ebb3828a8a145b 100644 (file)
--- a/zbd.c
+++ b/zbd.c
@@ -70,6 +70,19 @@ static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
        return z->start + z->capacity;
 }
 
+/**
+ * zbd_zone_remainder - Return the number of bytes that are still available for
+ *                      writing before the zone gets full
+ * @z: zone info pointer.
+ */
+static inline uint64_t zbd_zone_remainder(struct fio_zone_info *z)
+{
+       if (z->wp >= zbd_zone_capacity_end(z))
+               return 0;
+
+       return zbd_zone_capacity_end(z) - z->wp;
+}
+
 /**
  * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
  * @f: file pointer.
@@ -83,8 +96,7 @@ static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
 {
        assert((required & 511) == 0);
 
-       return z->has_wp &&
-               z->wp + required > zbd_zone_capacity_end(z);
+       return z->has_wp && required > zbd_zone_remainder(z);
 }
 
 static void zone_lock(struct thread_data *td, const struct fio_file *f,
@@ -440,7 +452,7 @@ static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
                 * already in-flight, handle it as a full zone instead of an
                 * open zone.
                 */
-               if (z->wp >= zbd_zone_capacity_end(z))
+               if (!zbd_zone_remainder(z))
                        res = false;
                goto out;
        }
@@ -1368,7 +1380,7 @@ found_candidate_zone:
        /* Both z->mutex and zbdi->mutex are held. */
 
 examine_zone:
-       if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
+       if (zbd_zone_remainder(z) >= min_bs) {
                pthread_mutex_unlock(&zbdi->mutex);
                goto out;
        }
@@ -1433,7 +1445,7 @@ retry:
                z = zbd_get_zone(f, zone_idx);
 
                zone_lock(td, f, z);
-               if (z->wp + min_bs <= zbd_zone_capacity_end(z))
+               if (zbd_zone_remainder(z) >= min_bs)
                        goto out;
                pthread_mutex_lock(&zbdi->mutex);
        }