Pushing Claudio's suggestion into potentially reductio ad absurdum
territory, does this version work?

It seems to read better to me if it does work.

I leave off the separate issue of freeing the work units. I definitely
lack the knowledge to know how good an idea that is.

I have 4K disks but no softraid, I only get dragged into softraid when
it's original sin of mixing bytes, DEV_BSIZE and disk sector values into
various fields blows something up. :-)
-- 
.... Ken

diff --git a/sys/dev/softraid.c b/sys/dev/softraid.c
index 60c5eb0bc02..b4b042c7f89 100644
--- a/sys/dev/softraid.c
+++ b/sys/dev/softraid.c
@@ -4677,24 +4677,25 @@ void
 sr_rebuild(struct sr_discipline *sd)
 {
        struct sr_softc         *sc = sd->sd_sc;
-       u_int64_t               sz, whole_blk, partial_blk, blk, restart;
-       daddr_t                 lba;
+       u_int64_t               io_bytes, partial_io;
+       u_int64_t               restart_lba, lba, final_lba;
+       u_int32_t               sec_size;
        struct sr_workunit      *wu_r, *wu_w;
        struct scsi_xfer        xs_r, xs_w;
        struct scsi_rw_16       *cr, *cw;
        int                     c, s, slept, percent = 0, old_percent = -1;
        u_int8_t                *buf;
 
-       whole_blk = sd->sd_meta->ssdi.ssd_size / SR_REBUILD_IO_SIZE;
-       partial_blk = sd->sd_meta->ssdi.ssd_size % SR_REBUILD_IO_SIZE;
+       sec_size = sd->sd_meta->ssdi.ssd_secsize;
+       restart_lba = sd->sd_meta->ssd_rebuild / (sec_size / DEV_BSIZE);
+       final_lba = ((sd->sd_meta->ssdi.ssd_size / SR_REBUILD_IO_SIZE) * 
SR_REBUILD_IO_SIZE) / sec_size;
 
-       restart = sd->sd_meta->ssd_rebuild / SR_REBUILD_IO_SIZE;
-       if (restart > whole_blk) {
+       if (restart_lba > final_lba) {
                printf("%s: bogus rebuild restart offset, starting from 0\n",
                    DEVNAME(sc));
-               restart = 0;
+               restart_lba = 0;
        }
-       if (restart) {
+       if (restart_lba) {
                /*
                 * XXX there is a hole here; there is a possibility that we
                 * had a restart however the chunk that was supposed to
@@ -4710,14 +4711,16 @@ sr_rebuild(struct sr_discipline *sd)
        }
 
        /* currently this is 64k therefore we can use dma_alloc */
-       buf = dma_alloc(SR_REBUILD_IO_SIZE << DEV_BSHIFT, PR_WAITOK);
-       for (blk = restart; blk <= whole_blk; blk++) {
-               lba = blk * SR_REBUILD_IO_SIZE;
-               sz = SR_REBUILD_IO_SIZE;
-               if (blk == whole_blk) {
-                       if (partial_blk == 0)
+       io_bytes = SR_REBUILD_IO_SIZE << DEV_BSHIFT;
+       buf = dma_alloc(io_bytes, PR_WAITOK);
+
+       partial_io = (sd->sd_meta->ssdi.ssd_size % SR_REBUILD_IO_SIZE) << 
DEV_BSHIFT;
+
+       for (lba = restart_lba; lba <= final_lba; lba += io_bytes / sec_size ) {
+               if (lba == final_lba) {
+                       if (partial_io == 0)
                                break;
-                       sz = partial_blk;
+                       io_bytes = partial_io;
                }
 
                /* get some wu */
@@ -4731,12 +4734,12 @@ sr_rebuild(struct sr_discipline *sd)
                bzero(&xs_r, sizeof xs_r);
                xs_r.error = XS_NOERROR;
                xs_r.flags = SCSI_DATA_IN;
-               xs_r.datalen = sz << DEV_BSHIFT;
+               xs_r.datalen = io_bytes;
                xs_r.data = buf;
                xs_r.cmdlen = sizeof(*cr);
                cr = (struct scsi_rw_16 *)&xs_r.cmd;
                cr->opcode = READ_16;
-               _lto4b(sz, cr->length);
+               _lto4b(io_bytes / sec_size, cr->length);
                _lto8b(lba, cr->addr);
                wu_r->swu_state = SR_WU_CONSTRUCT;
                wu_r->swu_flags |= SR_WUF_REBUILD;
@@ -4751,12 +4754,12 @@ sr_rebuild(struct sr_discipline *sd)
                bzero(&xs_w, sizeof xs_w);
                xs_w.error = XS_NOERROR;
                xs_w.flags = SCSI_DATA_OUT;
-               xs_w.datalen = sz << DEV_BSHIFT;
+               xs_w.datalen = io_bytes;
                xs_w.data = buf;
                xs_w.cmdlen = sizeof(*cw);
                cw = (struct scsi_rw_16 *)&xs_w.cmd;
                cw->opcode = WRITE_16;
-               _lto4b(sz, cw->length);
+               _lto4b(io_bytes / sec_size , cr->length);
                _lto8b(lba, cw->addr);
                wu_w->swu_state = SR_WU_CONSTRUCT;
                wu_w->swu_flags |= SR_WUF_REBUILD | SR_WUF_WAKEUP;
@@ -4796,12 +4799,12 @@ sr_rebuild(struct sr_discipline *sd)
                sr_scsi_wu_put(sd, wu_r);
                sr_scsi_wu_put(sd, wu_w);
 
-               sd->sd_meta->ssd_rebuild = lba;
+               sd->sd_meta->ssd_rebuild = lba * (sec_size / DEV_BSIZE);
 
                /* XXX - this should be based on size, not percentage. */
                /* save metadata every percent */
                percent = sr_rebuild_percent(sd);
-               if (percent != old_percent && blk != whole_blk) {
+               if (percent != old_percent && lba != final_lba) {
                        if (sr_meta_save(sd, SR_META_DIRTY))
                                printf("%s: could not save metadata to %s\n",
                                    DEVNAME(sc), sd->sd_meta->ssd_devname);

Reply via email to