From: Darrick J. Wong <darrick.wong@xxxxxxxxxx> Create xfrog analogues of the libxfs byte/sector/block conversion functions and convert spaceman to use them instead of open-coded arithmatic we do now. Signed-off-by: Darrick J. Wong <darrick.wong@xxxxxxxxxx> --- libfrog/fsgeom.c | 1 + libfrog/fsgeom.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ spaceman/freesp.c | 15 +++------ spaceman/trim.c | 9 +++--- 4 files changed, 96 insertions(+), 14 deletions(-) diff --git a/libfrog/fsgeom.c b/libfrog/fsgeom.c index bc872834..631286cd 100644 --- a/libfrog/fsgeom.c +++ b/libfrog/fsgeom.c @@ -113,6 +113,7 @@ xfd_prepare_geometry( xfd->inodelog = highbit32(xfd->fsgeom.inodesize); xfd->inopblog = xfd->blocklog - xfd->inodelog; xfd->aginolog = xfd->agblklog + xfd->inopblog; + xfd->blkbb_log = xfd->blocklog - BBSHIFT; return 0; } diff --git a/libfrog/fsgeom.h b/libfrog/fsgeom.h index 6993dafb..5dcfc1bb 100644 --- a/libfrog/fsgeom.h +++ b/libfrog/fsgeom.h @@ -35,6 +35,9 @@ struct xfs_fd { /* bits for agino in inum */ unsigned int aginolog; + + /* log2 of sb_blocksize / sb_sectsize */ + unsigned int blkbb_log; }; /* Static initializers */ @@ -99,4 +102,86 @@ cvt_b_to_off_fsbt( return bytes >> xfd->blocklog; } +/* Convert sector number to bytes. */ +static inline uint64_t +cvt_bbtob( + uint64_t daddr) +{ + return daddr << BBSHIFT; +} + +/* Convert bytes to sector number, rounding down. */ +static inline uint64_t +cvt_btobbt( + uint64_t bytes) +{ + return bytes >> BBSHIFT; +} + +/* Convert fs block number to sector number. */ +static inline uint64_t +cvt_off_fsb_to_bb( + struct xfs_fd *xfd, + uint64_t fsbno) +{ + return fsbno << xfd->blkbb_log; +} + +/* Convert sector number to fs block number, rounded down. */ +static inline uint64_t +cvt_bb_to_off_fsbt( + struct xfs_fd *xfd, + uint64_t daddr) +{ + return daddr >> xfd->blkbb_log; +} + +/* Convert AG number and AG block to fs block number */ +static inline uint64_t +cvt_agb_to_daddr( + struct xfs_fd *xfd, + uint32_t agno, + uint32_t agbno) +{ + return cvt_off_fsb_to_bb(xfd, + (uint64_t)agno * xfd->fsgeom.agblocks + agbno); +} + +/* Convert sector number to AG number. */ +static inline uint32_t +cvt_daddr_to_agno( + struct xfs_fd *xfd, + uint64_t daddr) +{ + return cvt_bb_to_off_fsbt(xfd, daddr) / xfd->fsgeom.agblocks; +} + +/* Convert sector number to AG block number. */ +static inline uint32_t +cvt_daddr_to_agbno( + struct xfs_fd *xfd, + uint64_t daddr) +{ + return cvt_bb_to_off_fsbt(xfd, daddr) % xfd->fsgeom.agblocks; +} + +/* Convert AG number and AG block to a byte location on disk. */ +static inline uint64_t +cvt_agbno_to_b( + struct xfs_fd *xfd, + xfs_agnumber_t agno, + xfs_agblock_t agbno) +{ + return cvt_bbtob(cvt_agb_to_daddr(xfd, agno, agbno)); +} + +/* Convert byte location on disk to AG block. */ +static inline xfs_agblock_t +cvt_b_to_agbno( + struct xfs_fd *xfd, + uint64_t byteno) +{ + return cvt_daddr_to_agbno(xfd, cvt_btobbt(byteno)); +} + #endif /* __LIBFROG_FSGEOM_H__ */ diff --git a/spaceman/freesp.c b/spaceman/freesp.c index f30171f0..92cdb743 100644 --- a/spaceman/freesp.c +++ b/spaceman/freesp.c @@ -150,9 +150,7 @@ scan_ag( struct fsmap *extent; struct fsmap *l, *h; struct fsmap *p; - struct xfs_fsop_geom *fsgeom = &file->xfd.fsgeom; - off64_t blocksize = fsgeom->blocksize; - off64_t bperag; + struct xfs_fd *xfd = &file->xfd; off64_t aglen; xfs_agblock_t agbno; unsigned long long freeblks = 0; @@ -160,8 +158,6 @@ scan_ag( int ret; int i; - bperag = (off64_t)fsgeom->agblocks * blocksize; - fsmap = malloc(fsmap_sizeof(NR_EXTENTS)); if (!fsmap) { fprintf(stderr, _("%s: fsmap malloc failed.\n"), progname); @@ -174,8 +170,8 @@ scan_ag( l = fsmap->fmh_keys; h = fsmap->fmh_keys + 1; if (agno != NULLAGNUMBER) { - l->fmr_physical = agno * bperag; - h->fmr_physical = ((agno + 1) * bperag) - 1; + l->fmr_physical = cvt_agbno_to_b(xfd, agno, 0); + h->fmr_physical = cvt_agbno_to_b(xfd, agno + 1, 0); l->fmr_device = h->fmr_device = file->fs_path.fs_datadev; } else { l->fmr_physical = 0; @@ -206,9 +202,8 @@ scan_ag( if (!(extent->fmr_flags & FMR_OF_SPECIAL_OWNER) || extent->fmr_owner != XFS_FMR_OWN_FREE) continue; - agbno = (extent->fmr_physical - (bperag * agno)) / - blocksize; - aglen = extent->fmr_length / blocksize; + agbno = cvt_b_to_agbno(xfd, extent->fmr_physical); + aglen = cvt_b_to_off_fsbt(xfd, extent->fmr_length); freeblks += aglen; freeexts++; diff --git a/spaceman/trim.c b/spaceman/trim.c index daf4e427..e9ed47e4 100644 --- a/spaceman/trim.c +++ b/spaceman/trim.c @@ -23,7 +23,8 @@ trim_f( char **argv) { struct fstrim_range trim = {0}; - struct xfs_fsop_geom *fsgeom = &file->xfd.fsgeom; + struct xfs_fd *xfd = &file->xfd; + struct xfs_fsop_geom *fsgeom = &xfd->fsgeom; xfs_agnumber_t agno = 0; off64_t offset = 0; ssize_t length = 0; @@ -66,11 +67,11 @@ trim_f( length = cvtnum(fsgeom->blocksize, fsgeom->sectsize, argv[optind + 1]); } else if (agno) { - offset = (off64_t)agno * fsgeom->agblocks * fsgeom->blocksize; - length = fsgeom->agblocks * fsgeom->blocksize; + offset = cvt_agbno_to_b(xfd, agno, 0); + length = cvt_off_fsb_to_b(xfd, fsgeom->agblocks); } else { offset = 0; - length = fsgeom->datablocks * fsgeom->blocksize; + length = cvt_off_fsb_to_b(xfd, fsgeom->datablocks); } trim.start = offset;