123397Smckusick /* 234535Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 323397Smckusick * All rights reserved. The Berkeley software License Agreement 423397Smckusick * specifies the terms and conditions for redistribution. 523397Smckusick * 6*34711Skarels * @(#)ufs_disksubr.c 7.10 (Berkeley) 06/11/88 723397Smckusick */ 816Sbill 930533Skarels #include "param.h" 1030533Skarels #include "systm.h" 1130533Skarels #include "buf.h" 1230533Skarels #include "disklabel.h" 1334535Skarels #include "syslog.h" 1430533Skarels 1530740Skarels #include "dir.h" 1630740Skarels #include "user.h" 1730740Skarels 1816Sbill /* 192626Swnj * Seek sort for disks. We depend on the driver 202626Swnj * which calls us using b_resid as the current cylinder number. 212626Swnj * 222626Swnj * The argument dp structure holds a b_actf activity chain pointer 232626Swnj * on which we keep two queues, sorted in ascending cylinder order. 242626Swnj * The first queue holds those requests which are positioned after 252626Swnj * the current cylinder (in the first request); the second holds 262626Swnj * requests which came in after their cylinder number was passed. 272626Swnj * Thus we implement a one way scan, retracting after reaching the 282626Swnj * end of the drive to the first request on the second queue, 292626Swnj * at which time it becomes the first queue. 302626Swnj * 312626Swnj * A one-way scan is natural because of the way UNIX read-ahead 322626Swnj * blocks are allocated. 3316Sbill */ 3416Sbill 3516Sbill #define b_cylin b_resid 3616Sbill 3716Sbill disksort(dp, bp) 382626Swnj register struct buf *dp, *bp; 3916Sbill { 4016Sbill register struct buf *ap; 4116Sbill 422626Swnj /* 432626Swnj * If nothing on the activity queue, then 442626Swnj * we become the only thing. 452626Swnj */ 4616Sbill ap = dp->b_actf; 4716Sbill if(ap == NULL) { 4816Sbill dp->b_actf = bp; 4916Sbill dp->b_actl = bp; 5016Sbill bp->av_forw = NULL; 5116Sbill return; 5216Sbill } 532626Swnj /* 542626Swnj * If we lie after the first (currently active) 552626Swnj * request, then we must locate the second request list 562626Swnj * and add ourselves to it. 572626Swnj */ 582626Swnj if (bp->b_cylin < ap->b_cylin) { 592626Swnj while (ap->av_forw) { 602626Swnj /* 612626Swnj * Check for an ``inversion'' in the 622626Swnj * normally ascending cylinder numbers, 632626Swnj * indicating the start of the second request list. 642626Swnj */ 652626Swnj if (ap->av_forw->b_cylin < ap->b_cylin) { 662626Swnj /* 672626Swnj * Search the second request list 682626Swnj * for the first request at a larger 692626Swnj * cylinder number. We go before that; 702626Swnj * if there is no such request, we go at end. 712626Swnj */ 722626Swnj do { 732626Swnj if (bp->b_cylin < ap->av_forw->b_cylin) 742626Swnj goto insert; 7532573Skarels if (bp->b_cylin == ap->av_forw->b_cylin && 7632573Skarels bp->b_blkno < ap->av_forw->b_blkno) 7732573Skarels goto insert; 782626Swnj ap = ap->av_forw; 792626Swnj } while (ap->av_forw); 802626Swnj goto insert; /* after last */ 812626Swnj } 822626Swnj ap = ap->av_forw; 8316Sbill } 842626Swnj /* 852626Swnj * No inversions... we will go after the last, and 862626Swnj * be the first request in the second request list. 872626Swnj */ 882626Swnj goto insert; 8916Sbill } 902626Swnj /* 912626Swnj * Request is at/after the current request... 922626Swnj * sort in the first request list. 932626Swnj */ 942626Swnj while (ap->av_forw) { 952626Swnj /* 962626Swnj * We want to go after the current request 972626Swnj * if there is an inversion after it (i.e. it is 982626Swnj * the end of the first request list), or if 992626Swnj * the next request is a larger cylinder than our request. 1002626Swnj */ 1012626Swnj if (ap->av_forw->b_cylin < ap->b_cylin || 10232573Skarels bp->b_cylin < ap->av_forw->b_cylin || 10332573Skarels (bp->b_cylin == ap->av_forw->b_cylin && 10432573Skarels bp->b_blkno < ap->av_forw->b_blkno)) 1052626Swnj goto insert; 1062626Swnj ap = ap->av_forw; 1072626Swnj } 1082626Swnj /* 1092626Swnj * Neither a second list nor a larger 1102626Swnj * request... we go at the end of the first list, 1112626Swnj * which is the same as the end of the whole schebang. 1122626Swnj */ 1132626Swnj insert: 1142626Swnj bp->av_forw = ap->av_forw; 1152626Swnj ap->av_forw = bp; 1162626Swnj if (ap == dp->b_actl) 11716Sbill dp->b_actl = bp; 11816Sbill } 11930533Skarels 12030533Skarels /* 12130740Skarels * Attempt to read a disk label from a device 12230740Skarels * using the indicated stategy routine. 12330740Skarels * The label must be partly set up before this: 12430740Skarels * secpercyl and anything required in the strategy routine 12530740Skarels * (e.g., sector size) must be filled in before calling us. 12630740Skarels * Returns null on success and an error string on failure. 12730740Skarels */ 12830740Skarels char * 12930740Skarels readdisklabel(dev, strat, lp) 13030740Skarels dev_t dev; 13130740Skarels int (*strat)(); 13230740Skarels register struct disklabel *lp; 13330740Skarels { 13430740Skarels register struct buf *bp; 13530740Skarels struct disklabel *dlp; 13630740Skarels char *msg = NULL; 13730740Skarels 13830740Skarels if (lp->d_secperunit == 0) 13930740Skarels lp->d_secperunit = 0x1fffffff; 14030740Skarels lp->d_npartitions = 1; 14130740Skarels if (lp->d_partitions[0].p_size == 0) 14230740Skarels lp->d_partitions[0].p_size = 0x1fffffff; 14330740Skarels lp->d_partitions[0].p_offset = 0; 14430740Skarels 14534102Skarels bp = geteblk((int)lp->d_secsize); 14630740Skarels bp->b_dev = dev; 14730740Skarels bp->b_blkno = LABELSECTOR; 14832068Skarels bp->b_bcount = lp->d_secsize; 14930740Skarels bp->b_flags = B_BUSY | B_READ; 15030740Skarels bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 15130740Skarels (*strat)(bp); 15230740Skarels biowait(bp); 15330740Skarels if (bp->b_flags & B_ERROR) { 15430740Skarels u.u_error = 0; /* XXX */ 15530740Skarels msg = "I/O error"; 15632068Skarels } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 15732068Skarels dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 15832068Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 15932068Skarels if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 16032068Skarels if (msg == NULL) 16132068Skarels msg = "no disk label"; 16232068Skarels } else if (dkcksum(dlp) != 0) 16330740Skarels msg = "disk label corrupted"; 16432068Skarels else { 16530740Skarels *lp = *dlp; 16632068Skarels msg = NULL; 16732068Skarels break; 16832068Skarels } 16930740Skarels } 17032068Skarels if (lp->d_npartitions > MAXPARTITIONS) 17132068Skarels lp->d_npartitions = MAXPARTITIONS; 17230740Skarels bp->b_flags = B_INVAL | B_AGE; 17330740Skarels brelse(bp); 17430740Skarels return (msg); 17530740Skarels } 17630740Skarels 17730740Skarels /* 17832573Skarels * Check new disk label for sensibility 17932573Skarels * before setting it. 18032573Skarels */ 18132573Skarels setdisklabel(olp, nlp, openmask) 18232573Skarels register struct disklabel *olp, *nlp; 18332573Skarels u_long openmask; 18432573Skarels { 18532573Skarels register i; 18632573Skarels register struct partition *opp, *npp; 18732573Skarels 18832573Skarels if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 18932573Skarels dkcksum(nlp) != 0) 19032573Skarels return (EINVAL); 19134473Smckusick while ((i = ffs((long)openmask)) != 0) { 19232573Skarels i--; 19332573Skarels openmask &= ~(1 << i); 19432573Skarels if (nlp->d_npartitions <= i) 19532573Skarels return (EBUSY); 19632573Skarels opp = &olp->d_partitions[i]; 19732573Skarels npp = &nlp->d_partitions[i]; 19832573Skarels if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 19932573Skarels return (EBUSY); 20032573Skarels /* 20132573Skarels * Copy internally-set partition information 20232573Skarels * if new label doesn't include it. XXX 20332573Skarels */ 20432573Skarels if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 20532573Skarels npp->p_fstype = opp->p_fstype; 20632573Skarels npp->p_fsize = opp->p_fsize; 20732573Skarels npp->p_frag = opp->p_frag; 20832573Skarels npp->p_cpg = opp->p_cpg; 20932573Skarels } 21032573Skarels } 21134102Skarels nlp->d_checksum = 0; 21234102Skarels nlp->d_checksum = dkcksum(nlp); 21332573Skarels *olp = *nlp; 21432573Skarels return (0); 21532573Skarels } 21632573Skarels 21732573Skarels /* encoding of disk minor numbers, should be elsewhere... */ 21832573Skarels #define dkunit(dev) (minor(dev) >> 3) 21932573Skarels #define dkpart(dev) (minor(dev) & 07) 22032573Skarels #define dkminor(unit, part) (((unit) << 3) | (part)) 22132573Skarels 22232573Skarels /* 22332573Skarels * Write disk label back to device after modification. 22432573Skarels */ 22532573Skarels writedisklabel(dev, strat, lp) 22632573Skarels dev_t dev; 22732573Skarels int (*strat)(); 22832573Skarels register struct disklabel *lp; 22932573Skarels { 23032573Skarels struct buf *bp; 23132573Skarels struct disklabel *dlp; 23232573Skarels int labelpart; 23332573Skarels int error = 0; 23432573Skarels 23532573Skarels labelpart = dkpart(dev); 23632573Skarels if (lp->d_partitions[labelpart].p_offset != 0) { 23732573Skarels if (lp->d_partitions[0].p_offset != 0) 23832573Skarels return (EXDEV); /* not quite right */ 23932573Skarels labelpart = 0; 24032573Skarels } 24134102Skarels bp = geteblk((int)lp->d_secsize); 24232573Skarels bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 24332573Skarels bp->b_blkno = LABELSECTOR; 24432573Skarels bp->b_bcount = lp->d_secsize; 24532573Skarels bp->b_flags = B_READ; 24632573Skarels (*strat)(bp); 24732573Skarels biowait(bp); 24832573Skarels if (bp->b_flags & B_ERROR) { 24932573Skarels error = u.u_error; /* XXX */ 25032573Skarels u.u_error = 0; 25134102Skarels goto done; 25232573Skarels } 25334102Skarels for (dlp = (struct disklabel *)bp->b_un.b_addr; 25434102Skarels dlp <= (struct disklabel *) 25534102Skarels (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 25634102Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 25734102Skarels if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 25834102Skarels dkcksum(dlp) == 0) { 25934102Skarels *dlp = *lp; 26034102Skarels bp->b_flags = B_WRITE; 26134102Skarels (*strat)(bp); 26234102Skarels biowait(bp); 26334102Skarels if (bp->b_flags & B_ERROR) { 26434102Skarels error = u.u_error; /* XXX */ 26534102Skarels u.u_error = 0; 26634102Skarels } 26734102Skarels goto done; 26834102Skarels } 26932573Skarels } 27034102Skarels error = ESRCH; 27134102Skarels done: 27232573Skarels brelse(bp); 27332573Skarels return (error); 27432573Skarels } 27532573Skarels 27632573Skarels /* 27730533Skarels * Compute checksum for disk label. 27830533Skarels */ 27930533Skarels dkcksum(lp) 28030533Skarels register struct disklabel *lp; 28130533Skarels { 28230533Skarels register u_short *start, *end; 28330533Skarels register u_short sum = 0; 28430533Skarels 28530533Skarels start = (u_short *)lp; 28630533Skarels end = (u_short *)&lp->d_partitions[lp->d_npartitions]; 28730533Skarels while (start < end) 28830533Skarels sum ^= *start++; 28930533Skarels return (sum); 29030533Skarels } 29134535Skarels 29234535Skarels /* 29334535Skarels * Disk error is the preface to plaintive error messages 29434535Skarels * about failing disk transfers. It prints messages of the form 29534637Skarels 29634637Skarels hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d) 29734637Skarels 29834535Skarels * if the offset of the error in the transfer and a disk label 29934535Skarels * are both available. blkdone should be -1 if the position of the error 30034535Skarels * is unknown; the disklabel pointer may be null from drivers that have not 30134535Skarels * been converted to use them. The message is printed with printf 30234535Skarels * if pri is LOG_PRINTF, otherwise it uses log at the specified priority. 30334535Skarels * The message should be completed (with at least a newline) with printf 30434535Skarels * or addlog, respectively. There is no trailing space. 30534535Skarels */ 30634535Skarels diskerr(bp, dname, what, pri, blkdone, lp) 30734535Skarels register struct buf *bp; 30834535Skarels char *dname, *what; 30934535Skarels int pri, blkdone; 31034535Skarels register struct disklabel *lp; 31134535Skarels { 31234535Skarels int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev); 31334535Skarels register int (*pr)(), sn; 31434535Skarels char partname = 'a' + part; 31534535Skarels extern printf(), addlog(); 31634535Skarels 31734535Skarels if (pri != LOG_PRINTF) { 31834535Skarels log(pri, ""); 31934535Skarels pr = addlog; 32034535Skarels } else 32134535Skarels pr = printf; 32234535Skarels (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what, 32334535Skarels bp->b_flags & B_READ ? "read" : "writ"); 32434535Skarels sn = bp->b_blkno; 32534535Skarels if (bp->b_bcount <= DEV_BSIZE) 32634535Skarels (*pr)("%d", sn); 32734535Skarels else { 32834535Skarels if (blkdone >= 0) { 32934535Skarels sn += blkdone; 33034535Skarels (*pr)("%d of ", sn); 33134535Skarels } 33234535Skarels (*pr)("%d-%d", bp->b_blkno, 33334535Skarels bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE); 33434535Skarels } 335*34711Skarels if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) { 336*34711Skarels #ifdef tahoe 337*34711Skarels sn *= DEV_BSIZE / lp->d_secsize; /* XXX */ 338*34711Skarels #endif 33934535Skarels sn += lp->d_partitions[part].p_offset; 34034637Skarels (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn, 34134637Skarels sn / lp->d_secpercyl); 34234637Skarels sn %= lp->d_secpercyl; 34334637Skarels (*pr)(" tn %d sn %d)", sn / lp->d_ntracks, sn % lp->d_ntracks); 34434535Skarels } 34534535Skarels } 346