xref: /csrg-svn/sys/ufs/ffs/ufs_disksubr.c (revision 44537)
123397Smckusick /*
234535Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
337736Smckusick  * All rights reserved.
423397Smckusick  *
5*44537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*44537Sbostic  *	@(#)ufs_disksubr.c	7.14 (Berkeley) 06/28/90
823397Smckusick  */
916Sbill 
1030533Skarels #include "param.h"
1130533Skarels #include "systm.h"
1230533Skarels #include "buf.h"
1330533Skarels #include "disklabel.h"
1434535Skarels #include "syslog.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 
14434102Skarels 	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);
15137736Smckusick 	if (biowait(bp)) {
15230740Skarels 		msg = "I/O error";
15332068Skarels 	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
15432068Skarels 	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
15532068Skarels 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
15632068Skarels 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
15732068Skarels 			if (msg == NULL)
15832068Skarels 				msg = "no disk label";
15939836Smckusick 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
16039836Smckusick 			   dkcksum(dlp) != 0)
16130740Skarels 			msg = "disk label corrupted";
16232068Skarels 		else {
16330740Skarels 			*lp = *dlp;
16432068Skarels 			msg = NULL;
16532068Skarels 			break;
16632068Skarels 		}
16730740Skarels 	}
16830740Skarels 	bp->b_flags = B_INVAL | B_AGE;
16930740Skarels 	brelse(bp);
17030740Skarels 	return (msg);
17130740Skarels }
17230740Skarels 
17330740Skarels /*
17432573Skarels  * Check new disk label for sensibility
17532573Skarels  * before setting it.
17632573Skarels  */
17732573Skarels setdisklabel(olp, nlp, openmask)
17832573Skarels 	register struct disklabel *olp, *nlp;
17932573Skarels 	u_long openmask;
18032573Skarels {
18132573Skarels 	register i;
18232573Skarels 	register struct partition *opp, *npp;
18332573Skarels 
18432573Skarels 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
18532573Skarels 	    dkcksum(nlp) != 0)
18632573Skarels 		return (EINVAL);
18734473Smckusick 	while ((i = ffs((long)openmask)) != 0) {
18832573Skarels 		i--;
18932573Skarels 		openmask &= ~(1 << i);
19032573Skarels 		if (nlp->d_npartitions <= i)
19132573Skarels 			return (EBUSY);
19232573Skarels 		opp = &olp->d_partitions[i];
19332573Skarels 		npp = &nlp->d_partitions[i];
19432573Skarels 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
19532573Skarels 			return (EBUSY);
19632573Skarels 		/*
19732573Skarels 		 * Copy internally-set partition information
19832573Skarels 		 * if new label doesn't include it.		XXX
19932573Skarels 		 */
20032573Skarels 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
20132573Skarels 			npp->p_fstype = opp->p_fstype;
20232573Skarels 			npp->p_fsize = opp->p_fsize;
20332573Skarels 			npp->p_frag = opp->p_frag;
20432573Skarels 			npp->p_cpg = opp->p_cpg;
20532573Skarels 		}
20632573Skarels 	}
20734102Skarels  	nlp->d_checksum = 0;
20834102Skarels  	nlp->d_checksum = dkcksum(nlp);
20932573Skarels 	*olp = *nlp;
21032573Skarels 	return (0);
21132573Skarels }
21232573Skarels 
21332573Skarels /* encoding of disk minor numbers, should be elsewhere... */
21432573Skarels #define dkunit(dev)		(minor(dev) >> 3)
21532573Skarels #define dkpart(dev)		(minor(dev) & 07)
21632573Skarels #define dkminor(unit, part)	(((unit) << 3) | (part))
21732573Skarels 
21832573Skarels /*
21932573Skarels  * Write disk label back to device after modification.
22032573Skarels  */
22132573Skarels writedisklabel(dev, strat, lp)
22232573Skarels 	dev_t dev;
22332573Skarels 	int (*strat)();
22432573Skarels 	register struct disklabel *lp;
22532573Skarels {
22632573Skarels 	struct buf *bp;
22732573Skarels 	struct disklabel *dlp;
22832573Skarels 	int labelpart;
22932573Skarels 	int error = 0;
23032573Skarels 
23132573Skarels 	labelpart = dkpart(dev);
23232573Skarels 	if (lp->d_partitions[labelpart].p_offset != 0) {
23332573Skarels 		if (lp->d_partitions[0].p_offset != 0)
23432573Skarels 			return (EXDEV);			/* not quite right */
23532573Skarels 		labelpart = 0;
23632573Skarels 	}
23734102Skarels 	bp = geteblk((int)lp->d_secsize);
23832573Skarels 	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
23932573Skarels 	bp->b_blkno = LABELSECTOR;
24032573Skarels 	bp->b_bcount = lp->d_secsize;
24132573Skarels 	bp->b_flags = B_READ;
24232573Skarels 	(*strat)(bp);
24337736Smckusick 	if (error = biowait(bp))
24434102Skarels 		goto done;
24534102Skarels 	for (dlp = (struct disklabel *)bp->b_un.b_addr;
24634102Skarels 	    dlp <= (struct disklabel *)
24734102Skarels 	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
24834102Skarels 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
24934102Skarels 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
25034102Skarels 		    dkcksum(dlp) == 0) {
25134102Skarels 			*dlp = *lp;
25234102Skarels 			bp->b_flags = B_WRITE;
25334102Skarels 			(*strat)(bp);
25437736Smckusick 			error = biowait(bp);
25534102Skarels 			goto done;
25634102Skarels 		}
25732573Skarels 	}
25834102Skarels 	error = ESRCH;
25934102Skarels done:
26032573Skarels 	brelse(bp);
26132573Skarels 	return (error);
26232573Skarels }
26332573Skarels 
26432573Skarels /*
26530533Skarels  * Compute checksum for disk label.
26630533Skarels  */
26730533Skarels dkcksum(lp)
26830533Skarels 	register struct disklabel *lp;
26930533Skarels {
27030533Skarels 	register u_short *start, *end;
27130533Skarels 	register u_short sum = 0;
27230533Skarels 
27330533Skarels 	start = (u_short *)lp;
27430533Skarels 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
27530533Skarels 	while (start < end)
27630533Skarels 		sum ^= *start++;
27730533Skarels 	return (sum);
27830533Skarels }
27934535Skarels 
28034535Skarels /*
28134535Skarels  * Disk error is the preface to plaintive error messages
28234535Skarels  * about failing disk transfers.  It prints messages of the form
28334637Skarels 
28434637Skarels hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
28534637Skarels 
28634535Skarels  * if the offset of the error in the transfer and a disk label
28734535Skarels  * are both available.  blkdone should be -1 if the position of the error
28834535Skarels  * is unknown; the disklabel pointer may be null from drivers that have not
28934535Skarels  * been converted to use them.  The message is printed with printf
29034535Skarels  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
29134535Skarels  * The message should be completed (with at least a newline) with printf
29234535Skarels  * or addlog, respectively.  There is no trailing space.
29334535Skarels  */
29434535Skarels diskerr(bp, dname, what, pri, blkdone, lp)
29534535Skarels 	register struct buf *bp;
29634535Skarels 	char *dname, *what;
29734535Skarels 	int pri, blkdone;
29834535Skarels 	register struct disklabel *lp;
29934535Skarels {
30034535Skarels 	int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
30134535Skarels 	register int (*pr)(), sn;
30234535Skarels 	char partname = 'a' + part;
30334535Skarels 	extern printf(), addlog();
30434535Skarels 
30534535Skarels 	if (pri != LOG_PRINTF) {
30634535Skarels 		log(pri, "");
30734535Skarels 		pr = addlog;
30834535Skarels 	} else
30934535Skarels 		pr = printf;
31034535Skarels 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
31134535Skarels 	    bp->b_flags & B_READ ? "read" : "writ");
31234535Skarels 	sn = bp->b_blkno;
31334535Skarels 	if (bp->b_bcount <= DEV_BSIZE)
31434535Skarels 		(*pr)("%d", sn);
31534535Skarels 	else {
31634535Skarels 		if (blkdone >= 0) {
31734535Skarels 			sn += blkdone;
31834535Skarels 			(*pr)("%d of ", sn);
31934535Skarels 		}
32034535Skarels 		(*pr)("%d-%d", bp->b_blkno,
32134535Skarels 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
32234535Skarels 	}
32334711Skarels 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
32434711Skarels #ifdef tahoe
32534711Skarels 		sn *= DEV_BSIZE / lp->d_secsize;		/* XXX */
32634711Skarels #endif
32734535Skarels 		sn += lp->d_partitions[part].p_offset;
32834637Skarels 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
32934637Skarels 		    sn / lp->d_secpercyl);
33034637Skarels 		sn %= lp->d_secpercyl;
33135703Stef 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
33234535Skarels 	}
33334535Skarels }
334