xfs: refactor non-power-of-two alignment checks
authorDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:54:12 +0000 (14:54 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:54:12 +0000 (14:54 -0700)
Create a helper function that can compute if a 64-bit number is an
integer multiple of a 32-bit number, where the 32-bit number is not
required to be an even power of two.  This is needed for some new code
for the realtime device, where we can set 37k allocation units and then
have to remap them.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/xfs_file.c
fs/xfs/xfs_linux.h

index 64278f8acaeee0675567431f7dc089eaef44de89..d1d4158441bd96f2326b9e302253a8b288696066 100644 (file)
@@ -47,15 +47,9 @@ xfs_is_falloc_aligned(
 {
        unsigned int            alloc_unit = xfs_inode_alloc_unitsize(ip);
 
-       if (!is_power_of_2(alloc_unit)) {
-               u32     mod;
-
-               div_u64_rem(pos, alloc_unit, &mod);
-               if (mod)
-                       return false;
-               div_u64_rem(len, alloc_unit, &mod);
-               return mod == 0;
-       }
+       if (!is_power_of_2(alloc_unit))
+               return isaligned_64(pos, alloc_unit) &&
+                      isaligned_64(len, alloc_unit);
 
        return !((pos | len) & (alloc_unit - 1));
 }
index 8f07c9f6157fbe87be564aaf843935cf337a4fd5..ac355328121ac9f4360b97c87aaa43d862979d51 100644 (file)
@@ -198,6 +198,11 @@ static inline uint64_t howmany_64(uint64_t x, uint32_t y)
        return x;
 }
 
+static inline bool isaligned_64(uint64_t x, uint32_t y)
+{
+       return do_div(x, y) == 0;
+}
+
 /* If @b is a power of 2, return log2(b).  Else return -1. */
 static inline int8_t log2_if_power2(unsigned long b)
 {