123397Smckusick /* 229116Smckusick * Copyright (c) 1982, 1986 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*34102Skarels * @(#)ufs_disksubr.c 7.6 (Berkeley) 04/24/88 723397Smckusick */ 816Sbill 930533Skarels #include "param.h" 1030533Skarels #include "systm.h" 1130533Skarels #include "buf.h" 1230533Skarels #include "disklabel.h" 1330533Skarels 1430740Skarels #include "dir.h" 1530740Skarels #include "user.h" 1630740Skarels 1716Sbill /* 182626Swnj * Seek sort for disks. We depend on the driver 192626Swnj * which calls us using b_resid as the current cylinder number. 202626Swnj * 212626Swnj * The argument dp structure holds a b_actf activity chain pointer 222626Swnj * on which we keep two queues, sorted in ascending cylinder order. 232626Swnj * The first queue holds those requests which are positioned after 242626Swnj * the current cylinder (in the first request); the second holds 252626Swnj * requests which came in after their cylinder number was passed. 262626Swnj * Thus we implement a one way scan, retracting after reaching the 272626Swnj * end of the drive to the first request on the second queue, 282626Swnj * at which time it becomes the first queue. 292626Swnj * 302626Swnj * A one-way scan is natural because of the way UNIX read-ahead 312626Swnj * blocks are allocated. 3216Sbill */ 3316Sbill 3416Sbill #define b_cylin b_resid 3516Sbill 3616Sbill disksort(dp, bp) 372626Swnj register struct buf *dp, *bp; 3816Sbill { 3916Sbill register struct buf *ap; 4016Sbill 412626Swnj /* 422626Swnj * If nothing on the activity queue, then 432626Swnj * we become the only thing. 442626Swnj */ 4516Sbill ap = dp->b_actf; 4616Sbill if(ap == NULL) { 4716Sbill dp->b_actf = bp; 4816Sbill dp->b_actl = bp; 4916Sbill bp->av_forw = NULL; 5016Sbill return; 5116Sbill } 522626Swnj /* 532626Swnj * If we lie after the first (currently active) 542626Swnj * request, then we must locate the second request list 552626Swnj * and add ourselves to it. 562626Swnj */ 572626Swnj if (bp->b_cylin < ap->b_cylin) { 582626Swnj while (ap->av_forw) { 592626Swnj /* 602626Swnj * Check for an ``inversion'' in the 612626Swnj * normally ascending cylinder numbers, 622626Swnj * indicating the start of the second request list. 632626Swnj */ 642626Swnj if (ap->av_forw->b_cylin < ap->b_cylin) { 652626Swnj /* 662626Swnj * Search the second request list 672626Swnj * for the first request at a larger 682626Swnj * cylinder number. We go before that; 692626Swnj * if there is no such request, we go at end. 702626Swnj */ 712626Swnj do { 722626Swnj if (bp->b_cylin < ap->av_forw->b_cylin) 732626Swnj goto insert; 7432573Skarels if (bp->b_cylin == ap->av_forw->b_cylin && 7532573Skarels bp->b_blkno < ap->av_forw->b_blkno) 7632573Skarels goto insert; 772626Swnj ap = ap->av_forw; 782626Swnj } while (ap->av_forw); 792626Swnj goto insert; /* after last */ 802626Swnj } 812626Swnj ap = ap->av_forw; 8216Sbill } 832626Swnj /* 842626Swnj * No inversions... we will go after the last, and 852626Swnj * be the first request in the second request list. 862626Swnj */ 872626Swnj goto insert; 8816Sbill } 892626Swnj /* 902626Swnj * Request is at/after the current request... 912626Swnj * sort in the first request list. 922626Swnj */ 932626Swnj while (ap->av_forw) { 942626Swnj /* 952626Swnj * We want to go after the current request 962626Swnj * if there is an inversion after it (i.e. it is 972626Swnj * the end of the first request list), or if 982626Swnj * the next request is a larger cylinder than our request. 992626Swnj */ 1002626Swnj if (ap->av_forw->b_cylin < ap->b_cylin || 10132573Skarels bp->b_cylin < ap->av_forw->b_cylin || 10232573Skarels (bp->b_cylin == ap->av_forw->b_cylin && 10332573Skarels bp->b_blkno < ap->av_forw->b_blkno)) 1042626Swnj goto insert; 1052626Swnj ap = ap->av_forw; 1062626Swnj } 1072626Swnj /* 1082626Swnj * Neither a second list nor a larger 1092626Swnj * request... we go at the end of the first list, 1102626Swnj * which is the same as the end of the whole schebang. 1112626Swnj */ 1122626Swnj insert: 1132626Swnj bp->av_forw = ap->av_forw; 1142626Swnj ap->av_forw = bp; 1152626Swnj if (ap == dp->b_actl) 11616Sbill dp->b_actl = bp; 11716Sbill } 11830533Skarels 11930533Skarels /* 12030740Skarels * Attempt to read a disk label from a device 12130740Skarels * using the indicated stategy routine. 12230740Skarels * The label must be partly set up before this: 12330740Skarels * secpercyl and anything required in the strategy routine 12430740Skarels * (e.g., sector size) must be filled in before calling us. 12530740Skarels * Returns null on success and an error string on failure. 12630740Skarels */ 12730740Skarels char * 12830740Skarels readdisklabel(dev, strat, lp) 12930740Skarels dev_t dev; 13030740Skarels int (*strat)(); 13130740Skarels register struct disklabel *lp; 13230740Skarels { 13330740Skarels register struct buf *bp; 13430740Skarels struct disklabel *dlp; 13530740Skarels char *msg = NULL; 13630740Skarels 13730740Skarels if (lp->d_secperunit == 0) 13830740Skarels lp->d_secperunit = 0x1fffffff; 13930740Skarels lp->d_npartitions = 1; 14030740Skarels if (lp->d_partitions[0].p_size == 0) 14130740Skarels lp->d_partitions[0].p_size = 0x1fffffff; 14230740Skarels lp->d_partitions[0].p_offset = 0; 14330740Skarels 144*34102Skarels bp = geteblk((int)lp->d_secsize); 14530740Skarels bp->b_dev = dev; 14630740Skarels bp->b_blkno = LABELSECTOR; 14732068Skarels bp->b_bcount = lp->d_secsize; 14830740Skarels bp->b_flags = B_BUSY | B_READ; 14930740Skarels bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 15030740Skarels (*strat)(bp); 15130740Skarels biowait(bp); 15230740Skarels if (bp->b_flags & B_ERROR) { 15330740Skarels u.u_error = 0; /* XXX */ 15430740Skarels msg = "I/O error"; 15532068Skarels } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 15632068Skarels dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 15732068Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 15832068Skarels if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 15932068Skarels if (msg == NULL) 16032068Skarels msg = "no disk label"; 16132068Skarels } else if (dkcksum(dlp) != 0) 16230740Skarels msg = "disk label corrupted"; 16332068Skarels else { 16430740Skarels *lp = *dlp; 16532068Skarels msg = NULL; 16632068Skarels break; 16732068Skarels } 16830740Skarels } 16932068Skarels if (lp->d_npartitions > MAXPARTITIONS) 17032068Skarels lp->d_npartitions = MAXPARTITIONS; 17130740Skarels bp->b_flags = B_INVAL | B_AGE; 17230740Skarels brelse(bp); 17330740Skarels return (msg); 17430740Skarels } 17530740Skarels 17630740Skarels /* 17732573Skarels * Check new disk label for sensibility 17832573Skarels * before setting it. 17932573Skarels */ 18032573Skarels setdisklabel(olp, nlp, openmask) 18132573Skarels register struct disklabel *olp, *nlp; 18232573Skarels u_long openmask; 18332573Skarels { 18432573Skarels register i; 18532573Skarels register struct partition *opp, *npp; 18632573Skarels 18732573Skarels if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 18832573Skarels dkcksum(nlp) != 0) 18932573Skarels return (EINVAL); 19032573Skarels while ((i = ffs(openmask)) != 0) { 19132573Skarels i--; 19232573Skarels openmask &= ~(1 << i); 19332573Skarels if (nlp->d_npartitions <= i) 19432573Skarels return (EBUSY); 19532573Skarels opp = &olp->d_partitions[i]; 19632573Skarels npp = &nlp->d_partitions[i]; 19732573Skarels if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 19832573Skarels return (EBUSY); 19932573Skarels /* 20032573Skarels * Copy internally-set partition information 20132573Skarels * if new label doesn't include it. XXX 20232573Skarels */ 20332573Skarels if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 20432573Skarels npp->p_fstype = opp->p_fstype; 20532573Skarels npp->p_fsize = opp->p_fsize; 20632573Skarels npp->p_frag = opp->p_frag; 20732573Skarels npp->p_cpg = opp->p_cpg; 20832573Skarels } 20932573Skarels } 210*34102Skarels nlp->d_checksum = 0; 211*34102Skarels nlp->d_checksum = dkcksum(nlp); 21232573Skarels *olp = *nlp; 21332573Skarels return (0); 21432573Skarels } 21532573Skarels 21632573Skarels /* encoding of disk minor numbers, should be elsewhere... */ 21732573Skarels #define dkunit(dev) (minor(dev) >> 3) 21832573Skarels #define dkpart(dev) (minor(dev) & 07) 21932573Skarels #define dkminor(unit, part) (((unit) << 3) | (part)) 22032573Skarels 22132573Skarels /* 22232573Skarels * Write disk label back to device after modification. 22332573Skarels */ 22432573Skarels writedisklabel(dev, strat, lp) 22532573Skarels dev_t dev; 22632573Skarels int (*strat)(); 22732573Skarels register struct disklabel *lp; 22832573Skarels { 22932573Skarels struct buf *bp; 23032573Skarels struct disklabel *dlp; 23132573Skarels int labelpart; 23232573Skarels int error = 0; 23332573Skarels 23432573Skarels labelpart = dkpart(dev); 23532573Skarels if (lp->d_partitions[labelpart].p_offset != 0) { 23632573Skarels if (lp->d_partitions[0].p_offset != 0) 23732573Skarels return (EXDEV); /* not quite right */ 23832573Skarels labelpart = 0; 23932573Skarels } 240*34102Skarels bp = geteblk((int)lp->d_secsize); 24132573Skarels bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart)); 24232573Skarels bp->b_blkno = LABELSECTOR; 24332573Skarels bp->b_bcount = lp->d_secsize; 24432573Skarels bp->b_flags = B_READ; 24532573Skarels (*strat)(bp); 24632573Skarels biowait(bp); 24732573Skarels if (bp->b_flags & B_ERROR) { 24832573Skarels error = u.u_error; /* XXX */ 24932573Skarels u.u_error = 0; 250*34102Skarels goto done; 25132573Skarels } 252*34102Skarels for (dlp = (struct disklabel *)bp->b_un.b_addr; 253*34102Skarels dlp <= (struct disklabel *) 254*34102Skarels (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp)); 255*34102Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 256*34102Skarels if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 257*34102Skarels dkcksum(dlp) == 0) { 258*34102Skarels *dlp = *lp; 259*34102Skarels bp->b_flags = B_WRITE; 260*34102Skarels (*strat)(bp); 261*34102Skarels biowait(bp); 262*34102Skarels if (bp->b_flags & B_ERROR) { 263*34102Skarels error = u.u_error; /* XXX */ 264*34102Skarels u.u_error = 0; 265*34102Skarels } 266*34102Skarels goto done; 267*34102Skarels } 26832573Skarels } 269*34102Skarels error = ESRCH; 270*34102Skarels done: 27132573Skarels brelse(bp); 27232573Skarels return (error); 27332573Skarels } 27432573Skarels 27532573Skarels /* 27630533Skarels * Compute checksum for disk label. 27730533Skarels */ 27830533Skarels dkcksum(lp) 27930533Skarels register struct disklabel *lp; 28030533Skarels { 28130533Skarels register u_short *start, *end; 28230533Skarels register u_short sum = 0; 28330533Skarels 28430533Skarels start = (u_short *)lp; 28530533Skarels end = (u_short *)&lp->d_partitions[lp->d_npartitions]; 28630533Skarels while (start < end) 28730533Skarels sum ^= *start++; 28830533Skarels return (sum); 28930533Skarels } 290