Home | History | Annotate | Line # | Download | only in kern
bufq_disksort.c revision 1.4
      1 /*	$NetBSD: bufq_disksort.c,v 1.4 2005/10/15 17:29:26 yamt Exp $	*/
      2 /*	NetBSD: subr_disk.c,v 1.61 2004/09/25 03:30:44 thorpej Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  * NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Copyright (c) 1982, 1986, 1988, 1993
     43  *	The Regents of the University of California.  All rights reserved.
     44  * (c) UNIX System Laboratories, Inc.
     45  * All or some portions of this file are derived from material licensed
     46  * to the University of California by American Telephone and Telegraph
     47  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     48  * the permission of UNIX System Laboratories, Inc.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: bufq_disksort.c,v 1.4 2005/10/15 17:29:26 yamt Exp $");
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/buf.h>
     83 #include <sys/bufq.h>
     84 #include <sys/bufq_impl.h>
     85 #include <sys/malloc.h>
     86 
     87 /*
     88  * Seek sort for disks.
     89  *
     90  * There are actually two queues, sorted in ascendening order.  The first
     91  * queue holds those requests which are positioned after the current block;
     92  * the second holds requests which came in after their position was passed.
     93  * Thus we implement a one-way scan, retracting after reaching the end of
     94  * the drive to the first request on the second queue, at which time it
     95  * becomes the first queue.
     96  *
     97  * A one-way scan is natural because of the way UNIX read-ahead blocks are
     98  * allocated.
     99  */
    100 
    101 struct bufq_disksort {
    102 	TAILQ_HEAD(, buf) bq_head;	/* actual list of buffers */
    103 };
    104 
    105 static void bufq_disksort_init(struct bufq_state *);
    106 static void bufq_disksort_put(struct bufq_state *, struct buf *);
    107 static struct buf *bufq_disksort_get(struct bufq_state *, int);
    108 
    109 BUFQ_DEFINE(disksort, 20, bufq_disksort_init);
    110 
    111 static void
    112 bufq_disksort_put(struct bufq_state *bufq, struct buf *bp)
    113 {
    114 	struct bufq_disksort *disksort = bufq_private(bufq);
    115 	struct buf *bq, *nbq;
    116 	int sortby;
    117 
    118 	sortby = bufq->bq_flags & BUFQ_SORT_MASK;
    119 
    120 	bq = TAILQ_FIRST(&disksort->bq_head);
    121 
    122 	/*
    123 	 * If the queue is empty it's easy; we just go on the end.
    124 	 */
    125 	if (bq == NULL) {
    126 		TAILQ_INSERT_TAIL(&disksort->bq_head, bp, b_actq);
    127 		return;
    128 	}
    129 
    130 	/*
    131 	 * If we lie before the currently active request, then we
    132 	 * must locate the second request list and add ourselves to it.
    133 	 */
    134 	if (buf_inorder(bp, bq, sortby)) {
    135 		while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
    136 			/*
    137 			 * Check for an ``inversion'' in the normally ascending
    138 			 * block numbers, indicating the start of the second
    139 			 * request list.
    140 			 */
    141 			if (buf_inorder(nbq, bq, sortby)) {
    142 				/*
    143 				 * Search the second request list for the first
    144 				 * request at a larger block number.  We go
    145 				 * after that; if there is no such request, we
    146 				 * go at the end.
    147 				 */
    148 				do {
    149 					if (buf_inorder(bp, nbq, sortby))
    150 						goto insert;
    151 					bq = nbq;
    152 				} while ((nbq =
    153 				    TAILQ_NEXT(bq, b_actq)) != NULL);
    154 				goto insert;		/* after last */
    155 			}
    156 			bq = nbq;
    157 		}
    158 		/*
    159 		 * No inversions... we will go after the last, and
    160 		 * be the first request in the second request list.
    161 		 */
    162 		goto insert;
    163 	}
    164 	/*
    165 	 * Request is at/after the current request...
    166 	 * sort in the first request list.
    167 	 */
    168 	while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
    169 		/*
    170 		 * We want to go after the current request if there is an
    171 		 * inversion after it (i.e. it is the end of the first
    172 		 * request list), or if the next request is a larger cylinder
    173 		 * than our request.
    174 		 */
    175 		if (buf_inorder(nbq, bq, sortby) ||
    176 		    buf_inorder(bp, nbq, sortby))
    177 			goto insert;
    178 		bq = nbq;
    179 	}
    180 	/*
    181 	 * Neither a second list nor a larger request... we go at the end of
    182 	 * the first list, which is the same as the end of the whole schebang.
    183 	 */
    184 insert:	TAILQ_INSERT_AFTER(&disksort->bq_head, bq, bp, b_actq);
    185 }
    186 
    187 static struct buf *
    188 bufq_disksort_get(struct bufq_state *bufq, int remove)
    189 {
    190 	struct bufq_disksort *disksort = bufq->bq_private;
    191 	struct buf *bp;
    192 
    193 	bp = TAILQ_FIRST(&disksort->bq_head);
    194 
    195 	if (bp != NULL && remove)
    196 		TAILQ_REMOVE(&disksort->bq_head, bp, b_actq);
    197 
    198 	return (bp);
    199 }
    200 
    201 static void
    202 bufq_disksort_init(struct bufq_state *bufq)
    203 {
    204 	struct bufq_disksort *disksort;
    205 
    206 	bufq->bq_get = bufq_disksort_get;
    207 	bufq->bq_put = bufq_disksort_put;
    208 	MALLOC(bufq->bq_private, struct bufq_disksort *,
    209 	    sizeof(struct bufq_disksort), M_DEVBUF, M_ZERO);
    210 	disksort = (struct bufq_disksort *)bufq->bq_private;
    211 	TAILQ_INIT(&disksort->bq_head);
    212 }
    213