]> git-server-git.apps.pok.os.sepia.ceph.com Git - xfsprogs-dev.git/commitdiff
libfrog: lift *BYTES helpers to convert.h
authorDarrick J. Wong <djwong@kernel.org>
Wed, 18 Mar 2026 17:15:06 +0000 (10:15 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 9 Apr 2026 22:30:18 +0000 (15:30 -0700)
Move these byte unit conversion macros to convert.h and amend the macros
to cast to unsigned long long to avoid shifting issues.  Now we can use
these same macros throughout the codebase instead of opencoding shifts
and possibly suffering from integer shifting problems.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
db/bmap_inflate.c
libfrog/convert.c
libfrog/convert.h
mdrestore/xfs_mdrestore.c
mkfs/xfs_mkfs.c
repair/agbtree.c
repair/bmap_repair.c
repair/xfs_repair.c
scrub/common.c
scrub/phase8.c

index 1de6d3439ab3d3490d4933c1c587c8767ce6e195..cc7c197e788d7f7047d1ff4cb7d905f124599b21 100644 (file)
@@ -437,7 +437,7 @@ bmapinflate_f(
        struct xfs_trans        *tp;
        char                    *p;
        unsigned long long      nextents = 0;
-       unsigned long long      dirty_bytes = 60U << 20; /* 60MiB */
+       unsigned long long      dirty_bytes = MEGABYTES(60);
        unsigned long long      dirty_blocks;
        unsigned int            resblks;
        bool                    estimate = false;
index 0ceeb389682ae14b7ce6a013c3dbadb468f17684..65b9e47459e234a95126c3d6a15f78624d3694d6 100644 (file)
@@ -173,13 +173,6 @@ cvt_u16(
        return i;
 }
 
-#define EXABYTES(x)    ((long long)(x) << 60)
-#define PETABYTES(x)   ((long long)(x) << 50)
-#define TERABYTES(x)   ((long long)(x) << 40)
-#define GIGABYTES(x)   ((long long)(x) << 30)
-#define MEGABYTES(x)   ((long long)(x) << 20)
-#define KILOBYTES(x)   ((long long)(x) << 10)
-
 long long
 cvtnum(
        size_t          blksize,
index 3e5fbe055986a4b19e7fa7af92595730401abb2b..223d3e73c9422b8a6ae28e6f32fd9e4f54c4e585 100644 (file)
@@ -22,4 +22,11 @@ extern uid_t uid_from_string(char *user);
 extern gid_t   gid_from_string(char *group);
 extern prid_t  prid_from_string(char *project);
 
+#define EXABYTES(x)    ((unsigned long long)(x) << 60)
+#define PETABYTES(x)   ((unsigned long long)(x) << 50)
+#define TERABYTES(x)   ((unsigned long long)(x) << 40)
+#define GIGABYTES(x)   ((unsigned long long)(x) << 30)
+#define MEGABYTES(x)   ((unsigned long long)(x) << 20)
+#define KILOBYTES(x)   ((unsigned long long)(x) << 10)
+
 #endif /* __LIBFROG_CONVERT_H__ */
index 8858026c87dc974a9c33f987d736f2d22a223136..ce7f596a6184feac4f7fb59eac0aafa31f662398 100644 (file)
@@ -8,6 +8,7 @@
 #include "xfs_metadump.h"
 #include <libfrog/platform.h>
 #include "libfrog/div64.h"
+#include "libfrog/convert.h"
 
 union mdrestore_headers {
        __be32                          magic;
@@ -92,10 +93,10 @@ final_print_progress(
        if (!mdrestore.show_progress)
                goto done;
 
-       if (bytes_read <= (*cursor << 20))
+       if (bytes_read <= MEGABYTES(*cursor))
                goto done;
 
-       print_progress("%lld MB read", howmany_64(bytes_read, 1U << 20));
+       print_progress("%lld MB read", howmany_64(bytes_read, MEGABYTES(1)));
 
 done:
        if (mdrestore.progress_since_warning)
index e0f0bb28e121593b001ba83e678f6bc45377be04..dd8a48c3633ef098150d1c8e427e809989923ffc 100644 (file)
@@ -15,6 +15,7 @@
 #include "libfrog/dahashselftest.h"
 #include "libfrog/fsproperties.h"
 #include "libfrog/zones.h"
+#include "libfrog/convert.h"
 #include "proto.h"
 #include <ini.h>
 
@@ -558,7 +559,7 @@ static struct opt_params iopts = {
                  .conflicts = { { NULL, LAST_CONFLICT } },
                  .convert = true,
                  .minval = 1,
-                 .maxval = 1ULL << 30, /* 1GiB */
+                 .maxval = GIGABYTES(1),
                  .defaultval = SUBOPT_NEEDS_VAL,
                },
        },
@@ -1576,7 +1577,7 @@ discard_blocks(int fd, uint64_t nsectors, int quiet)
 {
        uint64_t        offset = 0;
        /* Discard the device 2G at a time */
-       const uint64_t  step = 2ULL << 30;
+       const uint64_t  step = GIGABYTES(2);
        const uint64_t  count = BBTOB(nsectors);
 
        /*
index 983b645e1a35a3b77ebfa9593f2497e2411b5b6a..fe28e5e94c575e0835bcf36224efe37ff0cf265b 100644 (file)
@@ -6,6 +6,7 @@
 #include <libxfs.h>
 #include "err_protos.h"
 #include "libfrog/bitmap.h"
+#include "libfrog/convert.h"
 #include "slab.h"
 #include "rmap.h"
 #include "incore.h"
@@ -23,7 +24,7 @@ init_rebuild(
        memset(btr, 0, sizeof(struct bt_rebuild));
 
        bulkload_init_ag(&btr->newbt, sc, oinfo, NULLFSBLOCK);
-       btr->bload.max_dirty = XFS_B_TO_FSBT(sc->mp, 256U << 10); /* 256K */
+       btr->bload.max_dirty = XFS_B_TO_FSBT(sc->mp, KILOBYTES(256));
        bulkload_estimate_ag_slack(sc, &btr->bload, est_agfreeblocks);
 }
 
index 5d1f639be81ff4471801579c1b8b22a86c48e4fb..192f189de0521734f11c8e689ab6036ccbc20979 100644 (file)
@@ -15,6 +15,7 @@
 #include "bulkload.h"
 #include "bmap_repair.h"
 #include "libfrog/util.h"
+#include "libfrog/convert.h"
 
 /*
  * Inode Fork Block Mapping (BMBT) Repair
@@ -499,7 +500,7 @@ xrep_bmap_btree_load(
        rb->bmap_bload.get_records = xrep_bmap_get_records;
        rb->bmap_bload.claim_block = xrep_bmap_claim_block;
        rb->bmap_bload.iroot_size = xrep_bmap_iroot_size;
-       rb->bmap_bload.max_dirty = XFS_B_TO_FSBT(sc->mp, 256U << 10); /* 256K */
+       rb->bmap_bload.max_dirty = XFS_B_TO_FSBT(sc->mp, KILOBYTES(256));
 
        /*
         * Always make the btree as small as possible, since we might need the
index 7bf75c09b94542f3d7ca8905c8e978d7f28ae4c1..6b97a8063840964a47782085894ff0f031e435a2 100644 (file)
@@ -28,6 +28,7 @@
 #include "quotacheck.h"
 #include "rcbag_btree.h"
 #include "rt.h"
+#include "libfrog/convert.h"
 
 /*
  * option tables for getsubopt calls
@@ -1321,8 +1322,8 @@ main(int argc, char **argv)
                }
 
                max_mem -= mem_used;
-               if (max_mem >= (1 << 30))
-                       max_mem = 1 << 30;
+               if (max_mem >= GIGABYTES(1))
+                       max_mem = GIGABYTES(1);
                libxfs_bhash_size = max_mem / (HASH_CACHE_RATIO *
                                (igeo->inode_cluster_size >> 10));
                if (libxfs_bhash_size < 512)
index a567d2a3c2f14e6ffd51db2e434c315e72cdbd82..34d91525928305206b7c9add6bbd4fbe0f46f7ca 100644 (file)
@@ -11,6 +11,7 @@
 #include "libfrog/paths.h"
 #include "libfrog/getparents.h"
 #include "libfrog/handle_priv.h"
+#include "libfrog/convert.h"
 #include "xfs_scrub.h"
 #include "common.h"
 #include "progress.h"
@@ -218,18 +219,18 @@ auto_space_units(
 {
        if (debug > 1)
                goto no_prefix;
-       if (bytes > (1ULL << 40)) {
+       if (bytes > TERABYTES(1)) {
                *units = "TiB";
-               return (double)bytes / (1ULL << 40);
-       } else if (bytes > (1ULL << 30)) {
+               return (double)bytes / TERABYTES(1);
+       } else if (bytes > GIGABYTES(1)) {
                *units = "GiB";
-               return (double)bytes / (1ULL << 30);
-       } else if (bytes > (1ULL << 20)) {
+               return (double)bytes / GIGABYTES(1);
+       } else if (bytes > MEGABYTES(1)) {
                *units = "MiB";
-               return (double)bytes / (1ULL << 20);
-       } else if (bytes > (1ULL << 10)) {
+               return (double)bytes / MEGABYTES(1);
+       } else if (bytes > KILOBYTES(1)) {
                *units = "KiB";
-               return (double)bytes / (1ULL << 10);
+               return (double)bytes / KILOBYTES(1);
        }
 
 no_prefix:
index e8c72d8eb851afc1f9d852b8a28cf1a3b8a6c193..0967832da890e3f2bc7849f8632b724bcfaa0ea2 100644 (file)
@@ -12,6 +12,7 @@
 #include "libfrog/paths.h"
 #include "libfrog/workqueue.h"
 #include "libfrog/histogram.h"
+#include "libfrog/convert.h"
 #include "xfs_scrub.h"
 #include "common.h"
 #include "progress.h"
@@ -51,7 +52,7 @@ fstrim_ok(
  * call so that we can implement decent progress reporting and CPU resource
  * control.  Pick a prime number of gigabytes for interest.
  */
-#define FSTRIM_MAX_BYTES       (11ULL << 30)
+#define FSTRIM_MAX_BYTES       GIGABYTES(11)
 
 /* Trim a certain range of the filesystem. */
 static int