xref: /netbsd-src/sys/kern/subr_disk.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: subr_disk.c,v 1.23 1997/12/30 09:51:24 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1988, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  * (c) UNIX System Laboratories, Inc.
44  * All or some portions of this file are derived from material licensed
45  * to the University of California by American Telephone and Telegraph
46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47  * the permission of UNIX System Laboratories, Inc.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 3. All advertising materials mentioning features or use of this software
58  *    must display the following acknowledgement:
59  *	This product includes software developed by the University of
60  *	California, Berkeley and its contributors.
61  * 4. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
78  */
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/buf.h>
85 #include <sys/syslog.h>
86 #include <sys/time.h>
87 #include <sys/disklabel.h>
88 #include <sys/disk.h>
89 
90 /*
91  * A global list of all disks attached to the system.  May grow or
92  * shrink over time.
93  */
94 struct	disklist_head disklist;	/* TAILQ_HEAD */
95 int	disk_count;		/* number of drives in global disklist */
96 
97 /*
98  * Seek sort for disks.  We depend on the driver which calls us using b_resid
99  * as the current cylinder number.
100  *
101  * The argument ap structure holds a b_actf activity chain pointer on which we
102  * keep two queues, sorted in ascending cylinder order.  The first queue holds
103  * those requests which are positioned after the current cylinder (in the first
104  * request); the second holds requests which came in after their cylinder number
105  * was passed.  Thus we implement a one way scan, retracting after reaching the
106  * end of the drive to the first request on the second queue, at which time it
107  * becomes the first queue.
108  *
109  * A one-way scan is natural because of the way UNIX read-ahead blocks are
110  * allocated.
111  */
112 
113 void
114 disksort(ap, bp)
115 	register struct buf *ap, *bp;
116 {
117 	register struct buf *bq;
118 
119 	/* If the queue is empty, then it's easy. */
120 	if (ap->b_actf == NULL) {
121 		bp->b_actf = NULL;
122 		ap->b_actf = bp;
123 		return;
124 	}
125 
126 	/*
127 	 * If we lie after the first (currently active) request, then we
128 	 * must locate the second request list and add ourselves to it.
129 	 */
130 	bq = ap->b_actf;
131 	if (bp->b_cylinder < bq->b_cylinder) {
132 		while (bq->b_actf) {
133 			/*
134 			 * Check for an ``inversion'' in the normally ascending
135 			 * cylinder numbers, indicating the start of the second
136 			 * request list.
137 			 */
138 			if (bq->b_actf->b_cylinder < bq->b_cylinder) {
139 				/*
140 				 * Search the second request list for the first
141 				 * request at a larger cylinder number.  We go
142 				 * before that; if there is no such request, we
143 				 * go at end.
144 				 */
145 				do {
146 					if (bp->b_cylinder <
147 					    bq->b_actf->b_cylinder)
148 						goto insert;
149 					if (bp->b_cylinder ==
150 					    bq->b_actf->b_cylinder &&
151 					    bp->b_blkno < bq->b_actf->b_blkno)
152 						goto insert;
153 					bq = bq->b_actf;
154 				} while (bq->b_actf);
155 				goto insert;		/* after last */
156 			}
157 			bq = bq->b_actf;
158 		}
159 		/*
160 		 * No inversions... we will go after the last, and
161 		 * be the first request in the second request list.
162 		 */
163 		goto insert;
164 	}
165 	/*
166 	 * Request is at/after the current request...
167 	 * sort in the first request list.
168 	 */
169 	while (bq->b_actf) {
170 		/*
171 		 * We want to go after the current request if there is an
172 		 * inversion after it (i.e. it is the end of the first
173 		 * request list), or if the next request is a larger cylinder
174 		 * than our request.
175 		 */
176 		if (bq->b_actf->b_cylinder < bq->b_cylinder ||
177 		    bp->b_cylinder < bq->b_actf->b_cylinder ||
178 		    (bp->b_cylinder == bq->b_actf->b_cylinder &&
179 		    bp->b_blkno < bq->b_actf->b_blkno))
180 			goto insert;
181 		bq = bq->b_actf;
182 	}
183 	/*
184 	 * Neither a second list nor a larger request... we go at the end of
185 	 * the first list, which is the same as the end of the whole schebang.
186 	 */
187 insert:	bp->b_actf = bq->b_actf;
188 	bq->b_actf = bp;
189 }
190 
191 /* encoding of disk minor numbers, should be elsewhere... */
192 #define dkunit(dev)		(minor(dev) >> 3)
193 #define dkpart(dev)		(minor(dev) & 07)
194 #define dkminor(unit, part)	(((unit) << 3) | (part))
195 
196 /*
197  * Compute checksum for disk label.
198  */
199 u_int
200 dkcksum(lp)
201 	register struct disklabel *lp;
202 {
203 	register u_short *start, *end;
204 	register u_short sum = 0;
205 
206 	start = (u_short *)lp;
207 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
208 	while (start < end)
209 		sum ^= *start++;
210 	return (sum);
211 }
212 
213 /*
214  * Disk error is the preface to plaintive error messages
215  * about failing disk transfers.  It prints messages of the form
216 
217 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
218 
219  * if the offset of the error in the transfer and a disk label
220  * are both available.  blkdone should be -1 if the position of the error
221  * is unknown; the disklabel pointer may be null from drivers that have not
222  * been converted to use them.  The message is printed with printf
223  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
224  * The message should be completed (with at least a newline) with printf
225  * or addlog, respectively.  There is no trailing space.
226  */
227 void
228 diskerr(bp, dname, what, pri, blkdone, lp)
229 	register struct buf *bp;
230 	char *dname, *what;
231 	int pri, blkdone;
232 	register struct disklabel *lp;
233 {
234 	int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
235 	register void (*pr) __P((const char *, ...));
236 	char partname = 'a' + part;
237 	int sn;
238 
239 	if (pri != LOG_PRINTF) {
240 		static const char fmt[] = "";
241 		log(pri, fmt);
242 		pr = addlog;
243 	} else
244 		pr = printf;
245 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
246 	    bp->b_flags & B_READ ? "read" : "writ");
247 	sn = bp->b_blkno;
248 	if (bp->b_bcount <= DEV_BSIZE)
249 		(*pr)("%d", sn);
250 	else {
251 		if (blkdone >= 0) {
252 			sn += blkdone;
253 			(*pr)("%d of ", sn);
254 		}
255 		(*pr)("%d-%d", bp->b_blkno,
256 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
257 	}
258 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
259 		sn += lp->d_partitions[part].p_offset;
260 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
261 		    sn / lp->d_secpercyl);
262 		sn %= lp->d_secpercyl;
263 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
264 	}
265 }
266 
267 /*
268  * Initialize the disklist.  Called by main() before autoconfiguration.
269  */
270 void
271 disk_init()
272 {
273 
274 	TAILQ_INIT(&disklist);
275 	disk_count = 0;
276 }
277 
278 /*
279  * Searches the disklist for the disk corresponding to the
280  * name provided.
281  */
282 struct disk *
283 disk_find(name)
284 	char *name;
285 {
286 	struct disk *diskp;
287 
288 	if ((name == NULL) || (disk_count <= 0))
289 		return (NULL);
290 
291 	for (diskp = disklist.tqh_first; diskp != NULL;
292 	    diskp = diskp->dk_link.tqe_next)
293 		if (strcmp(diskp->dk_name, name) == 0)
294 			return (diskp);
295 
296 	return (NULL);
297 }
298 
299 /*
300  * Attach a disk.
301  */
302 void
303 disk_attach(diskp)
304 	struct disk *diskp;
305 {
306 	int s;
307 
308 	/*
309 	 * Allocate and initialize the disklabel structures.  Note that
310 	 * it's not safe to sleep here, since we're probably going to be
311 	 * called during autoconfiguration.
312 	 */
313 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
314 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
315 	    M_NOWAIT);
316 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
317 		panic("disk_attach: can't allocate storage for disklabel");
318 
319 	bzero(diskp->dk_label, sizeof(struct disklabel));
320 	bzero(diskp->dk_cpulabel, sizeof(struct cpu_disklabel));
321 
322 	/*
323 	 * Set the attached timestamp.
324 	 */
325 	s = splclock();
326 	diskp->dk_attachtime = mono_time;
327 	splx(s);
328 
329 	/*
330 	 * Link into the disklist.
331 	 */
332 	TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
333 	++disk_count;
334 }
335 
336 /*
337  * Detach a disk.
338  */
339 void
340 disk_detach(diskp)
341 	struct disk *diskp;
342 {
343 
344 	/*
345 	 * Remove from the disklist.
346 	 */
347 	if (--disk_count < 0)
348 		panic("disk_detach: disk_count < 0");
349 	TAILQ_REMOVE(&disklist, diskp, dk_link);
350 
351 	/*
352 	 * Free the space used by the disklabel structures.
353 	 */
354 	free(diskp->dk_label, M_DEVBUF);
355 	free(diskp->dk_cpulabel, M_DEVBUF);
356 }
357 
358 /*
359  * Increment a disk's busy counter.  If the counter is going from
360  * 0 to 1, set the timestamp.
361  */
362 void
363 disk_busy(diskp)
364 	struct disk *diskp;
365 {
366 	int s;
367 
368 	/*
369 	 * XXX We'd like to use something as accurate as microtime(),
370 	 * but that doesn't depend on the system TOD clock.
371 	 */
372 	if (diskp->dk_busy++ == 0) {
373 		s = splclock();
374 		diskp->dk_timestamp = mono_time;
375 		splx(s);
376 	}
377 }
378 
379 /*
380  * Decrement a disk's busy counter, increment the byte count, total busy
381  * time, and reset the timestamp.
382  */
383 void
384 disk_unbusy(diskp, bcount)
385 	struct disk *diskp;
386 	long bcount;
387 {
388 	int s;
389 	struct timeval dv_time, diff_time;
390 
391 	if (diskp->dk_busy-- == 0) {
392 		printf("%s: dk_busy < 0\n", diskp->dk_name);
393 		panic("disk_unbusy");
394 	}
395 
396 	s = splclock();
397 	dv_time = mono_time;
398 	splx(s);
399 
400 	timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
401 	timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
402 
403 	diskp->dk_timestamp = dv_time;
404 	if (bcount > 0) {
405 		diskp->dk_bytes += bcount;
406 		diskp->dk_xfer++;
407 	}
408 }
409 
410 /*
411  * Reset the metrics counters on the given disk.  Note that we cannot
412  * reset the busy counter, as it may case a panic in disk_unbusy().
413  * We also must avoid playing with the timestamp information, as it
414  * may skew any pending transfer results.
415  */
416 void
417 disk_resetstat(diskp)
418 	struct disk *diskp;
419 {
420 	int s = splbio(), t;
421 
422 	diskp->dk_xfer = 0;
423 	diskp->dk_bytes = 0;
424 
425 	t = splclock();
426 	diskp->dk_attachtime = mono_time;
427 	splx(t);
428 
429 	timerclear(&diskp->dk_time);
430 
431 	splx(s);
432 }
433