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*32068Skarels * @(#)ufs_disksubr.c 7.4 (Berkeley) 08/23/87 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; 742626Swnj ap = ap->av_forw; 752626Swnj } while (ap->av_forw); 762626Swnj goto insert; /* after last */ 772626Swnj } 782626Swnj ap = ap->av_forw; 7916Sbill } 802626Swnj /* 812626Swnj * No inversions... we will go after the last, and 822626Swnj * be the first request in the second request list. 832626Swnj */ 842626Swnj goto insert; 8516Sbill } 862626Swnj /* 872626Swnj * Request is at/after the current request... 882626Swnj * sort in the first request list. 892626Swnj */ 902626Swnj while (ap->av_forw) { 912626Swnj /* 922626Swnj * We want to go after the current request 932626Swnj * if there is an inversion after it (i.e. it is 942626Swnj * the end of the first request list), or if 952626Swnj * the next request is a larger cylinder than our request. 962626Swnj */ 972626Swnj if (ap->av_forw->b_cylin < ap->b_cylin || 982626Swnj bp->b_cylin < ap->av_forw->b_cylin) 992626Swnj goto insert; 1002626Swnj ap = ap->av_forw; 1012626Swnj } 1022626Swnj /* 1032626Swnj * Neither a second list nor a larger 1042626Swnj * request... we go at the end of the first list, 1052626Swnj * which is the same as the end of the whole schebang. 1062626Swnj */ 1072626Swnj insert: 1082626Swnj bp->av_forw = ap->av_forw; 1092626Swnj ap->av_forw = bp; 1102626Swnj if (ap == dp->b_actl) 11116Sbill dp->b_actl = bp; 11216Sbill } 11330533Skarels 11430533Skarels /* 11530740Skarels * Attempt to read a disk label from a device 11630740Skarels * using the indicated stategy routine. 11730740Skarels * The label must be partly set up before this: 11830740Skarels * secpercyl and anything required in the strategy routine 11930740Skarels * (e.g., sector size) must be filled in before calling us. 12030740Skarels * Returns null on success and an error string on failure. 12130740Skarels */ 12230740Skarels char * 12330740Skarels readdisklabel(dev, strat, lp) 12430740Skarels dev_t dev; 12530740Skarels int (*strat)(); 12630740Skarels register struct disklabel *lp; 12730740Skarels { 12830740Skarels register struct buf *bp; 12930740Skarels struct disklabel *dlp; 13030740Skarels char *msg = NULL; 13130740Skarels 13230740Skarels if (lp->d_secperunit == 0) 13330740Skarels lp->d_secperunit = 0x1fffffff; 13430740Skarels lp->d_npartitions = 1; 13530740Skarels if (lp->d_partitions[0].p_size == 0) 13630740Skarels lp->d_partitions[0].p_size = 0x1fffffff; 13730740Skarels lp->d_partitions[0].p_offset = 0; 13830740Skarels 139*32068Skarels bp = geteblk(lp->d_secsize); 14030740Skarels bp->b_dev = dev; 14130740Skarels bp->b_blkno = LABELSECTOR; 142*32068Skarels bp->b_bcount = lp->d_secsize; 14330740Skarels bp->b_flags = B_BUSY | B_READ; 14430740Skarels bp->b_cylin = LABELSECTOR / lp->d_secpercyl; 14530740Skarels (*strat)(bp); 14630740Skarels biowait(bp); 14730740Skarels if (bp->b_flags & B_ERROR) { 14830740Skarels u.u_error = 0; /* XXX */ 14930740Skarels msg = "I/O error"; 150*32068Skarels } else for (dlp = (struct disklabel *)bp->b_un.b_addr; 151*32068Skarels dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp)); 152*32068Skarels dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 153*32068Skarels if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 154*32068Skarels if (msg == NULL) 155*32068Skarels msg = "no disk label"; 156*32068Skarels } else if (dkcksum(dlp) != 0) 15730740Skarels msg = "disk label corrupted"; 158*32068Skarels else { 15930740Skarels *lp = *dlp; 160*32068Skarels msg = NULL; 161*32068Skarels break; 162*32068Skarels } 16330740Skarels } 164*32068Skarels if (lp->d_npartitions > MAXPARTITIONS) 165*32068Skarels lp->d_npartitions = MAXPARTITIONS; 16630740Skarels bp->b_flags = B_INVAL | B_AGE; 16730740Skarels brelse(bp); 16830740Skarels return (msg); 16930740Skarels } 17030740Skarels 17130740Skarels /* 17230533Skarels * Compute checksum for disk label. 17330533Skarels */ 17430533Skarels dkcksum(lp) 17530533Skarels register struct disklabel *lp; 17630533Skarels { 17730533Skarels register u_short *start, *end; 17830533Skarels register u_short sum = 0; 17930533Skarels 18030533Skarels start = (u_short *)lp; 18130533Skarels end = (u_short *)&lp->d_partitions[lp->d_npartitions]; 18230533Skarels while (start < end) 18330533Skarels sum ^= *start++; 18430533Skarels return (sum); 18530533Skarels } 186