xref: /freebsd/sys/kern/subr_disk.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12 
13 #include "opt_geom.h"
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/stdint.h>
18 #include <sys/bio.h>
19 #include <sys/conf.h>
20 #include <sys/disk.h>
21 #include <sys/disklabel.h>
22 
23 /*-
24  * Disk error is the preface to plaintive error messages
25  * about failing disk transfers.  It prints messages of the form
26  * 	"hp0g: BLABLABLA cmd=read fsbn 12345 of 12344-12347"
27  * blkdone should be -1 if the position of the error is unknown.
28  * The message is printed with printf.
29  */
30 void
31 disk_err(struct bio *bp, const char *what, int blkdone, int nl)
32 {
33 	daddr_t sn;
34 
35 	if (bp->bio_dev != NULL)
36 		printf("%s: %s ", devtoname(bp->bio_dev), what);
37 	else if (bp->bio_disk != NULL)
38 		printf("%s%d: %s ",
39 		    bp->bio_disk->d_name, bp->bio_disk->d_unit, what);
40 	else
41 		printf("disk??: %s ", what);
42 	switch(bp->bio_cmd) {
43 	case BIO_READ:		printf("cmd=read "); break;
44 	case BIO_WRITE:		printf("cmd=write "); break;
45 	case BIO_DELETE:	printf("cmd=delete "); break;
46 	case BIO_GETATTR:	printf("cmd=getattr "); break;
47 	case BIO_SETATTR:	printf("cmd=setattr "); break;
48 	default:		printf("cmd=%x ", bp->bio_cmd); break;
49 	}
50 	sn = bp->bio_blkno;
51 	if (bp->bio_bcount <= DEV_BSIZE) {
52 		printf("fsbn %jd%s", (intmax_t)sn, nl ? "\n" : "");
53 		return;
54 	}
55 	if (blkdone >= 0) {
56 		sn += blkdone;
57 		printf("fsbn %jd of ", (intmax_t)sn);
58 	}
59 	printf("%jd-%jd", (intmax_t)bp->bio_blkno,
60 	    (intmax_t)(bp->bio_blkno + (bp->bio_bcount - 1) / DEV_BSIZE));
61 	if (nl)
62 		printf("\n");
63 }
64 
65 /*
66  * Seek sort for disks.
67  *
68  * The buf_queue keep two queues, sorted in ascending block order.  The first
69  * queue holds those requests which are positioned after the current block
70  * (in the first request); the second, which starts at queue->switch_point,
71  * holds requests which came in after their block number was passed.  Thus
72  * we implement a one way scan, retracting after reaching the end of the drive
73  * to the first request on the second queue, at which time it becomes the
74  * first queue.
75  *
76  * A one-way scan is natural because of the way UNIX read-ahead blocks are
77  * allocated.
78  */
79 
80 void
81 bioq_disksort(bioq, bp)
82 	struct bio_queue_head *bioq;
83 	struct bio *bp;
84 {
85 	struct bio *bq;
86 	struct bio *bn;
87 	struct bio *be;
88 
89 	if (!atomic_cmpset_int(&bioq->busy, 0, 1))
90 		panic("Recursing in bioq_disksort()");
91 	be = TAILQ_LAST(&bioq->queue, bio_queue);
92 	/*
93 	 * If the queue is empty or we are an
94 	 * ordered transaction, then it's easy.
95 	 */
96 	if ((bq = bioq_first(bioq)) == NULL) {
97 		bioq_insert_tail(bioq, bp);
98 		bioq->busy = 0;
99 		return;
100 	} else if (bioq->insert_point != NULL) {
101 
102 		/*
103 		 * A certain portion of the list is
104 		 * "locked" to preserve ordering, so
105 		 * we can only insert after the insert
106 		 * point.
107 		 */
108 		bq = bioq->insert_point;
109 	} else {
110 
111 		/*
112 		 * If we lie before the last removed (currently active)
113 		 * request, and are not inserting ourselves into the
114 		 * "locked" portion of the list, then we must add ourselves
115 		 * to the second request list.
116 		 */
117 		if (bp->bio_pblkno < bioq->last_pblkno) {
118 
119 			bq = bioq->switch_point;
120 			/*
121 			 * If we are starting a new secondary list,
122 			 * then it's easy.
123 			 */
124 			if (bq == NULL) {
125 				bioq->switch_point = bp;
126 				bioq_insert_tail(bioq, bp);
127 				bioq->busy = 0;
128 				return;
129 			}
130 			/*
131 			 * If we lie ahead of the current switch point,
132 			 * insert us before the switch point and move
133 			 * the switch point.
134 			 */
135 			if (bp->bio_pblkno < bq->bio_pblkno) {
136 				bioq->switch_point = bp;
137 				TAILQ_INSERT_BEFORE(bq, bp, bio_queue);
138 				bioq->busy = 0;
139 				return;
140 			}
141 		} else {
142 			if (bioq->switch_point != NULL)
143 				be = TAILQ_PREV(bioq->switch_point,
144 						bio_queue, bio_queue);
145 			/*
146 			 * If we lie between last_pblkno and bq,
147 			 * insert before bq.
148 			 */
149 			if (bp->bio_pblkno < bq->bio_pblkno) {
150 				TAILQ_INSERT_BEFORE(bq, bp, bio_queue);
151 				bioq->busy = 0;
152 				return;
153 			}
154 		}
155 	}
156 
157 	/*
158 	 * Request is at/after our current position in the list.
159 	 * Optimize for sequential I/O by seeing if we go at the tail.
160 	 */
161 	if (bp->bio_pblkno > be->bio_pblkno) {
162 		TAILQ_INSERT_AFTER(&bioq->queue, be, bp, bio_queue);
163 		bioq->busy = 0;
164 		return;
165 	}
166 
167 	/* Otherwise, insertion sort */
168 	while ((bn = TAILQ_NEXT(bq, bio_queue)) != NULL) {
169 
170 		/*
171 		 * We want to go after the current request if it is the end
172 		 * of the first request list, or if the next request is a
173 		 * larger cylinder than our request.
174 		 */
175 		if (bn == bioq->switch_point
176 		 || bp->bio_pblkno < bn->bio_pblkno)
177 			break;
178 		bq = bn;
179 	}
180 	TAILQ_INSERT_AFTER(&bioq->queue, bq, bp, bio_queue);
181 	bioq->busy = 0;
182 }
183 
184 
185