Skip to content

Commit 86437e6

Browse files
Christoph HellwigDarrick J. Wong
authored andcommitted
xfs: switch perag iteration from the for_each macros to a while based iterator
The current for_each_perag* macros are a bit annoying in that they require the caller to both provide an object and an index iterator, and also somewhat obsfucate the underlying control flow mechanism. Switch to open coded while loops using new xfs_perag_next{,_from,_range} helpers that return the next pag structure to iterate on based on the previous one or NULL for the loop start. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]>
1 parent 8199287 commit 86437e6

17 files changed

+79
-105
lines changed

fs/xfs/libxfs/xfs_ag.h

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,34 @@ xfs_perag_rele(
208208
xfs_group_rele(pag_group(pag));
209209
}
210210

211+
static inline struct xfs_perag *
212+
xfs_perag_next_range(
213+
struct xfs_mount *mp,
214+
struct xfs_perag *pag,
215+
xfs_agnumber_t start_agno,
216+
xfs_agnumber_t end_agno)
217+
{
218+
return to_perag(xfs_group_next_range(mp, pag ? pag_group(pag) : NULL,
219+
start_agno, end_agno, XG_TYPE_AG));
220+
}
221+
222+
static inline struct xfs_perag *
223+
xfs_perag_next_from(
224+
struct xfs_mount *mp,
225+
struct xfs_perag *pag,
226+
xfs_agnumber_t start_agno)
227+
{
228+
return xfs_perag_next_range(mp, pag, start_agno, mp->m_sb.sb_agcount - 1);
229+
}
230+
231+
static inline struct xfs_perag *
232+
xfs_perag_next(
233+
struct xfs_mount *mp,
234+
struct xfs_perag *pag)
235+
{
236+
return xfs_perag_next_from(mp, pag, 0);
237+
}
238+
211239
/*
212240
* Per-ag geometry infomation and validation
213241
*/
@@ -273,40 +301,6 @@ xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
273301
agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
274302
}
275303

276-
/*
277-
* Perag iteration APIs
278-
*/
279-
static inline struct xfs_perag *
280-
xfs_perag_next(
281-
struct xfs_perag *pag,
282-
xfs_agnumber_t *agno,
283-
xfs_agnumber_t end_agno)
284-
{
285-
struct xfs_mount *mp = pag_mount(pag);
286-
287-
*agno = pag_agno(pag) + 1;
288-
xfs_perag_rele(pag);
289-
while (*agno <= end_agno) {
290-
pag = xfs_perag_grab(mp, *agno);
291-
if (pag)
292-
return pag;
293-
(*agno)++;
294-
}
295-
return NULL;
296-
}
297-
298-
#define for_each_perag_range(mp, agno, end_agno, pag) \
299-
for ((pag) = xfs_perag_grab((mp), (agno)); \
300-
(pag) != NULL; \
301-
(pag) = xfs_perag_next((pag), &(agno), (end_agno)))
302-
303-
#define for_each_perag_from(mp, agno, pag) \
304-
for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
305-
306-
#define for_each_perag(mp, agno, pag) \
307-
(agno) = 0; \
308-
for_each_perag_from((mp), (agno), (pag))
309-
310304
static inline struct xfs_perag *
311305
xfs_perag_next_wrap(
312306
struct xfs_perag *pag,

fs/xfs/libxfs/xfs_sb.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,14 +1109,13 @@ int
11091109
xfs_update_secondary_sbs(
11101110
struct xfs_mount *mp)
11111111
{
1112-
struct xfs_perag *pag;
1113-
xfs_agnumber_t agno = 1;
1112+
struct xfs_perag *pag = NULL;
11141113
int saved_error = 0;
11151114
int error = 0;
11161115
LIST_HEAD (buffer_list);
11171116

11181117
/* update secondary superblocks. */
1119-
for_each_perag_from(mp, agno, pag) {
1118+
while ((pag = xfs_perag_next_from(mp, pag, 1))) {
11201119
struct xfs_buf *bp;
11211120

11221121
error = xfs_buf_get(mp->m_ddev_targp,
@@ -1146,7 +1145,7 @@ xfs_update_secondary_sbs(
11461145
xfs_buf_relse(bp);
11471146

11481147
/* don't hold too many buffers at once */
1149-
if (agno % 16)
1148+
if (pag_agno(pag) % 16)
11501149
continue;
11511150

11521151
error = xfs_buf_delwri_submit(&buffer_list);
@@ -1160,12 +1159,8 @@ xfs_update_secondary_sbs(
11601159
}
11611160
}
11621161
error = xfs_buf_delwri_submit(&buffer_list);
1163-
if (error) {
1164-
xfs_warn(mp,
1165-
"write error %d updating a secondary superblock near ag %d",
1166-
error, agno);
1167-
}
1168-
1162+
if (error)
1163+
xfs_warn(mp, "error %d writing secondary superblocks", error);
11691164
return saved_error ? saved_error : error;
11701165
}
11711166

fs/xfs/libxfs/xfs_types.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,12 @@ xfs_icount_range(
170170
unsigned long long *max)
171171
{
172172
unsigned long long nr_inos = 0;
173-
struct xfs_perag *pag;
174-
xfs_agnumber_t agno;
173+
struct xfs_perag *pag = NULL;
175174

176175
/* root, rtbitmap, rtsum all live in the first chunk */
177176
*min = XFS_INODES_PER_CHUNK;
178177

179-
for_each_perag(mp, agno, pag)
178+
while ((pag = xfs_perag_next(mp, pag)))
180179
nr_inos += pag->agino_max - pag->agino_min + 1;
181180
*max = nr_inos;
182181
}

fs/xfs/scrub/bmap.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,10 @@ xchk_bmap_check_rmaps(
760760
struct xfs_scrub *sc,
761761
int whichfork)
762762
{
763-
struct xfs_perag *pag;
764-
xfs_agnumber_t agno;
763+
struct xfs_perag *pag = NULL;
765764
int error;
766765

767-
for_each_perag(sc->mp, agno, pag) {
766+
while ((pag = xfs_perag_next(sc->mp, pag))) {
768767
error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag);
769768
if (error ||
770769
(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {

fs/xfs/scrub/bmap_repair.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,11 @@ xrep_bmap_find_mappings(
407407
struct xrep_bmap *rb)
408408
{
409409
struct xfs_scrub *sc = rb->sc;
410-
struct xfs_perag *pag;
411-
xfs_agnumber_t agno;
410+
struct xfs_perag *pag = NULL;
412411
int error = 0;
413412

414413
/* Iterate the rmaps for extents. */
415-
for_each_perag(sc->mp, agno, pag) {
414+
while ((pag = xfs_perag_next(sc->mp, pag))) {
416415
error = xrep_bmap_scan_ag(rb, pag);
417416
if (error) {
418417
xfs_perag_rele(pag);

fs/xfs/scrub/fscounters.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ xchk_fscount_warmup(
7474
struct xfs_buf *agi_bp = NULL;
7575
struct xfs_buf *agf_bp = NULL;
7676
struct xfs_perag *pag = NULL;
77-
xfs_agnumber_t agno;
7877
int error = 0;
7978

80-
for_each_perag(mp, agno, pag) {
79+
while ((pag = xfs_perag_next(mp, pag))) {
8180
if (xchk_should_terminate(sc, &error))
8281
break;
8382
if (xfs_perag_initialised_agi(pag) &&
@@ -295,9 +294,8 @@ xchk_fscount_aggregate_agcounts(
295294
struct xchk_fscounters *fsc)
296295
{
297296
struct xfs_mount *mp = sc->mp;
298-
struct xfs_perag *pag;
297+
struct xfs_perag *pag = NULL;
299298
uint64_t delayed;
300-
xfs_agnumber_t agno;
301299
int tries = 8;
302300
int error = 0;
303301

@@ -306,7 +304,7 @@ xchk_fscount_aggregate_agcounts(
306304
fsc->ifree = 0;
307305
fsc->fdblocks = 0;
308306

309-
for_each_perag(mp, agno, pag) {
307+
while ((pag = xfs_perag_next(mp, pag))) {
310308
if (xchk_should_terminate(sc, &error))
311309
break;
312310

@@ -327,7 +325,7 @@ xchk_fscount_aggregate_agcounts(
327325
if (xfs_has_lazysbcount(sc->mp)) {
328326
fsc->fdblocks += pag->pagf_btreeblks;
329327
} else {
330-
error = xchk_fscount_btreeblks(sc, fsc, agno);
328+
error = xchk_fscount_btreeblks(sc, fsc, pag_agno(pag));
331329
if (error)
332330
break;
333331
}

fs/xfs/scrub/health.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,11 @@ STATIC void
160160
xchk_mark_all_healthy(
161161
struct xfs_mount *mp)
162162
{
163-
struct xfs_perag *pag;
164-
xfs_agnumber_t agno;
163+
struct xfs_perag *pag = NULL;
165164

166165
xfs_fs_mark_healthy(mp, XFS_SICK_FS_INDIRECT);
167166
xfs_rt_mark_healthy(mp, XFS_SICK_RT_INDIRECT);
168-
for_each_perag(mp, agno, pag)
167+
while ((pag = xfs_perag_next(mp, pag)))
169168
xfs_ag_mark_healthy(pag, XFS_SICK_AG_INDIRECT);
170169
}
171170

@@ -294,9 +293,7 @@ xchk_health_record(
294293
struct xfs_scrub *sc)
295294
{
296295
struct xfs_mount *mp = sc->mp;
297-
struct xfs_perag *pag;
298-
xfs_agnumber_t agno;
299-
296+
struct xfs_perag *pag = NULL;
300297
unsigned int sick;
301298
unsigned int checked;
302299

@@ -308,7 +305,7 @@ xchk_health_record(
308305
if (sick & XFS_SICK_RT_PRIMARY)
309306
xchk_set_corrupt(sc);
310307

311-
for_each_perag(mp, agno, pag) {
308+
while ((pag = xfs_perag_next(mp, pag))) {
312309
xfs_ag_measure_sickness(pag, &sick, &checked);
313310
if (sick & XFS_SICK_AG_PRIMARY)
314311
xchk_set_corrupt(sc);

fs/xfs/scrub/inode_repair.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,13 @@ STATIC int
761761
xrep_dinode_count_rmaps(
762762
struct xrep_inode *ri)
763763
{
764-
struct xfs_perag *pag;
765-
xfs_agnumber_t agno;
764+
struct xfs_perag *pag = NULL;
766765
int error;
767766

768767
if (!xfs_has_rmapbt(ri->sc->mp) || xfs_has_realtime(ri->sc->mp))
769768
return -EOPNOTSUPP;
770769

771-
for_each_perag(ri->sc->mp, agno, pag) {
770+
while ((pag = xfs_perag_next(ri->sc->mp, pag))) {
772771
error = xrep_dinode_count_ag_rmaps(ri, pag);
773772
if (error) {
774773
xfs_perag_rele(pag);

fs/xfs/xfs_discard.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ xfs_trim_datadev_extents(
387387
{
388388
xfs_agnumber_t start_agno, end_agno;
389389
xfs_agblock_t start_agbno, end_agbno;
390+
struct xfs_perag *pag = NULL;
390391
xfs_daddr_t ddev_end;
391-
struct xfs_perag *pag;
392392
int last_error = 0, error;
393393

394394
ddev_end = min_t(xfs_daddr_t, end,
@@ -399,10 +399,10 @@ xfs_trim_datadev_extents(
399399
end_agno = xfs_daddr_to_agno(mp, ddev_end);
400400
end_agbno = xfs_daddr_to_agbno(mp, ddev_end);
401401

402-
for_each_perag_range(mp, start_agno, end_agno, pag) {
402+
while ((pag = xfs_perag_next_range(mp, pag, start_agno, end_agno))) {
403403
xfs_agblock_t agend = pag->block_count;
404404

405-
if (start_agno == end_agno)
405+
if (pag_agno(pag) == end_agno)
406406
agend = end_agbno;
407407
error = xfs_trim_perag_extents(pag, start_agbno, agend, minlen);
408408
if (error)

fs/xfs/xfs_extent_busy.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,10 @@ void
629629
xfs_extent_busy_wait_all(
630630
struct xfs_mount *mp)
631631
{
632-
struct xfs_perag *pag;
632+
struct xfs_perag *pag = NULL;
633633
DEFINE_WAIT (wait);
634-
xfs_agnumber_t agno;
635634

636-
for_each_perag(mp, agno, pag) {
635+
while ((pag = xfs_perag_next(mp, pag))) {
637636
do {
638637
prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
639638
if (RB_EMPTY_ROOT(&pag->pagb_tree))

0 commit comments

Comments
 (0)