sys: extend use of the howmany() macro when available.

We have a howmany() macro in the <sys/param.h> header that is
convenient to re-use as it makes things easier to read.
This commit is contained in:
Pedro F. Giffuni 2016-04-26 15:38:17 +00:00
parent c9087a9303
commit 55e0987aea
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=298649
21 changed files with 68 additions and 77 deletions

View File

@ -1699,12 +1699,10 @@ ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
CAM_DIR_OUT,
0,
req->data,
((ranges + ATA_DSM_BLK_RANGES - 1) /
ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
ada_default_timeout * 1000);
ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1) / ATA_DSM_BLK_RANGES);
ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES));
}
static void
@ -1720,13 +1718,12 @@ ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio
CAM_DIR_OUT,
0,
req->data,
((ranges + ATA_DSM_BLK_RANGES - 1) /
ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
ada_default_timeout * 1000);
ata_ncq_cmd(ataio,
ATA_SEND_FPDMA_QUEUED,
0,
(ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES);
howmany(ranges, ATA_DSM_BLK_RANGES));
ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM;
ataio->ata_flags |= ATA_FLAG_AUX;
ataio->aux = 1;

View File

@ -2868,7 +2868,7 @@ da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
}
} while (1);
block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
scsi_ata_trim(&ccb->csio,
/*retries*/da_retry_count,
/*cbfcnp*/dadone,

View File

@ -4207,7 +4207,7 @@ KeQueryInterruptTime(void)
ticks = tvtohz(&tv);
return ticks * ((10000000 + hz - 1) / hz);
return ticks * howmany(10000000, hz);
}
static struct thread *

View File

@ -606,9 +606,8 @@ mountmsdosfs(struct vnode *devvp, struct mount *mp)
} else {
pmp->pm_rootdirblk = pmp->pm_fatblk +
(pmp->pm_FATs * pmp->pm_FATsecs);
pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
+ DEV_BSIZE - 1)
/ DEV_BSIZE; /* in blocks */
pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
sizeof(struct direntry), DEV_BSIZE); /* in blocks */
pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
}

View File

@ -472,8 +472,7 @@ gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
hdr->hdr_lba_table <= hdr->hdr_lba_end)
goto fail;
lba = hdr->hdr_lba_table +
(hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
pp->sectorsize - 1;
howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1;
if (lba >= last)
goto fail;
if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
@ -515,7 +514,7 @@ gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
table->state[elt] = GPT_STATE_MISSING;
tblsz = hdr->hdr_entries * hdr->hdr_entsz;
sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
sectors = howmany(tblsz, pp->sectorsize);
buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) {
size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS:
@ -653,8 +652,8 @@ g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
table = (struct g_part_gpt_table *)basetable;
pp = gpp->gpp_provider;
tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
pp->sectorsize - 1) / pp->sectorsize;
tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
pp->sectorsize);
if (pp->sectorsize < MBRSIZE ||
pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
pp->sectorsize)
@ -1134,8 +1133,8 @@ g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
pp = cp->provider;
table = (struct g_part_gpt_table *)basetable;
tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
pp->sectorsize - 1) / pp->sectorsize;
tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
pp->sectorsize);
/* Reconstruct the MBR from the GPT if under Boot Camp. */
if (table->bootcamp)
@ -1239,8 +1238,8 @@ g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
table = (struct g_part_gpt_table *)basetable;
last = pp->mediasize / pp->sectorsize - 1;
tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
pp->sectorsize - 1) / pp->sectorsize;
tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
pp->sectorsize);
table->lba[GPT_ELT_PRIHDR] = 1;
table->lba[GPT_ELT_PRITBL] = 2;

View File

@ -1013,8 +1013,7 @@ ldm_vmdb_parse(struct ldm_db *db, struct g_consumer *cp)
int error;
pp = cp->provider;
size = (db->dh.last_seq * db->dh.size +
pp->sectorsize - 1) / pp->sectorsize;
size = howmany(db->dh.last_seq * db->dh.size, pp->sectorsize);
size -= 1; /* one sector takes vmdb header */
for (n = 0; n < size; n += MAXPHYS / pp->sectorsize) {
offset = db->ph.db_offset + db->th.conf_offset + n + 1;

View File

@ -593,35 +593,36 @@ ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
if (ss != sample->sectorsize) {
SET32(meta, hdr->WorkSpace_Length,
(GET32(sample, hdr->WorkSpace_Length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->WorkSpace_Length) *
sample->sectorsize, ss));
SET16(meta, hdr->Configuration_Record_Length,
(GET16(sample, hdr->Configuration_Record_Length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET16(sample,
hdr->Configuration_Record_Length) *
sample->sectorsize, ss));
SET32(meta, hdr->cd_length,
(GET32(sample, hdr->cd_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->cd_length) *
sample->sectorsize, ss));
SET32(meta, hdr->pdr_length,
(GET32(sample, hdr->pdr_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->pdr_length) *
sample->sectorsize, ss));
SET32(meta, hdr->vdr_length,
(GET32(sample, hdr->vdr_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->vdr_length) *
sample->sectorsize, ss));
SET32(meta, hdr->cr_length,
(GET32(sample, hdr->cr_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->cr_length) *
sample->sectorsize, ss));
SET32(meta, hdr->pdd_length,
(GET32(sample, hdr->pdd_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->pdd_length) *
sample->sectorsize, ss));
SET32(meta, hdr->bbmlog_length,
(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize, ss));
SET32(meta, hdr->Diagnostic_Space,
(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize, ss));
SET32(meta, hdr->Vendor_Specific_Logs,
(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize + ss - 1) / ss);
howmany(GET32(sample, hdr->bbmlog_length) *
sample->sectorsize, ss));
}
} else {
SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
@ -635,24 +636,23 @@ ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
SET16(meta, hdr->Configuration_Record_Length,
(sizeof(struct ddf_vdc_record) +
(4 + 8) * GET16(meta, hdr->Max_Primary_Element_Entries) +
ss - 1) / ss);
howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
GET16(meta, hdr->Max_Primary_Element_Entries), ss));
SET32(meta, hdr->cd_length,
(sizeof(struct ddf_cd_record) + ss - 1) / ss);
howmany(sizeof(struct ddf_cd_record), ss));
SET32(meta, hdr->pdr_length,
(sizeof(struct ddf_pd_record) +
sizeof(struct ddf_pd_entry) *
GET16(meta, hdr->Max_PD_Entries) + ss - 1) / ss);
howmany(sizeof(struct ddf_pd_record) +
sizeof(struct ddf_pd_entry) * GET16(meta,
hdr->Max_PD_Entries), ss));
SET32(meta, hdr->vdr_length,
(sizeof(struct ddf_vd_record) +
sizeof(struct ddf_vd_entry) *
GET16(meta, hdr->Max_VD_Entries) + ss - 1) / ss);
howmany(sizeof(struct ddf_vd_record) +
sizeof(struct ddf_vd_entry) *
GET16(meta, hdr->Max_VD_Entries), ss));
SET32(meta, hdr->cr_length,
GET16(meta, hdr->Configuration_Record_Length) *
(GET16(meta, hdr->Max_Partitions) + 1));
SET32(meta, hdr->pdd_length,
(sizeof(struct ddf_pdd_record) + ss - 1) / ss);
howmany(sizeof(struct ddf_pdd_record), ss));
SET32(meta, hdr->bbmlog_length, 0);
SET32(meta, hdr->Diagnostic_Space_Length, 0);
SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);

View File

@ -773,7 +773,7 @@ intel_meta_write(struct g_consumer *cp, struct intel_raid_conf *meta)
meta->checksum = checksum;
/* Create and fill buffer. */
sectors = (meta->config_size + pp->sectorsize - 1) / pp->sectorsize;
sectors = howmany(meta->config_size, pp->sectorsize);
buf = malloc(sectors * pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO);
if (sectors > 1) {
memcpy(buf, ((char *)meta) + pp->sectorsize,

View File

@ -195,9 +195,8 @@ g_uzip_cached(struct g_geom *gp, struct bio *bp)
#define TOFF_2_BOFF(sc, pp, bi) ((sc)->toc[(bi)].offset - \
(sc)->toc[(bi)].offset % (pp)->sectorsize)
#define TLEN_2_BLEN(sc, pp, bp, ei) ((BLK_ENDS((sc), (ei)) - \
(bp)->bio_offset + (pp)->sectorsize - 1) / \
(pp)->sectorsize * (pp)->sectorsize)
#define TLEN_2_BLEN(sc, pp, bp, ei) roundup(BLK_ENDS((sc), (ei)) - \
(bp)->bio_offset, (pp)->sectorsize)
static int
g_uzip_request(struct g_geom *gp, struct bio *bp)
@ -220,7 +219,7 @@ g_uzip_request(struct g_geom *gp, struct bio *bp)
ofs = bp->bio_offset + bp->bio_completed;
start_blk = ofs / sc->blksz;
KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
end_blk = (ofs + bp->bio_resid + sc->blksz - 1) / sc->blksz;
end_blk = howmany(ofs + bp->bio_resid, sc->blksz);
KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
for (; BLK_IS_NIL(sc, start_blk) && start_blk < end_blk; start_blk++) {

View File

@ -625,11 +625,10 @@ tvtohz(tv)
#endif
ticks = 1;
} else if (sec <= LONG_MAX / 1000000)
ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
/ tick + 1;
ticks = howmany(sec * 1000000 + (unsigned long)usec, tick) + 1;
else if (sec <= LONG_MAX / hz)
ticks = sec * hz
+ ((unsigned long)usec + (tick - 1)) / tick + 1;
+ howmany((unsigned long)usec, tick) + 1;
else
ticks = LONG_MAX;
if (ticks > INT_MAX)

View File

@ -337,7 +337,7 @@ pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
sbt -= SBT_1S;
}
/* Do the delay remainder, if any */
sbt = (sbt + SBT_1US - 1) / SBT_1US;
sbt = howmany(sbt, SBT_1US);
if (sbt > 0)
DELAY(sbt);
return (0);

View File

@ -453,7 +453,7 @@ kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
case CLOCK_VIRTUAL:
case CLOCK_PROF:
/* Accurately round up here because we can do so cheaply. */
ts->tv_nsec = (1000000000 + hz - 1) / hz;
ts->tv_nsec = howmany(1000000000, hz);
break;
case CLOCK_SECOND:
ts->tv_sec = 1;

View File

@ -1464,11 +1464,11 @@ adjust_timeout_calltodo(struct timeval *time_change)
if (time_change->tv_sec < 0)
return;
else if (time_change->tv_sec <= LONG_MAX / 1000000)
delta_ticks = (time_change->tv_sec * 1000000 +
time_change->tv_usec + (tick - 1)) / tick + 1;
delta_ticks = howmany(time_change->tv_sec * 1000000 +
time_change->tv_usec, tick) + 1;
else if (time_change->tv_sec <= LONG_MAX / hz)
delta_ticks = time_change->tv_sec * hz +
(time_change->tv_usec + (tick - 1)) / tick + 1;
howmany(time_change->tv_usec, tick) + 1;
else
delta_ticks = LONG_MAX;

View File

@ -71,7 +71,7 @@ __FBSDID("$FreeBSD$");
#define PCTRIE_COUNT (1 << PCTRIE_WIDTH)
#define PCTRIE_MASK (PCTRIE_COUNT - 1)
#define PCTRIE_LIMIT (howmany((sizeof(uint64_t) * NBBY), PCTRIE_WIDTH) - 1)
#define PCTRIE_LIMIT (howmany(sizeof(uint64_t) * NBBY, PCTRIE_WIDTH) - 1)
/* Flag bits stored in node pointers. */
#define PCTRIE_ISLEAF 0x1

View File

@ -1542,7 +1542,7 @@ pipe_stat(fp, ub, active_cred, td)
ub->st_size = pipe->pipe_map.cnt;
else
ub->st_size = pipe->pipe_buffer.cnt;
ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
ub->st_blocks = howmany(ub->st_size, ub->st_blksize);
ub->st_atim = pipe->pipe_atime;
ub->st_mtim = pipe->pipe_mtime;
ub->st_ctim = pipe->pipe_ctime;

View File

@ -842,7 +842,7 @@ kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
}
#endif
segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
segs_needed = howmany(msgsz, msginfo.msgssz);
DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
msginfo.msgssz, segs_needed));
for (;;) {

View File

@ -380,7 +380,7 @@ shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
bzero(sb, sizeof(*sb));
sb->st_blksize = PAGE_SIZE;
sb->st_size = shmfd->shm_size;
sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
sb->st_blocks = howmany(sb->st_size, sb->st_blksize);
mtx_lock(&shm_timestamp_lock);
sb->st_atim = shmfd->shm_atime;
sb->st_ctim = shmfd->shm_ctime;

View File

@ -1745,7 +1745,7 @@ vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
/*
* Round up to the *next* lbn.
*/
trunclbn = (length + blksize - 1) / blksize;
trunclbn = howmany(length, blksize);
ASSERT_VOP_LOCKED(vp, "vtruncbuf");
restart:

View File

@ -202,8 +202,7 @@ krb5_derive_key(struct krb5_key_state *inkey,
* Generate enough bytes for keybits rounded up to a multiple
* of blocklen.
*/
randomlen = ((ec->ec_keybits/8 + ec->ec_blocklen - 1) / ec->ec_blocklen)
* ec->ec_blocklen;
randomlen = roundup(ec->ec_keybits / 8, ec->ec_blocklen);
bytes = malloc(randomlen, M_GSSAPI, M_WAITOK);
MGET(m, M_WAITOK, MT_DATA);
m->m_len = ec->ec_blocklen;

View File

@ -274,7 +274,7 @@ arc_frag_next(struct ifnet *ifp)
return m;
++ac->ac_seqid; /* make the seqid unique */
tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
tfrags = howmany(m->m_pkthdr.len, ARC_MAX_DATA);
ac->fsflag = 2 * tfrags - 3;
ac->sflag = 0;
ac->rsflag = ac->fsflag;

View File

@ -846,7 +846,7 @@ ng_pptpgre_start_recv_ack_timer(hpriv_p hpriv)
remain = 0;
/* Be conservative: timeout can happen up to 1 tick early */
ticks = (((remain * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE) + 1;
ticks = howmany(remain * hz, PPTP_TIME_SCALE) + 1;
ng_callout(&hpriv->rackTimer, hpriv->node, hpriv->hook,
ticks, ng_pptpgre_recv_ack_timeout, hpriv, 0);
}
@ -894,7 +894,7 @@ ng_pptpgre_start_send_ack_timer(hpriv_p hpriv)
ackTimeout = PPTP_MAX_ACK_DELAY;
/* Be conservative: timeout can happen up to 1 tick early */
ticks = (((ackTimeout * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE);
ticks = howmany(ackTimeout * hz, PPTP_TIME_SCALE);
ng_callout(&hpriv->sackTimer, hpriv->node, hpriv->hook,
ticks, ng_pptpgre_send_ack_timeout, hpriv, 0);
}