xref: /netbsd-src/sys/kern/bufq_fcfs.c (revision ec8060020862b5ac256de2a4e79555a03fb31d96)
1 /*	$NetBSD: bufq_fcfs.c,v 1.13 2017/05/04 11:03:27 kamil 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  * (c) UNIX System Laboratories, Inc.
38  * All or some portions of this file are derived from material licensed
39  * to the University of California by American Telephone and Telegraph
40  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41  * the permission of UNIX System Laboratories, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: bufq_fcfs.c,v 1.13 2017/05/04 11:03:27 kamil Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/buf.h>
76 #include <sys/bufq.h>
77 #include <sys/bufq_impl.h>
78 #include <sys/kmem.h>
79 #include <sys/module.h>
80 
81 /*
82  * First-come first-served sort for disks.
83  *
84  * Requests are appended to the queue without any reordering.
85  */
86 
87 struct bufq_fcfs {
88 	TAILQ_HEAD(, buf) bq_head;	/* actual list of buffers */
89 };
90 
91 static void bufq_fcfs_init(struct bufq_state *);
92 static void bufq_fcfs_put(struct bufq_state *, struct buf *);
93 static struct buf *bufq_fcfs_get(struct bufq_state *, int);
94 
95 BUFQ_DEFINE(fcfs, 10, bufq_fcfs_init);
96 
97 static void
bufq_fcfs_put(struct bufq_state * bufq,struct buf * bp)98 bufq_fcfs_put(struct bufq_state *bufq, struct buf *bp)
99 {
100 	struct bufq_fcfs *fcfs = bufq_private(bufq);
101 
102 	TAILQ_INSERT_TAIL(&fcfs->bq_head, bp, b_actq);
103 }
104 
105 static struct buf *
bufq_fcfs_get(struct bufq_state * bufq,int remove)106 bufq_fcfs_get(struct bufq_state *bufq, int remove)
107 {
108 	struct bufq_fcfs *fcfs = bufq_private(bufq);
109 	struct buf *bp;
110 
111 	bp = TAILQ_FIRST(&fcfs->bq_head);
112 
113 	if (bp != NULL && remove)
114 		TAILQ_REMOVE(&fcfs->bq_head, bp, b_actq);
115 
116 	return (bp);
117 }
118 
119 static struct buf *
bufq_fcfs_cancel(struct bufq_state * bufq,struct buf * buf)120 bufq_fcfs_cancel(struct bufq_state *bufq, struct buf *buf)
121 {
122 	struct bufq_fcfs *fcfs = bufq_private(bufq);
123 	struct buf *bp;
124 
125 	TAILQ_FOREACH(bp, &fcfs->bq_head, b_actq) {
126 		if (bp == buf) {
127 			TAILQ_REMOVE(&fcfs->bq_head, bp, b_actq);
128 			return buf;
129 		}
130 	}
131 	return NULL;
132 }
133 
134 static void
bufq_fcfs_fini(struct bufq_state * bufq)135 bufq_fcfs_fini(struct bufq_state *bufq)
136 {
137 
138 	KASSERT(bufq->bq_private != NULL);
139 	kmem_free(bufq->bq_private, sizeof(struct bufq_fcfs));
140 }
141 
142 static void
bufq_fcfs_init(struct bufq_state * bufq)143 bufq_fcfs_init(struct bufq_state *bufq)
144 {
145 	struct bufq_fcfs *fcfs;
146 
147 	bufq->bq_get = bufq_fcfs_get;
148 	bufq->bq_put = bufq_fcfs_put;
149 	bufq->bq_cancel = bufq_fcfs_cancel;
150 	bufq->bq_fini = bufq_fcfs_fini;
151 	bufq->bq_private = kmem_zalloc(sizeof(struct bufq_fcfs), KM_SLEEP);
152 	fcfs = (struct bufq_fcfs *)bufq->bq_private;
153 	TAILQ_INIT(&fcfs->bq_head);
154 }
155 
156 MODULE(MODULE_CLASS_BUFQ, bufq_fcfs, NULL);
157 
158 static int
bufq_fcfs_modcmd(modcmd_t cmd,void * opaque)159 bufq_fcfs_modcmd(modcmd_t cmd, void *opaque)
160 {
161 
162 	switch (cmd) {
163 	case MODULE_CMD_INIT:
164 		return bufq_register(&bufq_strat_fcfs);
165 	case MODULE_CMD_FINI:
166 		return bufq_unregister(&bufq_strat_fcfs);
167 	default:
168 		return ENOTTY;
169 	}
170 }
171