1206497Sluigi/*-
2206552Sluigi * Copyright (c) 2009-2010 Fabio Checconi
3206552Sluigi * Copyright (c) 2009-2010 Luigi Rizzo, Universita` di Pisa
4206497Sluigi * All rights reserved.
5206497Sluigi *
6206497Sluigi * Redistribution and use in source and binary forms, with or without
7206497Sluigi * modification, are permitted provided that the following conditions
8206497Sluigi * are met:
9206497Sluigi * 1. Redistributions of source code must retain the above copyright
10206497Sluigi *    notice, this list of conditions and the following disclaimer.
11206497Sluigi * 2. Redistributions in binary form must reproduce the above copyright
12206497Sluigi *    notice, this list of conditions and the following disclaimer in the
13206497Sluigi *    documentation and/or other materials provided with the distribution.
14206497Sluigi *
15206497Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16206497Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17206497Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18206497Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19206497Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20206497Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21206497Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22206497Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23206497Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24206497Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25206497Sluigi * SUCH DAMAGE.
26206497Sluigi */
27206497Sluigi
28206497Sluigi#ifndef	_G_SCHED_H_
29206497Sluigi#define	_G_SCHED_H_
30206497Sluigi
31206497Sluigi/*
32206497Sluigi * $Id$
33206497Sluigi * $FreeBSD$
34206497Sluigi *
35206497Sluigi * Header for the geom_sched class (userland library and kernel part).
36206497Sluigi * See g_sched.c for documentation.
37206497Sluigi * The userland code only needs the three G_SCHED_* values below.
38206497Sluigi */
39206497Sluigi
40206497Sluigi#define	G_SCHED_CLASS_NAME	"SCHED"
41206497Sluigi#define	G_SCHED_VERSION		0
42206497Sluigi#define	G_SCHED_SUFFIX		".sched."
43206497Sluigi
44206497Sluigi#ifdef _KERNEL
45206497Sluigi#define	G_SCHED_DEBUG(lvl, ...)	do {				\
46206497Sluigi	if (me.gs_debug >= (lvl)) {				\
47206497Sluigi		printf("GEOM_SCHED");				\
48206497Sluigi		if (me.gs_debug > 0)				\
49206497Sluigi			printf("[%u]", lvl);			\
50206497Sluigi		printf(": ");					\
51206497Sluigi		printf(__VA_ARGS__);				\
52206497Sluigi		printf("\n");					\
53206497Sluigi	}							\
54206497Sluigi} while (0)
55206497Sluigi
56206497Sluigi#define	G_SCHED_LOGREQ(bp, ...)	do {				\
57206497Sluigi	if (me.gs_debug >= 2) {					\
58206497Sluigi		printf("GEOM_SCHED[2]: ");			\
59206497Sluigi		printf(__VA_ARGS__);				\
60206497Sluigi		printf(" ");					\
61206497Sluigi		g_print_bio(bp);				\
62206497Sluigi		printf("\n");					\
63206497Sluigi	}							\
64206497Sluigi} while (0)
65206497Sluigi
66206497SluigiLIST_HEAD(g_hash, g_sched_class);
67206497Sluigi
68206497Sluigi/*
69206497Sluigi * Descriptor of a scheduler.
70206497Sluigi * In addition to the obvious fields, sc_flushing and sc_pending
71206497Sluigi * support dynamic switching of scheduling algorithm.
72206497Sluigi * Normally, sc_flushing is 0, and requests that are scheduled are
73206497Sluigi * also added to the sc_pending queue, and removed when we receive
74206497Sluigi * the 'done' event.
75206497Sluigi *
76206497Sluigi * When we are transparently inserted on an existing provider,
77206497Sluigi * sc_proxying is set. The detach procedure is slightly different.
78206497Sluigi *
79206497Sluigi * When switching schedulers, sc_flushing is set so requests bypass us,
80206497Sluigi * and at the same time we update the pointer in the pending bios
81206497Sluigi * to ignore us when they return up.
82206497Sluigi * XXX it would be more efficient to implement sc_pending with
83206497Sluigi * a generation number: the softc generation is increased when
84206497Sluigi * we change scheduling algorithm, we store the current generation
85206497Sluigi * number in the pending bios, and when they come back we ignore
86206497Sluigi * the done() call if the generation number do not match.
87206497Sluigi */
88206497Sluigistruct g_sched_softc {
89206497Sluigi	/*
90206497Sluigi	 * Generic fields used by any scheduling algorithm:
91206497Sluigi	 * a mutex, the class descriptor, flags, list of pending
92206497Sluigi	 * requests (used when flushing the module) and support
93206497Sluigi	 * for hash tables where we store per-flow queues.
94206497Sluigi	 */
95206497Sluigi	struct mtx	sc_mtx;
96206497Sluigi	struct g_gsched	*sc_gsched;	/* Scheduler descriptor. */
97206497Sluigi	int		sc_pending;	/* Pending requests. */
98206497Sluigi	int		sc_flags;	/* Various flags. */
99206497Sluigi
100206497Sluigi	/*
101206497Sluigi	 * Hash tables to store per-flow queues are generally useful
102206497Sluigi	 * so we handle them in the common code.
103206497Sluigi	 * sc_hash and sc_mask are parameters of the hash table,
104206497Sluigi	 * the last two fields are used to periodically remove
105206497Sluigi	 * expired items from the hash table.
106206497Sluigi	 */
107206497Sluigi	struct g_hash	*sc_hash;
108206497Sluigi	u_long		sc_mask;
109206497Sluigi	int		sc_flush_ticks;	/* Next tick for a flush. */
110206497Sluigi	int		sc_flush_bucket; /* Next bucket to flush. */
111206497Sluigi
112206497Sluigi	/*
113206497Sluigi	 * Pointer to the algorithm's private data, which is the value
114206497Sluigi	 * returned by sc_gsched->gs_init() . A NULL here means failure.
115206497Sluigi	 * XXX intptr_t might be more appropriate.
116206497Sluigi	 */
117206497Sluigi	void		*sc_data;
118206497Sluigi};
119206497Sluigi
120206497Sluigi#define	G_SCHED_PROXYING	1
121206497Sluigi#define	G_SCHED_FLUSHING	2
122206497Sluigi
123206497Sluigi/*
124206497Sluigi * Temporary- our own version of the disksort, because the
125206497Sluigi * version in 7.x and 8.x before march 2009 is buggy.
126206497Sluigi */
127206497Sluigivoid gs_bioq_init(struct bio_queue_head *);
128206497Sluigivoid gs_bioq_remove(struct bio_queue_head *, struct bio *);
129206497Sluigivoid gs_bioq_flush(struct bio_queue_head *, struct devstat *, int);
130206497Sluigivoid gs_bioq_insert_head(struct bio_queue_head *, struct bio *);
131206497Sluigivoid gs_bioq_insert_tail(struct bio_queue_head *, struct bio *);
132206497Sluigistruct bio *gs_bioq_first(struct bio_queue_head *);
133206497Sluigistruct bio *gs_bioq_takefirst(struct bio_queue_head *);
134206497Sluigivoid gs_bioq_disksort(struct bio_queue_head *, struct bio *);
135206497Sluigi
136206497Sluigi#endif	/* _KERNEL */
137206497Sluigi
138206497Sluigi#endif	/* _G_SCHED_H_ */
139