Skip to content

Commit 9d4ca5a

Browse files
author
Chandan Babu R
committed
Merge tag 'refactor-rt-unit-conversions-6.7_2023-10-19' of https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux into xfs-6.7-mergeA
xfs: refactor rt extent unit conversions [v1.1] This series replaces all the open-coded integer division and multiplication conversions between rt blocks and rt extents with calls to static inline helpers. Having cleaned all that up, the helpers are augmented to skip the expensive operations in favor of bit shifts and masking if the rt extent size is a power of two. v1.1: various cleanups suggested by hch With a bit of luck, this should all go splendidly. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Chandan Babu R <chandanbabu@kernel.org> * tag 'refactor-rt-unit-conversions-6.7_2023-10-19' of https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux: xfs: use shifting and masking when converting rt extents, if possible xfs: create rt extent rounding helpers for realtime extent blocks xfs: convert do_div calls to xfs_rtb_to_rtx helper calls xfs: create helpers to convert rt block numbers to rt extent numbers xfs: create a helper to convert extlen to rtextlen xfs: create a helper to compute leftovers of realtime extents xfs: create a helper to convert rtextents to rtblocks
2 parents 3ef52c0 + ef5a83b commit 9d4ca5a

17 files changed

+200
-66
lines changed

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,7 +2989,7 @@ xfs_bmap_extsize_align(
29892989
* If realtime, and the result isn't a multiple of the realtime
29902990
* extent size we need to remove blocks until it is.
29912991
*/
2992-
if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2992+
if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
29932993
/*
29942994
* We're not covering the original request, or
29952995
* we won't be able to once we fix the length.
@@ -3016,7 +3016,7 @@ xfs_bmap_extsize_align(
30163016
else {
30173017
align_alen -= orig_off - align_off;
30183018
align_off = orig_off;
3019-
align_alen -= align_alen % mp->m_sb.sb_rextsize;
3019+
align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
30203020
}
30213021
/*
30223022
* Result doesn't cover the request, fail it.
@@ -4826,12 +4826,8 @@ xfs_bmap_del_extent_delay(
48264826
ASSERT(got->br_startoff <= del->br_startoff);
48274827
ASSERT(got_endoff >= del_endoff);
48284828

4829-
if (isrt) {
4830-
uint64_t rtexts = del->br_blockcount;
4831-
4832-
do_div(rtexts, mp->m_sb.sb_rextsize);
4833-
xfs_mod_frextents(mp, rtexts);
4834-
}
4829+
if (isrt)
4830+
xfs_mod_frextents(mp, xfs_rtb_to_rtx(mp, del->br_blockcount));
48354831

48364832
/*
48374833
* Update the inode delalloc counter now and wait to update the
@@ -5276,7 +5272,6 @@ __xfs_bunmapi(
52765272
int tmp_logflags; /* partial logging flags */
52775273
int wasdel; /* was a delayed alloc extent */
52785274
int whichfork; /* data or attribute fork */
5279-
xfs_fsblock_t sum;
52805275
xfs_filblks_t len = *rlen; /* length to unmap in file */
52815276
xfs_fileoff_t end;
52825277
struct xfs_iext_cursor icur;
@@ -5371,8 +5366,8 @@ __xfs_bunmapi(
53715366
if (!isrt)
53725367
goto delete;
53735368

5374-
sum = del.br_startblock + del.br_blockcount;
5375-
div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5369+
mod = xfs_rtb_to_rtxoff(mp,
5370+
del.br_startblock + del.br_blockcount);
53765371
if (mod) {
53775372
/*
53785373
* Realtime extent not lined up at the end.
@@ -5419,7 +5414,8 @@ __xfs_bunmapi(
54195414
goto error0;
54205415
goto nodelete;
54215416
}
5422-
div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5417+
5418+
mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
54235419
if (mod) {
54245420
xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
54255421

fs/xfs/libxfs/xfs_rtbitmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,13 +1024,13 @@ xfs_rtfree_blocks(
10241024

10251025
ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
10261026

1027-
len = div_u64_rem(rtlen, mp->m_sb.sb_rextsize, &mod);
1027+
len = xfs_rtb_to_rtxrem(mp, rtlen, &mod);
10281028
if (mod) {
10291029
ASSERT(mod == 0);
10301030
return -EIO;
10311031
}
10321032

1033-
start = div_u64_rem(rtbno, mp->m_sb.sb_rextsize, &mod);
1033+
start = xfs_rtb_to_rtxrem(mp, rtbno, &mod);
10341034
if (mod) {
10351035
ASSERT(mod == 0);
10361036
return -EIO;

fs/xfs/libxfs/xfs_rtbitmap.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,131 @@
66
#ifndef __XFS_RTBITMAP_H__
77
#define __XFS_RTBITMAP_H__
88

9+
static inline xfs_rtblock_t
10+
xfs_rtx_to_rtb(
11+
struct xfs_mount *mp,
12+
xfs_rtxnum_t rtx)
13+
{
14+
if (mp->m_rtxblklog >= 0)
15+
return rtx << mp->m_rtxblklog;
16+
17+
return rtx * mp->m_sb.sb_rextsize;
18+
}
19+
20+
static inline xfs_extlen_t
21+
xfs_rtxlen_to_extlen(
22+
struct xfs_mount *mp,
23+
xfs_rtxlen_t rtxlen)
24+
{
25+
if (mp->m_rtxblklog >= 0)
26+
return rtxlen << mp->m_rtxblklog;
27+
28+
return rtxlen * mp->m_sb.sb_rextsize;
29+
}
30+
31+
/* Compute the misalignment between an extent length and a realtime extent .*/
32+
static inline unsigned int
33+
xfs_extlen_to_rtxmod(
34+
struct xfs_mount *mp,
35+
xfs_extlen_t len)
36+
{
37+
if (mp->m_rtxblklog >= 0)
38+
return len & mp->m_rtxblkmask;
39+
40+
return len % mp->m_sb.sb_rextsize;
41+
}
42+
43+
static inline xfs_rtxlen_t
44+
xfs_extlen_to_rtxlen(
45+
struct xfs_mount *mp,
46+
xfs_extlen_t len)
47+
{
48+
if (mp->m_rtxblklog >= 0)
49+
return len >> mp->m_rtxblklog;
50+
51+
return len / mp->m_sb.sb_rextsize;
52+
}
53+
54+
/* Convert an rt block number into an rt extent number. */
55+
static inline xfs_rtxnum_t
56+
xfs_rtb_to_rtx(
57+
struct xfs_mount *mp,
58+
xfs_rtblock_t rtbno)
59+
{
60+
if (likely(mp->m_rtxblklog >= 0))
61+
return rtbno >> mp->m_rtxblklog;
62+
63+
return div_u64(rtbno, mp->m_sb.sb_rextsize);
64+
}
65+
66+
/* Return the offset of an rt block number within an rt extent. */
67+
static inline xfs_extlen_t
68+
xfs_rtb_to_rtxoff(
69+
struct xfs_mount *mp,
70+
xfs_rtblock_t rtbno)
71+
{
72+
if (likely(mp->m_rtxblklog >= 0))
73+
return rtbno & mp->m_rtxblkmask;
74+
75+
return do_div(rtbno, mp->m_sb.sb_rextsize);
76+
}
77+
78+
/*
79+
* Crack an rt block number into an rt extent number and an offset within that
80+
* rt extent. Returns the rt extent number directly and the offset in @off.
81+
*/
82+
static inline xfs_rtxnum_t
83+
xfs_rtb_to_rtxrem(
84+
struct xfs_mount *mp,
85+
xfs_rtblock_t rtbno,
86+
xfs_extlen_t *off)
87+
{
88+
if (likely(mp->m_rtxblklog >= 0)) {
89+
*off = rtbno & mp->m_rtxblkmask;
90+
return rtbno >> mp->m_rtxblklog;
91+
}
92+
93+
return div_u64_rem(rtbno, mp->m_sb.sb_rextsize, off);
94+
}
95+
96+
/*
97+
* Convert an rt block number into an rt extent number, rounding up to the next
98+
* rt extent if the rt block is not aligned to an rt extent boundary.
99+
*/
100+
static inline xfs_rtxnum_t
101+
xfs_rtb_to_rtxup(
102+
struct xfs_mount *mp,
103+
xfs_rtblock_t rtbno)
104+
{
105+
if (likely(mp->m_rtxblklog >= 0)) {
106+
if (rtbno & mp->m_rtxblkmask)
107+
return (rtbno >> mp->m_rtxblklog) + 1;
108+
return rtbno >> mp->m_rtxblklog;
109+
}
110+
111+
if (do_div(rtbno, mp->m_sb.sb_rextsize))
112+
rtbno++;
113+
return rtbno;
114+
}
115+
116+
/* Round this rtblock up to the nearest rt extent size. */
117+
static inline xfs_rtblock_t
118+
xfs_rtb_roundup_rtx(
119+
struct xfs_mount *mp,
120+
xfs_rtblock_t rtbno)
121+
{
122+
return roundup_64(rtbno, mp->m_sb.sb_rextsize);
123+
}
124+
125+
/* Round this rtblock down to the nearest rt extent size. */
126+
static inline xfs_rtblock_t
127+
xfs_rtb_rounddown_rtx(
128+
struct xfs_mount *mp,
129+
xfs_rtblock_t rtbno)
130+
{
131+
return rounddown_64(rtbno, mp->m_sb.sb_rextsize);
132+
}
133+
9134
/*
10135
* Functions for walking free space rtextents in the realtime bitmap.
11136
*/

fs/xfs/libxfs/xfs_sb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ xfs_sb_mount_common(
975975
mp->m_blockmask = sbp->sb_blocksize - 1;
976976
mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
977977
mp->m_blockwmask = mp->m_blockwsize - 1;
978+
mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize);
979+
mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize);
978980

979981
mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
980982
mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);

fs/xfs/libxfs/xfs_trans_resv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "xfs_trans.h"
2020
#include "xfs_qm.h"
2121
#include "xfs_trans_space.h"
22+
#include "xfs_rtbitmap.h"
2223

2324
#define _ALLOC true
2425
#define _FREE false
@@ -220,7 +221,7 @@ xfs_rtalloc_block_count(
220221
unsigned int blksz = XFS_FSB_TO_B(mp, 1);
221222
unsigned int rtbmp_bytes;
222223

223-
rtbmp_bytes = (XFS_MAX_BMBT_EXTLEN / mp->m_sb.sb_rextsize) / NBBY;
224+
rtbmp_bytes = xfs_extlen_to_rtxlen(mp, XFS_MAX_BMBT_EXTLEN) / NBBY;
224225
return (howmany(rtbmp_bytes, blksz) + 1) * num_ops;
225226
}
226227

fs/xfs/scrub/inode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "xfs_reflink.h"
2121
#include "xfs_rmap.h"
2222
#include "xfs_bmap_util.h"
23+
#include "xfs_rtbitmap.h"
2324
#include "scrub/scrub.h"
2425
#include "scrub/common.h"
2526
#include "scrub/btree.h"
@@ -225,7 +226,7 @@ xchk_inode_extsize(
225226
*/
226227
if ((flags & XFS_DIFLAG_RTINHERIT) &&
227228
(flags & XFS_DIFLAG_EXTSZINHERIT) &&
228-
value % sc->mp->m_sb.sb_rextsize > 0)
229+
xfs_extlen_to_rtxmod(sc->mp, value) > 0)
229230
xchk_ino_set_warning(sc, ino);
230231
}
231232

fs/xfs/scrub/rtbitmap.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ xchk_rtbitmap_rec(
5050
xfs_rtblock_t startblock;
5151
xfs_filblks_t blockcount;
5252

53-
startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
54-
blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
53+
startblock = xfs_rtx_to_rtb(mp, rec->ar_startext);
54+
blockcount = xfs_rtx_to_rtb(mp, rec->ar_extcount);
5555

5656
if (!xfs_verify_rtbext(mp, startblock, blockcount))
5757
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
@@ -128,26 +128,22 @@ xchk_rtbitmap(
128128
void
129129
xchk_xref_is_used_rt_space(
130130
struct xfs_scrub *sc,
131-
xfs_rtblock_t fsbno,
131+
xfs_rtblock_t rtbno,
132132
xfs_extlen_t len)
133133
{
134134
xfs_rtxnum_t startext;
135135
xfs_rtxnum_t endext;
136-
xfs_rtxlen_t extcount;
137136
bool is_free;
138137
int error;
139138

140139
if (xchk_skip_xref(sc->sm))
141140
return;
142141

143-
startext = fsbno;
144-
endext = fsbno + len - 1;
145-
do_div(startext, sc->mp->m_sb.sb_rextsize);
146-
do_div(endext, sc->mp->m_sb.sb_rextsize);
147-
extcount = endext - startext + 1;
142+
startext = xfs_rtb_to_rtx(sc->mp, rtbno);
143+
endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1);
148144
xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
149-
error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
150-
&is_free);
145+
error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext,
146+
endext - startext + 1, &is_free);
151147
if (!xchk_should_check_xref(sc, &error, NULL))
152148
goto out_unlock;
153149
if (is_free)

fs/xfs/scrub/rtsummary.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ xchk_rtsum_record_free(
134134
lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
135135
offs = XFS_SUMOFFS(mp, lenlog, rbmoff);
136136

137-
rtbno = rec->ar_startext * mp->m_sb.sb_rextsize;
138-
rtlen = rec->ar_extcount * mp->m_sb.sb_rextsize;
137+
rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
138+
rtlen = xfs_rtx_to_rtb(mp, rec->ar_extcount);
139139

140140
if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
141141
xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);

0 commit comments

Comments
 (0)