xref: /csrg-svn/sys/ufs/ffs/ufs_disksubr.c (revision 65774)
123397Smckusick /*
263376Sbostic  * Copyright (c) 1982, 1986, 1988, 1993
363376Sbostic  *	The Regents of the University of California.  All rights reserved.
4*65774Sbostic  * (c) UNIX System Laboratories, Inc.
5*65774Sbostic  * All or some portions of this file are derived from material licensed
6*65774Sbostic  * to the University of California by American Telephone and Telegraph
7*65774Sbostic  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8*65774Sbostic  * the permission of UNIX System Laboratories, Inc.
923397Smckusick  *
1044537Sbostic  * %sccs.include.redist.c%
1137736Smckusick  *
12*65774Sbostic  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 01/21/94
1323397Smckusick  */
1416Sbill 
1551508Sbostic #include <sys/param.h>
1651508Sbostic #include <sys/systm.h>
1751508Sbostic #include <sys/buf.h>
1851508Sbostic #include <sys/disklabel.h>
1951508Sbostic #include <sys/syslog.h>
2030740Skarels 
2116Sbill /*
2251508Sbostic  * Seek sort for disks.  We depend on the driver which calls us using b_resid
2351508Sbostic  * as the current cylinder number.
242626Swnj  *
2564597Sbostic  * The argument ap structure holds a b_actf activity chain pointer on which we
2651508Sbostic  * keep two queues, sorted in ascending cylinder order.  The first queue holds
2751508Sbostic  * those requests which are positioned after the current cylinder (in the first
2851508Sbostic  * request); the second holds requests which came in after their cylinder number
2951508Sbostic  * was passed.  Thus we implement a one way scan, retracting after reaching the
3051508Sbostic  * end of the drive to the first request on the second queue, at which time it
3151508Sbostic  * becomes the first queue.
322626Swnj  *
3351508Sbostic  * A one-way scan is natural because of the way UNIX read-ahead blocks are
3451508Sbostic  * allocated.
3516Sbill  */
3616Sbill 
3764426Sbostic /*
3864426Sbostic  * For portability with historic industry practice, the
3964426Sbostic  * cylinder number has to be maintained in the `b_resid'
4064426Sbostic  * field.
4164426Sbostic  */
4264426Sbostic #define	b_cylinder	b_resid
4316Sbill 
4451508Sbostic void
disksort(ap,bp)4564597Sbostic disksort(ap, bp)
4664597Sbostic 	register struct buf *ap, *bp;
4716Sbill {
4864426Sbostic 	register struct buf *bq;
4916Sbill 
5064426Sbostic 	/* If the queue is empty, then it's easy. */
5164597Sbostic 	if (ap->b_actf == NULL) {
5264426Sbostic 		bp->b_actf = NULL;
5364597Sbostic 		ap->b_actf = bp;
5416Sbill 		return;
5516Sbill 	}
5664426Sbostic 
572626Swnj 	/*
5864426Sbostic 	 * If we lie after the first (currently active) request, then we
5964426Sbostic 	 * must locate the second request list and add ourselves to it.
602626Swnj 	 */
6164597Sbostic 	bq = ap->b_actf;
6264426Sbostic 	if (bp->b_cylinder < bq->b_cylinder) {
6364426Sbostic 		while (bq->b_actf) {
642626Swnj 			/*
6564426Sbostic 			 * Check for an ``inversion'' in the normally ascending
6664426Sbostic 			 * cylinder numbers, indicating the start of the second
6764426Sbostic 			 * request list.
682626Swnj 			 */
6964426Sbostic 			if (bq->b_actf->b_cylinder < bq->b_cylinder) {
702626Swnj 				/*
7164426Sbostic 				 * Search the second request list for the first
7264426Sbostic 				 * request at a larger cylinder number.  We go
7364426Sbostic 				 * before that; if there is no such request, we
7464426Sbostic 				 * go at end.
752626Swnj 				 */
762626Swnj 				do {
7764426Sbostic 					if (bp->b_cylinder <
7864426Sbostic 					    bq->b_actf->b_cylinder)
792626Swnj 						goto insert;
8064426Sbostic 					if (bp->b_cylinder ==
8164426Sbostic 					    bq->b_actf->b_cylinder &&
8264426Sbostic 					    bp->b_blkno < bq->b_actf->b_blkno)
8332573Skarels 						goto insert;
8464426Sbostic 					bq = bq->b_actf;
8564426Sbostic 				} while (bq->b_actf);
862626Swnj 				goto insert;		/* after last */
872626Swnj 			}
8864426Sbostic 			bq = bq->b_actf;
8916Sbill 		}
902626Swnj 		/*
912626Swnj 		 * No inversions... we will go after the last, and
922626Swnj 		 * be the first request in the second request list.
932626Swnj 		 */
942626Swnj 		goto insert;
9516Sbill 	}
962626Swnj 	/*
972626Swnj 	 * Request is at/after the current request...
982626Swnj 	 * sort in the first request list.
992626Swnj 	 */
10064426Sbostic 	while (bq->b_actf) {
1012626Swnj 		/*
10264426Sbostic 		 * We want to go after the current request if there is an
10364426Sbostic 		 * inversion after it (i.e. it is the end of the first
10464426Sbostic 		 * request list), or if the next request is a larger cylinder
10564426Sbostic 		 * than our request.
1062626Swnj 		 */
10764426Sbostic 		if (bq->b_actf->b_cylinder < bq->b_cylinder ||
10864426Sbostic 		    bp->b_cylinder < bq->b_actf->b_cylinder ||
10964426Sbostic 		    (bp->b_cylinder == bq->b_actf->b_cylinder &&
11064426Sbostic 		    bp->b_blkno < bq->b_actf->b_blkno))
1112626Swnj 			goto insert;
11264426Sbostic 		bq = bq->b_actf;
1132626Swnj 	}
1142626Swnj 	/*
11564426Sbostic 	 * Neither a second list nor a larger request... we go at the end of
11664426Sbostic 	 * the first list, which is the same as the end of the whole schebang.
1172626Swnj 	 */
11864426Sbostic insert:	bp->b_actf = bq->b_actf;
11964426Sbostic 	bq->b_actf = bp;
12016Sbill }
12130533Skarels 
12230533Skarels /*
12351508Sbostic  * Attempt to read a disk label from a device using the indicated stategy
12451508Sbostic  * routine.  The label must be partly set up before this: secpercyl and
12551508Sbostic  * anything required in the strategy routine (e.g., sector size) must be
12651508Sbostic  * filled in before calling us.  Returns NULL on success and an error
12751508Sbostic  * string on failure.
12830740Skarels  */
12930740Skarels char *
readdisklabel(dev,strat,lp)13030740Skarels readdisklabel(dev, strat, lp)
13130740Skarels 	dev_t dev;
13230740Skarels 	int (*strat)();
13330740Skarels 	register struct disklabel *lp;
13430740Skarels {
13530740Skarels 	register struct buf *bp;
13630740Skarels 	struct disklabel *dlp;
13730740Skarels 	char *msg = NULL;
13830740Skarels 
13930740Skarels 	if (lp->d_secperunit == 0)
14030740Skarels 		lp->d_secperunit = 0x1fffffff;
14130740Skarels 	lp->d_npartitions = 1;
14230740Skarels 	if (lp->d_partitions[0].p_size == 0)
14330740Skarels 		lp->d_partitions[0].p_size = 0x1fffffff;
14430740Skarels 	lp->d_partitions[0].p_offset = 0;
14530740Skarels 
14634102Skarels 	bp = geteblk((int)lp->d_secsize);
14730740Skarels 	bp->b_dev = dev;
14830740Skarels 	bp->b_blkno = LABELSECTOR;
14932068Skarels 	bp->b_bcount = lp->d_secsize;
15030740Skarels 	bp->b_flags = B_BUSY | B_READ;
15164426Sbostic 	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
15230740Skarels 	(*strat)(bp);
15364426Sbostic 	if (biowait(bp))
15430740Skarels 		msg = "I/O error";
15564515Sbostic 	else for (dlp = (struct disklabel *)bp->b_data;
15664515Sbostic 	    dlp <= (struct disklabel *)((char *)bp->b_data +
15764515Sbostic 	    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";
16239836Smckusick 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
16339836Smckusick 			   dkcksum(dlp) != 0)
16430740Skarels 			msg = "disk label corrupted";
16532068Skarels 		else {
16630740Skarels 			*lp = *dlp;
16732068Skarels 			msg = NULL;
16832068Skarels 			break;
16932068Skarels 		}
17030740Skarels 	}
17130740Skarels 	bp->b_flags = B_INVAL | B_AGE;
17230740Skarels 	brelse(bp);
17330740Skarels 	return (msg);
17430740Skarels }
17530740Skarels 
17630740Skarels /*
17751508Sbostic  * Check new disk label for sensibility before setting it.
17832573Skarels  */
17951508Sbostic int
setdisklabel(olp,nlp,openmask)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);
19034473Smckusick 	while ((i = ffs((long)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 	}
21034102Skarels  	nlp->d_checksum = 0;
21134102Skarels  	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  */
22451508Sbostic int
writedisklabel(dev,strat,lp)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);
24737736Smckusick 	if (error = biowait(bp))
24834102Skarels 		goto done;
24964515Sbostic 	for (dlp = (struct disklabel *)bp->b_data;
25034102Skarels 	    dlp <= (struct disklabel *)
25164515Sbostic 	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
25234102Skarels 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
25334102Skarels 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
25434102Skarels 		    dkcksum(dlp) == 0) {
25534102Skarels 			*dlp = *lp;
25634102Skarels 			bp->b_flags = B_WRITE;
25734102Skarels 			(*strat)(bp);
25837736Smckusick 			error = biowait(bp);
25934102Skarels 			goto done;
26034102Skarels 		}
26132573Skarels 	}
26234102Skarels 	error = ESRCH;
26334102Skarels done:
26432573Skarels 	brelse(bp);
26532573Skarels 	return (error);
26632573Skarels }
26732573Skarels 
26832573Skarels /*
26930533Skarels  * Compute checksum for disk label.
27030533Skarels  */
dkcksum(lp)27130533Skarels dkcksum(lp)
27230533Skarels 	register struct disklabel *lp;
27330533Skarels {
27430533Skarels 	register u_short *start, *end;
27530533Skarels 	register u_short sum = 0;
27630533Skarels 
27730533Skarels 	start = (u_short *)lp;
27830533Skarels 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
27930533Skarels 	while (start < end)
28030533Skarels 		sum ^= *start++;
28130533Skarels 	return (sum);
28230533Skarels }
28334535Skarels 
28434535Skarels /*
28534535Skarels  * Disk error is the preface to plaintive error messages
28634535Skarels  * about failing disk transfers.  It prints messages of the form
28734637Skarels 
28834637Skarels hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
28934637Skarels 
29034535Skarels  * if the offset of the error in the transfer and a disk label
29134535Skarels  * are both available.  blkdone should be -1 if the position of the error
29234535Skarels  * is unknown; the disklabel pointer may be null from drivers that have not
29334535Skarels  * been converted to use them.  The message is printed with printf
29434535Skarels  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
29534535Skarels  * The message should be completed (with at least a newline) with printf
29634535Skarels  * or addlog, respectively.  There is no trailing space.
29734535Skarels  */
29851508Sbostic void
diskerr(bp,dname,what,pri,blkdone,lp)29934535Skarels diskerr(bp, dname, what, pri, blkdone, lp)
30034535Skarels 	register struct buf *bp;
30134535Skarels 	char *dname, *what;
30234535Skarels 	int pri, blkdone;
30334535Skarels 	register struct disklabel *lp;
30434535Skarels {
30534535Skarels 	int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
30649110Skarels 	register void (*pr) __P((const char *, ...));
30734535Skarels 	char partname = 'a' + part;
30849110Skarels 	int sn;
30934535Skarels 
31034535Skarels 	if (pri != LOG_PRINTF) {
31134535Skarels 		log(pri, "");
31234535Skarels 		pr = addlog;
31334535Skarels 	} else
31434535Skarels 		pr = printf;
31534535Skarels 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
31634535Skarels 	    bp->b_flags & B_READ ? "read" : "writ");
31734535Skarels 	sn = bp->b_blkno;
31834535Skarels 	if (bp->b_bcount <= DEV_BSIZE)
31934535Skarels 		(*pr)("%d", sn);
32034535Skarels 	else {
32134535Skarels 		if (blkdone >= 0) {
32234535Skarels 			sn += blkdone;
32334535Skarels 			(*pr)("%d of ", sn);
32434535Skarels 		}
32534535Skarels 		(*pr)("%d-%d", bp->b_blkno,
32634535Skarels 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
32734535Skarels 	}
32834711Skarels 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
32934711Skarels #ifdef tahoe
33034711Skarels 		sn *= DEV_BSIZE / lp->d_secsize;		/* XXX */
33134711Skarels #endif
33234535Skarels 		sn += lp->d_partitions[part].p_offset;
33334637Skarels 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
33434637Skarels 		    sn / lp->d_secpercyl);
33534637Skarels 		sn %= lp->d_secpercyl;
33635703Stef 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
33734535Skarels 	}
33834535Skarels }
339