123397Smckusick /* 234535Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 337736Smckusick * All rights reserved. 423397Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*49110Skarels * @(#)ufs_disksubr.c 7.16 (Berkeley) 05/04/91 823397Smckusick */ 916Sbill 1030533Skarels #include "param.h" 1130533Skarels #include "systm.h" 1230533Skarels #include "buf.h" 1330533Skarels #include "disklabel.h" 1434535Skarels #include "syslog.h" 1530740Skarels 1616Sbill /* 172626Swnj * Seek sort for disks. We depend on the driver 182626Swnj * which calls us using b_resid as the current cylinder number. 192626Swnj * 202626Swnj * The argument dp structure holds a b_actf activity chain pointer 212626Swnj * on which we keep two queues, sorted in ascending cylinder order. 222626Swnj * The first queue holds those requests which are positioned after 232626Swnj * the current cylinder (in the first request); the second holds 242626Swnj * requests which came in after their cylinder number was passed. 252626Swnj * Thus we implement a one way scan, retracting after reaching the 262626Swnj * end of the drive to the first request on the second queue, 272626Swnj * at which time it becomes the first queue. 282626Swnj * 292626Swnj * A one-way scan is natural because of the way UNIX read-ahead 302626Swnj * blocks are allocated. 3116Sbill */ 3216Sbill 3316Sbill #define b_cylin b_resid 3416Sbill 3516Sbill disksort(dp, bp) 362626Swnj register struct buf *dp, *bp; 3716Sbill { 3816Sbill register struct buf *ap; 3916Sbill 402626Swnj /* 412626Swnj * If nothing on the activity queue, then 422626Swnj * we become the only thing. 432626Swnj */ 4416Sbill ap = dp->b_actf; 4516Sbill if(ap == NULL) { 4616Sbill dp->b_actf = bp; 4716Sbill dp->b_actl = bp; 4816Sbill bp->av_forw = NULL; 4916Sbill return; 5016Sbill } 512626Swnj /* 522626Swnj * If we lie after the first (currently active) 532626Swnj * request, then we must locate the second request list 542626Swnj * and add ourselves to it. 552626Swnj */ 562626Swnj if (bp->b_cylin < ap->b_cylin) { 572626Swnj while (ap->av_forw) { 582626Swnj /* 592626Swnj * Check for an ``inversion'' in the 602626Swnj * normally ascending cylinder numbers, 612626Swnj * indicating the start of the second request list. 622626Swnj */ 632626Swnj if (ap->av_forw->b_cylin < ap->b_cylin) { 642626Swnj /* 652626Swnj * Search the second request list 662626Swnj * for the first request at a larger 672626Swnj * cylinder number. We go before that; 682626Swnj * if there is no such request, we go at end. 692626Swnj */ 702626Swnj do { 712626Swnj if (bp->b_cylin < ap->av_forw->b_cylin) 722626Swnj goto insert; 7332573Skarels if (bp->b_cylin == ap->av_forw->b_cylin && 7432573Skarels bp->b_blkno < ap->av_forw->b_blkno) 7532573Skarels goto insert; 762626Swnj ap = ap->av_forw; 772626Swnj } while (ap->av_forw); 782626Swnj goto insert; /* after last */ 792626Swnj } 802626Swnj ap = ap->av_forw; 8116Sbill } 822626Swnj /* 832626Swnj * No inversions... we will go after the last, and 842626Swnj * be the first request in the second request list. 852626Swnj */ 862626Swnj goto insert; 8716Sbill } 882626Swnj /* 892626Swnj * Request is at/after the current request... 902626Swnj * sort in the first request list. 912626Swnj */ 922626Swnj while (ap->av_forw) { 932626Swnj /* 942626Swnj * We want to go after the current request 952626Swnj * if there is an inversion after it (i.e. it is 962626Swnj * the end of the first request list), or if 972626Swnj * the next request is a larger cylinder than our request. 982626Swnj */ 992626Swnj if (ap->av_forw->b_cylin < ap->b_cylin || 10032573Skarels bp->b_cylin < ap->av_forw->b_cylin || 10132573Skarels (bp->b_cylin == ap->av_forw->b_cylin && 10232573Skarels bp->b_blkno < ap->av_forw->b_blkno)) 1032626Swnj goto insert; 1042626Swnj ap = ap->av_forw; 1052626Swnj } 1062626Swnj /* 1072626Swnj * Neither a second list nor a larger 1082626Swnj * request... we go at the end of the first list, 1092626Swnj * which is the same as the end of the whole schebang. 1102626Swnj */ 1112626Swnj insert: 1122626Swnj bp->av_forw = ap->av_forw; 1132626Swnj ap->av_forw = bp; 1142626Swnj if (ap == dp->b_actl) 11516Sbill dp->b_actl = bp; 11616Sbill } 11730533Skarels 11830533Skarels /* 11930740Skarels * Attempt to read a disk label from a device 12030740Skarels * using the indicated stategy routine. 12130740Skarels * The label must be partly set up before this: 12230740Skarels * secpercyl and anything required in the strategy routine 12330740Skarels * (e.g., sector size) must be filled in before calling us. 12430740Skarels * Returns null on success and an error string on failure. 12530740Skarels */ 12630740Skarels char * 12730740Skarels readdisklabel(dev, strat, lp) 12830740Skarels dev_t dev; 12930740Skarels int (*strat)(); 13030740Skarels register struct disklabel *lp; 13130740Skarels { 13230740Skarels register struct buf *bp; 13330740Skarels struct disklabel *dlp; 13430740Skarels char *msg = NULL; 13530740Skarels 13630740Skarels if (lp->d_secperunit == 0) 13730740Skarels lp->d_secperunit = 0x1fffffff; 13830740Skarels lp->d_npartitions = 1; 13930740Skarels if (lp->d_partitions[0].p_size == 0) 14030740Skarels lp->d_partitions[0].p_size = 0x1fffffff; 14130740Skarels lp->d_partitions[0].p_offset = 0; 14230740Skarels 14334102Skarels bp = geteblk((int)lp->d_secsize); 14430740Skarels bp->b_dev = dev; 14530740Skarels bp->b_blkno = LABELSECTOR; 14632068Skarels bp->b_bcount = lp->d_secsize; 14730740Skarels bp->b_flags = B_BUSY | B_READ; 14830740Skarels bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 14930740Skarels (*strat)(bp); 15037736Smckusick if (biowait(bp)) { 15130740Skarels msg = "I/O error"; 15232068Skarels } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 15332068Skarels dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 15432068Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 15532068Skarels if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 15632068Skarels if (msg == NULL) 15732068Skarels msg = "no disk label"; 15839836Smckusick } else if (dlp->d_npartitions > MAXPARTITIONS || 15939836Smckusick dkcksum(dlp) != 0) 16030740Skarels msg = "disk label corrupted"; 16132068Skarels else { 16230740Skarels *lp = *dlp; 16332068Skarels msg = NULL; 16432068Skarels break; 16532068Skarels } 16630740Skarels } 16730740Skarels bp->b_flags = B_INVAL | B_AGE; 16830740Skarels brelse(bp); 16930740Skarels return (msg); 17030740Skarels } 17130740Skarels 17230740Skarels /* 17332573Skarels * Check new disk label for sensibility 17432573Skarels * before setting it. 17532573Skarels */ 17632573Skarels setdisklabel(olp, nlp, openmask) 17732573Skarels register struct disklabel *olp, *nlp; 17832573Skarels u_long openmask; 17932573Skarels { 18032573Skarels register i; 18132573Skarels register struct partition *opp, *npp; 18232573Skarels 18332573Skarels if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 18432573Skarels dkcksum(nlp) != 0) 18532573Skarels return (EINVAL); 18634473Smckusick while ((i = ffs((long)openmask)) != 0) { 18732573Skarels i--; 18832573Skarels openmask &= ~(1 << i); 18932573Skarels if (nlp->d_npartitions <= i) 19032573Skarels return (EBUSY); 19132573Skarels opp = &olp->d_partitions[i]; 19232573Skarels npp = &nlp->d_partitions[i]; 19332573Skarels if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 19432573Skarels return (EBUSY); 19532573Skarels /* 19632573Skarels * Copy internally-set partition information 19732573Skarels * if new label doesn't include it. XXX 19832573Skarels */ 19932573Skarels if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 20032573Skarels npp->p_fstype = opp->p_fstype; 20132573Skarels npp->p_fsize = opp->p_fsize; 20232573Skarels npp->p_frag = opp->p_frag; 20332573Skarels npp->p_cpg = opp->p_cpg; 20432573Skarels } 20532573Skarels } 20634102Skarels nlp->d_checksum = 0; 20734102Skarels nlp->d_checksum = dkcksum(nlp); 20832573Skarels *olp = *nlp; 20932573Skarels return (0); 21032573Skarels } 21132573Skarels 21232573Skarels /* encoding of disk minor numbers, should be elsewhere... */ 21332573Skarels #define dkunit(dev) (minor(dev) >> 3) 21432573Skarels #define dkpart(dev) (minor(dev) & 07) 21532573Skarels #define dkminor(unit, part) (((unit) << 3) | (part)) 21632573Skarels 21732573Skarels /* 21832573Skarels * Write disk label back to device after modification. 21932573Skarels */ 22032573Skarels writedisklabel(dev, strat, lp) 22132573Skarels dev_t dev; 22232573Skarels int (*strat)(); 22332573Skarels register struct disklabel *lp; 22432573Skarels { 22532573Skarels struct buf *bp; 22632573Skarels struct disklabel *dlp; 22732573Skarels int labelpart; 22832573Skarels int error = 0; 22932573Skarels 23032573Skarels labelpart = dkpart(dev); 23132573Skarels if (lp->d_partitions[labelpart].p_offset != 0) { 23232573Skarels if (lp->d_partitions[0].p_offset != 0) 23332573Skarels return (EXDEV); /* not quite right */ 23432573Skarels labelpart = 0; 23532573Skarels } 23634102Skarels bp = geteblk((int)lp->d_secsize); 23732573Skarels bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 23832573Skarels bp->b_blkno = LABELSECTOR; 23932573Skarels bp->b_bcount = lp->d_secsize; 24032573Skarels bp->b_flags = B_READ; 24132573Skarels (*strat)(bp); 24237736Smckusick if (error = biowait(bp)) 24334102Skarels goto done; 24434102Skarels for (dlp = (struct disklabel *)bp->b_un.b_addr; 24534102Skarels dlp <= (struct disklabel *) 24634102Skarels (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 24734102Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 24834102Skarels if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 24934102Skarels dkcksum(dlp) == 0) { 25034102Skarels *dlp = *lp; 25134102Skarels bp->b_flags = B_WRITE; 25234102Skarels (*strat)(bp); 25337736Smckusick error = biowait(bp); 25434102Skarels goto done; 25534102Skarels } 25632573Skarels } 25734102Skarels error = ESRCH; 25834102Skarels done: 25932573Skarels brelse(bp); 26032573Skarels return (error); 26132573Skarels } 26232573Skarels 26332573Skarels /* 26430533Skarels * Compute checksum for disk label. 26530533Skarels */ 26630533Skarels dkcksum(lp) 26730533Skarels register struct disklabel *lp; 26830533Skarels { 26930533Skarels register u_short *start, *end; 27030533Skarels register u_short sum = 0; 27130533Skarels 27230533Skarels start = (u_short *)lp; 27330533Skarels end = (u_short *)&lp->d_partitions[lp->d_npartitions]; 27430533Skarels while (start < end) 27530533Skarels sum ^= *start++; 27630533Skarels return (sum); 27730533Skarels } 27834535Skarels 27934535Skarels /* 28034535Skarels * Disk error is the preface to plaintive error messages 28134535Skarels * about failing disk transfers. It prints messages of the form 28234637Skarels 28334637Skarels hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d) 28434637Skarels 28534535Skarels * if the offset of the error in the transfer and a disk label 28634535Skarels * are both available. blkdone should be -1 if the position of the error 28734535Skarels * is unknown; the disklabel pointer may be null from drivers that have not 28834535Skarels * been converted to use them. The message is printed with printf 28934535Skarels * if pri is LOG_PRINTF, otherwise it uses log at the specified priority. 29034535Skarels * The message should be completed (with at least a newline) with printf 29134535Skarels * or addlog, respectively. There is no trailing space. 29234535Skarels */ 29334535Skarels diskerr(bp, dname, what, pri, blkdone, lp) 29434535Skarels register struct buf *bp; 29534535Skarels char *dname, *what; 29634535Skarels int pri, blkdone; 29734535Skarels register struct disklabel *lp; 29834535Skarels { 29934535Skarels int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev); 300*49110Skarels register void (*pr) __P((const char *, ...)); 30134535Skarels char partname = 'a' + part; 302*49110Skarels int sn; 30334535Skarels 30434535Skarels if (pri != LOG_PRINTF) { 30534535Skarels log(pri, ""); 30634535Skarels pr = addlog; 30734535Skarels } else 30834535Skarels pr = printf; 30934535Skarels (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what, 31034535Skarels bp->b_flags & B_READ ? "read" : "writ"); 31134535Skarels sn = bp->b_blkno; 31234535Skarels if (bp->b_bcount <= DEV_BSIZE) 31334535Skarels (*pr)("%d", sn); 31434535Skarels else { 31534535Skarels if (blkdone >= 0) { 31634535Skarels sn += blkdone; 31734535Skarels (*pr)("%d of ", sn); 31834535Skarels } 31934535Skarels (*pr)("%d-%d", bp->b_blkno, 32034535Skarels bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE); 32134535Skarels } 32234711Skarels if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) { 32334711Skarels #ifdef tahoe 32434711Skarels sn *= DEV_BSIZE / lp->d_secsize; /* XXX */ 32534711Skarels #endif 32634535Skarels sn += lp->d_partitions[part].p_offset; 32734637Skarels (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn, 32834637Skarels sn / lp->d_secpercyl); 32934637Skarels sn %= lp->d_secpercyl; 33035703Stef (*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors); 33134535Skarels } 33234535Skarels } 333